blob: 51752f42336fc271a8c8f79c2d4f4d3cfc0ecd14 [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
Skip Montanaro40fc1602001-03-01 04:27:19 +000016__all__ = ["error", "toaiff"]
17
Guido van Rossum2db91351992-10-18 17:09:59 +000018table = {}
19
Guido van Rossum7bc817d1993-12-17 15:25:27 +000020t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000021t.append('sox -t au - -t aiff -r 8000 -', '--')
22table['au'] = t
23
24# XXX The following is actually sub-optimal.
25# XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4.
26# XXX We must force the output sampling rate else the SGI won't play
27# XXX files sampled at 5.5k or 7.333k; however this means that files
28# XXX sampled at 11k are unnecessarily expanded.
29# XXX Similar comments apply to some other file types.
Guido van Rossum7bc817d1993-12-17 15:25:27 +000030t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000031t.append('sox -t hcom - -t aiff -r 22050 -', '--')
32table['hcom'] = t
33
Guido van Rossum7bc817d1993-12-17 15:25:27 +000034t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000035t.append('sox -t voc - -t aiff -r 11025 -', '--')
36table['voc'] = t
37
Guido van Rossum7bc817d1993-12-17 15:25:27 +000038t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000039t.append('sox -t wav - -t aiff -', '--')
40table['wav'] = t
41
Guido van Rossum7bc817d1993-12-17 15:25:27 +000042t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000043t.append('sox -t 8svx - -t aiff -r 16000 -', '--')
44table['8svx'] = t
45
Guido van Rossum7bc817d1993-12-17 15:25:27 +000046t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000047t.append('sox -t sndt - -t aiff -r 16000 -', '--')
48table['sndt'] = t
49
Guido van Rossum7bc817d1993-12-17 15:25:27 +000050t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000051t.append('sox -t sndr - -t aiff -r 16000 -', '--')
52table['sndr'] = t
53
Guido van Rossum7bc817d1993-12-17 15:25:27 +000054uncompress = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000055uncompress.append('uncompress', '--')
56
57
Fred Drake9b8d8012000-08-17 04:45:13 +000058class error(Exception):
Tim Petersb90f89a2001-01-15 03:26:36 +000059 pass
Guido van Rossum2db91351992-10-18 17:09:59 +000060
61def toaiff(filename):
Tim Petersb90f89a2001-01-15 03:26:36 +000062 temps = []
63 ret = None
64 try:
65 ret = _toaiff(filename, temps)
66 finally:
67 for temp in temps[:]:
68 if temp != ret:
69 try:
70 os.unlink(temp)
71 except os.error:
72 pass
73 temps.remove(temp)
74 return ret
Guido van Rossum2db91351992-10-18 17:09:59 +000075
76def _toaiff(filename, temps):
Tim Petersb90f89a2001-01-15 03:26:36 +000077 if filename[-2:] == '.Z':
Guido van Rossum3b0a3292002-08-09 16:38:32 +000078 (fd, fname) = tempfile.mkstemp()
79 os.close(fd)
Tim Petersb90f89a2001-01-15 03:26:36 +000080 temps.append(fname)
81 sts = uncompress.copy(filename, fname)
82 if sts:
83 raise error, filename + ': uncompress failed'
84 else:
85 fname = filename
86 try:
87 ftype = sndhdr.whathdr(fname)
88 if ftype:
89 ftype = ftype[0] # All we're interested in
Andrew M. Kuchling994b51e2001-08-13 14:40:47 +000090 except IOError, msg:
Tim Petersb90f89a2001-01-15 03:26:36 +000091 if type(msg) == type(()) and len(msg) == 2 and \
92 type(msg[0]) == type(0) and type(msg[1]) == type(''):
93 msg = msg[1]
94 if type(msg) != type(''):
95 msg = `msg`
96 raise error, filename + ': ' + msg
97 if ftype == 'aiff':
98 return fname
Raymond Hettinger54f02222002-06-01 14:18:47 +000099 if ftype is None or not ftype in table:
Tim Petersb90f89a2001-01-15 03:26:36 +0000100 raise error, \
101 filename + ': unsupported audio file type ' + `ftype`
Guido van Rossumdd8ddac2002-08-10 00:17:59 +0000102 (fd, temp) = tempfile.mkstemp()
Guido van Rossum3b0a3292002-08-09 16:38:32 +0000103 os.close(fd)
Tim Petersb90f89a2001-01-15 03:26:36 +0000104 temps.append(temp)
105 sts = table[ftype].copy(fname, temp)
106 if sts:
107 raise error, filename + ': conversion to aiff failed'
108 return temp