-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,7 @@ class Error(Exception): | |
pass | ||
|
||
WAVE_FORMAT_PCM = 0x0001 | ||
WAVE_FORMAT_EXTENSIBLE = 0xFFFE | ||
|
||
_array_fmts = None, 'b', 'h', None, 'i' | ||
|
||
|
@@ -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)) | ||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, we should also catch |
||
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 | ||
|
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. |
There was a problem hiding this comment.
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 andwFormatTag
was something other thanWAVE_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 checkedwFormatTag