@@ -87,6 +87,12 @@ class Error(Exception):
8787from chunk import Chunk
8888from collections import namedtuple
8989
90+ def _byteswap3 (data ):
91+ ba = bytearray (data )
92+ ba [::3 ] = data [2 ::3 ]
93+ ba [2 ::3 ] = data [::3 ]
94+ return bytes (ba )
95+
9096_wave_params = namedtuple ('_wave_params' ,
9197 'nchannels sampwidth framerate nframes comptype compname' )
9298
@@ -237,7 +243,7 @@ def readframes(self, nframes):
237243 self ._data_seek_needed = 0
238244 if nframes == 0 :
239245 return b''
240- if self ._sampwidth > 1 and sys .byteorder == 'big' :
246+ if self ._sampwidth in ( 2 , 4 ) and sys .byteorder == 'big' :
241247 # unfortunately the fromfile() method does not take
242248 # something that only looks like a file object, so
243249 # we have to reach into the innards of the chunk object
@@ -258,6 +264,8 @@ def readframes(self, nframes):
258264 data = data .tobytes ()
259265 else :
260266 data = self ._data_chunk .read (nframes * self ._framesize )
267+ if self ._sampwidth == 3 and sys .byteorder == 'big' :
268+ data = _byteswap3 (data )
261269 if self ._convert and data :
262270 data = self ._convert (data )
263271 self ._soundpos = self ._soundpos + len (data ) // (self ._nchannels * self ._sampwidth )
@@ -431,14 +439,16 @@ def writeframesraw(self, data):
431439 nframes = len (data ) // (self ._sampwidth * self ._nchannels )
432440 if self ._convert :
433441 data = self ._convert (data )
434- if self ._sampwidth > 1 and sys .byteorder == 'big' :
442+ if self ._sampwidth in ( 2 , 4 ) and sys .byteorder == 'big' :
435443 import array
436444 data = array .array (_array_fmts [self ._sampwidth ], data )
437445 assert data .itemsize == self ._sampwidth
438446 data .byteswap ()
439447 data .tofile (self ._file )
440448 self ._datawritten = self ._datawritten + len (data ) * self ._sampwidth
441449 else :
450+ if self ._sampwidth == 3 and sys .byteorder == 'big' :
451+ data = _byteswap3 (data )
442452 self ._file .write (data )
443453 self ._datawritten = self ._datawritten + len (data )
444454 self ._nframeswritten = self ._nframeswritten + nframes
0 commit comments