Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: wave.py
"""Stuff to parse WAVE files.
[0] Fix | Delete
[1] Fix | Delete
Usage.
[2] Fix | Delete
[3] Fix | Delete
Reading WAVE files:
[4] Fix | Delete
f = wave.open(file, 'r')
[5] Fix | Delete
where file is either the name of a file or an open file pointer.
[6] Fix | Delete
The open file pointer must have methods read(), seek(), and close().
[7] Fix | Delete
When the setpos() and rewind() methods are not used, the seek()
[8] Fix | Delete
method is not necessary.
[9] Fix | Delete
[10] Fix | Delete
This returns an instance of a class with the following public methods:
[11] Fix | Delete
getnchannels() -- returns number of audio channels (1 for
[12] Fix | Delete
mono, 2 for stereo)
[13] Fix | Delete
getsampwidth() -- returns sample width in bytes
[14] Fix | Delete
getframerate() -- returns sampling frequency
[15] Fix | Delete
getnframes() -- returns number of audio frames
[16] Fix | Delete
getcomptype() -- returns compression type ('NONE' for linear samples)
[17] Fix | Delete
getcompname() -- returns human-readable version of
[18] Fix | Delete
compression type ('not compressed' linear samples)
[19] Fix | Delete
getparams() -- returns a tuple consisting of all of the
[20] Fix | Delete
above in the above order
[21] Fix | Delete
getmarkers() -- returns None (for compatibility with the
[22] Fix | Delete
aifc module)
[23] Fix | Delete
getmark(id) -- raises an error since the mark does not
[24] Fix | Delete
exist (for compatibility with the aifc module)
[25] Fix | Delete
readframes(n) -- returns at most n frames of audio
[26] Fix | Delete
rewind() -- rewind to the beginning of the audio stream
[27] Fix | Delete
setpos(pos) -- seek to the specified position
[28] Fix | Delete
tell() -- return the current position
[29] Fix | Delete
close() -- close the instance (make it unusable)
[30] Fix | Delete
The position returned by tell() and the position given to setpos()
[31] Fix | Delete
are compatible and have nothing to do with the actual position in the
[32] Fix | Delete
file.
[33] Fix | Delete
The close() method is called automatically when the class instance
[34] Fix | Delete
is destroyed.
[35] Fix | Delete
[36] Fix | Delete
Writing WAVE files:
[37] Fix | Delete
f = wave.open(file, 'w')
[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 write(), tell(), seek(), and
[40] Fix | Delete
close().
[41] Fix | Delete
[42] Fix | Delete
This returns an instance of a class with the following public methods:
[43] Fix | Delete
setnchannels(n) -- set the number of channels
[44] Fix | Delete
setsampwidth(n) -- set the sample width
[45] Fix | Delete
setframerate(n) -- set the frame rate
[46] Fix | Delete
setnframes(n) -- set the number of frames
[47] Fix | Delete
setcomptype(type, name)
[48] Fix | Delete
-- set the compression type and the
[49] Fix | Delete
human-readable compression type
[50] Fix | Delete
setparams(tuple)
[51] Fix | Delete
-- set all parameters at once
[52] Fix | Delete
tell() -- return current position in output file
[53] Fix | Delete
writeframesraw(data)
[54] Fix | Delete
-- write audio frames without pathing up the
[55] Fix | Delete
file header
[56] Fix | Delete
writeframes(data)
[57] Fix | Delete
-- write audio frames and patch up the file header
[58] Fix | Delete
close() -- patch up the file header and close the
[59] Fix | Delete
output file
[60] Fix | Delete
You should set the parameters before the first writeframesraw or
[61] Fix | Delete
writeframes. The total number of frames does not need to be set,
[62] Fix | Delete
but when it is set to the correct value, the header does not have to
[63] Fix | Delete
be patched up.
[64] Fix | Delete
It is best to first set all parameters, perhaps possibly the
[65] Fix | Delete
compression type, and then write audio frames using writeframesraw.
[66] Fix | Delete
When all frames have been written, either call writeframes('') or
[67] Fix | Delete
close() to patch up the sizes in the header.
[68] Fix | Delete
The close() method is called automatically when the class instance
[69] Fix | Delete
is destroyed.
[70] Fix | Delete
"""
[71] Fix | Delete
[72] Fix | Delete
import __builtin__
[73] Fix | Delete
[74] Fix | Delete
__all__ = ["open", "openfp", "Error"]
[75] Fix | Delete
[76] Fix | Delete
class Error(Exception):
[77] Fix | Delete
pass
[78] Fix | Delete
[79] Fix | Delete
WAVE_FORMAT_PCM = 0x0001
[80] Fix | Delete
[81] Fix | Delete
_array_fmts = None, 'b', 'h', None, 'i'
[82] Fix | Delete
[83] Fix | Delete
import struct
[84] Fix | Delete
import sys
[85] Fix | Delete
from chunk import Chunk
[86] Fix | Delete
[87] Fix | Delete
def _byteswap3(data):
[88] Fix | Delete
ba = bytearray(data)
[89] Fix | Delete
ba[::3] = data[2::3]
[90] Fix | Delete
ba[2::3] = data[::3]
[91] Fix | Delete
return bytes(ba)
[92] Fix | Delete
[93] Fix | Delete
class Wave_read:
[94] Fix | Delete
"""Variables used in this class:
[95] Fix | Delete
[96] Fix | Delete
These variables are available to the user though appropriate
[97] Fix | Delete
methods of this class:
[98] Fix | Delete
_file -- the open file with methods read(), close(), and seek()
[99] Fix | Delete
set through the __init__() method
[100] Fix | Delete
_nchannels -- the number of audio channels
[101] Fix | Delete
available through the getnchannels() method
[102] Fix | Delete
_nframes -- the number of audio frames
[103] Fix | Delete
available through the getnframes() method
[104] Fix | Delete
_sampwidth -- the number of bytes per audio sample
[105] Fix | Delete
available through the getsampwidth() method
[106] Fix | Delete
_framerate -- the sampling frequency
[107] Fix | Delete
available through the getframerate() method
[108] Fix | Delete
_comptype -- the AIFF-C compression type ('NONE' if AIFF)
[109] Fix | Delete
available through the getcomptype() method
[110] Fix | Delete
_compname -- the human-readable AIFF-C compression type
[111] Fix | Delete
available through the getcomptype() method
[112] Fix | Delete
_soundpos -- the position in the audio stream
[113] Fix | Delete
available through the tell() method, set through the
[114] Fix | Delete
setpos() method
[115] Fix | Delete
[116] Fix | Delete
These variables are used internally only:
[117] Fix | Delete
_fmt_chunk_read -- 1 iff the FMT chunk has been read
[118] Fix | Delete
_data_seek_needed -- 1 iff positioned correctly in audio
[119] Fix | Delete
file for readframes()
[120] Fix | Delete
_data_chunk -- instantiation of a chunk class for the DATA chunk
[121] Fix | Delete
_framesize -- size of one frame in the file
[122] Fix | Delete
"""
[123] Fix | Delete
[124] Fix | Delete
def initfp(self, file):
[125] Fix | Delete
self._convert = None
[126] Fix | Delete
self._soundpos = 0
[127] Fix | Delete
self._file = Chunk(file, bigendian = 0)
[128] Fix | Delete
if self._file.getname() != 'RIFF':
[129] Fix | Delete
raise Error, 'file does not start with RIFF id'
[130] Fix | Delete
if self._file.read(4) != 'WAVE':
[131] Fix | Delete
raise Error, 'not a WAVE file'
[132] Fix | Delete
self._fmt_chunk_read = 0
[133] Fix | Delete
self._data_chunk = None
[134] Fix | Delete
while 1:
[135] Fix | Delete
self._data_seek_needed = 1
[136] Fix | Delete
try:
[137] Fix | Delete
chunk = Chunk(self._file, bigendian = 0)
[138] Fix | Delete
except EOFError:
[139] Fix | Delete
break
[140] Fix | Delete
chunkname = chunk.getname()
[141] Fix | Delete
if chunkname == 'fmt ':
[142] Fix | Delete
self._read_fmt_chunk(chunk)
[143] Fix | Delete
self._fmt_chunk_read = 1
[144] Fix | Delete
elif chunkname == 'data':
[145] Fix | Delete
if not self._fmt_chunk_read:
[146] Fix | Delete
raise Error, 'data chunk before fmt chunk'
[147] Fix | Delete
self._data_chunk = chunk
[148] Fix | Delete
self._nframes = chunk.chunksize // self._framesize
[149] Fix | Delete
self._data_seek_needed = 0
[150] Fix | Delete
break
[151] Fix | Delete
chunk.skip()
[152] Fix | Delete
if not self._fmt_chunk_read or not self._data_chunk:
[153] Fix | Delete
raise Error, 'fmt chunk and/or data chunk missing'
[154] Fix | Delete
[155] Fix | Delete
def __init__(self, f):
[156] Fix | Delete
self._i_opened_the_file = None
[157] Fix | Delete
if isinstance(f, basestring):
[158] Fix | Delete
f = __builtin__.open(f, 'rb')
[159] Fix | Delete
self._i_opened_the_file = f
[160] Fix | Delete
# else, assume it is an open file object already
[161] Fix | Delete
try:
[162] Fix | Delete
self.initfp(f)
[163] Fix | Delete
except:
[164] Fix | Delete
if self._i_opened_the_file:
[165] Fix | Delete
f.close()
[166] Fix | Delete
raise
[167] Fix | Delete
[168] Fix | Delete
def __del__(self):
[169] Fix | Delete
self.close()
[170] Fix | Delete
#
[171] Fix | Delete
# User visible methods.
[172] Fix | Delete
#
[173] Fix | Delete
def getfp(self):
[174] Fix | Delete
return self._file
[175] Fix | Delete
[176] Fix | Delete
def rewind(self):
[177] Fix | Delete
self._data_seek_needed = 1
[178] Fix | Delete
self._soundpos = 0
[179] Fix | Delete
[180] Fix | Delete
def close(self):
[181] Fix | Delete
self._file = None
[182] Fix | Delete
file = self._i_opened_the_file
[183] Fix | Delete
if file:
[184] Fix | Delete
self._i_opened_the_file = None
[185] Fix | Delete
file.close()
[186] Fix | Delete
[187] Fix | Delete
def tell(self):
[188] Fix | Delete
return self._soundpos
[189] Fix | Delete
[190] Fix | Delete
def getnchannels(self):
[191] Fix | Delete
return self._nchannels
[192] Fix | Delete
[193] Fix | Delete
def getnframes(self):
[194] Fix | Delete
return self._nframes
[195] Fix | Delete
[196] Fix | Delete
def getsampwidth(self):
[197] Fix | Delete
return self._sampwidth
[198] Fix | Delete
[199] Fix | Delete
def getframerate(self):
[200] Fix | Delete
return self._framerate
[201] Fix | Delete
[202] Fix | Delete
def getcomptype(self):
[203] Fix | Delete
return self._comptype
[204] Fix | Delete
[205] Fix | Delete
def getcompname(self):
[206] Fix | Delete
return self._compname
[207] Fix | Delete
[208] Fix | Delete
def getparams(self):
[209] Fix | Delete
return self.getnchannels(), self.getsampwidth(), \
[210] Fix | Delete
self.getframerate(), self.getnframes(), \
[211] Fix | Delete
self.getcomptype(), self.getcompname()
[212] Fix | Delete
[213] Fix | Delete
def getmarkers(self):
[214] Fix | Delete
return None
[215] Fix | Delete
[216] Fix | Delete
def getmark(self, id):
[217] Fix | Delete
raise Error, 'no marks'
[218] Fix | Delete
[219] Fix | Delete
def setpos(self, pos):
[220] Fix | Delete
if pos < 0 or pos > self._nframes:
[221] Fix | Delete
raise Error, 'position not in range'
[222] Fix | Delete
self._soundpos = pos
[223] Fix | Delete
self._data_seek_needed = 1
[224] Fix | Delete
[225] Fix | Delete
def readframes(self, nframes):
[226] Fix | Delete
if self._data_seek_needed:
[227] Fix | Delete
self._data_chunk.seek(0, 0)
[228] Fix | Delete
pos = self._soundpos * self._framesize
[229] Fix | Delete
if pos:
[230] Fix | Delete
self._data_chunk.seek(pos, 0)
[231] Fix | Delete
self._data_seek_needed = 0
[232] Fix | Delete
if nframes == 0:
[233] Fix | Delete
return ''
[234] Fix | Delete
if self._sampwidth in (2, 4) and sys.byteorder == 'big':
[235] Fix | Delete
# unfortunately the fromfile() method does not take
[236] Fix | Delete
# something that only looks like a file object, so
[237] Fix | Delete
# we have to reach into the innards of the chunk object
[238] Fix | Delete
import array
[239] Fix | Delete
chunk = self._data_chunk
[240] Fix | Delete
data = array.array(_array_fmts[self._sampwidth])
[241] Fix | Delete
assert data.itemsize == self._sampwidth
[242] Fix | Delete
nitems = nframes * self._nchannels
[243] Fix | Delete
if nitems * self._sampwidth > chunk.chunksize - chunk.size_read:
[244] Fix | Delete
nitems = (chunk.chunksize - chunk.size_read) // self._sampwidth
[245] Fix | Delete
data.fromfile(chunk.file.file, nitems)
[246] Fix | Delete
# "tell" data chunk how much was read
[247] Fix | Delete
chunk.size_read = chunk.size_read + nitems * self._sampwidth
[248] Fix | Delete
# do the same for the outermost chunk
[249] Fix | Delete
chunk = chunk.file
[250] Fix | Delete
chunk.size_read = chunk.size_read + nitems * self._sampwidth
[251] Fix | Delete
data.byteswap()
[252] Fix | Delete
data = data.tostring()
[253] Fix | Delete
else:
[254] Fix | Delete
data = self._data_chunk.read(nframes * self._framesize)
[255] Fix | Delete
if self._sampwidth == 3 and sys.byteorder == 'big':
[256] Fix | Delete
data = _byteswap3(data)
[257] Fix | Delete
if self._convert and data:
[258] Fix | Delete
data = self._convert(data)
[259] Fix | Delete
self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
[260] Fix | Delete
return data
[261] Fix | Delete
[262] Fix | Delete
#
[263] Fix | Delete
# Internal methods.
[264] Fix | Delete
#
[265] Fix | Delete
[266] Fix | Delete
def _read_fmt_chunk(self, chunk):
[267] Fix | Delete
wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack('<HHLLH', chunk.read(14))
[268] Fix | Delete
if wFormatTag == WAVE_FORMAT_PCM:
[269] Fix | Delete
sampwidth = struct.unpack('<H', chunk.read(2))[0]
[270] Fix | Delete
self._sampwidth = (sampwidth + 7) // 8
[271] Fix | Delete
else:
[272] Fix | Delete
raise Error, 'unknown format: %r' % (wFormatTag,)
[273] Fix | Delete
self._framesize = self._nchannels * self._sampwidth
[274] Fix | Delete
self._comptype = 'NONE'
[275] Fix | Delete
self._compname = 'not compressed'
[276] Fix | Delete
[277] Fix | Delete
class Wave_write:
[278] Fix | Delete
"""Variables used in this class:
[279] Fix | Delete
[280] Fix | Delete
These variables are user settable through appropriate methods
[281] Fix | Delete
of this class:
[282] Fix | Delete
_file -- the open file with methods write(), close(), tell(), seek()
[283] Fix | Delete
set through the __init__() method
[284] Fix | Delete
_comptype -- the AIFF-C compression type ('NONE' in AIFF)
[285] Fix | Delete
set through the setcomptype() or setparams() method
[286] Fix | Delete
_compname -- the human-readable AIFF-C compression type
[287] Fix | Delete
set through the setcomptype() or setparams() method
[288] Fix | Delete
_nchannels -- the number of audio channels
[289] Fix | Delete
set through the setnchannels() or setparams() method
[290] Fix | Delete
_sampwidth -- the number of bytes per audio sample
[291] Fix | Delete
set through the setsampwidth() or setparams() method
[292] Fix | Delete
_framerate -- the sampling frequency
[293] Fix | Delete
set through the setframerate() or setparams() method
[294] Fix | Delete
_nframes -- the number of audio frames written to the header
[295] Fix | Delete
set through the setnframes() or setparams() method
[296] Fix | Delete
[297] Fix | Delete
These variables are used internally only:
[298] Fix | Delete
_datalength -- the size of the audio samples written to the header
[299] Fix | Delete
_nframeswritten -- the number of frames actually written
[300] Fix | Delete
_datawritten -- the size of the audio samples actually written
[301] Fix | Delete
"""
[302] Fix | Delete
[303] Fix | Delete
def __init__(self, f):
[304] Fix | Delete
self._i_opened_the_file = None
[305] Fix | Delete
if isinstance(f, basestring):
[306] Fix | Delete
f = __builtin__.open(f, 'wb')
[307] Fix | Delete
self._i_opened_the_file = f
[308] Fix | Delete
try:
[309] Fix | Delete
self.initfp(f)
[310] Fix | Delete
except:
[311] Fix | Delete
if self._i_opened_the_file:
[312] Fix | Delete
f.close()
[313] Fix | Delete
raise
[314] Fix | Delete
[315] Fix | Delete
def initfp(self, file):
[316] Fix | Delete
self._file = file
[317] Fix | Delete
self._convert = None
[318] Fix | Delete
self._nchannels = 0
[319] Fix | Delete
self._sampwidth = 0
[320] Fix | Delete
self._framerate = 0
[321] Fix | Delete
self._nframes = 0
[322] Fix | Delete
self._nframeswritten = 0
[323] Fix | Delete
self._datawritten = 0
[324] Fix | Delete
self._datalength = 0
[325] Fix | Delete
self._headerwritten = False
[326] Fix | Delete
[327] Fix | Delete
def __del__(self):
[328] Fix | Delete
self.close()
[329] Fix | Delete
[330] Fix | Delete
#
[331] Fix | Delete
# User visible methods.
[332] Fix | Delete
#
[333] Fix | Delete
def setnchannels(self, nchannels):
[334] Fix | Delete
if self._datawritten:
[335] Fix | Delete
raise Error, 'cannot change parameters after starting to write'
[336] Fix | Delete
if nchannels < 1:
[337] Fix | Delete
raise Error, 'bad # of channels'
[338] Fix | Delete
self._nchannels = nchannels
[339] Fix | Delete
[340] Fix | Delete
def getnchannels(self):
[341] Fix | Delete
if not self._nchannels:
[342] Fix | Delete
raise Error, 'number of channels not set'
[343] Fix | Delete
return self._nchannels
[344] Fix | Delete
[345] Fix | Delete
def setsampwidth(self, sampwidth):
[346] Fix | Delete
if self._datawritten:
[347] Fix | Delete
raise Error, 'cannot change parameters after starting to write'
[348] Fix | Delete
if sampwidth < 1 or sampwidth > 4:
[349] Fix | Delete
raise Error, 'bad sample width'
[350] Fix | Delete
self._sampwidth = sampwidth
[351] Fix | Delete
[352] Fix | Delete
def getsampwidth(self):
[353] Fix | Delete
if not self._sampwidth:
[354] Fix | Delete
raise Error, 'sample width not set'
[355] Fix | Delete
return self._sampwidth
[356] Fix | Delete
[357] Fix | Delete
def setframerate(self, framerate):
[358] Fix | Delete
if self._datawritten:
[359] Fix | Delete
raise Error, 'cannot change parameters after starting to write'
[360] Fix | Delete
if framerate <= 0:
[361] Fix | Delete
raise Error, 'bad frame rate'
[362] Fix | Delete
self._framerate = framerate
[363] Fix | Delete
[364] Fix | Delete
def getframerate(self):
[365] Fix | Delete
if not self._framerate:
[366] Fix | Delete
raise Error, 'frame rate not set'
[367] Fix | Delete
return self._framerate
[368] Fix | Delete
[369] Fix | Delete
def setnframes(self, nframes):
[370] Fix | Delete
if self._datawritten:
[371] Fix | Delete
raise Error, 'cannot change parameters after starting to write'
[372] Fix | Delete
self._nframes = nframes
[373] Fix | Delete
[374] Fix | Delete
def getnframes(self):
[375] Fix | Delete
return self._nframeswritten
[376] Fix | Delete
[377] Fix | Delete
def setcomptype(self, comptype, compname):
[378] Fix | Delete
if self._datawritten:
[379] Fix | Delete
raise Error, 'cannot change parameters after starting to write'
[380] Fix | Delete
if comptype not in ('NONE',):
[381] Fix | Delete
raise Error, 'unsupported compression type'
[382] Fix | Delete
self._comptype = comptype
[383] Fix | Delete
self._compname = compname
[384] Fix | Delete
[385] Fix | Delete
def getcomptype(self):
[386] Fix | Delete
return self._comptype
[387] Fix | Delete
[388] Fix | Delete
def getcompname(self):
[389] Fix | Delete
return self._compname
[390] Fix | Delete
[391] Fix | Delete
def setparams(self, params):
[392] Fix | Delete
nchannels, sampwidth, framerate, nframes, comptype, compname = params
[393] Fix | Delete
if self._datawritten:
[394] Fix | Delete
raise Error, 'cannot change parameters after starting to write'
[395] Fix | Delete
self.setnchannels(nchannels)
[396] Fix | Delete
self.setsampwidth(sampwidth)
[397] Fix | Delete
self.setframerate(framerate)
[398] Fix | Delete
self.setnframes(nframes)
[399] Fix | Delete
self.setcomptype(comptype, compname)
[400] Fix | Delete
[401] Fix | Delete
def getparams(self):
[402] Fix | Delete
if not self._nchannels or not self._sampwidth or not self._framerate:
[403] Fix | Delete
raise Error, 'not all parameters set'
[404] Fix | Delete
return self._nchannels, self._sampwidth, self._framerate, \
[405] Fix | Delete
self._nframes, self._comptype, self._compname
[406] Fix | Delete
[407] Fix | Delete
def setmark(self, id, pos, name):
[408] Fix | Delete
raise Error, 'setmark() not supported'
[409] Fix | Delete
[410] Fix | Delete
def getmark(self, id):
[411] Fix | Delete
raise Error, 'no marks'
[412] Fix | Delete
[413] Fix | Delete
def getmarkers(self):
[414] Fix | Delete
return None
[415] Fix | Delete
[416] Fix | Delete
def tell(self):
[417] Fix | Delete
return self._nframeswritten
[418] Fix | Delete
[419] Fix | Delete
def writeframesraw(self, data):
[420] Fix | Delete
self._ensure_header_written(len(data))
[421] Fix | Delete
nframes = len(data) // (self._sampwidth * self._nchannels)
[422] Fix | Delete
if self._convert:
[423] Fix | Delete
data = self._convert(data)
[424] Fix | Delete
if self._sampwidth in (2, 4) and sys.byteorder == 'big':
[425] Fix | Delete
import array
[426] Fix | Delete
a = array.array(_array_fmts[self._sampwidth])
[427] Fix | Delete
a.fromstring(data)
[428] Fix | Delete
data = a
[429] Fix | Delete
assert data.itemsize == self._sampwidth
[430] Fix | Delete
data.byteswap()
[431] Fix | Delete
data.tofile(self._file)
[432] Fix | Delete
self._datawritten = self._datawritten + len(data) * self._sampwidth
[433] Fix | Delete
else:
[434] Fix | Delete
if self._sampwidth == 3 and sys.byteorder == 'big':
[435] Fix | Delete
data = _byteswap3(data)
[436] Fix | Delete
self._file.write(data)
[437] Fix | Delete
self._datawritten = self._datawritten + len(data)
[438] Fix | Delete
self._nframeswritten = self._nframeswritten + nframes
[439] Fix | Delete
[440] Fix | Delete
def writeframes(self, data):
[441] Fix | Delete
self.writeframesraw(data)
[442] Fix | Delete
if self._datalength != self._datawritten:
[443] Fix | Delete
self._patchheader()
[444] Fix | Delete
[445] Fix | Delete
def close(self):
[446] Fix | Delete
try:
[447] Fix | Delete
if self._file:
[448] Fix | Delete
self._ensure_header_written(0)
[449] Fix | Delete
if self._datalength != self._datawritten:
[450] Fix | Delete
self._patchheader()
[451] Fix | Delete
self._file.flush()
[452] Fix | Delete
finally:
[453] Fix | Delete
self._file = None
[454] Fix | Delete
file = self._i_opened_the_file
[455] Fix | Delete
if file:
[456] Fix | Delete
self._i_opened_the_file = None
[457] Fix | Delete
file.close()
[458] Fix | Delete
[459] Fix | Delete
#
[460] Fix | Delete
# Internal methods.
[461] Fix | Delete
#
[462] Fix | Delete
[463] Fix | Delete
def _ensure_header_written(self, datasize):
[464] Fix | Delete
if not self._headerwritten:
[465] Fix | Delete
if not self._nchannels:
[466] Fix | Delete
raise Error, '# channels not specified'
[467] Fix | Delete
if not self._sampwidth:
[468] Fix | Delete
raise Error, 'sample width not specified'
[469] Fix | Delete
if not self._framerate:
[470] Fix | Delete
raise Error, 'sampling rate not specified'
[471] Fix | Delete
self._write_header(datasize)
[472] Fix | Delete
[473] Fix | Delete
def _write_header(self, initlength):
[474] Fix | Delete
assert not self._headerwritten
[475] Fix | Delete
self._file.write('RIFF')
[476] Fix | Delete
if not self._nframes:
[477] Fix | Delete
self._nframes = initlength / (self._nchannels * self._sampwidth)
[478] Fix | Delete
self._datalength = self._nframes * self._nchannels * self._sampwidth
[479] Fix | Delete
self._form_length_pos = self._file.tell()
[480] Fix | Delete
self._file.write(struct.pack('<L4s4sLHHLLHH4s',
[481] Fix | Delete
36 + self._datalength, 'WAVE', 'fmt ', 16,
[482] Fix | Delete
WAVE_FORMAT_PCM, self._nchannels, self._framerate,
[483] Fix | Delete
self._nchannels * self._framerate * self._sampwidth,
[484] Fix | Delete
self._nchannels * self._sampwidth,
[485] Fix | Delete
self._sampwidth * 8, 'data'))
[486] Fix | Delete
self._data_length_pos = self._file.tell()
[487] Fix | Delete
self._file.write(struct.pack('<L', self._datalength))
[488] Fix | Delete
self._headerwritten = True
[489] Fix | Delete
[490] Fix | Delete
def _patchheader(self):
[491] Fix | Delete
assert self._headerwritten
[492] Fix | Delete
if self._datawritten == self._datalength:
[493] Fix | Delete
return
[494] Fix | Delete
curpos = self._file.tell()
[495] Fix | Delete
self._file.seek(self._form_length_pos, 0)
[496] Fix | Delete
self._file.write(struct.pack('<L', 36 + self._datawritten))
[497] Fix | Delete
self._file.seek(self._data_length_pos, 0)
[498] Fix | Delete
self._file.write(struct.pack('<L', self._datawritten))
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function