Issue #1575020: Fixed support of 24-bit wave files on big-endian platforms.

This commit is contained in:
Serhiy Storchaka 2013-11-09 23:12:06 +02:00
parent affb9b255b
commit a44372fd0b
3 changed files with 15 additions and 11 deletions

View File

@ -49,9 +49,6 @@ class WavePCM16Test(audiotests.AudioWriteTests,
frames = audiotests.byteswap2(frames) frames = audiotests.byteswap2(frames)
@unittest.skipIf(sys.byteorder == 'big',
'24-bit wave files are supported only on little-endian '
'platforms')
class WavePCM24Test(audiotests.AudioWriteTests, class WavePCM24Test(audiotests.AudioWriteTests,
audiotests.AudioTestsWithSourceFile, audiotests.AudioTestsWithSourceFile,
unittest.TestCase): unittest.TestCase):

View File

@ -82,15 +82,16 @@ WAVE_FORMAT_PCM = 0x0001
_array_fmts = None, 'b', 'h', None, 'i' _array_fmts = None, 'b', 'h', None, 'i'
# Determine endian-ness
import struct import struct
if struct.pack("h", 1) == b"\000\001": import sys
big_endian = 1
else:
big_endian = 0
from chunk import Chunk from chunk import Chunk
def _byteswap3(data):
ba = bytearray(data)
ba[::3] = data[2::3]
ba[2::3] = data[::3]
return bytes(ba)
class Wave_read: class Wave_read:
"""Variables used in this class: """Variables used in this class:
@ -231,7 +232,7 @@ class Wave_read:
self._data_seek_needed = 0 self._data_seek_needed = 0
if nframes == 0: if nframes == 0:
return b'' return b''
if self._sampwidth > 1 and big_endian: if self._sampwidth in (2, 4) and sys.byteorder == 'big':
# unfortunately the fromfile() method does not take # unfortunately the fromfile() method does not take
# something that only looks like a file object, so # something that only looks like a file object, so
# we have to reach into the innards of the chunk object # we have to reach into the innards of the chunk object
@ -252,6 +253,8 @@ class Wave_read:
data = data.tobytes() data = data.tobytes()
else: else:
data = self._data_chunk.read(nframes * self._framesize) data = self._data_chunk.read(nframes * self._framesize)
if self._sampwidth == 3 and sys.byteorder == 'big':
data = _byteswap3(data)
if self._convert and data: if self._convert and data:
data = self._convert(data) data = self._convert(data)
self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth) self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
@ -419,7 +422,7 @@ class Wave_write:
nframes = len(data) // (self._sampwidth * self._nchannels) nframes = len(data) // (self._sampwidth * self._nchannels)
if self._convert: if self._convert:
data = self._convert(data) data = self._convert(data)
if self._sampwidth > 1 and big_endian: if self._sampwidth in (2, 4) and sys.byteorder == 'big':
import array import array
data = array.array(_array_fmts[self._sampwidth], data) data = array.array(_array_fmts[self._sampwidth], data)
assert data.itemsize == self._sampwidth assert data.itemsize == self._sampwidth
@ -427,6 +430,8 @@ class Wave_write:
data.tofile(self._file) data.tofile(self._file)
self._datawritten = self._datawritten + len(data) * self._sampwidth self._datawritten = self._datawritten + len(data) * self._sampwidth
else: else:
if self._sampwidth == 3 and sys.byteorder == 'big':
data = _byteswap3(data)
self._file.write(data) self._file.write(data)
self._datawritten = self._datawritten + len(data) self._datawritten = self._datawritten + len(data)
self._nframeswritten = self._nframeswritten + nframes self._nframeswritten = self._nframeswritten + nframes

View File

@ -13,6 +13,8 @@ Core and Builtins
Library Library
------- -------
- Issue #1575020: Fixed support of 24-bit wave files on big-endian platforms.
- Issue #19480: HTMLParser now accepts all valid start-tag names as defined - Issue #19480: HTMLParser now accepts all valid start-tag names as defined
by the HTML5 standard. by the HTML5 standard.