Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/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
[107] Fix | Delete
_sunau_params = namedtuple('_sunau_params',
[108] Fix | Delete
'nchannels sampwidth framerate nframes comptype compname')
[109] Fix | Delete
[110] Fix | Delete
# from <multimedia/audio_filehdr.h>
[111] Fix | Delete
AUDIO_FILE_MAGIC = 0x2e736e64
[112] Fix | Delete
AUDIO_FILE_ENCODING_MULAW_8 = 1
[113] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_8 = 2
[114] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_16 = 3
[115] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_24 = 4
[116] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_32 = 5
[117] Fix | Delete
AUDIO_FILE_ENCODING_FLOAT = 6
[118] Fix | Delete
AUDIO_FILE_ENCODING_DOUBLE = 7
[119] Fix | Delete
AUDIO_FILE_ENCODING_ADPCM_G721 = 23
[120] Fix | Delete
AUDIO_FILE_ENCODING_ADPCM_G722 = 24
[121] Fix | Delete
AUDIO_FILE_ENCODING_ADPCM_G723_3 = 25
[122] Fix | Delete
AUDIO_FILE_ENCODING_ADPCM_G723_5 = 26
[123] Fix | Delete
AUDIO_FILE_ENCODING_ALAW_8 = 27
[124] Fix | Delete
[125] Fix | Delete
# from <multimedia/audio_hdr.h>
[126] Fix | Delete
AUDIO_UNKNOWN_SIZE = 0xFFFFFFFF # ((unsigned)(~0))
[127] Fix | Delete
[128] Fix | Delete
_simple_encodings = [AUDIO_FILE_ENCODING_MULAW_8,
[129] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_8,
[130] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_16,
[131] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_24,
[132] Fix | Delete
AUDIO_FILE_ENCODING_LINEAR_32,
[133] Fix | Delete
AUDIO_FILE_ENCODING_ALAW_8]
[134] Fix | Delete
[135] Fix | Delete
class Error(Exception):
[136] Fix | Delete
pass
[137] Fix | Delete
[138] Fix | Delete
def _read_u32(file):
[139] Fix | Delete
x = 0
[140] Fix | Delete
for i in range(4):
[141] Fix | Delete
byte = file.read(1)
[142] Fix | Delete
if not byte:
[143] Fix | Delete
raise EOFError
[144] Fix | Delete
x = x*256 + ord(byte)
[145] Fix | Delete
return x
[146] Fix | Delete
[147] Fix | Delete
def _write_u32(file, x):
[148] Fix | Delete
data = []
[149] Fix | Delete
for i in range(4):
[150] Fix | Delete
d, m = divmod(x, 256)
[151] Fix | Delete
data.insert(0, int(m))
[152] Fix | Delete
x = d
[153] Fix | Delete
file.write(bytes(data))
[154] Fix | Delete
[155] Fix | Delete
class Au_read:
[156] Fix | Delete
[157] Fix | Delete
def __init__(self, f):
[158] Fix | Delete
if type(f) == type(''):
[159] Fix | Delete
import builtins
[160] Fix | Delete
f = builtins.open(f, 'rb')
[161] Fix | Delete
self._opened = True
[162] Fix | Delete
else:
[163] Fix | Delete
self._opened = False
[164] Fix | Delete
self.initfp(f)
[165] Fix | Delete
[166] Fix | Delete
def __del__(self):
[167] Fix | Delete
if self._file:
[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
def initfp(self, file):
[177] Fix | Delete
self._file = file
[178] Fix | Delete
self._soundpos = 0
[179] Fix | Delete
magic = int(_read_u32(file))
[180] Fix | Delete
if magic != AUDIO_FILE_MAGIC:
[181] Fix | Delete
raise Error('bad magic number')
[182] Fix | Delete
self._hdr_size = int(_read_u32(file))
[183] Fix | Delete
if self._hdr_size < 24:
[184] Fix | Delete
raise Error('header size too small')
[185] Fix | Delete
if self._hdr_size > 100:
[186] Fix | Delete
raise Error('header size ridiculously large')
[187] Fix | Delete
self._data_size = _read_u32(file)
[188] Fix | Delete
if self._data_size != AUDIO_UNKNOWN_SIZE:
[189] Fix | Delete
self._data_size = int(self._data_size)
[190] Fix | Delete
self._encoding = int(_read_u32(file))
[191] Fix | Delete
if self._encoding not in _simple_encodings:
[192] Fix | Delete
raise Error('encoding not (yet) supported')
[193] Fix | Delete
if self._encoding in (AUDIO_FILE_ENCODING_MULAW_8,
[194] Fix | Delete
AUDIO_FILE_ENCODING_ALAW_8):
[195] Fix | Delete
self._sampwidth = 2
[196] Fix | Delete
self._framesize = 1
[197] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_8:
[198] Fix | Delete
self._framesize = self._sampwidth = 1
[199] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_16:
[200] Fix | Delete
self._framesize = self._sampwidth = 2
[201] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_24:
[202] Fix | Delete
self._framesize = self._sampwidth = 3
[203] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_32:
[204] Fix | Delete
self._framesize = self._sampwidth = 4
[205] Fix | Delete
else:
[206] Fix | Delete
raise Error('unknown encoding')
[207] Fix | Delete
self._framerate = int(_read_u32(file))
[208] Fix | Delete
self._nchannels = int(_read_u32(file))
[209] Fix | Delete
if not self._nchannels:
[210] Fix | Delete
raise Error('bad # of channels')
[211] Fix | Delete
self._framesize = self._framesize * self._nchannels
[212] Fix | Delete
if self._hdr_size > 24:
[213] Fix | Delete
self._info = file.read(self._hdr_size - 24)
[214] Fix | Delete
self._info, _, _ = self._info.partition(b'\0')
[215] Fix | Delete
else:
[216] Fix | Delete
self._info = b''
[217] Fix | Delete
try:
[218] Fix | Delete
self._data_pos = file.tell()
[219] Fix | Delete
except (AttributeError, OSError):
[220] Fix | Delete
self._data_pos = None
[221] Fix | Delete
[222] Fix | Delete
def getfp(self):
[223] Fix | Delete
return self._file
[224] Fix | Delete
[225] Fix | Delete
def getnchannels(self):
[226] Fix | Delete
return self._nchannels
[227] Fix | Delete
[228] Fix | Delete
def getsampwidth(self):
[229] Fix | Delete
return self._sampwidth
[230] Fix | Delete
[231] Fix | Delete
def getframerate(self):
[232] Fix | Delete
return self._framerate
[233] Fix | Delete
[234] Fix | Delete
def getnframes(self):
[235] Fix | Delete
if self._data_size == AUDIO_UNKNOWN_SIZE:
[236] Fix | Delete
return AUDIO_UNKNOWN_SIZE
[237] Fix | Delete
if self._encoding in _simple_encodings:
[238] Fix | Delete
return self._data_size // self._framesize
[239] Fix | Delete
return 0 # XXX--must do some arithmetic here
[240] Fix | Delete
[241] Fix | Delete
def getcomptype(self):
[242] Fix | Delete
if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
[243] Fix | Delete
return 'ULAW'
[244] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
[245] Fix | Delete
return 'ALAW'
[246] Fix | Delete
else:
[247] Fix | Delete
return 'NONE'
[248] Fix | Delete
[249] Fix | Delete
def getcompname(self):
[250] Fix | Delete
if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
[251] Fix | Delete
return 'CCITT G.711 u-law'
[252] Fix | Delete
elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
[253] Fix | Delete
return 'CCITT G.711 A-law'
[254] Fix | Delete
else:
[255] Fix | Delete
return 'not compressed'
[256] Fix | Delete
[257] Fix | Delete
def getparams(self):
[258] Fix | Delete
return _sunau_params(self.getnchannels(), self.getsampwidth(),
[259] Fix | Delete
self.getframerate(), self.getnframes(),
[260] Fix | Delete
self.getcomptype(), self.getcompname())
[261] Fix | Delete
[262] Fix | Delete
def getmarkers(self):
[263] Fix | Delete
return None
[264] Fix | Delete
[265] Fix | Delete
def getmark(self, id):
[266] Fix | Delete
raise Error('no marks')
[267] Fix | Delete
[268] Fix | Delete
def readframes(self, nframes):
[269] Fix | Delete
if self._encoding in _simple_encodings:
[270] Fix | Delete
if nframes == AUDIO_UNKNOWN_SIZE:
[271] Fix | Delete
data = self._file.read()
[272] Fix | Delete
else:
[273] Fix | Delete
data = self._file.read(nframes * self._framesize)
[274] Fix | Delete
self._soundpos += len(data) // self._framesize
[275] Fix | Delete
if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
[276] Fix | Delete
import audioop
[277] Fix | Delete
data = audioop.ulaw2lin(data, self._sampwidth)
[278] Fix | Delete
return data
[279] Fix | Delete
return None # XXX--not implemented yet
[280] Fix | Delete
[281] Fix | Delete
def rewind(self):
[282] Fix | Delete
if self._data_pos is None:
[283] Fix | Delete
raise OSError('cannot seek')
[284] Fix | Delete
self._file.seek(self._data_pos)
[285] Fix | Delete
self._soundpos = 0
[286] Fix | Delete
[287] Fix | Delete
def tell(self):
[288] Fix | Delete
return self._soundpos
[289] Fix | Delete
[290] Fix | Delete
def setpos(self, pos):
[291] Fix | Delete
if pos < 0 or pos > self.getnframes():
[292] Fix | Delete
raise Error('position not in range')
[293] Fix | Delete
if self._data_pos is None:
[294] Fix | Delete
raise OSError('cannot seek')
[295] Fix | Delete
self._file.seek(self._data_pos + pos * self._framesize)
[296] Fix | Delete
self._soundpos = pos
[297] Fix | Delete
[298] Fix | Delete
def close(self):
[299] Fix | Delete
file = self._file
[300] Fix | Delete
if file:
[301] Fix | Delete
self._file = None
[302] Fix | Delete
if self._opened:
[303] Fix | Delete
file.close()
[304] Fix | Delete
[305] Fix | Delete
class Au_write:
[306] Fix | Delete
[307] Fix | Delete
def __init__(self, f):
[308] Fix | Delete
if type(f) == type(''):
[309] Fix | Delete
import builtins
[310] Fix | Delete
f = builtins.open(f, 'wb')
[311] Fix | Delete
self._opened = True
[312] Fix | Delete
else:
[313] Fix | Delete
self._opened = False
[314] Fix | Delete
self.initfp(f)
[315] Fix | Delete
[316] Fix | Delete
def __del__(self):
[317] Fix | Delete
if self._file:
[318] Fix | Delete
self.close()
[319] Fix | Delete
self._file = None
[320] Fix | Delete
[321] Fix | Delete
def __enter__(self):
[322] Fix | Delete
return self
[323] Fix | Delete
[324] Fix | Delete
def __exit__(self, *args):
[325] Fix | Delete
self.close()
[326] Fix | Delete
[327] Fix | Delete
def initfp(self, file):
[328] Fix | Delete
self._file = file
[329] Fix | Delete
self._framerate = 0
[330] Fix | Delete
self._nchannels = 0
[331] Fix | Delete
self._sampwidth = 0
[332] Fix | Delete
self._framesize = 0
[333] Fix | Delete
self._nframes = AUDIO_UNKNOWN_SIZE
[334] Fix | Delete
self._nframeswritten = 0
[335] Fix | Delete
self._datawritten = 0
[336] Fix | Delete
self._datalength = 0
[337] Fix | Delete
self._info = b''
[338] Fix | Delete
self._comptype = 'ULAW' # default is U-law
[339] Fix | Delete
[340] Fix | Delete
def setnchannels(self, nchannels):
[341] Fix | Delete
if self._nframeswritten:
[342] Fix | Delete
raise Error('cannot change parameters after starting to write')
[343] Fix | Delete
if nchannels not in (1, 2, 4):
[344] Fix | Delete
raise Error('only 1, 2, or 4 channels supported')
[345] Fix | Delete
self._nchannels = nchannels
[346] Fix | Delete
[347] Fix | Delete
def getnchannels(self):
[348] Fix | Delete
if not self._nchannels:
[349] Fix | Delete
raise Error('number of channels not set')
[350] Fix | Delete
return self._nchannels
[351] Fix | Delete
[352] Fix | Delete
def setsampwidth(self, sampwidth):
[353] Fix | Delete
if self._nframeswritten:
[354] Fix | Delete
raise Error('cannot change parameters after starting to write')
[355] Fix | Delete
if sampwidth not in (1, 2, 3, 4):
[356] Fix | Delete
raise Error('bad sample width')
[357] Fix | Delete
self._sampwidth = sampwidth
[358] Fix | Delete
[359] Fix | Delete
def getsampwidth(self):
[360] Fix | Delete
if not self._framerate:
[361] Fix | Delete
raise Error('sample width not specified')
[362] Fix | Delete
return self._sampwidth
[363] Fix | Delete
[364] Fix | Delete
def setframerate(self, framerate):
[365] Fix | Delete
if self._nframeswritten:
[366] Fix | Delete
raise Error('cannot change parameters after starting to write')
[367] Fix | Delete
self._framerate = framerate
[368] Fix | Delete
[369] Fix | Delete
def getframerate(self):
[370] Fix | Delete
if not self._framerate:
[371] Fix | Delete
raise Error('frame rate not set')
[372] Fix | Delete
return self._framerate
[373] Fix | Delete
[374] Fix | Delete
def setnframes(self, nframes):
[375] Fix | Delete
if self._nframeswritten:
[376] Fix | Delete
raise Error('cannot change parameters after starting to write')
[377] Fix | Delete
if nframes < 0:
[378] Fix | Delete
raise Error('# of frames cannot be negative')
[379] Fix | Delete
self._nframes = nframes
[380] Fix | Delete
[381] Fix | Delete
def getnframes(self):
[382] Fix | Delete
return self._nframeswritten
[383] Fix | Delete
[384] Fix | Delete
def setcomptype(self, type, name):
[385] Fix | Delete
if type in ('NONE', 'ULAW'):
[386] Fix | Delete
self._comptype = type
[387] Fix | Delete
else:
[388] Fix | Delete
raise Error('unknown compression type')
[389] Fix | Delete
[390] Fix | Delete
def getcomptype(self):
[391] Fix | Delete
return self._comptype
[392] Fix | Delete
[393] Fix | Delete
def getcompname(self):
[394] Fix | Delete
if self._comptype == 'ULAW':
[395] Fix | Delete
return 'CCITT G.711 u-law'
[396] Fix | Delete
elif self._comptype == 'ALAW':
[397] Fix | Delete
return 'CCITT G.711 A-law'
[398] Fix | Delete
else:
[399] Fix | Delete
return 'not compressed'
[400] Fix | Delete
[401] Fix | Delete
def setparams(self, params):
[402] Fix | Delete
nchannels, sampwidth, framerate, nframes, comptype, compname = params
[403] Fix | Delete
self.setnchannels(nchannels)
[404] Fix | Delete
self.setsampwidth(sampwidth)
[405] Fix | Delete
self.setframerate(framerate)
[406] Fix | Delete
self.setnframes(nframes)
[407] Fix | Delete
self.setcomptype(comptype, compname)
[408] Fix | Delete
[409] Fix | Delete
def getparams(self):
[410] Fix | Delete
return _sunau_params(self.getnchannels(), self.getsampwidth(),
[411] Fix | Delete
self.getframerate(), self.getnframes(),
[412] Fix | Delete
self.getcomptype(), self.getcompname())
[413] Fix | Delete
[414] Fix | Delete
def tell(self):
[415] Fix | Delete
return self._nframeswritten
[416] Fix | Delete
[417] Fix | Delete
def writeframesraw(self, data):
[418] Fix | Delete
if not isinstance(data, (bytes, bytearray)):
[419] Fix | Delete
data = memoryview(data).cast('B')
[420] Fix | Delete
self._ensure_header_written()
[421] Fix | Delete
if self._comptype == 'ULAW':
[422] Fix | Delete
import audioop
[423] Fix | Delete
data = audioop.lin2ulaw(data, self._sampwidth)
[424] Fix | Delete
nframes = len(data) // self._framesize
[425] Fix | Delete
self._file.write(data)
[426] Fix | Delete
self._nframeswritten = self._nframeswritten + nframes
[427] Fix | Delete
self._datawritten = self._datawritten + len(data)
[428] Fix | Delete
[429] Fix | Delete
def writeframes(self, data):
[430] Fix | Delete
self.writeframesraw(data)
[431] Fix | Delete
if self._nframeswritten != self._nframes or \
[432] Fix | Delete
self._datalength != self._datawritten:
[433] Fix | Delete
self._patchheader()
[434] Fix | Delete
[435] Fix | Delete
def close(self):
[436] Fix | Delete
if self._file:
[437] Fix | Delete
try:
[438] Fix | Delete
self._ensure_header_written()
[439] Fix | Delete
if self._nframeswritten != self._nframes or \
[440] Fix | Delete
self._datalength != self._datawritten:
[441] Fix | Delete
self._patchheader()
[442] Fix | Delete
self._file.flush()
[443] Fix | Delete
finally:
[444] Fix | Delete
file = self._file
[445] Fix | Delete
self._file = None
[446] Fix | Delete
if self._opened:
[447] Fix | Delete
file.close()
[448] Fix | Delete
[449] Fix | Delete
#
[450] Fix | Delete
# private methods
[451] Fix | Delete
#
[452] Fix | Delete
[453] Fix | Delete
def _ensure_header_written(self):
[454] Fix | Delete
if not self._nframeswritten:
[455] Fix | Delete
if not self._nchannels:
[456] Fix | Delete
raise Error('# of channels not specified')
[457] Fix | Delete
if not self._sampwidth:
[458] Fix | Delete
raise Error('sample width not specified')
[459] Fix | Delete
if not self._framerate:
[460] Fix | Delete
raise Error('frame rate not specified')
[461] Fix | Delete
self._write_header()
[462] Fix | Delete
[463] Fix | Delete
def _write_header(self):
[464] Fix | Delete
if self._comptype == 'NONE':
[465] Fix | Delete
if self._sampwidth == 1:
[466] Fix | Delete
encoding = AUDIO_FILE_ENCODING_LINEAR_8
[467] Fix | Delete
self._framesize = 1
[468] Fix | Delete
elif self._sampwidth == 2:
[469] Fix | Delete
encoding = AUDIO_FILE_ENCODING_LINEAR_16
[470] Fix | Delete
self._framesize = 2
[471] Fix | Delete
elif self._sampwidth == 3:
[472] Fix | Delete
encoding = AUDIO_FILE_ENCODING_LINEAR_24
[473] Fix | Delete
self._framesize = 3
[474] Fix | Delete
elif self._sampwidth == 4:
[475] Fix | Delete
encoding = AUDIO_FILE_ENCODING_LINEAR_32
[476] Fix | Delete
self._framesize = 4
[477] Fix | Delete
else:
[478] Fix | Delete
raise Error('internal error')
[479] Fix | Delete
elif self._comptype == 'ULAW':
[480] Fix | Delete
encoding = AUDIO_FILE_ENCODING_MULAW_8
[481] Fix | Delete
self._framesize = 1
[482] Fix | Delete
else:
[483] Fix | Delete
raise Error('internal error')
[484] Fix | Delete
self._framesize = self._framesize * self._nchannels
[485] Fix | Delete
_write_u32(self._file, AUDIO_FILE_MAGIC)
[486] Fix | Delete
header_size = 25 + len(self._info)
[487] Fix | Delete
header_size = (header_size + 7) & ~7
[488] Fix | Delete
_write_u32(self._file, header_size)
[489] Fix | Delete
if self._nframes == AUDIO_UNKNOWN_SIZE:
[490] Fix | Delete
length = AUDIO_UNKNOWN_SIZE
[491] Fix | Delete
else:
[492] Fix | Delete
length = self._nframes * self._framesize
[493] Fix | Delete
try:
[494] Fix | Delete
self._form_length_pos = self._file.tell()
[495] Fix | Delete
except (AttributeError, OSError):
[496] Fix | Delete
self._form_length_pos = None
[497] Fix | Delete
_write_u32(self._file, length)
[498] Fix | Delete
self._datalength = length
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function