blob: 3a2b80cbe383be8075f1b2198e9a1c1c330067b5 [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"""
Brett Cannonfa24d9a2008-05-09 05:32:42 +000010from warnings import warnpy3k
11warnpy3k("the toaiff module has been removed in Python 3.0", stacklevel=2)
12del warnpy3k
Guido van Rossum2db91351992-10-18 17:09:59 +000013
14import os
15import tempfile
16import pipes
Fred Drake0652a4e1999-02-24 18:49:15 +000017import sndhdr
Guido van Rossum2db91351992-10-18 17:09:59 +000018
Skip Montanaro40fc1602001-03-01 04:27:19 +000019__all__ = ["error", "toaiff"]
20
Guido van Rossum2db91351992-10-18 17:09:59 +000021table = {}
22
Guido van Rossum7bc817d1993-12-17 15:25:27 +000023t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000024t.append('sox -t au - -t aiff -r 8000 -', '--')
25table['au'] = t
26
27# XXX The following is actually sub-optimal.
28# XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4.
29# XXX We must force the output sampling rate else the SGI won't play
30# XXX files sampled at 5.5k or 7.333k; however this means that files
31# XXX sampled at 11k are unnecessarily expanded.
32# XXX Similar comments apply to some other file types.
Guido van Rossum7bc817d1993-12-17 15:25:27 +000033t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000034t.append('sox -t hcom - -t aiff -r 22050 -', '--')
35table['hcom'] = t
36
Guido van Rossum7bc817d1993-12-17 15:25:27 +000037t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000038t.append('sox -t voc - -t aiff -r 11025 -', '--')
39table['voc'] = t
40
Guido van Rossum7bc817d1993-12-17 15:25:27 +000041t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000042t.append('sox -t wav - -t aiff -', '--')
43table['wav'] = t
44
Guido van Rossum7bc817d1993-12-17 15:25:27 +000045t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000046t.append('sox -t 8svx - -t aiff -r 16000 -', '--')
47table['8svx'] = t
48
Guido van Rossum7bc817d1993-12-17 15:25:27 +000049t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000050t.append('sox -t sndt - -t aiff -r 16000 -', '--')
51table['sndt'] = t
52
Guido van Rossum7bc817d1993-12-17 15:25:27 +000053t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000054t.append('sox -t sndr - -t aiff -r 16000 -', '--')
55table['sndr'] = t
56
Guido van Rossum7bc817d1993-12-17 15:25:27 +000057uncompress = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000058uncompress.append('uncompress', '--')
59
60
Fred Drake9b8d8012000-08-17 04:45:13 +000061class error(Exception):
Tim Petersb90f89a2001-01-15 03:26:36 +000062 pass
Guido van Rossum2db91351992-10-18 17:09:59 +000063
64def toaiff(filename):
Tim Petersb90f89a2001-01-15 03:26:36 +000065 temps = []
66 ret = None
67 try:
68 ret = _toaiff(filename, temps)
69 finally:
70 for temp in temps[:]:
71 if temp != ret:
72 try:
73 os.unlink(temp)
74 except os.error:
75 pass
76 temps.remove(temp)
77 return ret
Guido van Rossum2db91351992-10-18 17:09:59 +000078
79def _toaiff(filename, temps):
Tim Petersb90f89a2001-01-15 03:26:36 +000080 if filename[-2:] == '.Z':
Guido van Rossum3b0a3292002-08-09 16:38:32 +000081 (fd, fname) = tempfile.mkstemp()
82 os.close(fd)
Tim Petersb90f89a2001-01-15 03:26:36 +000083 temps.append(fname)
84 sts = uncompress.copy(filename, fname)
85 if sts:
86 raise error, filename + ': uncompress failed'
87 else:
88 fname = filename
89 try:
90 ftype = sndhdr.whathdr(fname)
91 if ftype:
92 ftype = ftype[0] # All we're interested in
Andrew M. Kuchling994b51e2001-08-13 14:40:47 +000093 except IOError, msg:
Tim Petersb90f89a2001-01-15 03:26:36 +000094 if type(msg) == type(()) and len(msg) == 2 and \
95 type(msg[0]) == type(0) and type(msg[1]) == type(''):
96 msg = msg[1]
97 if type(msg) != type(''):
Walter Dörwald70a6b492004-02-12 17:35:32 +000098 msg = repr(msg)
Tim Petersb90f89a2001-01-15 03:26:36 +000099 raise error, filename + ': ' + msg
100 if ftype == 'aiff':
101 return fname
Raymond Hettinger54f02222002-06-01 14:18:47 +0000102 if ftype is None or not ftype in table:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000103 raise error, '%s: unsupported audio file type %r' % (filename, ftype)
Guido van Rossumdd8ddac2002-08-10 00:17:59 +0000104 (fd, temp) = tempfile.mkstemp()
Guido van Rossum3b0a3292002-08-09 16:38:32 +0000105 os.close(fd)
Tim Petersb90f89a2001-01-15 03:26:36 +0000106 temps.append(temp)
107 sts = table[ftype].copy(fname, temp)
108 if sts:
109 raise error, filename + ': conversion to aiff failed'
110 return temp