Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: toaiff.py
"""Convert "arbitrary" sound files to AIFF (Apple and SGI's audio format).
[0] Fix | Delete
[1] Fix | Delete
Input may be compressed.
[2] Fix | Delete
Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others.
[3] Fix | Delete
An exception is raised if the file is not of a recognized type.
[4] Fix | Delete
Returned filename is either the input filename or a temporary filename;
[5] Fix | Delete
in the latter case the caller must ensure that it is removed.
[6] Fix | Delete
Other temporary files used are removed by the function.
[7] Fix | Delete
"""
[8] Fix | Delete
from warnings import warnpy3k
[9] Fix | Delete
warnpy3k("the toaiff module has been removed in Python 3.0", stacklevel=2)
[10] Fix | Delete
del warnpy3k
[11] Fix | Delete
[12] Fix | Delete
import os
[13] Fix | Delete
import tempfile
[14] Fix | Delete
import pipes
[15] Fix | Delete
import sndhdr
[16] Fix | Delete
[17] Fix | Delete
__all__ = ["error", "toaiff"]
[18] Fix | Delete
[19] Fix | Delete
table = {}
[20] Fix | Delete
[21] Fix | Delete
t = pipes.Template()
[22] Fix | Delete
t.append('sox -t au - -t aiff -r 8000 -', '--')
[23] Fix | Delete
table['au'] = t
[24] Fix | Delete
[25] Fix | Delete
# XXX The following is actually sub-optimal.
[26] Fix | Delete
# XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4.
[27] Fix | Delete
# XXX We must force the output sampling rate else the SGI won't play
[28] Fix | Delete
# XXX files sampled at 5.5k or 7.333k; however this means that files
[29] Fix | Delete
# XXX sampled at 11k are unnecessarily expanded.
[30] Fix | Delete
# XXX Similar comments apply to some other file types.
[31] Fix | Delete
t = pipes.Template()
[32] Fix | Delete
t.append('sox -t hcom - -t aiff -r 22050 -', '--')
[33] Fix | Delete
table['hcom'] = t
[34] Fix | Delete
[35] Fix | Delete
t = pipes.Template()
[36] Fix | Delete
t.append('sox -t voc - -t aiff -r 11025 -', '--')
[37] Fix | Delete
table['voc'] = t
[38] Fix | Delete
[39] Fix | Delete
t = pipes.Template()
[40] Fix | Delete
t.append('sox -t wav - -t aiff -', '--')
[41] Fix | Delete
table['wav'] = t
[42] Fix | Delete
[43] Fix | Delete
t = pipes.Template()
[44] Fix | Delete
t.append('sox -t 8svx - -t aiff -r 16000 -', '--')
[45] Fix | Delete
table['8svx'] = t
[46] Fix | Delete
[47] Fix | Delete
t = pipes.Template()
[48] Fix | Delete
t.append('sox -t sndt - -t aiff -r 16000 -', '--')
[49] Fix | Delete
table['sndt'] = t
[50] Fix | Delete
[51] Fix | Delete
t = pipes.Template()
[52] Fix | Delete
t.append('sox -t sndr - -t aiff -r 16000 -', '--')
[53] Fix | Delete
table['sndr'] = t
[54] Fix | Delete
[55] Fix | Delete
uncompress = pipes.Template()
[56] Fix | Delete
uncompress.append('uncompress', '--')
[57] Fix | Delete
[58] Fix | Delete
[59] Fix | Delete
class error(Exception):
[60] Fix | Delete
pass
[61] Fix | Delete
[62] Fix | Delete
def toaiff(filename):
[63] Fix | Delete
temps = []
[64] Fix | Delete
ret = None
[65] Fix | Delete
try:
[66] Fix | Delete
ret = _toaiff(filename, temps)
[67] Fix | Delete
finally:
[68] Fix | Delete
for temp in temps[:]:
[69] Fix | Delete
if temp != ret:
[70] Fix | Delete
try:
[71] Fix | Delete
os.unlink(temp)
[72] Fix | Delete
except os.error:
[73] Fix | Delete
pass
[74] Fix | Delete
temps.remove(temp)
[75] Fix | Delete
return ret
[76] Fix | Delete
[77] Fix | Delete
def _toaiff(filename, temps):
[78] Fix | Delete
if filename[-2:] == '.Z':
[79] Fix | Delete
(fd, fname) = tempfile.mkstemp()
[80] Fix | Delete
os.close(fd)
[81] Fix | Delete
temps.append(fname)
[82] Fix | Delete
sts = uncompress.copy(filename, fname)
[83] Fix | Delete
if sts:
[84] Fix | Delete
raise error, filename + ': uncompress failed'
[85] Fix | Delete
else:
[86] Fix | Delete
fname = filename
[87] Fix | Delete
try:
[88] Fix | Delete
ftype = sndhdr.whathdr(fname)
[89] Fix | Delete
if ftype:
[90] Fix | Delete
ftype = ftype[0] # All we're interested in
[91] Fix | Delete
except IOError, msg:
[92] Fix | Delete
if type(msg) == type(()) and len(msg) == 2 and \
[93] Fix | Delete
type(msg[0]) == type(0) and type(msg[1]) == type(''):
[94] Fix | Delete
msg = msg[1]
[95] Fix | Delete
if type(msg) != type(''):
[96] Fix | Delete
msg = repr(msg)
[97] Fix | Delete
raise error, filename + ': ' + msg
[98] Fix | Delete
if ftype == 'aiff':
[99] Fix | Delete
return fname
[100] Fix | Delete
if ftype is None or not ftype in table:
[101] Fix | Delete
raise error, '%s: unsupported audio file type %r' % (filename, ftype)
[102] Fix | Delete
(fd, temp) = tempfile.mkstemp()
[103] Fix | Delete
os.close(fd)
[104] Fix | Delete
temps.append(temp)
[105] Fix | Delete
sts = table[ftype].copy(fname, temp)
[106] Fix | Delete
if sts:
[107] Fix | Delete
raise error, filename + ': conversion to aiff failed'
[108] Fix | Delete
return temp
[109] Fix | Delete
[110] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function