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 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(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
[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
wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH', chunk.read(14))
[254] Fix | Delete
if wFormatTag == WAVE_FORMAT_PCM:
[255] Fix | Delete
sampwidth = struct.unpack_from('<H', chunk.read(2))[0]
[256] Fix | Delete
self._sampwidth = (sampwidth + 7) // 8
[257] Fix | Delete
else:
[258] Fix | Delete
raise Error('unknown format: %r' % (wFormatTag,))
[259] Fix | Delete
self._framesize = self._nchannels * self._sampwidth
[260] Fix | Delete
self._comptype = 'NONE'
[261] Fix | Delete
self._compname = 'not compressed'
[262] Fix | Delete
[263] Fix | Delete
class Wave_write:
[264] Fix | Delete
"""Variables used in this class:
[265] Fix | Delete
[266] Fix | Delete
These variables are user settable through appropriate methods
[267] Fix | Delete
of this class:
[268] Fix | Delete
_file -- the open file with methods write(), close(), tell(), seek()
[269] Fix | Delete
set through the __init__() method
[270] Fix | Delete
_comptype -- the AIFF-C compression type ('NONE' in AIFF)
[271] Fix | Delete
set through the setcomptype() or setparams() method
[272] Fix | Delete
_compname -- the human-readable AIFF-C compression type
[273] Fix | Delete
set through the setcomptype() or setparams() method
[274] Fix | Delete
_nchannels -- the number of audio channels
[275] Fix | Delete
set through the setnchannels() or setparams() method
[276] Fix | Delete
_sampwidth -- the number of bytes per audio sample
[277] Fix | Delete
set through the setsampwidth() or setparams() method
[278] Fix | Delete
_framerate -- the sampling frequency
[279] Fix | Delete
set through the setframerate() or setparams() method
[280] Fix | Delete
_nframes -- the number of audio frames written to the header
[281] Fix | Delete
set through the setnframes() or setparams() method
[282] Fix | Delete
[283] Fix | Delete
These variables are used internally only:
[284] Fix | Delete
_datalength -- the size of the audio samples written to the header
[285] Fix | Delete
_nframeswritten -- the number of frames actually written
[286] Fix | Delete
_datawritten -- the size of the audio samples actually written
[287] Fix | Delete
"""
[288] Fix | Delete
[289] Fix | Delete
def __init__(self, f):
[290] Fix | Delete
self._i_opened_the_file = None
[291] Fix | Delete
if isinstance(f, str):
[292] Fix | Delete
f = builtins.open(f, 'wb')
[293] Fix | Delete
self._i_opened_the_file = f
[294] Fix | Delete
try:
[295] Fix | Delete
self.initfp(f)
[296] Fix | Delete
except:
[297] Fix | Delete
if self._i_opened_the_file:
[298] Fix | Delete
f.close()
[299] Fix | Delete
raise
[300] Fix | Delete
[301] Fix | Delete
def initfp(self, file):
[302] Fix | Delete
self._file = file
[303] Fix | Delete
self._convert = None
[304] Fix | Delete
self._nchannels = 0
[305] Fix | Delete
self._sampwidth = 0
[306] Fix | Delete
self._framerate = 0
[307] Fix | Delete
self._nframes = 0
[308] Fix | Delete
self._nframeswritten = 0
[309] Fix | Delete
self._datawritten = 0
[310] Fix | Delete
self._datalength = 0
[311] Fix | Delete
self._headerwritten = False
[312] Fix | Delete
[313] Fix | Delete
def __del__(self):
[314] Fix | Delete
self.close()
[315] Fix | Delete
[316] Fix | Delete
def __enter__(self):
[317] Fix | Delete
return self
[318] Fix | Delete
[319] Fix | Delete
def __exit__(self, *args):
[320] Fix | Delete
self.close()
[321] Fix | Delete
[322] Fix | Delete
#
[323] Fix | Delete
# User visible methods.
[324] Fix | Delete
#
[325] Fix | Delete
def setnchannels(self, nchannels):
[326] Fix | Delete
if self._datawritten:
[327] Fix | Delete
raise Error('cannot change parameters after starting to write')
[328] Fix | Delete
if nchannels < 1:
[329] Fix | Delete
raise Error('bad # of channels')
[330] Fix | Delete
self._nchannels = nchannels
[331] Fix | Delete
[332] Fix | Delete
def getnchannels(self):
[333] Fix | Delete
if not self._nchannels:
[334] Fix | Delete
raise Error('number of channels not set')
[335] Fix | Delete
return self._nchannels
[336] Fix | Delete
[337] Fix | Delete
def setsampwidth(self, sampwidth):
[338] Fix | Delete
if self._datawritten:
[339] Fix | Delete
raise Error('cannot change parameters after starting to write')
[340] Fix | Delete
if sampwidth < 1 or sampwidth > 4:
[341] Fix | Delete
raise Error('bad sample width')
[342] Fix | Delete
self._sampwidth = sampwidth
[343] Fix | Delete
[344] Fix | Delete
def getsampwidth(self):
[345] Fix | Delete
if not self._sampwidth:
[346] Fix | Delete
raise Error('sample width not set')
[347] Fix | Delete
return self._sampwidth
[348] Fix | Delete
[349] Fix | Delete
def setframerate(self, framerate):
[350] Fix | Delete
if self._datawritten:
[351] Fix | Delete
raise Error('cannot change parameters after starting to write')
[352] Fix | Delete
if framerate <= 0:
[353] Fix | Delete
raise Error('bad frame rate')
[354] Fix | Delete
self._framerate = int(round(framerate))
[355] Fix | Delete
[356] Fix | Delete
def getframerate(self):
[357] Fix | Delete
if not self._framerate:
[358] Fix | Delete
raise Error('frame rate not set')
[359] Fix | Delete
return self._framerate
[360] Fix | Delete
[361] Fix | Delete
def setnframes(self, nframes):
[362] Fix | Delete
if self._datawritten:
[363] Fix | Delete
raise Error('cannot change parameters after starting to write')
[364] Fix | Delete
self._nframes = nframes
[365] Fix | Delete
[366] Fix | Delete
def getnframes(self):
[367] Fix | Delete
return self._nframeswritten
[368] Fix | Delete
[369] Fix | Delete
def setcomptype(self, comptype, compname):
[370] Fix | Delete
if self._datawritten:
[371] Fix | Delete
raise Error('cannot change parameters after starting to write')
[372] Fix | Delete
if comptype not in ('NONE',):
[373] Fix | Delete
raise Error('unsupported compression type')
[374] Fix | Delete
self._comptype = comptype
[375] Fix | Delete
self._compname = compname
[376] Fix | Delete
[377] Fix | Delete
def getcomptype(self):
[378] Fix | Delete
return self._comptype
[379] Fix | Delete
[380] Fix | Delete
def getcompname(self):
[381] Fix | Delete
return self._compname
[382] Fix | Delete
[383] Fix | Delete
def setparams(self, params):
[384] Fix | Delete
nchannels, sampwidth, framerate, nframes, comptype, compname = params
[385] Fix | Delete
if self._datawritten:
[386] Fix | Delete
raise Error('cannot change parameters after starting to write')
[387] Fix | Delete
self.setnchannels(nchannels)
[388] Fix | Delete
self.setsampwidth(sampwidth)
[389] Fix | Delete
self.setframerate(framerate)
[390] Fix | Delete
self.setnframes(nframes)
[391] Fix | Delete
self.setcomptype(comptype, compname)
[392] Fix | Delete
[393] Fix | Delete
def getparams(self):
[394] Fix | Delete
if not self._nchannels or not self._sampwidth or not self._framerate:
[395] Fix | Delete
raise Error('not all parameters set')
[396] Fix | Delete
return _wave_params(self._nchannels, self._sampwidth, self._framerate,
[397] Fix | Delete
self._nframes, self._comptype, self._compname)
[398] Fix | Delete
[399] Fix | Delete
def setmark(self, id, pos, name):
[400] Fix | Delete
raise Error('setmark() not supported')
[401] Fix | Delete
[402] Fix | Delete
def getmark(self, id):
[403] Fix | Delete
raise Error('no marks')
[404] Fix | Delete
[405] Fix | Delete
def getmarkers(self):
[406] Fix | Delete
return None
[407] Fix | Delete
[408] Fix | Delete
def tell(self):
[409] Fix | Delete
return self._nframeswritten
[410] Fix | Delete
[411] Fix | Delete
def writeframesraw(self, data):
[412] Fix | Delete
if not isinstance(data, (bytes, bytearray)):
[413] Fix | Delete
data = memoryview(data).cast('B')
[414] Fix | Delete
self._ensure_header_written(len(data))
[415] Fix | Delete
nframes = len(data) // (self._sampwidth * self._nchannels)
[416] Fix | Delete
if self._convert:
[417] Fix | Delete
data = self._convert(data)
[418] Fix | Delete
if self._sampwidth != 1 and sys.byteorder == 'big':
[419] Fix | Delete
data = audioop.byteswap(data, self._sampwidth)
[420] Fix | Delete
self._file.write(data)
[421] Fix | Delete
self._datawritten += len(data)
[422] Fix | Delete
self._nframeswritten = self._nframeswritten + nframes
[423] Fix | Delete
[424] Fix | Delete
def writeframes(self, data):
[425] Fix | Delete
self.writeframesraw(data)
[426] Fix | Delete
if self._datalength != self._datawritten:
[427] Fix | Delete
self._patchheader()
[428] Fix | Delete
[429] Fix | Delete
def close(self):
[430] Fix | Delete
try:
[431] Fix | Delete
if self._file:
[432] Fix | Delete
self._ensure_header_written(0)
[433] Fix | Delete
if self._datalength != self._datawritten:
[434] Fix | Delete
self._patchheader()
[435] Fix | Delete
self._file.flush()
[436] Fix | Delete
finally:
[437] Fix | Delete
self._file = None
[438] Fix | Delete
file = self._i_opened_the_file
[439] Fix | Delete
if file:
[440] Fix | Delete
self._i_opened_the_file = None
[441] Fix | Delete
file.close()
[442] Fix | Delete
[443] Fix | Delete
#
[444] Fix | Delete
# Internal methods.
[445] Fix | Delete
#
[446] Fix | Delete
[447] Fix | Delete
def _ensure_header_written(self, datasize):
[448] Fix | Delete
if not self._headerwritten:
[449] Fix | Delete
if not self._nchannels:
[450] Fix | Delete
raise Error('# channels not specified')
[451] Fix | Delete
if not self._sampwidth:
[452] Fix | Delete
raise Error('sample width not specified')
[453] Fix | Delete
if not self._framerate:
[454] Fix | Delete
raise Error('sampling rate not specified')
[455] Fix | Delete
self._write_header(datasize)
[456] Fix | Delete
[457] Fix | Delete
def _write_header(self, initlength):
[458] Fix | Delete
assert not self._headerwritten
[459] Fix | Delete
self._file.write(b'RIFF')
[460] Fix | Delete
if not self._nframes:
[461] Fix | Delete
self._nframes = initlength // (self._nchannels * self._sampwidth)
[462] Fix | Delete
self._datalength = self._nframes * self._nchannels * self._sampwidth
[463] Fix | Delete
try:
[464] Fix | Delete
self._form_length_pos = self._file.tell()
[465] Fix | Delete
except (AttributeError, OSError):
[466] Fix | Delete
self._form_length_pos = None
[467] Fix | Delete
self._file.write(struct.pack('<L4s4sLHHLLHH4s',
[468] Fix | Delete
36 + self._datalength, b'WAVE', b'fmt ', 16,
[469] Fix | Delete
WAVE_FORMAT_PCM, self._nchannels, self._framerate,
[470] Fix | Delete
self._nchannels * self._framerate * self._sampwidth,
[471] Fix | Delete
self._nchannels * self._sampwidth,
[472] Fix | Delete
self._sampwidth * 8, b'data'))
[473] Fix | Delete
if self._form_length_pos is not None:
[474] Fix | Delete
self._data_length_pos = self._file.tell()
[475] Fix | Delete
self._file.write(struct.pack('<L', self._datalength))
[476] Fix | Delete
self._headerwritten = True
[477] Fix | Delete
[478] Fix | Delete
def _patchheader(self):
[479] Fix | Delete
assert self._headerwritten
[480] Fix | Delete
if self._datawritten == self._datalength:
[481] Fix | Delete
return
[482] Fix | Delete
curpos = self._file.tell()
[483] Fix | Delete
self._file.seek(self._form_length_pos, 0)
[484] Fix | Delete
self._file.write(struct.pack('<L', 36 + self._datawritten))
[485] Fix | Delete
self._file.seek(self._data_length_pos, 0)
[486] Fix | Delete
self._file.write(struct.pack('<L', self._datawritten))
[487] Fix | Delete
self._file.seek(curpos, 0)
[488] Fix | Delete
self._datalength = self._datawritten
[489] Fix | Delete
[490] Fix | Delete
def open(f, mode=None):
[491] Fix | Delete
if mode is None:
[492] Fix | Delete
if hasattr(f, 'mode'):
[493] Fix | Delete
mode = f.mode
[494] Fix | Delete
else:
[495] Fix | Delete
mode = 'rb'
[496] Fix | Delete
if mode in ('r', 'rb'):
[497] Fix | Delete
return Wave_read(f)
[498] Fix | Delete
elif mode in ('w', 'wb'):
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function