Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: sunau.py
"""Stuff to parse Sun and NeXT audio files.
[0] Fix | Delete
[1] Fix | Delete
An audio file consists of a header followed by the data. The structure
[2] Fix | Delete
of the header is as follows.
[3] Fix | Delete
[4] Fix | Delete
+---------------+
[5] Fix | Delete
| magic word |
[6] Fix | Delete
+---------------+
[7] Fix | Delete
| header size |
[8] Fix | Delete
+---------------+
[9] Fix | Delete
| data size |
[10] Fix | Delete
+---------------+
[11] Fix | Delete
| encoding |
[12] Fix | Delete
+---------------+
[13] Fix | Delete
| sample rate |
[14] Fix | Delete
+---------------+
[15] Fix | Delete
| # of channels |
[16] Fix | Delete
+---------------+
[17] Fix | Delete
| info |
[18] Fix | Delete
| |
[19] Fix | Delete
+---------------+
[20] Fix | Delete
[21] Fix | Delete
The magic word consists of the 4 characters '.snd'. Apart from the
[22] Fix | Delete
info field, all header fields are 4 bytes in size. They are all
[23] Fix | Delete
32-bit unsigned integers encoded in big-endian byte order.
[24] Fix | Delete
[25] Fix | Delete
The header size really gives the start of the data.
[26] Fix | Delete
The data size is the physical size of the data. From the other
[27] Fix | Delete
parameters the number of frames can be calculated.
[28] Fix | Delete
The encoding gives the way in which audio samples are encoded.
[29] Fix | Delete
Possible values are listed below.
[30] Fix | Delete
The info field currently consists of an ASCII string giving a
[31] Fix | Delete
human-readable description of the audio file. The info field is
[32] Fix | Delete
padded with NUL bytes to the header size.
[33] Fix | Delete
[34] Fix | Delete
Usage.
[35] Fix | Delete
[36] Fix | Delete
Reading audio files:
[37] Fix | Delete
f = sunau.open(file, 'r')
[38] Fix | Delete
where file is either the name of a file or an open file pointer.
[39] Fix | Delete
The open file pointer must have methods read(), seek(), and close().
[40] Fix | Delete
When the setpos() and rewind() methods are not used, the seek()
[41] Fix | Delete
method is not necessary.
[42] Fix | Delete
[43] Fix | Delete
This returns an instance of a class with the following public methods:
[44] Fix | Delete
getnchannels() -- returns number of audio channels (1 for
[45] Fix | Delete
mono, 2 for stereo)
[46] Fix | Delete
getsampwidth() -- returns sample width in bytes
[47] Fix | Delete
getframerate() -- returns sampling frequency
[48] Fix | Delete
getnframes() -- returns number of audio frames
[49] Fix | Delete
getcomptype() -- returns compression type ('NONE' or 'ULAW')
[50] Fix | Delete
getcompname() -- returns human-readable version of
[51] Fix | Delete
compression type ('not compressed' matches 'NONE')
[52] Fix | Delete
getparams() -- returns a tuple consisting of all of the
[53] Fix | Delete
above in the above order
[54] Fix | Delete
getmarkers() -- returns None (for compatibility with the
[55] Fix | Delete
aifc module)
[56] Fix | Delete
getmark(id) -- raises an error since the mark does not
[57] Fix | Delete
exist (for compatibility with the aifc module)
[58] Fix | Delete
readframes(n) -- returns at most n frames of audio
[59] Fix | Delete
rewind() -- rewind to the beginning of the audio stream
[60] Fix | Delete
setpos(pos) -- seek to the specified position
[61] Fix | Delete
tell() -- return the current position
[62] Fix | Delete
close() -- close the instance (make it unusable)
[63] Fix | Delete
The position returned by tell() and the position given to setpos()
[64] Fix | Delete
are compatible and have nothing to do with the actual position in the
[65] Fix | Delete
file.
[66] Fix | Delete
The close() method is called automatically when the class instance
[67] Fix | Delete
is destroyed.
[68] Fix | Delete
[69] Fix | Delete
Writing audio files:
[70] Fix | Delete
f = sunau.open(file, 'w')
[71] Fix | Delete
where file is either the name of a file or an open file pointer.
[72] Fix | Delete
The open file pointer must have methods write(), tell(), seek(), and
[73] Fix | Delete
close().
[74] Fix | Delete
[75] Fix | Delete
This returns an instance of a class with the following public methods:
[76] Fix | Delete
setnchannels(n) -- set the number of channels
[77] Fix | Delete
setsampwidth(n) -- set the sample width
[78] Fix | Delete
setframerate(n) -- set the frame rate
[79] Fix | Delete
setnframes(n) -- set the number of frames
[80] Fix | Delete
setcomptype(type, name)
[81] Fix | Delete
-- set the compression type and the
[82] Fix | Delete
human-readable compression type
[83] Fix | Delete
setparams(tuple)-- set all parameters at once
[84] Fix | Delete
tell() -- return current position in output file
[85] Fix | Delete
writeframesraw(data)
[86] Fix | Delete
-- write audio frames without pathing up the
[87] Fix | Delete
file header
[88] Fix | Delete
writeframes(data)
[89] Fix | Delete
-- write audio frames and patch up the file header
[90] Fix | Delete
close() -- patch up the file header and close the
[91] Fix | Delete
output file
[92] Fix | Delete
You should set the parameters before the first writeframesraw or
[93] Fix | Delete
writeframes. The total number of frames does not need to be set,
[94] Fix | Delete
but when it is set to the correct value, the header does not have to
[95] Fix | Delete
be patched up.
[96] Fix | Delete
It is best to first set all parameters, perhaps possibly the
[97] Fix | Delete
compression type, and then write audio frames using writeframesraw.
[98] Fix | Delete
When all frames have been written, either call writeframes('') or
[99] Fix | Delete
close() to patch up the sizes in the header.
[100] Fix | Delete
The close() method is called automatically when the class instance
[101] Fix | Delete
is destroyed.
[102] Fix | Delete
"""
[103] Fix | Delete
[104] Fix | Delete
# from <multimedia/audio_filehdr.h>
[105] Fix | Delete
AUDIO_FILE_MAGIC = 0x2e736e64
[106] Fix | Delete
AUDIO_FILE_ENCODING_MULAW_8 = 1
[107] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_8 = 2
[108] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_16 = 3
[109] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_24 = 4
[110] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_32 = 5
[111] Fix | Delete
AUDIO_FILE_ENCODING_FLOAT = 6
[112] Fix | Delete
AUDIO_FILE_ENCODING_DOUBLE = 7
[113] Fix | Delete
AUDIO_FILE_ENCODING_ADPCM_G721 = 23
[114] Fix | Delete
AUDIO_FILE_ENCODING_ADPCM_G722 = 24
[115] Fix | Delete
AUDIO_FILE_ENCODING_ADPCM_G723_3 = 25
[116] Fix | Delete
AUDIO_FILE_ENCODING_ADPCM_G723_5 = 26
[117] Fix | Delete
AUDIO_FILE_ENCODING_ALAW_8 = 27
[118] Fix | Delete
[119] Fix | Delete
# from <multimedia/audio_hdr.h>
[120] Fix | Delete
AUDIO_UNKNOWN_SIZE = 0xFFFFFFFFL # ((unsigned)(~0))
[121] Fix | Delete
[122] Fix | Delete
_simple_encodings = [AUDIO_FILE_ENCODING_MULAW_8,
[123] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_8,
[124] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_16,
[125] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_24,
[126] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_32,
[127] Fix | Delete
AUDIO_FILE_ENCODING_ALAW_8]
[128] Fix | Delete
[129] Fix | Delete
class Error(Exception):
[130] Fix | Delete
pass
[131] Fix | Delete
[132] Fix | Delete
def _read_u32(file):
[133] Fix | Delete
x = 0L
[134] Fix | Delete
for i in range(4):
[135] Fix | Delete
byte = file.read(1)
[136] Fix | Delete
if byte == '':
[137] Fix | Delete
raise EOFError
[138] Fix | Delete
x = x*256 + ord(byte)
[139] Fix | Delete
return x
[140] Fix | Delete
[141] Fix | Delete
def _write_u32(file, x):
[142] Fix | Delete
data = []
[143] Fix | Delete
for i in range(4):
[144] Fix | Delete
d, m = divmod(x, 256)
[145] Fix | Delete
data.insert(0, m)
[146] Fix | Delete
x = d
[147] Fix | Delete
for i in range(4):
[148] Fix | Delete
file.write(chr(int(data[i])))
[149] Fix | Delete
[150] Fix | Delete
class Au_read:
[151] Fix | Delete
[152] Fix | Delete
def __init__(self, f):
[153] Fix | Delete
if type(f) == type(''):
[154] Fix | Delete
import __builtin__
[155] Fix | Delete
f = __builtin__.open(f, 'rb')
[156] Fix | Delete
self.initfp(f)
[157] Fix | Delete
[158] Fix | Delete
def __del__(self):
[159] Fix | Delete
if self._file:
[160] Fix | Delete
self.close()
[161] Fix | Delete
[162] Fix | Delete
def initfp(self, file):
[163] Fix | Delete
self._file = file
[164] Fix | Delete
self._soundpos = 0
[165] Fix | Delete
magic = int(_read_u32(file))
[166] Fix | Delete
if magic != AUDIO_FILE_MAGIC:
[167] Fix | Delete
raise Error, 'bad magic number'
[168] Fix | Delete
self._hdr_size = int(_read_u32(file))
[169] Fix | Delete
if self._hdr_size < 24:
[170] Fix | Delete
raise Error, 'header size too small'
[171] Fix | Delete
if self._hdr_size > 100:
[172] Fix | Delete
raise Error, 'header size ridiculously large'
[173] Fix | Delete
self._data_size = _read_u32(file)
[174] Fix | Delete
if self._data_size != AUDIO_UNKNOWN_SIZE:
[175] Fix | Delete
self._data_size = int(self._data_size)
[176] Fix | Delete
self._encoding = int(_read_u32(file))
[177] Fix | Delete
if self._encoding not in _simple_encodings:
[178] Fix | Delete
raise Error, 'encoding not (yet) supported'
[179] Fix | Delete
if self._encoding in (AUDIO_FILE_ENCODING_MULAW_8,
[180] Fix | Delete
AUDIO_FILE_ENCODING_ALAW_8):
[181] Fix | Delete
self._sampwidth = 2
[182] Fix | Delete
self._framesize = 1
[183] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_8:
[184] Fix | Delete
self._framesize = self._sampwidth = 1
[185] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_16:
[186] Fix | Delete
self._framesize = self._sampwidth = 2
[187] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_24:
[188] Fix | Delete
self._framesize = self._sampwidth = 3
[189] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_32:
[190] Fix | Delete
self._framesize = self._sampwidth = 4
[191] Fix | Delete
else:
[192] Fix | Delete
raise Error, 'unknown encoding'
[193] Fix | Delete
self._framerate = int(_read_u32(file))
[194] Fix | Delete
self._nchannels = int(_read_u32(file))
[195] Fix | Delete
self._framesize = self._framesize * self._nchannels
[196] Fix | Delete
if self._hdr_size > 24:
[197] Fix | Delete
self._info = file.read(self._hdr_size - 24)
[198] Fix | Delete
for i in range(len(self._info)):
[199] Fix | Delete
if self._info[i] == '\0':
[200] Fix | Delete
self._info = self._info[:i]
[201] Fix | Delete
break
[202] Fix | Delete
else:
[203] Fix | Delete
self._info = ''
[204] Fix | Delete
try:
[205] Fix | Delete
self._data_pos = file.tell()
[206] Fix | Delete
except (AttributeError, IOError):
[207] Fix | Delete
self._data_pos = None
[208] Fix | Delete
[209] Fix | Delete
def getfp(self):
[210] Fix | Delete
return self._file
[211] Fix | Delete
[212] Fix | Delete
def getnchannels(self):
[213] Fix | Delete
return self._nchannels
[214] Fix | Delete
[215] Fix | Delete
def getsampwidth(self):
[216] Fix | Delete
return self._sampwidth
[217] Fix | Delete
[218] Fix | Delete
def getframerate(self):
[219] Fix | Delete
return self._framerate
[220] Fix | Delete
[221] Fix | Delete
def getnframes(self):
[222] Fix | Delete
if self._data_size == AUDIO_UNKNOWN_SIZE:
[223] Fix | Delete
return AUDIO_UNKNOWN_SIZE
[224] Fix | Delete
if self._encoding in _simple_encodings:
[225] Fix | Delete
return self._data_size // self._framesize
[226] Fix | Delete
return 0 # XXX--must do some arithmetic here
[227] Fix | Delete
[228] Fix | Delete
def getcomptype(self):
[229] Fix | Delete
if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
[230] Fix | Delete
return 'ULAW'
[231] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
[232] Fix | Delete
return 'ALAW'
[233] Fix | Delete
else:
[234] Fix | Delete
return 'NONE'
[235] Fix | Delete
[236] Fix | Delete
def getcompname(self):
[237] Fix | Delete
if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
[238] Fix | Delete
return 'CCITT G.711 u-law'
[239] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
[240] Fix | Delete
return 'CCITT G.711 A-law'
[241] Fix | Delete
else:
[242] Fix | Delete
return 'not compressed'
[243] Fix | Delete
[244] Fix | Delete
def getparams(self):
[245] Fix | Delete
return self.getnchannels(), self.getsampwidth(), \
[246] Fix | Delete
self.getframerate(), self.getnframes(), \
[247] Fix | Delete
self.getcomptype(), self.getcompname()
[248] Fix | Delete
[249] Fix | Delete
def getmarkers(self):
[250] Fix | Delete
return None
[251] Fix | Delete
[252] Fix | Delete
def getmark(self, id):
[253] Fix | Delete
raise Error, 'no marks'
[254] Fix | Delete
[255] Fix | Delete
def readframes(self, nframes):
[256] Fix | Delete
if self._encoding in _simple_encodings:
[257] Fix | Delete
if nframes == AUDIO_UNKNOWN_SIZE:
[258] Fix | Delete
data = self._file.read()
[259] Fix | Delete
else:
[260] Fix | Delete
data = self._file.read(nframes * self._framesize)
[261] Fix | Delete
self._soundpos += len(data) // self._framesize
[262] Fix | Delete
if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
[263] Fix | Delete
import audioop
[264] Fix | Delete
data = audioop.ulaw2lin(data, self._sampwidth)
[265] Fix | Delete
return data
[266] Fix | Delete
return None # XXX--not implemented yet
[267] Fix | Delete
[268] Fix | Delete
def rewind(self):
[269] Fix | Delete
if self._data_pos is None:
[270] Fix | Delete
raise IOError('cannot seek')
[271] Fix | Delete
self._file.seek(self._data_pos)
[272] Fix | Delete
self._soundpos = 0
[273] Fix | Delete
[274] Fix | Delete
def tell(self):
[275] Fix | Delete
return self._soundpos
[276] Fix | Delete
[277] Fix | Delete
def setpos(self, pos):
[278] Fix | Delete
if pos < 0 or pos > self.getnframes():
[279] Fix | Delete
raise Error, 'position not in range'
[280] Fix | Delete
if self._data_pos is None:
[281] Fix | Delete
raise IOError('cannot seek')
[282] Fix | Delete
self._file.seek(self._data_pos + pos * self._framesize)
[283] Fix | Delete
self._soundpos = pos
[284] Fix | Delete
[285] Fix | Delete
def close(self):
[286] Fix | Delete
self._file = None
[287] Fix | Delete
[288] Fix | Delete
class Au_write:
[289] Fix | Delete
[290] Fix | Delete
def __init__(self, f):
[291] Fix | Delete
if type(f) == type(''):
[292] Fix | Delete
import __builtin__
[293] Fix | Delete
f = __builtin__.open(f, 'wb')
[294] Fix | Delete
self.initfp(f)
[295] Fix | Delete
[296] Fix | Delete
def __del__(self):
[297] Fix | Delete
if self._file:
[298] Fix | Delete
self.close()
[299] Fix | Delete
[300] Fix | Delete
def initfp(self, file):
[301] Fix | Delete
self._file = file
[302] Fix | Delete
self._framerate = 0
[303] Fix | Delete
self._nchannels = 0
[304] Fix | Delete
self._sampwidth = 0
[305] Fix | Delete
self._framesize = 0
[306] Fix | Delete
self._nframes = AUDIO_UNKNOWN_SIZE
[307] Fix | Delete
self._nframeswritten = 0
[308] Fix | Delete
self._datawritten = 0
[309] Fix | Delete
self._datalength = 0
[310] Fix | Delete
self._info = ''
[311] Fix | Delete
self._comptype = 'ULAW' # default is U-law
[312] Fix | Delete
[313] Fix | Delete
def setnchannels(self, nchannels):
[314] Fix | Delete
if self._nframeswritten:
[315] Fix | Delete
raise Error, 'cannot change parameters after starting to write'
[316] Fix | Delete
if nchannels not in (1, 2, 4):
[317] Fix | Delete
raise Error, 'only 1, 2, or 4 channels supported'
[318] Fix | Delete
self._nchannels = nchannels
[319] Fix | Delete
[320] Fix | Delete
def getnchannels(self):
[321] Fix | Delete
if not self._nchannels:
[322] Fix | Delete
raise Error, 'number of channels not set'
[323] Fix | Delete
return self._nchannels
[324] Fix | Delete
[325] Fix | Delete
def setsampwidth(self, sampwidth):
[326] Fix | Delete
if self._nframeswritten:
[327] Fix | Delete
raise Error, 'cannot change parameters after starting to write'
[328] Fix | Delete
if sampwidth not in (1, 2, 4):
[329] Fix | Delete
raise Error, 'bad sample width'
[330] Fix | Delete
self._sampwidth = sampwidth
[331] Fix | Delete
[332] Fix | Delete
def getsampwidth(self):
[333] Fix | Delete
if not self._framerate:
[334] Fix | Delete
raise Error, 'sample width not specified'
[335] Fix | Delete
return self._sampwidth
[336] Fix | Delete
[337] Fix | Delete
def setframerate(self, framerate):
[338] Fix | Delete
if self._nframeswritten:
[339] Fix | Delete
raise Error, 'cannot change parameters after starting to write'
[340] Fix | Delete
self._framerate = framerate
[341] Fix | Delete
[342] Fix | Delete
def getframerate(self):
[343] Fix | Delete
if not self._framerate:
[344] Fix | Delete
raise Error, 'frame rate not set'
[345] Fix | Delete
return self._framerate
[346] Fix | Delete
[347] Fix | Delete
def setnframes(self, nframes):
[348] Fix | Delete
if self._nframeswritten:
[349] Fix | Delete
raise Error, 'cannot change parameters after starting to write'
[350] Fix | Delete
if nframes < 0:
[351] Fix | Delete
raise Error, '# of frames cannot be negative'
[352] Fix | Delete
self._nframes = nframes
[353] Fix | Delete
[354] Fix | Delete
def getnframes(self):
[355] Fix | Delete
return self._nframeswritten
[356] Fix | Delete
[357] Fix | Delete
def setcomptype(self, type, name):
[358] Fix | Delete
if type in ('NONE', 'ULAW'):
[359] Fix | Delete
self._comptype = type
[360] Fix | Delete
else:
[361] Fix | Delete
raise Error, 'unknown compression type'
[362] Fix | Delete
[363] Fix | Delete
def getcomptype(self):
[364] Fix | Delete
return self._comptype
[365] Fix | Delete
[366] Fix | Delete
def getcompname(self):
[367] Fix | Delete
if self._comptype == 'ULAW':
[368] Fix | Delete
return 'CCITT G.711 u-law'
[369] Fix | Delete
elif self._comptype == 'ALAW':
[370] Fix | Delete
return 'CCITT G.711 A-law'
[371] Fix | Delete
else:
[372] Fix | Delete
return 'not compressed'
[373] Fix | Delete
[374] Fix | Delete
def setparams(self, params):
[375] Fix | Delete
nchannels, sampwidth, framerate, nframes, comptype, compname = params
[376] Fix | Delete
self.setnchannels(nchannels)
[377] Fix | Delete
self.setsampwidth(sampwidth)
[378] Fix | Delete
self.setframerate(framerate)
[379] Fix | Delete
self.setnframes(nframes)
[380] Fix | Delete
self.setcomptype(comptype, compname)
[381] Fix | Delete
[382] Fix | Delete
def getparams(self):
[383] Fix | Delete
return self.getnchannels(), self.getsampwidth(), \
[384] Fix | Delete
self.getframerate(), self.getnframes(), \
[385] Fix | Delete
self.getcomptype(), self.getcompname()
[386] Fix | Delete
[387] Fix | Delete
def tell(self):
[388] Fix | Delete
return self._nframeswritten
[389] Fix | Delete
[390] Fix | Delete
def writeframesraw(self, data):
[391] Fix | Delete
self._ensure_header_written()
[392] Fix | Delete
if self._comptype == 'ULAW':
[393] Fix | Delete
import audioop
[394] Fix | Delete
data = audioop.lin2ulaw(data, self._sampwidth)
[395] Fix | Delete
nframes = len(data) // self._framesize
[396] Fix | Delete
self._file.write(data)
[397] Fix | Delete
self._nframeswritten = self._nframeswritten + nframes
[398] Fix | Delete
self._datawritten = self._datawritten + len(data)
[399] Fix | Delete
[400] Fix | Delete
def writeframes(self, data):
[401] Fix | Delete
self.writeframesraw(data)
[402] Fix | Delete
if self._nframeswritten != self._nframes or \
[403] Fix | Delete
self._datalength != self._datawritten:
[404] Fix | Delete
self._patchheader()
[405] Fix | Delete
[406] Fix | Delete
def close(self):
[407] Fix | Delete
if self._file:
[408] Fix | Delete
try:
[409] Fix | Delete
self._ensure_header_written()
[410] Fix | Delete
if self._nframeswritten != self._nframes or \
[411] Fix | Delete
self._datalength != self._datawritten:
[412] Fix | Delete
self._patchheader()
[413] Fix | Delete
self._file.flush()
[414] Fix | Delete
finally:
[415] Fix | Delete
self._file = None
[416] Fix | Delete
[417] Fix | Delete
#
[418] Fix | Delete
# private methods
[419] Fix | Delete
#
[420] Fix | Delete
[421] Fix | Delete
def _ensure_header_written(self):
[422] Fix | Delete
if not self._nframeswritten:
[423] Fix | Delete
if not self._nchannels:
[424] Fix | Delete
raise Error, '# of channels not specified'
[425] Fix | Delete
if not self._sampwidth:
[426] Fix | Delete
raise Error, 'sample width not specified'
[427] Fix | Delete
if not self._framerate:
[428] Fix | Delete
raise Error, 'frame rate not specified'
[429] Fix | Delete
self._write_header()
[430] Fix | Delete
[431] Fix | Delete
def _write_header(self):
[432] Fix | Delete
if self._comptype == 'NONE':
[433] Fix | Delete
if self._sampwidth == 1:
[434] Fix | Delete
encoding = AUDIO_FILE_ENCODING_LINEAR_8
[435] Fix | Delete
self._framesize = 1
[436] Fix | Delete
elif self._sampwidth == 2:
[437] Fix | Delete
encoding = AUDIO_FILE_ENCODING_LINEAR_16
[438] Fix | Delete
self._framesize = 2
[439] Fix | Delete
elif self._sampwidth == 4:
[440] Fix | Delete
encoding = AUDIO_FILE_ENCODING_LINEAR_32
[441] Fix | Delete
self._framesize = 4
[442] Fix | Delete
else:
[443] Fix | Delete
raise Error, 'internal error'
[444] Fix | Delete
elif self._comptype == 'ULAW':
[445] Fix | Delete
encoding = AUDIO_FILE_ENCODING_MULAW_8
[446] Fix | Delete
self._framesize = 1
[447] Fix | Delete
else:
[448] Fix | Delete
raise Error, 'internal error'
[449] Fix | Delete
self._framesize = self._framesize * self._nchannels
[450] Fix | Delete
_write_u32(self._file, AUDIO_FILE_MAGIC)
[451] Fix | Delete
header_size = 25 + len(self._info)
[452] Fix | Delete
header_size = (header_size + 7) & ~7
[453] Fix | Delete
_write_u32(self._file, header_size)
[454] Fix | Delete
if self._nframes == AUDIO_UNKNOWN_SIZE:
[455] Fix | Delete
length = AUDIO_UNKNOWN_SIZE
[456] Fix | Delete
else:
[457] Fix | Delete
length = self._nframes * self._framesize
[458] Fix | Delete
try:
[459] Fix | Delete
self._form_length_pos = self._file.tell()
[460] Fix | Delete
except (AttributeError, IOError):
[461] Fix | Delete
self._form_length_pos = None
[462] Fix | Delete
_write_u32(self._file, length)
[463] Fix | Delete
self._datalength = length
[464] Fix | Delete
_write_u32(self._file, encoding)
[465] Fix | Delete
_write_u32(self._file, self._framerate)
[466] Fix | Delete
_write_u32(self._file, self._nchannels)
[467] Fix | Delete
self._file.write(self._info)
[468] Fix | Delete
self._file.write('\0'*(header_size - len(self._info) - 24))
[469] Fix | Delete
[470] Fix | Delete
def _patchheader(self):
[471] Fix | Delete
if self._form_length_pos is None:
[472] Fix | Delete
raise IOError('cannot seek')
[473] Fix | Delete
self._file.seek(self._form_length_pos)
[474] Fix | Delete
_write_u32(self._file, self._datawritten)
[475] Fix | Delete
self._datalength = self._datawritten
[476] Fix | Delete
self._file.seek(0, 2)
[477] Fix | Delete
[478] Fix | Delete
def open(f, mode=None):
[479] Fix | Delete
if mode is None:
[480] Fix | Delete
if hasattr(f, 'mode'):
[481] Fix | Delete
mode = f.mode
[482] Fix | Delete
else:
[483] Fix | Delete
mode = 'rb'
[484] Fix | Delete
if mode in ('r', 'rb'):
[485] Fix | Delete
return Au_read(f)
[486] Fix | Delete
elif mode in ('w', 'wb'):
[487] Fix | Delete
return Au_write(f)
[488] Fix | Delete
else:
[489] Fix | Delete
raise Error, "mode must be 'r', 'rb', 'w', or 'wb'"
[490] Fix | Delete
[491] Fix | Delete
openfp = open
[492] Fix | Delete
[493] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function