2000-02-04 16:28:42 +01:00
|
|
|
"""Convert "arbitrary" sound files to AIFF (Apple and SGI's audio format).
|
|
|
|
|
|
|
|
Input may be compressed.
|
|
|
|
Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others.
|
|
|
|
An exception is raised if the file is not of a recognized type.
|
|
|
|
Returned filename is either the input filename or a temporary filename;
|
|
|
|
in the latter case the caller must ensure that it is removed.
|
|
|
|
Other temporary files used are removed by the function.
|
|
|
|
"""
|
1992-10-18 18:09:59 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import pipes
|
1999-02-24 19:49:15 +01:00
|
|
|
import sndhdr
|
1992-10-18 18:09:59 +01:00
|
|
|
|
2001-03-01 05:27:19 +01:00
|
|
|
__all__ = ["error", "toaiff"]
|
|
|
|
|
1992-10-18 18:09:59 +01:00
|
|
|
table = {}
|
|
|
|
|
1993-12-17 16:25:27 +01:00
|
|
|
t = pipes.Template()
|
1992-10-18 18:09:59 +01:00
|
|
|
t.append('sox -t au - -t aiff -r 8000 -', '--')
|
|
|
|
table['au'] = t
|
|
|
|
|
|
|
|
# XXX The following is actually sub-optimal.
|
|
|
|
# XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4.
|
|
|
|
# XXX We must force the output sampling rate else the SGI won't play
|
|
|
|
# XXX files sampled at 5.5k or 7.333k; however this means that files
|
|
|
|
# XXX sampled at 11k are unnecessarily expanded.
|
|
|
|
# XXX Similar comments apply to some other file types.
|
1993-12-17 16:25:27 +01:00
|
|
|
t = pipes.Template()
|
1992-10-18 18:09:59 +01:00
|
|
|
t.append('sox -t hcom - -t aiff -r 22050 -', '--')
|
|
|
|
table['hcom'] = t
|
|
|
|
|
1993-12-17 16:25:27 +01:00
|
|
|
t = pipes.Template()
|
1992-10-18 18:09:59 +01:00
|
|
|
t.append('sox -t voc - -t aiff -r 11025 -', '--')
|
|
|
|
table['voc'] = t
|
|
|
|
|
1993-12-17 16:25:27 +01:00
|
|
|
t = pipes.Template()
|
1992-10-18 18:09:59 +01:00
|
|
|
t.append('sox -t wav - -t aiff -', '--')
|
|
|
|
table['wav'] = t
|
|
|
|
|
1993-12-17 16:25:27 +01:00
|
|
|
t = pipes.Template()
|
1992-10-18 18:09:59 +01:00
|
|
|
t.append('sox -t 8svx - -t aiff -r 16000 -', '--')
|
|
|
|
table['8svx'] = t
|
|
|
|
|
1993-12-17 16:25:27 +01:00
|
|
|
t = pipes.Template()
|
1992-10-18 18:09:59 +01:00
|
|
|
t.append('sox -t sndt - -t aiff -r 16000 -', '--')
|
|
|
|
table['sndt'] = t
|
|
|
|
|
1993-12-17 16:25:27 +01:00
|
|
|
t = pipes.Template()
|
1992-10-18 18:09:59 +01:00
|
|
|
t.append('sox -t sndr - -t aiff -r 16000 -', '--')
|
|
|
|
table['sndr'] = t
|
|
|
|
|
1993-12-17 16:25:27 +01:00
|
|
|
uncompress = pipes.Template()
|
1992-10-18 18:09:59 +01:00
|
|
|
uncompress.append('uncompress', '--')
|
|
|
|
|
|
|
|
|
2000-08-17 06:45:13 +02:00
|
|
|
class error(Exception):
|
2001-01-15 04:26:36 +01:00
|
|
|
pass
|
1992-10-18 18:09:59 +01:00
|
|
|
|
|
|
|
def toaiff(filename):
|
2001-01-15 04:26:36 +01:00
|
|
|
temps = []
|
|
|
|
ret = None
|
|
|
|
try:
|
|
|
|
ret = _toaiff(filename, temps)
|
|
|
|
finally:
|
|
|
|
for temp in temps[:]:
|
|
|
|
if temp != ret:
|
|
|
|
try:
|
|
|
|
os.unlink(temp)
|
|
|
|
except os.error:
|
|
|
|
pass
|
|
|
|
temps.remove(temp)
|
|
|
|
return ret
|
1992-10-18 18:09:59 +01:00
|
|
|
|
|
|
|
def _toaiff(filename, temps):
|
2001-01-15 04:26:36 +01:00
|
|
|
if filename[-2:] == '.Z':
|
2002-08-09 18:38:32 +02:00
|
|
|
(fd, fname) = tempfile.mkstemp()
|
|
|
|
os.close(fd)
|
2001-01-15 04:26:36 +01:00
|
|
|
temps.append(fname)
|
|
|
|
sts = uncompress.copy(filename, fname)
|
|
|
|
if sts:
|
|
|
|
raise error, filename + ': uncompress failed'
|
|
|
|
else:
|
|
|
|
fname = filename
|
|
|
|
try:
|
|
|
|
ftype = sndhdr.whathdr(fname)
|
|
|
|
if ftype:
|
|
|
|
ftype = ftype[0] # All we're interested in
|
2001-08-13 16:40:47 +02:00
|
|
|
except IOError, msg:
|
2001-01-15 04:26:36 +01:00
|
|
|
if type(msg) == type(()) and len(msg) == 2 and \
|
|
|
|
type(msg[0]) == type(0) and type(msg[1]) == type(''):
|
|
|
|
msg = msg[1]
|
|
|
|
if type(msg) != type(''):
|
2004-02-12 18:35:32 +01:00
|
|
|
msg = repr(msg)
|
2001-01-15 04:26:36 +01:00
|
|
|
raise error, filename + ': ' + msg
|
|
|
|
if ftype == 'aiff':
|
|
|
|
return fname
|
2002-06-01 16:18:47 +02:00
|
|
|
if ftype is None or not ftype in table:
|
2004-02-12 18:35:32 +01:00
|
|
|
raise error, '%s: unsupported audio file type %r' % (filename, ftype)
|
2002-08-10 02:17:59 +02:00
|
|
|
(fd, temp) = tempfile.mkstemp()
|
2002-08-09 18:38:32 +02:00
|
|
|
os.close(fd)
|
2001-01-15 04:26:36 +01:00
|
|
|
temps.append(temp)
|
|
|
|
sts = table[ftype].copy(fname, temp)
|
|
|
|
if sts:
|
|
|
|
raise error, filename + ': conversion to aiff failed'
|
|
|
|
return temp
|