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

Skip to content

Commit 29828a6

Browse files
Issue #1575020: Fixed support of 24-bit wave files on big-endian platforms.
2 parents 35ac05e + a44372f commit 29828a6

3 files changed

Lines changed: 14 additions & 5 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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class Error(Exception):
8787
from chunk import Chunk
8888
from 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

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Core and Builtins
3434
Library
3535
-------
3636

37+
- Issue #1575020: Fixed support of 24-bit wave files on big-endian platforms.
38+
3739
- Issue #19378: Fixed a number of cases in the dis module where the new
3840
"file" parameter was not being honoured correctly
3941

0 commit comments

Comments
 (0)