Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../proc/self/root/lib64/python3....
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 namedtuple 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 patching 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(b'') 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 builtins
[73] Fix | Delete
[74] Fix | Delete
__all__ = ["open", "openfp", "Error", "Wave_read", "Wave_write"]
[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 audioop
[84] Fix | Delete
import struct
[85] Fix | Delete
import sys
[86] Fix | Delete
from chunk import Chunk
[87] Fix | Delete
from collections import namedtuple
[88] Fix | Delete
import warnings
[89] Fix | Delete
[90] Fix | Delete
_wave_params = namedtuple('_wave_params',
[91] Fix | Delete
'nchannels sampwidth framerate nframes comptype compname')
[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() != b'RIFF':
[129] Fix | Delete
raise Error('file does not start with RIFF id')
[130] Fix | Delete
if self._file.read(4) != b'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 == b'fmt ':
[142] Fix | Delete
self._read_fmt_chunk(chunk)
[143] Fix | Delete
self._fmt_chunk_read = 1
[144] Fix | Delete
elif chunkname == b'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, str):
[158] Fix | Delete
f = builtins.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
def __enter__(self):
[172] Fix | Delete
return self
[173] Fix | Delete
[174] Fix | Delete
def __exit__(self, *args):
[175] Fix | Delete
self.close()
[176] Fix | Delete
[177] Fix | Delete
#
[178] Fix | Delete
# User visible methods.
[179] Fix | Delete
#
[180] Fix | Delete
def getfp(self):
[181] Fix | Delete
return self._file
[182] Fix | Delete
[183] Fix | Delete
def rewind(self):
[184] Fix | Delete
self._data_seek_needed = 1
[185] Fix | Delete
self._soundpos = 0
[186] Fix | Delete
[187] Fix | Delete
def close(self):
[188] Fix | Delete
self._file = None
[189] Fix | Delete
file = self._i_opened_the_file
[190] Fix | Delete
if file:
[191] Fix | Delete
self._i_opened_the_file = None
[192] Fix | Delete
file.close()
[193] Fix | Delete
[194] Fix | Delete
def tell(self):
[195] Fix | Delete
return self._soundpos
[196] Fix | Delete
[197] Fix | Delete
def getnchannels(self):
[198] Fix | Delete
return self._nchannels
[199] Fix | Delete
[200] Fix | Delete
def getnframes(self):
[201] Fix | Delete
return self._nframes
[202] Fix | Delete
[203] Fix | Delete
def getsampwidth(self):
[204] Fix | Delete
return self._sampwidth
[205] Fix | Delete
[206] Fix | Delete
def getframerate(self):
[207] Fix | Delete
return self._framerate
[208] Fix | Delete
[209] Fix | Delete
def getcomptype(self):
[210] Fix | Delete
return self._comptype
[211] Fix | Delete
[212] Fix | Delete
def getcompname(self):
[213] Fix | Delete
return self._compname
[214] Fix | Delete
[215] Fix | Delete
def getparams(self):
[216] Fix | Delete
return _wave_params(self.getnchannels(), self.getsampwidth(),
[217] Fix | Delete
self.getframerate(), self.getnframes(),
[218] Fix | Delete
self.getcomptype(), self.getcompname())
[219] Fix | Delete
[220] Fix | Delete
def getmarkers(self):
[221] Fix | Delete
return None
[222] Fix | Delete
[223] Fix | Delete
def getmark(self, id):
[224] Fix | Delete
raise Error('no marks')
[225] Fix | Delete
[226] Fix | Delete
def setpos(self, pos):
[227] Fix | Delete
if pos < 0 or pos > self._nframes:
[228] Fix | Delete
raise Error('position not in range')
[229] Fix | Delete
self._soundpos = pos
[230] Fix | Delete
self._data_seek_needed = 1
[231] Fix | Delete
[232] Fix | Delete
def readframes(self, nframes):
[233] Fix | Delete
if self._data_seek_needed:
[234] Fix | Delete
self._data_chunk.seek(0, 0)
[235] Fix | Delete
pos = self._soundpos * self._framesize
[236] Fix | Delete
if pos:
[237] Fix | Delete
self._data_chunk.seek(pos, 0)
[238] Fix | Delete
self._data_seek_needed = 0
[239] Fix | Delete
if nframes == 0:
[240] Fix | Delete
return b''
[241] Fix | Delete
data = self._data_chunk.read(nframes * self._framesize)
[242] Fix | Delete
if self._sampwidth != 1 and sys.byteorder == 'big':
[243] Fix | Delete
data = audioop.byteswap(data, self._sampwidth)
[244] Fix | Delete
if self._convert and data:
[245] Fix | Delete
data = self._convert(data)
[246] Fix | Delete
self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
[247] Fix | Delete
return data
[248] Fix | Delete
[249] Fix | Delete
#
[250] Fix | Delete
# Internal methods.
[251] Fix | Delete
#
[252] Fix | Delete
[253] Fix | Delete
def _read_fmt_chunk(self, chunk):
[254] Fix | Delete
try:
[255] Fix | Delete
wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH', chunk.read(14))
[256] Fix | Delete
except struct.error:
[257] Fix | Delete
raise EOFError from None
[258] Fix | Delete
if wFormatTag == WAVE_FORMAT_PCM:
[259] Fix | Delete
try:
[260] Fix | Delete
sampwidth = struct.unpack_from('<H', chunk.read(2))[0]
[261] Fix | Delete
except struct.error:
[262] Fix | Delete
raise EOFError from None
[263] Fix | Delete
self._sampwidth = (sampwidth + 7) // 8
[264] Fix | Delete
if not self._sampwidth:
[265] Fix | Delete
raise Error('bad sample width')
[266] Fix | Delete
else:
[267] Fix | Delete
raise Error('unknown format: %r' % (wFormatTag,))
[268] Fix | Delete
if not self._nchannels:
[269] Fix | Delete
raise Error('bad # of channels')
[270] Fix | Delete
self._framesize = self._nchannels * self._sampwidth
[271] Fix | Delete
self._comptype = 'NONE'
[272] Fix | Delete
self._compname = 'not compressed'
[273] Fix | Delete
[274] Fix | Delete
class Wave_write:
[275] Fix | Delete
"""Variables used in this class:
[276] Fix | Delete
[277] Fix | Delete
These variables are user settable through appropriate methods
[278] Fix | Delete
of this class:
[279] Fix | Delete
_file -- the open file with methods write(), close(), tell(), seek()
[280] Fix | Delete
set through the __init__() method
[281] Fix | Delete
_comptype -- the AIFF-C compression type ('NONE' in AIFF)
[282] Fix | Delete
set through the setcomptype() or setparams() method
[283] Fix | Delete
_compname -- the human-readable AIFF-C compression type
[284] Fix | Delete
set through the setcomptype() or setparams() method
[285] Fix | Delete
_nchannels -- the number of audio channels
[286] Fix | Delete
set through the setnchannels() or setparams() method
[287] Fix | Delete
_sampwidth -- the number of bytes per audio sample
[288] Fix | Delete
set through the setsampwidth() or setparams() method
[289] Fix | Delete
_framerate -- the sampling frequency
[290] Fix | Delete
set through the setframerate() or setparams() method
[291] Fix | Delete
_nframes -- the number of audio frames written to the header
[292] Fix | Delete
set through the setnframes() or setparams() method
[293] Fix | Delete
[294] Fix | Delete
These variables are used internally only:
[295] Fix | Delete
_datalength -- the size of the audio samples written to the header
[296] Fix | Delete
_nframeswritten -- the number of frames actually written
[297] Fix | Delete
_datawritten -- the size of the audio samples actually written
[298] Fix | Delete
"""
[299] Fix | Delete
[300] Fix | Delete
def __init__(self, f):
[301] Fix | Delete
self._i_opened_the_file = None
[302] Fix | Delete
if isinstance(f, str):
[303] Fix | Delete
f = builtins.open(f, 'wb')
[304] Fix | Delete
self._i_opened_the_file = f
[305] Fix | Delete
try:
[306] Fix | Delete
self.initfp(f)
[307] Fix | Delete
except:
[308] Fix | Delete
if self._i_opened_the_file:
[309] Fix | Delete
f.close()
[310] Fix | Delete
raise
[311] Fix | Delete
[312] Fix | Delete
def initfp(self, file):
[313] Fix | Delete
self._file = file
[314] Fix | Delete
self._convert = None
[315] Fix | Delete
self._nchannels = 0
[316] Fix | Delete
self._sampwidth = 0
[317] Fix | Delete
self._framerate = 0
[318] Fix | Delete
self._nframes = 0
[319] Fix | Delete
self._nframeswritten = 0
[320] Fix | Delete
self._datawritten = 0
[321] Fix | Delete
self._datalength = 0
[322] Fix | Delete
self._headerwritten = False
[323] Fix | Delete
[324] Fix | Delete
def __del__(self):
[325] Fix | Delete
self.close()
[326] Fix | Delete
[327] Fix | Delete
def __enter__(self):
[328] Fix | Delete
return self
[329] Fix | Delete
[330] Fix | Delete
def __exit__(self, *args):
[331] Fix | Delete
self.close()
[332] Fix | Delete
[333] Fix | Delete
#
[334] Fix | Delete
# User visible methods.
[335] Fix | Delete
#
[336] Fix | Delete
def setnchannels(self, nchannels):
[337] Fix | Delete
if self._datawritten:
[338] Fix | Delete
raise Error('cannot change parameters after starting to write')
[339] Fix | Delete
if nchannels < 1:
[340] Fix | Delete
raise Error('bad # of channels')
[341] Fix | Delete
self._nchannels = nchannels
[342] Fix | Delete
[343] Fix | Delete
def getnchannels(self):
[344] Fix | Delete
if not self._nchannels:
[345] Fix | Delete
raise Error('number of channels not set')
[346] Fix | Delete
return self._nchannels
[347] Fix | Delete
[348] Fix | Delete
def setsampwidth(self, sampwidth):
[349] Fix | Delete
if self._datawritten:
[350] Fix | Delete
raise Error('cannot change parameters after starting to write')
[351] Fix | Delete
if sampwidth < 1 or sampwidth > 4:
[352] Fix | Delete
raise Error('bad sample width')
[353] Fix | Delete
self._sampwidth = sampwidth
[354] Fix | Delete
[355] Fix | Delete
def getsampwidth(self):
[356] Fix | Delete
if not self._sampwidth:
[357] Fix | Delete
raise Error('sample width not set')
[358] Fix | Delete
return self._sampwidth
[359] Fix | Delete
[360] Fix | Delete
def setframerate(self, framerate):
[361] Fix | Delete
if self._datawritten:
[362] Fix | Delete
raise Error('cannot change parameters after starting to write')
[363] Fix | Delete
if framerate <= 0:
[364] Fix | Delete
raise Error('bad frame rate')
[365] Fix | Delete
self._framerate = int(round(framerate))
[366] Fix | Delete
[367] Fix | Delete
def getframerate(self):
[368] Fix | Delete
if not self._framerate:
[369] Fix | Delete
raise Error('frame rate not set')
[370] Fix | Delete
return self._framerate
[371] Fix | Delete
[372] Fix | Delete
def setnframes(self, nframes):
[373] Fix | Delete
if self._datawritten:
[374] Fix | Delete
raise Error('cannot change parameters after starting to write')
[375] Fix | Delete
self._nframes = nframes
[376] Fix | Delete
[377] Fix | Delete
def getnframes(self):
[378] Fix | Delete
return self._nframeswritten
[379] Fix | Delete
[380] Fix | Delete
def setcomptype(self, comptype, compname):
[381] Fix | Delete
if self._datawritten:
[382] Fix | Delete
raise Error('cannot change parameters after starting to write')
[383] Fix | Delete
if comptype not in ('NONE',):
[384] Fix | Delete
raise Error('unsupported compression type')
[385] Fix | Delete
self._comptype = comptype
[386] Fix | Delete
self._compname = compname
[387] Fix | Delete
[388] Fix | Delete
def getcomptype(self):
[389] Fix | Delete
return self._comptype
[390] Fix | Delete
[391] Fix | Delete
def getcompname(self):
[392] Fix | Delete
return self._compname
[393] Fix | Delete
[394] Fix | Delete
def setparams(self, params):
[395] Fix | Delete
nchannels, sampwidth, framerate, nframes, comptype, compname = params
[396] Fix | Delete
if self._datawritten:
[397] Fix | Delete
raise Error('cannot change parameters after starting to write')
[398] Fix | Delete
self.setnchannels(nchannels)
[399] Fix | Delete
self.setsampwidth(sampwidth)
[400] Fix | Delete
self.setframerate(framerate)
[401] Fix | Delete
self.setnframes(nframes)
[402] Fix | Delete
self.setcomptype(comptype, compname)
[403] Fix | Delete
[404] Fix | Delete
def getparams(self):
[405] Fix | Delete
if not self._nchannels or not self._sampwidth or not self._framerate:
[406] Fix | Delete
raise Error('not all parameters set')
[407] Fix | Delete
return _wave_params(self._nchannels, self._sampwidth, self._framerate,
[408] Fix | Delete
self._nframes, self._comptype, self._compname)
[409] Fix | Delete
[410] Fix | Delete
def setmark(self, id, pos, name):
[411] Fix | Delete
raise Error('setmark() not supported')
[412] Fix | Delete
[413] Fix | Delete
def getmark(self, id):
[414] Fix | Delete
raise Error('no marks')
[415] Fix | Delete
[416] Fix | Delete
def getmarkers(self):
[417] Fix | Delete
return None
[418] Fix | Delete
[419] Fix | Delete
def tell(self):
[420] Fix | Delete
return self._nframeswritten
[421] Fix | Delete
[422] Fix | Delete
def writeframesraw(self, data):
[423] Fix | Delete
if not isinstance(data, (bytes, bytearray)):
[424] Fix | Delete
data = memoryview(data).cast('B')
[425] Fix | Delete
self._ensure_header_written(len(data))
[426] Fix | Delete
nframes = len(data) // (self._sampwidth * self._nchannels)
[427] Fix | Delete
if self._convert:
[428] Fix | Delete
data = self._convert(data)
[429] Fix | Delete
if self._sampwidth != 1 and sys.byteorder == 'big':
[430] Fix | Delete
data = audioop.byteswap(data, self._sampwidth)
[431] Fix | Delete
self._file.write(data)
[432] Fix | Delete
self._datawritten += len(data)
[433] Fix | Delete
self._nframeswritten = self._nframeswritten + nframes
[434] Fix | Delete
[435] Fix | Delete
def writeframes(self, data):
[436] Fix | Delete
self.writeframesraw(data)
[437] Fix | Delete
if self._datalength != self._datawritten:
[438] Fix | Delete
self._patchheader()
[439] Fix | Delete
[440] Fix | Delete
def close(self):
[441] Fix | Delete
try:
[442] Fix | Delete
if self._file:
[443] Fix | Delete
self._ensure_header_written(0)
[444] Fix | Delete
if self._datalength != self._datawritten:
[445] Fix | Delete
self._patchheader()
[446] Fix | Delete
self._file.flush()
[447] Fix | Delete
finally:
[448] Fix | Delete
self._file = None
[449] Fix | Delete
file = self._i_opened_the_file
[450] Fix | Delete
if file:
[451] Fix | Delete
self._i_opened_the_file = None
[452] Fix | Delete
file.close()
[453] Fix | Delete
[454] Fix | Delete
#
[455] Fix | Delete
# Internal methods.
[456] Fix | Delete
#
[457] Fix | Delete
[458] Fix | Delete
def _ensure_header_written(self, datasize):
[459] Fix | Delete
if not self._headerwritten:
[460] Fix | Delete
if not self._nchannels:
[461] Fix | Delete
raise Error('# channels not specified')
[462] Fix | Delete
if not self._sampwidth:
[463] Fix | Delete
raise Error('sample width not specified')
[464] Fix | Delete
if not self._framerate:
[465] Fix | Delete
raise Error('sampling rate not specified')
[466] Fix | Delete
self._write_header(datasize)
[467] Fix | Delete
[468] Fix | Delete
def _write_header(self, initlength):
[469] Fix | Delete
assert not self._headerwritten
[470] Fix | Delete
self._file.write(b'RIFF')
[471] Fix | Delete
if not self._nframes:
[472] Fix | Delete
self._nframes = initlength // (self._nchannels * self._sampwidth)
[473] Fix | Delete
self._datalength = self._nframes * self._nchannels * self._sampwidth
[474] Fix | Delete
try:
[475] Fix | Delete
self._form_length_pos = self._file.tell()
[476] Fix | Delete
except (AttributeError, OSError):
[477] Fix | Delete
self._form_length_pos = None
[478] Fix | Delete
self._file.write(struct.pack('<L4s4sLHHLLHH4s',
[479] Fix | Delete
36 + self._datalength, b'WAVE', b'fmt ', 16,
[480] Fix | Delete
WAVE_FORMAT_PCM, self._nchannels, self._framerate,
[481] Fix | Delete
self._nchannels * self._framerate * self._sampwidth,
[482] Fix | Delete
self._nchannels * self._sampwidth,
[483] Fix | Delete
self._sampwidth * 8, b'data'))
[484] Fix | Delete
if self._form_length_pos is not None:
[485] Fix | Delete
self._data_length_pos = self._file.tell()
[486] Fix | Delete
self._file.write(struct.pack('<L', self._datalength))
[487] Fix | Delete
self._headerwritten = True
[488] Fix | Delete
[489] Fix | Delete
def _patchheader(self):
[490] Fix | Delete
assert self._headerwritten
[491] Fix | Delete
if self._datawritten == self._datalength:
[492] Fix | Delete
return
[493] Fix | Delete
curpos = self._file.tell()
[494] Fix | Delete
self._file.seek(self._form_length_pos, 0)
[495] Fix | Delete
self._file.write(struct.pack('<L', 36 + self._datawritten))
[496] Fix | Delete
self._file.seek(self._data_length_pos, 0)
[497] Fix | Delete
self._file.write(struct.pack('<L', self._datawritten))
[498] Fix | Delete
self._file.seek(curpos, 0)
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function