Skip to content

bpo-32990: Support WAVE_FORMAT_EXTENSIBLE in the wave module #9515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Lib/test/audiodata/pluck-pcm24-ext.wav
Binary file not shown.
29 changes: 28 additions & 1 deletion Lib/test/test_wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,33 @@ class WavePCM24Test(WaveTest, unittest.TestCase):
frames = byteswap(frames, 3)


class WavePCM24ExtTest(WaveTest, unittest.TestCase):
sndfilename = 'pluck-pcm24-ext.wav'
sndfilenframes = 3307
nchannels = 2
sampwidth = 3
framerate = 11025
nframes = 48
comptype = 'NONE'
compname = 'not compressed'
frames = bytes.fromhex("""\
022D65FFEB9D 4B5A0F00FA54 3113C304EE2B 80DCD6084303 \
CBDEC006B261 48A99803F2F8 BFE82401B07D 036BFBFE7B5D \
B85756FA3EC9 B4B055F3502B 299830EBCB62 1A5CA7E6D99A \
EDFA3EE491BD C625EBE27884 0E05A9E0B6CF EF2929E02922 \
5758D8E27067 FB3557E83E16 1377BFEF8402 D82C5BF7272A \
978F16FB7745 F5F865FC1013 086635FB9C4E DF30FCFB40EE \
117FE0FA3438 3EE6B8FB5AC3 BC77A3FCB2F4 66D6DAFF5F32 \
CF13B9041275 431D69097A8C C1BB600EC74E 5120B912A2BA \
EEDF641754C0 8207001664B7 7FFFFF14453F 8000001294E6 \
499C1B0EB3B2 52B73E0DBCA0 EFB2B20F5FD8 CE3CDB0FBE12 \
E4B49C0CEA2D 6344A80A5A7C 08C8FE0A1FFE 2BB9860B0A0E \
51486F0E44E1 8BCC64113B05 B6F4EC0EEB36 4413170A5B48 \
""")
if sys.byteorder != 'big':
frames = byteswap(frames, 3)


class WavePCM32Test(WaveTest, unittest.TestCase):
sndfilename = 'pluck-pcm32.wav'
sndfilenframes = 3307
Expand Down Expand Up @@ -109,7 +136,7 @@ class MiscTestCase(audiotests.AudioMiscTests, unittest.TestCase):
module = wave

def test__all__(self):
blacklist = {'WAVE_FORMAT_PCM'}
blacklist = {'WAVE_FORMAT_PCM', 'WAVE_FORMAT_EXTENSIBLE'}
support.check__all__(self, wave, blacklist=blacklist)


Expand Down
24 changes: 14 additions & 10 deletions Lib/wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Error(Exception):
pass

WAVE_FORMAT_PCM = 0x0001
WAVE_FORMAT_EXTENSIBLE = 0xFFFE

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

Expand Down Expand Up @@ -254,19 +255,22 @@ def readframes(self, nframes):

def _read_fmt_chunk(self, chunk):
try:
wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH', chunk.read(14))
wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, \
wBlockAlign, wBitsPerSample = \
struct.unpack_from('<HHLLHH', chunk.read(16))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously if there was no wBitsPerSample value and wFormatTag was something other than WAVE_FORMAT_PCM, we may get a different error.

Can we be sure that the longer chunk.read will always return 16 bytes? Or should we keep deferring the second read until after we've checked wFormatTag

except struct.error:
raise EOFError from None
if wFormatTag == WAVE_FORMAT_PCM:
try:
sampwidth = struct.unpack_from('<H', chunk.read(2))[0]
except struct.error:
raise EOFError from None
self._sampwidth = (sampwidth + 7) // 8
if not self._sampwidth:
raise Error('bad sample width')
else:
if wFormatTag == WAVE_FORMAT_EXTENSIBLE:
# Only the first 2 bytes (of 16) of SubFormat are needed.
cbSize, wValidBitsPerSample, dwChannelMask, \
SubFormatFmt = struct.unpack_from('<HHLH', chunk.read(10))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you reformat this line in a way that makes it easier to read? Right now it looks like two separate statements.

Using parentheses around the left hand side is fine, and then indent the second line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, we should also catch struct.error here and raise, like we do above.

if SubFormatFmt != WAVE_FORMAT_PCM:
raise Error(f'unknown format: {SubFormatFmt}')
elif wFormatTag != WAVE_FORMAT_PCM:
raise Error('unknown format: %r' % (wFormatTag,))
self._sampwidth = (wBitsPerSample + 7) // 8
if not self._sampwidth:
raise Error('bad sample width')
if not self._nchannels:
raise Error('bad # of channels')
self._framesize = self._nchannels * self._sampwidth
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Support reading wave files with the ``WAVE_FORMAT_EXTENSIBLE`` format in the
:mod:`wave` module.