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