Lines Matching refs:self

314     def initfp(self, file):
315 self._version = 0
316 self._convert = None
317 self._markers = []
318 self._soundpos = 0
319 self._file = file
325 self._aifc = 0
327 self._aifc = 1
330 self._comm_chunk_read = 0
331 self._ssnd_chunk = None
333 self._ssnd_seek_needed = 1
335 chunk = Chunk(self._file)
340 self._read_comm_chunk(chunk)
341 self._comm_chunk_read = 1
343 self._ssnd_chunk = chunk
345 self._ssnd_seek_needed = 0
347 self._version = _read_ulong(chunk)
349 self._readmark(chunk)
351 if not self._comm_chunk_read or not self._ssnd_chunk:
354 def __init__(self, f):
358 self.initfp(file_object)
364 self.initfp(f)
366 def __enter__(self):
367 return self
369 def __exit__(self, *args):
370 self.close()
375 def getfp(self):
376 return self._file
378 def rewind(self):
379 self._ssnd_seek_needed = 1
380 self._soundpos = 0
382 def close(self):
383 file = self._file
385 self._file = None
388 def tell(self):
389 return self._soundpos
391 def getnchannels(self):
392 return self._nchannels
394 def getnframes(self):
395 return self._nframes
397 def getsampwidth(self):
398 return self._sampwidth
400 def getframerate(self):
401 return self._framerate
403 def getcomptype(self):
404 return self._comptype
406 def getcompname(self):
407 return self._compname
409 ## def getversion(self):
410 ## return self._version
412 def getparams(self):
413 return _aifc_params(self.getnchannels(), self.getsampwidth(),
414 self.getframerate(), self.getnframes(),
415 self.getcomptype(), self.getcompname())
417 def getmarkers(self):
418 if len(self._markers) == 0:
420 return self._markers
422 def getmark(self, id):
423 for marker in self._markers:
428 def setpos(self, pos):
429 if pos < 0 or pos > self._nframes:
431 self._soundpos = pos
432 self._ssnd_seek_needed = 1
434 def readframes(self, nframes):
435 if self._ssnd_seek_needed:
436 self._ssnd_chunk.seek(0)
437 dummy = self._ssnd_chunk.read(8)
438 pos = self._soundpos * self._framesize
440 self._ssnd_chunk.seek(pos + 8)
441 self._ssnd_seek_needed = 0
444 data = self._ssnd_chunk.read(nframes * self._framesize)
445 if self._convert and data:
446 data = self._convert(data)
447 self._soundpos = self._soundpos + len(data) // (self._nchannels
448 * self._sampwidth)
455 def _alaw2lin(self, data):
461 def _ulaw2lin(self, data):
467 def _adpcm2lin(self, data):
471 if not hasattr(self, '_adpcmstate'):
473 self._adpcmstate = None
474 data, self._adpcmstate = audioop.adpcm2lin(data, 2, self._adpcmstate)
477 def _sowt2lin(self, data):
483 def _read_comm_chunk(self, chunk):
484 self._nchannels = _read_short(chunk)
485 self._nframes = _read_long(chunk)
486 self._sampwidth = (_read_short(chunk) + 7) // 8
487 self._framerate = int(_read_float(chunk))
488 if self._sampwidth <= 0:
490 if self._nchannels <= 0:
492 self._framesize = self._nchannels * self._sampwidth
493 if self._aifc:
501 self._comptype = chunk.read(4)
510 self._compname = _read_string(chunk)
511 if self._comptype != b'NONE':
512 if self._comptype == b'G722':
513 self._convert = self._adpcm2lin
514 elif self._comptype in (b'ulaw', b'ULAW'):
515 self._convert = self._ulaw2lin
516 elif self._comptype in (b'alaw', b'ALAW'):
517 self._convert = self._alaw2lin
518 elif self._comptype in (b'sowt', b'SOWT'):
519 self._convert = self._sowt2lin
522 self._sampwidth = 2
524 self._comptype = b'NONE'
525 self._compname = b'not compressed'
527 def _readmark(self, chunk):
540 self._markers.append((id, pos, name))
543 (len(self._markers), '' if len(self._markers) == 1 else 's',
579 def __init__(self, f):
583 self.initfp(file_object)
590 self._aifc = 0
593 self.initfp(f)
595 def initfp(self, file):
596 self._file = file
597 self._version = _AIFC_version
598 self._comptype = b'NONE'
599 self._compname = b'not compressed'
600 self._convert = None
601 self._nchannels = 0
602 self._sampwidth = 0
603 self._framerate = 0
604 self._nframes = 0
605 self._nframeswritten = 0
606 self._datawritten = 0
607 self._datalength = 0
608 self._markers = []
609 self._marklength = 0
610 self._aifc = 1 # AIFF-C is default
612 def __del__(self):
613 self.close()
615 def __enter__(self):
616 return self
618 def __exit__(self, *args):
619 self.close()
624 def aiff(self):
625 if self._nframeswritten:
627 self._aifc = 0
629 def aifc(self):
630 if self._nframeswritten:
632 self._aifc = 1
634 def setnchannels(self, nchannels):
635 if self._nframeswritten:
639 self._nchannels = nchannels
641 def getnchannels(self):
642 if not self._nchannels:
644 return self._nchannels
646 def setsampwidth(self, sampwidth):
647 if self._nframeswritten:
651 self._sampwidth = sampwidth
653 def getsampwidth(self):
654 if not self._sampwidth:
656 return self._sampwidth
658 def setframerate(self, framerate):
659 if self._nframeswritten:
663 self._framerate = framerate
665 def getframerate(self):
666 if not self._framerate:
668 return self._framerate
670 def setnframes(self, nframes):
671 if self._nframeswritten:
673 self._nframes = nframes
675 def getnframes(self):
676 return self._nframeswritten
678 def setcomptype(self, comptype, compname):
679 if self._nframeswritten:
684 self._comptype = comptype
685 self._compname = compname
687 def getcomptype(self):
688 return self._comptype
690 def getcompname(self):
691 return self._compname
693 ## def setversion(self, version):
694 ## if self._nframeswritten:
696 ## self._version = version
698 def setparams(self, params):
700 if self._nframeswritten:
705 self.setnchannels(nchannels)
706 self.setsampwidth(sampwidth)
707 self.setframerate(framerate)
708 self.setnframes(nframes)
709 self.setcomptype(comptype, compname)
711 def getparams(self):
712 if not self._nchannels or not self._sampwidth or not self._framerate:
714 return _aifc_params(self._nchannels, self._sampwidth, self._framerate,
715 self._nframes, self._comptype, self._compname)
717 def setmark(self, id, pos, name):
724 for i in range(len(self._markers)):
725 if id == self._markers[i][0]:
726 self._markers[i] = id, pos, name
728 self._markers.append((id, pos, name))
730 def getmark(self, id):
731 for marker in self._markers:
736 def getmarkers(self):
737 if len(self._markers) == 0:
739 return self._markers
741 def tell(self):
742 return self._nframeswritten
744 def writeframesraw(self, data):
747 self._ensure_header_written(len(data))
748 nframes = len(data) // (self._sampwidth * self._nchannels)
749 if self._convert:
750 data = self._convert(data)
751 self._file.write(data)
752 self._nframeswritten = self._nframeswritten + nframes
753 self._datawritten = self._datawritten + len(data)
755 def writeframes(self, data):
756 self.writeframesraw(data)
757 if self._nframeswritten != self._nframes or \
758 self._datalength != self._datawritten:
759 self._patchheader()
761 def close(self):
762 if self._file is None:
765 self._ensure_header_written(0)
766 if self._datawritten & 1:
768 self._file.write(b'\x00')
769 self._datawritten = self._datawritten + 1
770 self._writemarkers()
771 if self._nframeswritten != self._nframes or \
772 self._datalength != self._datawritten or \
773 self._marklength:
774 self._patchheader()
777 self._convert = None
778 f = self._file
779 self._file = None
786 def _lin2alaw(self, data):
792 def _lin2ulaw(self, data):
798 def _lin2adpcm(self, data):
802 if not hasattr(self, '_adpcmstate'):
803 self._adpcmstate = None
804 data, self._adpcmstate = audioop.lin2adpcm(data, 2, self._adpcmstate)
807 def _lin2sowt(self, data):
813 def _ensure_header_written(self, datasize):
814 if not self._nframeswritten:
815 if self._comptype in (b'ULAW', b'ulaw',
818 if not self._sampwidth:
819 self._sampwidth = 2
820 if self._sampwidth != 2:
824 if not self._nchannels:
826 if not self._sampwidth:
828 if not self._framerate:
830 self._write_header(datasize)
832 def _init_compression(self):
833 if self._comptype == b'G722':
834 self._convert = self._lin2adpcm
835 elif self._comptype in (b'ulaw', b'ULAW'):
836 self._convert = self._lin2ulaw
837 elif self._comptype in (b'alaw', b'ALAW'):
838 self._convert = self._lin2alaw
839 elif self._comptype in (b'sowt', b'SOWT'):
840 self._convert = self._lin2sowt
842 def _write_header(self, initlength):
843 if self._aifc and self._comptype != b'NONE':
844 self._init_compression()
845 self._file.write(b'FORM')
846 if not self._nframes:
847 self._nframes = initlength // (self._nchannels * self._sampwidth)
848 self._datalength = self._nframes * self._nchannels * self._sampwidth
849 if self._datalength & 1:
850 self._datalength = self._datalength + 1
851 if self._aifc:
852 if self._comptype in (b'ulaw', b'ULAW', b'alaw', b'ALAW'):
853 self._datalength = self._datalength // 2
854 if self._datalength & 1:
855 self._datalength = self._datalength + 1
856 elif self._comptype == b'G722':
857 self._datalength = (self._datalength + 3) // 4
858 if self._datalength & 1:
859 self._datalength = self._datalength + 1
861 self._form_length_pos = self._file.tell()
863 self._form_length_pos = None
864 commlength = self._write_form_length(self._datalength)
865 if self._aifc:
866 self._file.write(b'AIFC')
867 self._file.write(b'FVER')
868 _write_ulong(self._file, 4)
869 _write_ulong(self._file, self._version)
871 self._file.write(b'AIFF')
872 self._file.write(b'COMM')
873 _write_ulong(self._file, commlength)
874 _write_short(self._file, self._nchannels)
875 if self._form_length_pos is not None:
876 self._nframes_pos = self._file.tell()
877 _write_ulong(self._file, self._nframes)
878 if self._comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
879 _write_short(self._file, 8)
881 _write_short(self._file, self._sampwidth * 8)
882 _write_float(self._file, self._framerate)
883 if self._aifc:
884 self._file.write(self._comptype)
885 _write_string(self._file, self._compname)
886 self._file.write(b'SSND')
887 if self._form_length_pos is not None:
888 self._ssnd_length_pos = self._file.tell()
889 _write_ulong(self._file, self._datalength + 8)
890 _write_ulong(self._file, 0)
891 _write_ulong(self._file, 0)
893 def _write_form_length(self, datalength):
894 if self._aifc:
895 commlength = 18 + 5 + len(self._compname)
902 _write_ulong(self._file, 4 + verslength + self._marklength + \
906 def _patchheader(self):
907 curpos = self._file.tell()
908 if self._datawritten & 1:
909 datalength = self._datawritten + 1
910 self._file.write(b'\x00')
912 datalength = self._datawritten
913 if datalength == self._datalength and \
914 self._nframes == self._nframeswritten and \
915 self._marklength == 0:
916 self._file.seek(curpos, 0)
918 self._file.seek(self._form_length_pos, 0)
919 dummy = self._write_form_length(datalength)
920 self._file.seek(self._nframes_pos, 0)
921 _write_ulong(self._file, self._nframeswritten)
922 self._file.seek(self._ssnd_length_pos, 0)
923 _write_ulong(self._file, datalength + 8)
924 self._file.seek(curpos, 0)
925 self._nframes = self._nframeswritten
926 self._datalength = datalength
928 def _writemarkers(self):
929 if len(self._markers) == 0:
931 self._file.write(b'MARK')
933 for marker in self._markers:
938 _write_ulong(self._file, length)
939 self._marklength = length + 8
940 _write_short(self._file, len(self._markers))
941 for marker in self._markers:
943 _write_short(self._file, id)
944 _write_ulong(self._file, pos)
945 _write_string(self._file, name)