Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a44372f

Browse files
Issue #1575020: Fixed support of 24-bit wave files on big-endian platforms.
1 parent affb9b2 commit a44372f

3 files changed

Lines changed: 15 additions & 11 deletions

File tree

Lib/test/test_wave.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ class WavePCM16Test(audiotests.AudioWriteTests,
4949
frames = audiotests.byteswap2(frames)
5050

5151

52-
@unittest.skipIf(sys.byteorder == 'big',
53-
'24-bit wave files are supported only on little-endian '
54-
'platforms')
5552
class WavePCM24Test(audiotests.AudioWriteTests,
5653
audiotests.AudioTestsWithSourceFile,
5754
unittest.TestCase):

Lib/wave.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,16 @@ class Error(Exception):
8282

8383
_array_fmts = None, 'b', 'h', None, 'i'
8484

85-
# Determine endian-ness
8685
import struct
87-
if struct.pack("h", 1) == b"\000\001":
88-
big_endian = 1
89-
else:
90-
big_endian = 0
91-
86+
import sys
9287
from 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+
9495
class 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

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issue #1575020: Fixed support of 24-bit wave files on big-endian platforms.
17+
1618
- Issue #19480: HTMLParser now accepts all valid start-tag names as defined
1719
by the HTML5 standard.
1820

0 commit comments

Comments
 (0)