@@ -514,6 +514,15 @@ def _ulaw2lin(self, data):
514514 import audioop
515515 return audioop .ulaw2lin (data , 2 )
516516
517+ def _adpcm2lin (self , data ):
518+ import audioop
519+ if not hasattr (self , '_adpcmstate' ):
520+ # first time
521+ self ._adpcmstate = None
522+ data , self ._adpcmstate = audioop .adpcm2lin (data , 2 ,
523+ self ._adpcmstate )
524+ return data
525+
517526 def _read_comm_chunk (self , chunk ):
518527 self ._nchannels = _read_short (chunk )
519528 self ._nframes = _read_long (chunk )
@@ -539,6 +548,16 @@ def _read_comm_chunk(self, chunk):
539548 #DEBUG end
540549 self ._compname = _read_string (chunk )
541550 if self ._comptype != 'NONE' :
551+ if self ._comptype == 'G722' :
552+ try :
553+ import audioop
554+ except ImportError :
555+ pass
556+ else :
557+ self ._convert = self ._adpcm2lin
558+ self ._framesize = self ._framesize / 4
559+ return
560+ # for ULAW and ALAW try Compression Library
542561 try :
543562 import cl , CL
544563 except ImportError :
@@ -711,7 +730,7 @@ def getnframes(self):
711730 def setcomptype (self , comptype , compname ):
712731 if self ._nframeswritten :
713732 raise Error , 'cannot change parameters after starting to write'
714- if comptype not in ('NONE' , 'ULAW' , 'ALAW' ):
733+ if comptype not in ('NONE' , 'ULAW' , 'ALAW' , 'G722' ):
715734 raise Error , 'unsupported compression type'
716735 self ._comptype = comptype
717736 self ._compname = compname
@@ -730,7 +749,7 @@ def getcompname(self):
730749 def setparams (self , (nchannels , sampwidth , framerate , nframes , comptype , compname )):
731750 if self ._nframeswritten :
732751 raise Error , 'cannot change parameters after starting to write'
733- if comptype not in ('NONE' , 'ULAW' , 'ALAW' ):
752+ if comptype not in ('NONE' , 'ULAW' , 'ALAW' , 'G722' ):
734753 raise Error , 'unsupported compression type'
735754 self .setnchannels (nchannels )
736755 self .setsampwidth (sampwidth )
@@ -817,13 +836,26 @@ def _lin2ulaw(self, data):
817836 import audioop
818837 return audioop .lin2ulaw (data , 2 )
819838
839+ def _lin2adpcm (self , data ):
840+ import audioop
841+ if not hasattr (self , '_adpcmstate' ):
842+ self ._adpcmstate = None
843+ data , self ._adpcmstate = audioop .lin2adpcm (data , 2 ,
844+ self ._adpcmstate )
845+ return data
846+
820847 def _ensure_header_written (self , datasize ):
821848 if not self ._nframeswritten :
822849 if self ._comptype in ('ULAW' , 'ALAW' ):
823850 if not self ._sampwidth :
824851 self ._sampwidth = 2
825852 if self ._sampwidth != 2 :
826853 raise Error , 'sample width must be 2 when compressing with ULAW or ALAW'
854+ if self ._comptype == 'G722' :
855+ if not self ._sampwidth :
856+ self ._sampwidth = 2
857+ if self ._sampwidth != 2 :
858+ raise Error , 'sample width must be 2 when compressing with G7.22 (ADPCM)'
827859 if not self ._nchannels :
828860 raise Error , '# channels not specified'
829861 if not self ._sampwidth :
@@ -833,6 +865,10 @@ def _ensure_header_written(self, datasize):
833865 self ._write_header (datasize )
834866
835867 def _init_compression (self ):
868+ if self ._comptype == 'G722' :
869+ import audioop
870+ self ._convert = self ._lin2adpcm
871+ return
836872 try :
837873 import cl , CL
838874 except ImportError :
@@ -876,10 +912,15 @@ def _write_header(self, initlength):
876912 self ._datalength = self ._nframes * self ._nchannels * self ._sampwidth
877913 if self ._datalength & 1 :
878914 self ._datalength = self ._datalength + 1
879- if self ._aifc and self ._comptype in ('ULAW' , 'ALAW' ):
880- self ._datalength = self ._datalength / 2
881- if self ._datalength & 1 :
882- self ._datalength = self ._datalength + 1
915+ if self ._aifc :
916+ if self ._comptype in ('ULAW' , 'ALAW' ):
917+ self ._datalength = self ._datalength / 2
918+ if self ._datalength & 1 :
919+ self ._datalength = self ._datalength + 1
920+ elif self ._comptype == 'G722' :
921+ self ._datalength = (self ._datalength + 3 ) / 4
922+ if self ._datalength & 1 :
923+ self ._datalength = self ._datalength + 1
883924 self ._form_length_pos = self ._file .tell ()
884925 commlength = self ._write_form_length (self ._datalength )
885926 if self ._aifc :
0 commit comments