@@ -259,6 +259,9 @@ def getcompname(self):
259259 else :
260260 return 'not compressed'
261261
262+ def getencoding (self ):
263+ return self ._encoding
264+
262265 def getparams (self ):
263266 return _sunau_params (self .getnchannels (), self .getsampwidth (),
264267 self .getframerate (), self .getnframes (),
@@ -342,6 +345,7 @@ def initfp(self, file):
342345 self ._datawritten = 0
343346 self ._datalength = 0
344347 self ._info = b''
348+ self ._encoding = AUDIO_FILE_ENCODING_MULAW_8
345349 self ._comptype = 'ULAW' # default is U-law
346350
347351 def setnchannels (self , nchannels ):
@@ -362,6 +366,7 @@ def setsampwidth(self, sampwidth):
362366 if sampwidth not in (1 , 2 , 3 , 4 ):
363367 raise Error ('bad sample width' )
364368 self ._sampwidth = sampwidth
369+ self ._update_encoding ()
365370
366371 def getsampwidth (self ):
367372 if not self ._framerate :
@@ -405,6 +410,31 @@ def getcompname(self):
405410 else :
406411 return 'not compressed'
407412
413+ def setencoding (self , encoding ):
414+ if self ._nframeswritten :
415+ raise Error ('cannot change parameters after starting to write' )
416+ if encoding == AUDIO_FILE_ENCODING_LINEAR_8 :
417+ self .setcomptype ('NONE' , None )
418+ self .setsampwidth (1 )
419+ elif encoding == AUDIO_FILE_ENCODING_LINEAR_16 :
420+ self .setcomptype ('NONE' , None )
421+ self .setsampwidth (2 )
422+ elif encoding == AUDIO_FILE_ENCODING_LINEAR_24 :
423+ self .setcomptype ('NONE' , None )
424+ self .setsampwidth (3 )
425+ elif encoding == AUDIO_FILE_ENCODING_LINEAR_32 :
426+ self .setcomptype ('NONE' , None )
427+ self .setsampwidth (4 )
428+ elif encoding == AUDIO_FILE_ENCODING_MULAW_8 :
429+ self .setcomptype ('ULAW' , None )
430+ self .setsampwidth (2 )
431+ else :
432+ raise Error ('unsupported encoding %r' , encoding )
433+ assert self ._encoding == encoding
434+
435+ def getencoding (self ):
436+ return self ._encoding
437+
408438 def setparams (self , params ):
409439 nchannels , sampwidth , framerate , nframes , comptype , compname = params
410440 self .setnchannels (nchannels )
@@ -458,38 +488,40 @@ def close(self):
458488 #
459489 # private methods
460490 #
461-
462- def _ensure_header_written (self ):
463- if not self ._nframeswritten :
464- if not self ._nchannels :
465- raise Error ('# of channels not specified' )
466- if not self ._sampwidth :
467- raise Error ('sample width not specified' )
468- if not self ._framerate :
469- raise Error ('frame rate not specified' )
470- self ._write_header ()
471-
472- def _write_header (self ):
491+ def _update_encoding (self ):
473492 if self ._comptype == 'NONE' :
474493 if self ._sampwidth == 1 :
475- encoding = AUDIO_FILE_ENCODING_LINEAR_8
494+ self . _encoding = AUDIO_FILE_ENCODING_LINEAR_8
476495 self ._framesize = 1
477496 elif self ._sampwidth == 2 :
478- encoding = AUDIO_FILE_ENCODING_LINEAR_16
497+ self . _encoding = AUDIO_FILE_ENCODING_LINEAR_16
479498 self ._framesize = 2
480499 elif self ._sampwidth == 3 :
481- encoding = AUDIO_FILE_ENCODING_LINEAR_24
500+ self . _encoding = AUDIO_FILE_ENCODING_LINEAR_24
482501 self ._framesize = 3
483502 elif self ._sampwidth == 4 :
484- encoding = AUDIO_FILE_ENCODING_LINEAR_32
503+ self . _encoding = AUDIO_FILE_ENCODING_LINEAR_32
485504 self ._framesize = 4
486505 else :
487506 raise Error ('internal error' )
488507 elif self ._comptype == 'ULAW' :
489- encoding = AUDIO_FILE_ENCODING_MULAW_8
508+ self . _encoding = AUDIO_FILE_ENCODING_MULAW_8
490509 self ._framesize = 1
491510 else :
492511 raise Error ('internal error' )
512+
513+ def _ensure_header_written (self ):
514+ if not self ._nframeswritten :
515+ if not self ._nchannels :
516+ raise Error ('# of channels not specified' )
517+ if not self ._sampwidth :
518+ raise Error ('sample width not specified' )
519+ if not self ._framerate :
520+ raise Error ('frame rate not specified' )
521+ self ._write_header ()
522+
523+ def _write_header (self ):
524+ self ._update_encoding ()
493525 self ._framesize = self ._framesize * self ._nchannels
494526 _write_u32 (self ._file , AUDIO_FILE_MAGIC )
495527 header_size = 25 + len (self ._info )
@@ -505,7 +537,7 @@ def _write_header(self):
505537 self ._form_length_pos = None
506538 _write_u32 (self ._file , length )
507539 self ._datalength = length
508- _write_u32 (self ._file , encoding )
540+ _write_u32 (self ._file , self . _encoding )
509541 _write_u32 (self ._file , self ._framerate )
510542 _write_u32 (self ._file , self ._nchannels )
511543 self ._file .write (self ._info )
0 commit comments