1 # Convert "arbitrary" sound files to AIFF files (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.
17 t
.append('sox -t au - -t aiff -r 8000 -', '--')
20 # XXX The following is actually sub-optimal.
21 # XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4.
22 # XXX We must force the output sampling rate else the SGI won't play
23 # XXX files sampled at 5.5k or 7.333k; however this means that files
24 # XXX sampled at 11k are unnecessarily expanded.
25 # XXX Similar comments apply to some other file types.
27 t
.append('sox -t hcom - -t aiff -r 22050 -', '--')
31 t
.append('sox -t voc - -t aiff -r 11025 -', '--')
35 t
.append('sox -t wav - -t aiff -', '--')
39 t
.append('sox -t 8svx - -t aiff -r 16000 -', '--')
43 t
.append('sox -t sndt - -t aiff -r 16000 -', '--')
47 t
.append('sox -t sndr - -t aiff -r 16000 -', '--')
50 uncompress
= pipes
.Template()
51 uncompress
.append('uncompress', '--')
54 error
= 'toaiff.error' # Exception
60 ret
= _toaiff(filename
, temps
)
71 def _toaiff(filename
, temps
):
72 if filename
[-2:] == '.Z':
73 fname
= tempfile
.mktemp()
75 sts
= uncompress
.copy(filename
, fname
)
77 raise error
, filename
+ ': uncomress failed'
81 ftype
= sndhdr
.whathdr(fname
)
83 ftype
= ftype
[0] # All we're interested in
85 if type(msg
) == type(()) and len(msg
) == 2 and \
86 type(msg
[0]) == type(0) and type(msg
[1]) == type(''):
88 if type(msg
) <> type(''):
90 raise error
, filename
+ ': ' + msg
93 if ftype
== None or not table
.has_key(ftype
):
95 filename
+ ': unsupported audio file type ' + `ftype`
96 temp
= tempfile
.mktemp()
98 sts
= table
[ftype
].copy(fname
, temp
)
100 raise error
, filename
+ ': conversion to aiff failed'