toaiff.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. import os
  10. import tempfile
  11. import pipes
  12. import sndhdr
  13. __all__ = ["error", "toaiff"]
  14. table = {}
  15. t = pipes.Template()
  16. t.append('sox -t au - -t aiff -r 8000 -', '--')
  17. table['au'] = t
  18. # XXX The following is actually sub-optimal.
  19. # XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4.
  20. # XXX We must force the output sampling rate else the SGI won't play
  21. # XXX files sampled at 5.5k or 7.333k; however this means that files
  22. # XXX sampled at 11k are unnecessarily expanded.
  23. # XXX Similar comments apply to some other file types.
  24. t = pipes.Template()
  25. t.append('sox -t hcom - -t aiff -r 22050 -', '--')
  26. table['hcom'] = t
  27. t = pipes.Template()
  28. t.append('sox -t voc - -t aiff -r 11025 -', '--')
  29. table['voc'] = t
  30. t = pipes.Template()
  31. t.append('sox -t wav - -t aiff -', '--')
  32. table['wav'] = t
  33. t = pipes.Template()
  34. t.append('sox -t 8svx - -t aiff -r 16000 -', '--')
  35. table['8svx'] = t
  36. t = pipes.Template()
  37. t.append('sox -t sndt - -t aiff -r 16000 -', '--')
  38. table['sndt'] = t
  39. t = pipes.Template()
  40. t.append('sox -t sndr - -t aiff -r 16000 -', '--')
  41. table['sndr'] = t
  42. uncompress = pipes.Template()
  43. uncompress.append('uncompress', '--')
  44. class error(Exception):
  45. pass
  46. def toaiff(filename):
  47. temps = []
  48. ret = None
  49. try:
  50. ret = _toaiff(filename, temps)
  51. finally:
  52. for temp in temps[:]:
  53. if temp != ret:
  54. try:
  55. os.unlink(temp)
  56. except os.error:
  57. pass
  58. temps.remove(temp)
  59. return ret
  60. def _toaiff(filename, temps):
  61. if filename[-2:] == '.Z':
  62. (fd, fname) = tempfile.mkstemp()
  63. os.close(fd)
  64. temps.append(fname)
  65. sts = uncompress.copy(filename, fname)
  66. if sts:
  67. raise error, filename + ': uncompress failed'
  68. else:
  69. fname = filename
  70. try:
  71. ftype = sndhdr.whathdr(fname)
  72. if ftype:
  73. ftype = ftype[0] # All we're interested in
  74. except IOError, msg:
  75. if type(msg) == type(()) and len(msg) == 2 and \
  76. type(msg[0]) == type(0) and type(msg[1]) == type(''):
  77. msg = msg[1]
  78. if type(msg) != type(''):
  79. msg = repr(msg)
  80. raise error, filename + ': ' + msg
  81. if ftype == 'aiff':
  82. return fname
  83. if ftype is None or not ftype in table:
  84. raise error, '%s: unsupported audio file type %r' % (filename, ftype)
  85. (fd, temp) = tempfile.mkstemp()
  86. os.close(fd)
  87. temps.append(temp)
  88. sts = table[ftype].copy(fname, temp)
  89. if sts:
  90. raise error, filename + ': conversion to aiff failed'
  91. return temp