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