aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-11-09 23:15:52 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2013-11-09 23:15:52 +0200
commit29828a6fa9a7d42b1f2383860d37cdf7d9e74d49 (patch)
tree6bac2f43bc35adc160d8fc2b5407a0861c5d514a /Lib/wave.py
parentFix Tkinter tests on Tk 8.5 with patchlevel < 8.5.11 (issue #19085). (diff)
parentIssue #1575020: Fixed support of 24-bit wave files on big-endian platforms. (diff)
downloadcpython-29828a6fa9a7d42b1f2383860d37cdf7d9e74d49.tar.gz
cpython-29828a6fa9a7d42b1f2383860d37cdf7d9e74d49.tar.bz2
cpython-29828a6fa9a7d42b1f2383860d37cdf7d9e74d49.zip
Issue #1575020: Fixed support of 24-bit wave files on big-endian platforms.
Diffstat (limited to 'Lib/wave.py')
-rw-r--r--Lib/wave.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/wave.py b/Lib/wave.py
index 0e6628b087a..e7234239454 100644
--- a/Lib/wave.py
+++ b/Lib/wave.py
@@ -87,6 +87,12 @@ import sys
from chunk import Chunk
from collections import namedtuple
+def _byteswap3(data):
+ ba = bytearray(data)
+ ba[::3] = data[2::3]
+ ba[2::3] = data[::3]
+ return bytes(ba)
+
_wave_params = namedtuple('_wave_params',
'nchannels sampwidth framerate nframes comptype compname')
@@ -237,7 +243,7 @@ class Wave_read:
self._data_seek_needed = 0
if nframes == 0:
return b''
- if self._sampwidth > 1 and sys.byteorder == 'big':
+ if self._sampwidth in (2, 4) and sys.byteorder == 'big':
# unfortunately the fromfile() method does not take
# something that only looks like a file object, so
# we have to reach into the innards of the chunk object
@@ -258,6 +264,8 @@ class Wave_read:
data = data.tobytes()
else:
data = self._data_chunk.read(nframes * self._framesize)
+ if self._sampwidth == 3 and sys.byteorder == 'big':
+ data = _byteswap3(data)
if self._convert and data:
data = self._convert(data)
self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
@@ -431,7 +439,7 @@ class Wave_write:
nframes = len(data) // (self._sampwidth * self._nchannels)
if self._convert:
data = self._convert(data)
- if self._sampwidth > 1 and sys.byteorder == 'big':
+ if self._sampwidth in (2, 4) and sys.byteorder == 'big':
import array
data = array.array(_array_fmts[self._sampwidth], data)
assert data.itemsize == self._sampwidth
@@ -439,6 +447,8 @@ class Wave_write:
data.tofile(self._file)
self._datawritten = self._datawritten + len(data) * self._sampwidth
else:
+ if self._sampwidth == 3 and sys.byteorder == 'big':
+ data = _byteswap3(data)
self._file.write(data)
self._datawritten = self._datawritten + len(data)
self._nframeswritten = self._nframeswritten + nframes