Lines Matching refs:self
125 def initfp(self, file): argument
126 self._convert = None
127 self._soundpos = 0
128 self._file = Chunk(file, bigendian = 0)
129 if self._file.getname() != b'RIFF':
131 if self._file.read(4) != b'WAVE':
133 self._fmt_chunk_read = 0
134 self._data_chunk = None
136 self._data_seek_needed = 1
138 chunk = Chunk(self._file, bigendian = 0)
143 self._read_fmt_chunk(chunk)
144 self._fmt_chunk_read = 1
146 if not self._fmt_chunk_read:
148 self._data_chunk = chunk
149 self._nframes = chunk.chunksize // self._framesize
150 self._data_seek_needed = 0
153 if not self._fmt_chunk_read or not self._data_chunk:
156 def __init__(self, f): argument
157 self._i_opened_the_file = None
160 self._i_opened_the_file = f
163 self.initfp(f)
165 if self._i_opened_the_file:
169 def __del__(self): argument
170 self.close()
172 def __enter__(self): argument
173 return self
175 def __exit__(self, *args): argument
176 self.close()
181 def getfp(self): argument
182 return self._file
184 def rewind(self): argument
185 self._data_seek_needed = 1
186 self._soundpos = 0
188 def close(self): argument
189 self._file = None
190 file = self._i_opened_the_file
192 self._i_opened_the_file = None
195 def tell(self): argument
196 return self._soundpos
198 def getnchannels(self): argument
199 return self._nchannels
201 def getnframes(self): argument
202 return self._nframes
204 def getsampwidth(self): argument
205 return self._sampwidth
207 def getframerate(self): argument
208 return self._framerate
210 def getcomptype(self): argument
211 return self._comptype
213 def getcompname(self): argument
214 return self._compname
216 def getparams(self): argument
217 return _wave_params(self.getnchannels(), self.getsampwidth(),
218 self.getframerate(), self.getnframes(),
219 self.getcomptype(), self.getcompname())
221 def getmarkers(self): argument
224 def getmark(self, id): argument
227 def setpos(self, pos): argument
228 if pos < 0 or pos > self._nframes:
230 self._soundpos = pos
231 self._data_seek_needed = 1
233 def readframes(self, nframes): argument
234 if self._data_seek_needed:
235 self._data_chunk.seek(0, 0)
236 pos = self._soundpos * self._framesize
238 self._data_chunk.seek(pos, 0)
239 self._data_seek_needed = 0
242 data = self._data_chunk.read(nframes * self._framesize)
243 if self._sampwidth != 1 and sys.byteorder == 'big':
244 data = audioop.byteswap(data, self._sampwidth)
245 if self._convert and data:
246 data = self._convert(data)
247 self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
254 def _read_fmt_chunk(self, chunk): argument
256 …wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('…
264 self._sampwidth = (sampwidth + 7) // 8
265 if not self._sampwidth:
269 if not self._nchannels:
271 self._framesize = self._nchannels * self._sampwidth
272 self._comptype = 'NONE'
273 self._compname = 'not compressed'
301 def __init__(self, f): argument
302 self._i_opened_the_file = None
305 self._i_opened_the_file = f
307 self.initfp(f)
309 if self._i_opened_the_file:
313 def initfp(self, file): argument
314 self._file = file
315 self._convert = None
316 self._nchannels = 0
317 self._sampwidth = 0
318 self._framerate = 0
319 self._nframes = 0
320 self._nframeswritten = 0
321 self._datawritten = 0
322 self._datalength = 0
323 self._headerwritten = False
325 def __del__(self): argument
326 self.close()
328 def __enter__(self): argument
329 return self
331 def __exit__(self, *args): argument
332 self.close()
337 def setnchannels(self, nchannels): argument
338 if self._datawritten:
342 self._nchannels = nchannels
344 def getnchannels(self): argument
345 if not self._nchannels:
347 return self._nchannels
349 def setsampwidth(self, sampwidth): argument
350 if self._datawritten:
354 self._sampwidth = sampwidth
356 def getsampwidth(self): argument
357 if not self._sampwidth:
359 return self._sampwidth
361 def setframerate(self, framerate): argument
362 if self._datawritten:
366 self._framerate = int(round(framerate))
368 def getframerate(self): argument
369 if not self._framerate:
371 return self._framerate
373 def setnframes(self, nframes): argument
374 if self._datawritten:
376 self._nframes = nframes
378 def getnframes(self): argument
379 return self._nframeswritten
381 def setcomptype(self, comptype, compname): argument
382 if self._datawritten:
386 self._comptype = comptype
387 self._compname = compname
389 def getcomptype(self): argument
390 return self._comptype
392 def getcompname(self): argument
393 return self._compname
395 def setparams(self, params): argument
397 if self._datawritten:
399 self.setnchannels(nchannels)
400 self.setsampwidth(sampwidth)
401 self.setframerate(framerate)
402 self.setnframes(nframes)
403 self.setcomptype(comptype, compname)
405 def getparams(self): argument
406 if not self._nchannels or not self._sampwidth or not self._framerate:
408 return _wave_params(self._nchannels, self._sampwidth, self._framerate,
409 self._nframes, self._comptype, self._compname)
411 def setmark(self, id, pos, name): argument
414 def getmark(self, id): argument
417 def getmarkers(self): argument
420 def tell(self): argument
421 return self._nframeswritten
423 def writeframesraw(self, data): argument
426 self._ensure_header_written(len(data))
427 nframes = len(data) // (self._sampwidth * self._nchannels)
428 if self._convert:
429 data = self._convert(data)
430 if self._sampwidth != 1 and sys.byteorder == 'big':
431 data = audioop.byteswap(data, self._sampwidth)
432 self._file.write(data)
433 self._datawritten += len(data)
434 self._nframeswritten = self._nframeswritten + nframes
436 def writeframes(self, data): argument
437 self.writeframesraw(data)
438 if self._datalength != self._datawritten:
439 self._patchheader()
441 def close(self): argument
443 if self._file:
444 self._ensure_header_written(0)
445 if self._datalength != self._datawritten:
446 self._patchheader()
447 self._file.flush()
449 self._file = None
450 file = self._i_opened_the_file
452 self._i_opened_the_file = None
459 def _ensure_header_written(self, datasize): argument
460 if not self._headerwritten:
461 if not self._nchannels:
463 if not self._sampwidth:
465 if not self._framerate:
467 self._write_header(datasize)
469 def _write_header(self, initlength): argument
470 assert not self._headerwritten
471 self._file.write(b'RIFF')
472 if not self._nframes:
473 self._nframes = initlength // (self._nchannels * self._sampwidth)
474 self._datalength = self._nframes * self._nchannels * self._sampwidth
476 self._form_length_pos = self._file.tell()
478 self._form_length_pos = None
479 self._file.write(struct.pack('<L4s4sLHHLLHH4s',
480 36 + self._datalength, b'WAVE', b'fmt ', 16,
481 WAVE_FORMAT_PCM, self._nchannels, self._framerate,
482 self._nchannels * self._framerate * self._sampwidth,
483 self._nchannels * self._sampwidth,
484 self._sampwidth * 8, b'data'))
485 if self._form_length_pos is not None:
486 self._data_length_pos = self._file.tell()
487 self._file.write(struct.pack('<L', self._datalength))
488 self._headerwritten = True
490 def _patchheader(self): argument
491 assert self._headerwritten
492 if self._datawritten == self._datalength:
494 curpos = self._file.tell()
495 self._file.seek(self._form_length_pos, 0)
496 self._file.write(struct.pack('<L', 36 + self._datawritten))
497 self._file.seek(self._data_length_pos, 0)
498 self._file.write(struct.pack('<L', self._datawritten))
499 self._file.seek(curpos, 0)
500 self._datalength = self._datawritten