PyXR

c:\python24\lib \ toaiff.py



0001 """Convert "arbitrary" sound files to AIFF (Apple and SGI's audio format).
0002 
0003 Input may be compressed.
0004 Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others.
0005 An exception is raised if the file is not of a recognized type.
0006 Returned filename is either the input filename or a temporary filename;
0007 in the latter case the caller must ensure that it is removed.
0008 Other temporary files used are removed by the function.
0009 """
0010 
0011 import os
0012 import tempfile
0013 import pipes
0014 import sndhdr
0015 
0016 __all__ = ["error", "toaiff"]
0017 
0018 table = {}
0019 
0020 t = pipes.Template()
0021 t.append('sox -t au - -t aiff -r 8000 -', '--')
0022 table['au'] = t
0023 
0024 # XXX The following is actually sub-optimal.
0025 # XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4.
0026 # XXX We must force the output sampling rate else the SGI won't play
0027 # XXX files sampled at 5.5k or 7.333k; however this means that files
0028 # XXX sampled at 11k are unnecessarily expanded.
0029 # XXX Similar comments apply to some other file types.
0030 t = pipes.Template()
0031 t.append('sox -t hcom - -t aiff -r 22050 -', '--')
0032 table['hcom'] = t
0033 
0034 t = pipes.Template()
0035 t.append('sox -t voc - -t aiff -r 11025 -', '--')
0036 table['voc'] = t
0037 
0038 t = pipes.Template()
0039 t.append('sox -t wav - -t aiff -', '--')
0040 table['wav'] = t
0041 
0042 t = pipes.Template()
0043 t.append('sox -t 8svx - -t aiff -r 16000 -', '--')
0044 table['8svx'] = t
0045 
0046 t = pipes.Template()
0047 t.append('sox -t sndt - -t aiff -r 16000 -', '--')
0048 table['sndt'] = t
0049 
0050 t = pipes.Template()
0051 t.append('sox -t sndr - -t aiff -r 16000 -', '--')
0052 table['sndr'] = t
0053 
0054 uncompress = pipes.Template()
0055 uncompress.append('uncompress', '--')
0056 
0057 
0058 class error(Exception):
0059     pass
0060 
0061 def toaiff(filename):
0062     temps = []
0063     ret = None
0064     try:
0065         ret = _toaiff(filename, temps)
0066     finally:
0067         for temp in temps[:]:
0068             if temp != ret:
0069                 try:
0070                     os.unlink(temp)
0071                 except os.error:
0072                     pass
0073                 temps.remove(temp)
0074     return ret
0075 
0076 def _toaiff(filename, temps):
0077     if filename[-2:] == '.Z':
0078         (fd, fname) = tempfile.mkstemp()
0079         os.close(fd)
0080         temps.append(fname)
0081         sts = uncompress.copy(filename, fname)
0082         if sts:
0083             raise error, filename + ': uncompress failed'
0084     else:
0085         fname = filename
0086     try:
0087         ftype = sndhdr.whathdr(fname)
0088         if ftype:
0089             ftype = ftype[0] # All we're interested in
0090     except IOError, msg:
0091         if type(msg) == type(()) and len(msg) == 2 and \
0092                 type(msg[0]) == type(0) and type(msg[1]) == type(''):
0093             msg = msg[1]
0094         if type(msg) != type(''):
0095             msg = repr(msg)
0096         raise error, filename + ': ' + msg
0097     if ftype == 'aiff':
0098         return fname
0099     if ftype is None or not ftype in table:
0100         raise error, '%s: unsupported audio file type %r' % (filename, ftype)
0101     (fd, temp) = tempfile.mkstemp()
0102     os.close(fd)
0103     temps.append(temp)
0104     sts = table[ftype].copy(fname, temp)
0105     if sts:
0106         raise error, filename + ': conversion to aiff failed'
0107     return temp
0108 

Generated by PyXR 0.9.4
SourceForge.net Logo