@@ -82,15 +82,16 @@ class Error(Exception):
8282
8383_array_fmts = None , 'b' , 'h' , None , 'i'
8484
85- # Determine endian-ness
8685import struct
87- if struct .pack ("h" , 1 ) == b"\000 \001 " :
88- big_endian = 1
89- else :
90- big_endian = 0
91-
86+ import sys
9287from chunk import Chunk
9388
89+ def _byteswap3 (data ):
90+ ba = bytearray (data )
91+ ba [::3 ] = data [2 ::3 ]
92+ ba [2 ::3 ] = data [::3 ]
93+ return bytes (ba )
94+
9495class Wave_read :
9596 """Variables used in this class:
9697
@@ -231,7 +232,7 @@ def readframes(self, nframes):
231232 self ._data_seek_needed = 0
232233 if nframes == 0 :
233234 return b''
234- if self ._sampwidth > 1 and big_endian :
235+ if self ._sampwidth in ( 2 , 4 ) and sys . byteorder == 'big' :
235236 # unfortunately the fromfile() method does not take
236237 # something that only looks like a file object, so
237238 # we have to reach into the innards of the chunk object
@@ -252,6 +253,8 @@ def readframes(self, nframes):
252253 data = data .tobytes ()
253254 else :
254255 data = self ._data_chunk .read (nframes * self ._framesize )
256+ if self ._sampwidth == 3 and sys .byteorder == 'big' :
257+ data = _byteswap3 (data )
255258 if self ._convert and data :
256259 data = self ._convert (data )
257260 self ._soundpos = self ._soundpos + len (data ) // (self ._nchannels * self ._sampwidth )
@@ -419,14 +422,16 @@ def writeframesraw(self, data):
419422 nframes = len (data ) // (self ._sampwidth * self ._nchannels )
420423 if self ._convert :
421424 data = self ._convert (data )
422- if self ._sampwidth > 1 and big_endian :
425+ if self ._sampwidth in ( 2 , 4 ) and sys . byteorder == 'big' :
423426 import array
424427 data = array .array (_array_fmts [self ._sampwidth ], data )
425428 assert data .itemsize == self ._sampwidth
426429 data .byteswap ()
427430 data .tofile (self ._file )
428431 self ._datawritten = self ._datawritten + len (data ) * self ._sampwidth
429432 else :
433+ if self ._sampwidth == 3 and sys .byteorder == 'big' :
434+ data = _byteswap3 (data )
430435 self ._file .write (data )
431436 self ._datawritten = self ._datawritten + len (data )
432437 self ._nframeswritten = self ._nframeswritten + nframes
0 commit comments