blob: 7fd0153356d8903cce79809e9d611b91082e619c [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Convert "arbitrary" sound files to AIFF (Apple and SGI's audio format).
2
3Input may be compressed.
4Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others.
5An exception is raised if the file is not of a recognized type.
6Returned filename is either the input filename or a temporary filename;
7in the latter case the caller must ensure that it is removed.
8Other temporary files used are removed by the function.
9"""
Guido van Rossum2db91351992-10-18 17:09:59 +000010
11import os
12import tempfile
13import pipes
Fred Drake0652a4e1999-02-24 18:49:15 +000014import sndhdr
Guido van Rossum2db91351992-10-18 17:09:59 +000015
16table = {}
17
Guido van Rossum7bc817d1993-12-17 15:25:27 +000018t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000019t.append('sox -t au - -t aiff -r 8000 -', '--')
20table['au'] = t
21
22# XXX The following is actually sub-optimal.
23# XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4.
24# XXX We must force the output sampling rate else the SGI won't play
25# XXX files sampled at 5.5k or 7.333k; however this means that files
26# XXX sampled at 11k are unnecessarily expanded.
27# XXX Similar comments apply to some other file types.
Guido van Rossum7bc817d1993-12-17 15:25:27 +000028t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000029t.append('sox -t hcom - -t aiff -r 22050 -', '--')
30table['hcom'] = t
31
Guido van Rossum7bc817d1993-12-17 15:25:27 +000032t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000033t.append('sox -t voc - -t aiff -r 11025 -', '--')
34table['voc'] = t
35
Guido van Rossum7bc817d1993-12-17 15:25:27 +000036t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000037t.append('sox -t wav - -t aiff -', '--')
38table['wav'] = t
39
Guido van Rossum7bc817d1993-12-17 15:25:27 +000040t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000041t.append('sox -t 8svx - -t aiff -r 16000 -', '--')
42table['8svx'] = t
43
Guido van Rossum7bc817d1993-12-17 15:25:27 +000044t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000045t.append('sox -t sndt - -t aiff -r 16000 -', '--')
46table['sndt'] = t
47
Guido van Rossum7bc817d1993-12-17 15:25:27 +000048t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000049t.append('sox -t sndr - -t aiff -r 16000 -', '--')
50table['sndr'] = t
51
Guido van Rossum7bc817d1993-12-17 15:25:27 +000052uncompress = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000053uncompress.append('uncompress', '--')
54
55
56error = 'toaiff.error' # Exception
57
58def toaiff(filename):
59 temps = []
60 ret = None
61 try:
62 ret = _toaiff(filename, temps)
63 finally:
64 for temp in temps[:]:
65 if temp <> ret:
66 try:
67 os.unlink(temp)
68 except os.error:
69 pass
70 temps.remove(temp)
71 return ret
72
73def _toaiff(filename, temps):
74 if filename[-2:] == '.Z':
75 fname = tempfile.mktemp()
76 temps.append(fname)
77 sts = uncompress.copy(filename, fname)
78 if sts:
79 raise error, filename + ': uncomress failed'
80 else:
81 fname = filename
82 try:
Fred Drake0652a4e1999-02-24 18:49:15 +000083 ftype = sndhdr.whathdr(fname)
Guido van Rossum2db91351992-10-18 17:09:59 +000084 if ftype:
85 ftype = ftype[0] # All we're interested in
86 except IOError:
87 if type(msg) == type(()) and len(msg) == 2 and \
88 type(msg[0]) == type(0) and type(msg[1]) == type(''):
89 msg = msg[1]
90 if type(msg) <> type(''):
91 msg = `msg`
92 raise error, filename + ': ' + msg
93 if ftype == 'aiff':
94 return fname
95 if ftype == None or not table.has_key(ftype):
96 raise error, \
97 filename + ': unsupported audio file type ' + `ftype`
98 temp = tempfile.mktemp()
99 temps.append(temp)
100 sts = table[ftype].copy(fname, temp)
101 if sts:
102 raise error, filename + ': conversion to aiff failed'
103 return temp