"""Convert "arbitrary" sound files to AIFF (Apple and SGI's audio format).
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.
__all__
= ["error", "toaiff"]
t
.append('sox -t au - -t aiff -r 8000 -', '--')
# 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.
t
.append('sox -t hcom - -t aiff -r 22050 -', '--')
t
.append('sox -t voc - -t aiff -r 11025 -', '--')
t
.append('sox -t wav - -t aiff -', '--')
t
.append('sox -t 8svx - -t aiff -r 16000 -', '--')
t
.append('sox -t sndt - -t aiff -r 16000 -', '--')
t
.append('sox -t sndr - -t aiff -r 16000 -', '--')
uncompress
= pipes
.Template()
uncompress
.append('uncompress', '--')
ret
= _toaiff(filename
, temps
)
def _toaiff(filename
, temps
):
if filename
[-2:] == '.Z':
(fd
, fname
) = tempfile
.mkstemp()
sts
= uncompress
.copy(filename
, fname
)
raise error
, filename
+ ': uncompress failed'
ftype
= sndhdr
.whathdr(fname
)
ftype
= ftype
[0] # All we're interested in
if type(msg
) == type(()) and len(msg
) == 2 and \
type(msg
[0]) == type(0) and type(msg
[1]) == type(''):
if type(msg
) != type(''):
raise error
, filename
+ ': ' + msg
if ftype
is None or not ftype
in table
:
raise error
, '%s: unsupported audio file type %r' % (filename
, ftype
)
(fd
, temp
) = tempfile
.mkstemp()
sts
= table
[ftype
].copy(fname
, temp
)
raise error
, filename
+ ': conversion to aiff failed'