PageRenderTime 63ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/lib-python/2.7/toaiff.py

https://bitbucket.org/dac_io/pypy
Python | 110 lines | 110 code | 0 blank | 0 comment | 0 complexity | bf73c009fbefe5c8fda728ed70443bd1 MD5 | raw file
  1. """Convert "arbitrary" sound files to AIFF (Apple and SGI's audio format).
  2. Input may be compressed.
  3. Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others.
  4. An exception is raised if the file is not of a recognized type.
  5. Returned filename is either the input filename or a temporary filename;
  6. in the latter case the caller must ensure that it is removed.
  7. Other temporary files used are removed by the function.
  8. """
  9. from warnings import warnpy3k
  10. warnpy3k("the toaiff module has been removed in Python 3.0", stacklevel=2)
  11. del warnpy3k
  12. import os
  13. import tempfile
  14. import pipes
  15. import sndhdr
  16. __all__ = ["error", "toaiff"]
  17. table = {}
  18. t = pipes.Template()
  19. t.append('sox -t au - -t aiff -r 8000 -', '--')
  20. table['au'] = t
  21. # XXX The following is actually sub-optimal.
  22. # XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4.
  23. # XXX We must force the output sampling rate else the SGI won't play
  24. # XXX files sampled at 5.5k or 7.333k; however this means that files
  25. # XXX sampled at 11k are unnecessarily expanded.
  26. # XXX Similar comments apply to some other file types.
  27. t = pipes.Template()
  28. t.append('sox -t hcom - -t aiff -r 22050 -', '--')
  29. table['hcom'] = t
  30. t = pipes.Template()
  31. t.append('sox -t voc - -t aiff -r 11025 -', '--')
  32. table['voc'] = t
  33. t = pipes.Template()
  34. t.append('sox -t wav - -t aiff -', '--')
  35. table['wav'] = t
  36. t = pipes.Template()
  37. t.append('sox -t 8svx - -t aiff -r 16000 -', '--')
  38. table['8svx'] = t
  39. t = pipes.Template()
  40. t.append('sox -t sndt - -t aiff -r 16000 -', '--')
  41. table['sndt'] = t
  42. t = pipes.Template()
  43. t.append('sox -t sndr - -t aiff -r 16000 -', '--')
  44. table['sndr'] = t
  45. uncompress = pipes.Template()
  46. uncompress.append('uncompress', '--')
  47. class error(Exception):
  48. pass
  49. def toaiff(filename):
  50. temps = []
  51. ret = None
  52. try:
  53. ret = _toaiff(filename, temps)
  54. finally:
  55. for temp in temps[:]:
  56. if temp != ret:
  57. try:
  58. os.unlink(temp)
  59. except os.error:
  60. pass
  61. temps.remove(temp)
  62. return ret
  63. def _toaiff(filename, temps):
  64. if filename[-2:] == '.Z':
  65. (fd, fname) = tempfile.mkstemp()
  66. os.close(fd)
  67. temps.append(fname)
  68. sts = uncompress.copy(filename, fname)
  69. if sts:
  70. raise error, filename + ': uncompress failed'
  71. else:
  72. fname = filename
  73. try:
  74. ftype = sndhdr.whathdr(fname)
  75. if ftype:
  76. ftype = ftype[0] # All we're interested in
  77. except IOError, msg:
  78. if type(msg) == type(()) and len(msg) == 2 and \
  79. type(msg[0]) == type(0) and type(msg[1]) == type(''):
  80. msg = msg[1]
  81. if type(msg) != type(''):
  82. msg = repr(msg)
  83. raise error, filename + ': ' + msg
  84. if ftype == 'aiff':
  85. return fname
  86. if ftype is None or not ftype in table:
  87. raise error, '%s: unsupported audio file type %r' % (filename, ftype)
  88. (fd, temp) = tempfile.mkstemp()
  89. os.close(fd)
  90. temps.append(temp)
  91. sts = table[ftype].copy(fname, temp)
  92. if sts:
  93. raise error, filename + ': conversion to aiff failed'
  94. return temp