@@ -356,6 +356,7 @@ class Aifc_read():
356356 # _ssnd_seek_needed -- 1 iff positioned correctly in audio
357357 # file for readframes()
358358 # _ssnd_chunk -- instantiation of a chunk class for the SSND chunk
359+ # _framesize -- size of one frame in the file
359360 def initfp (self , file ):
360361 self ._file = file
361362 self ._version = 0
@@ -497,24 +498,15 @@ def readframes(self, nframes):
497498 if self ._ssnd_seek_needed :
498499 self ._ssnd_chunk .rewind ()
499500 dummy = self ._ssnd_chunk .read (8 )
500- pos = self ._soundpos * self ._nchannels * self ._sampwidth
501- if self ._decomp :
502- if self ._comptype in ('ULAW' , 'ALAW' ):
503- pos = pos / 2
501+ pos = self ._soundpos * self ._framesize
504502 if pos :
505503 self ._ssnd_chunk .setpos (pos + 8 )
506504 self ._ssnd_seek_needed = 0
507505 if nframes == 0 :
508506 return ''
509- size = nframes * self ._nchannels * self ._sampwidth
510- if self ._decomp :
511- if self ._comptype in ('ULAW' , 'ALAW' ):
512- size = size / 2
513- data = self ._ssnd_chunk .read (size )
507+ data = self ._ssnd_chunk .read (nframes * self ._framesize )
514508 if self ._decomp and data :
515- params = [CL .FRAME_BUFFER_SIZE , len (data ) * 2 , \
516- CL .COMPRESSED_BUFFER_SIZE , len (data )]
517- self ._decomp .SetParams (params )
509+ self ._decomp .SetParam (CL .FRAME_BUFFER_SIZE , len (data ) * 2 )
518510 data = self ._decomp .Decompress (len (data ) / self ._nchannels , data )
519511 self ._soundpos = self ._soundpos + len (data ) / (self ._nchannels * self ._sampwidth )
520512 return data
@@ -530,6 +522,7 @@ def _read_comm_chunk(self, chunk):
530522 self ._sampwidth = _convert1 (sampwidth , _sampwidthlist )
531523 framerate = _read_float (chunk )
532524 self ._framerate = _convert1 (framerate , _frameratelist )
525+ self ._framesize = self ._nchannels * self ._sampwidth
533526 if self ._aifc :
534527 #DEBUG: SGI's soundeditor produces a bad size :-(
535528 kludge = 0
@@ -555,8 +548,10 @@ def _read_comm_chunk(self, chunk):
555548 raise Error , 'cannot read compressed AIFF-C files'
556549 if self ._comptype == 'ULAW' :
557550 scheme = CL .G711_ULAW
551+ self ._framesize = self ._framesize / 2
558552 elif self ._comptype == 'ALAW' :
559553 scheme = CL .G711_ALAW
554+ self ._framesize = self ._framesize / 2
560555 else :
561556 raise Error , 'unsupported compression type'
562557 self ._decomp = cl .OpenDecompressor (scheme )
@@ -643,6 +638,7 @@ def aifc(self):
643638 def setnchannels (self , nchannels ):
644639 if self ._nframeswritten :
645640 raise Error , 'cannot change parameters after starting to write'
641+ dummy = _convert (nchannels , _nchannelslist )
646642 self ._nchannels = nchannels
647643
648644 def getnchannels (self ):
@@ -653,6 +649,7 @@ def getnchannels(self):
653649 def setsampwidth (self , sampwidth ):
654650 if self ._nframeswritten :
655651 raise Error , 'cannot change parameters after starting to write'
652+ dummy = _convert2 (sampwidth , _sampwidthlist )
656653 self ._sampwidth = sampwidth
657654
658655 def getsampwidth (self ):
@@ -663,6 +660,7 @@ def getsampwidth(self):
663660 def setframerate (self , framerate ):
664661 if self ._nframeswritten :
665662 raise Error , 'cannot change parameters after starting to write'
663+ dummy = _convert2 (framerate , _frameratelist )
666664 self ._framerate = framerate
667665
668666 def getframerate (self ):
@@ -702,6 +700,9 @@ def setparams(self, (nchannels, sampwidth, framerate, nframes, comptype, compnam
702700 raise Error , 'cannot change parameters after starting to write'
703701 if comptype not in ('NONE' , 'ULAW' , 'ALAW' ):
704702 raise Error , 'unsupported compression type'
703+ dummy = _convert2 (nchannels , _nchannelslist )
704+ dummy = _convert2 (sampwidth , _sampwidthlist )
705+ dummy = _convert2 (framerate , _frameratelist )
705706 self ._nchannels = nchannels
706707 self ._sampwidth = sampwidth
707708 self ._framerate = framerate
@@ -755,9 +756,9 @@ def writeframesraw(self, data):
755756 self ._write_header (len (data ))
756757 nframes = len (data ) / (self ._sampwidth * self ._nchannels )
757758 if self ._comp :
758- params = [ CL .FRAME_BUFFER_SIZE , len (data ), \
759- CL .COMPRESSED_BUFFER_SIZE , len ( data )]
760- self . _comp . SetParams ( params )
759+ self . _comp . SetParam ( CL .FRAME_BUFFER_SIZE , len (data ))
760+ self . _comp . SetParam ( CL .COMPRESSED_BUFFER_SIZE , \
761+ len ( data ) )
761762 data = self ._comp .Compress (nframes , data )
762763 self ._file .write (data )
763764 self ._nframeswritten = self ._nframeswritten + nframes
@@ -803,7 +804,9 @@ def _write_header(self, initlength):
803804 self ._comp = cl .OpenCompressor (scheme )
804805 params = [CL .ORIGINAL_FORMAT , 0 , \
805806 CL .BITS_PER_COMPONENT , 0 , \
806- CL .FRAME_RATE , self ._framerate ]
807+ CL .FRAME_RATE , self ._framerate , \
808+ CL .FRAME_BUFFER_SIZE , 100 , \
809+ CL .COMPRESSED_BUFFER_SIZE , 100 ]
807810 if self ._nchannels == AL .MONO :
808811 params [1 ] = CL .MONO
809812 else :
@@ -815,6 +818,8 @@ def _write_header(self, initlength):
815818 else :
816819 params [3 ] = 24
817820 self ._comp .SetParams (params )
821+ # the compressor produces a header which we ignore
822+ dummy = self ._comp .Compress (0 , '' )
818823 self ._file .write ('FORM' )
819824 if not self ._nframes :
820825 self ._nframes = initlength / (self ._nchannels * self ._sampwidth )
@@ -836,7 +841,7 @@ def _write_header(self, initlength):
836841 self ._file .write ('AIFF' )
837842 self ._file .write ('COMM' )
838843 _write_long (self ._file , commlength )
839- _write_short (self ._file , self ._nchannels )
844+ _write_short (self ._file , _convert2 ( self ._nchannels , _nchannelslist ) )
840845 self ._nframes_pos = self ._file .tell ()
841846 _write_long (self ._file , self ._nframes )
842847 _write_short (self ._file , _convert2 (self ._sampwidth , _sampwidthlist ))
0 commit comments