ยปCore Development>Code coverage>Lib/toaiff.py

Python code coverage for Lib/toaiff.py

#countcontent
1n/a"""Convert "arbitrary" sound files to AIFF (Apple and SGI's audio format).
2n/a
3n/aInput may be compressed.
4n/aUncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others.
5n/aAn exception is raised if the file is not of a recognized type.
6n/aReturned filename is either the input filename or a temporary filename;
7n/ain the latter case the caller must ensure that it is removed.
8n/aOther temporary files used are removed by the function.
91"""
101from warnings import warnpy3k
111warnpy3k("the toaiff module has been removed in Python 3.0", stacklevel=2)
121del warnpy3k
13n/a
141import os
151import tempfile
161import pipes
171import sndhdr
18n/a
191__all__ = ["error", "toaiff"]
20n/a
211table = {}
22n/a
231t = pipes.Template()
241t.append('sox -t au - -t aiff -r 8000 -', '--')
251table['au'] = t
26n/a
27n/a# XXX The following is actually sub-optimal.
28n/a# XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4.
29n/a# XXX We must force the output sampling rate else the SGI won't play
30n/a# XXX files sampled at 5.5k or 7.333k; however this means that files
31n/a# XXX sampled at 11k are unnecessarily expanded.
32n/a# XXX Similar comments apply to some other file types.
331t = pipes.Template()
341t.append('sox -t hcom - -t aiff -r 22050 -', '--')
351table['hcom'] = t
36n/a
371t = pipes.Template()
381t.append('sox -t voc - -t aiff -r 11025 -', '--')
391table['voc'] = t
40n/a
411t = pipes.Template()
421t.append('sox -t wav - -t aiff -', '--')
431table['wav'] = t
44n/a
451t = pipes.Template()
461t.append('sox -t 8svx - -t aiff -r 16000 -', '--')
471table['8svx'] = t
48n/a
491t = pipes.Template()
501t.append('sox -t sndt - -t aiff -r 16000 -', '--')
511table['sndt'] = t
52n/a
531t = pipes.Template()
541t.append('sox -t sndr - -t aiff -r 16000 -', '--')
551table['sndr'] = t
56n/a
571uncompress = pipes.Template()
581uncompress.append('uncompress', '--')
59n/a
60n/a
612class error(Exception):
621 pass
63n/a
641def toaiff(filename):
650 temps = []
660 ret = None
670 try:
680 ret = _toaiff(filename, temps)
69n/a finally:
700 for temp in temps[:]:
710 if temp != ret:
720 try:
730 os.unlink(temp)
740 except os.error:
750 pass
760 temps.remove(temp)
770 return ret
78n/a
791def _toaiff(filename, temps):
800 if filename[-2:] == '.Z':
810 (fd, fname) = tempfile.mkstemp()
820 os.close(fd)
830 temps.append(fname)
840 sts = uncompress.copy(filename, fname)
850 if sts:
860 raise error, filename + ': uncompress failed'
87n/a else:
880 fname = filename
890 try:
900 ftype = sndhdr.whathdr(fname)
910 if ftype:
920 ftype = ftype[0] # All we're interested in
930 except IOError, msg:
940 if type(msg) == type(()) and len(msg) == 2 and \
950 type(msg[0]) == type(0) and type(msg[1]) == type(''):
960 msg = msg[1]
970 if type(msg) != type(''):
980 msg = repr(msg)
990 raise error, filename + ': ' + msg
1000 if ftype == 'aiff':
1010 return fname
1020 if ftype is None or not ftype in table:
1030 raise error, '%s: unsupported audio file type %r' % (filename, ftype)
1040 (fd, temp) = tempfile.mkstemp()
1050 os.close(fd)
1060 temps.append(temp)
1070 sts = table[ftype].copy(fname, temp)
1080 if sts:
1090 raise error, filename + ': conversion to aiff failed'
1100 return temp