]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-77171 Support WAVE_FORMAT_EXTENSIBLE in the wave module (GH-96777)
authorYusuke Kadowaki <60080334+yusuke-kadowaki@users.noreply.github.com>
Wed, 14 Sep 2022 13:34:40 +0000 (22:34 +0900)
committerGitHub <noreply@github.com>
Wed, 14 Sep 2022 13:34:40 +0000 (14:34 +0100)
The test file, a modified version of Lib/test/audiodata/pluck-pcm24.wav, was provided by Andrea Celletti on the bug tracker.

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Lib/test/audiodata/pluck-pcm24-ext.wav [new file with mode: 0644]
Lib/test/test_wave.py
Lib/wave.py
Misc/NEWS.d/next/Library/2018-09-23-07-47-29.bpo-32990.2FVVTU.rst [new file with mode: 0644]

diff --git a/Lib/test/audiodata/pluck-pcm24-ext.wav b/Lib/test/audiodata/pluck-pcm24-ext.wav
new file mode 100644 (file)
index 0000000..e4c2d13
Binary files /dev/null and b/Lib/test/audiodata/pluck-pcm24-ext.wav differ
index 0cc94e88b437d7e9200d0beb2e72209a0cf26606..35660fd4d34e183a3b5c8318d83d414a98673344 100644 (file)
@@ -77,6 +77,33 @@ class WavePCM24Test(WaveTest, unittest.TestCase):
         frames = wave._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 = wave._byteswap(frames, 3)
+
+
 class WavePCM32Test(WaveTest, unittest.TestCase):
     sndfilename = 'pluck-pcm32.wav'
     sndfilenframes = 3307
@@ -106,7 +133,8 @@ class WavePCM32Test(WaveTest, unittest.TestCase):
 
 class MiscTestCase(unittest.TestCase):
     def test__all__(self):
-        support.check__all__(self, wave, not_exported={'WAVE_FORMAT_PCM'})
+        not_exported = {'WAVE_FORMAT_PCM', 'WAVE_FORMAT_EXTENSIBLE'}
+        support.check__all__(self, wave, not_exported=not_exported)
 
 
 class WaveLowLevelTest(unittest.TestCase):
index 9a4557487b6e6392de7d6f67b20bdb1677ed1a09..e446174b4b22b9ff1c0272df33c26bf3be82b076 100644 (file)
@@ -83,6 +83,7 @@ class Error(Exception):
     pass
 
 WAVE_FORMAT_PCM = 0x0001
+WAVE_FORMAT_EXTENSIBLE = 0xFFFE
 
 _array_fmts = None, 'b', 'h', None, 'i'
 
@@ -377,16 +378,23 @@ class Wave_read:
             wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH', chunk.read(14))
         except struct.error:
             raise EOFError from None
-        if wFormatTag == WAVE_FORMAT_PCM:
+        if wFormatTag != WAVE_FORMAT_PCM and wFormatTag != WAVE_FORMAT_EXTENSIBLE:
+            raise Error('unknown format: %r' % (wFormatTag,))
+        try:
+            sampwidth = struct.unpack_from('<H', chunk.read(2))[0]
+        except struct.error:
+            raise EOFError from None
+        if wFormatTag == WAVE_FORMAT_EXTENSIBLE:
             try:
-                sampwidth = struct.unpack_from('<H', chunk.read(2))[0]
+                # Only the first 2 bytes (of 16) of SubFormat are needed.
+                cbSize, wValidBitsPerSample, dwChannelMask, SubFormatFmt = struct.unpack_from('<HHLH', chunk.read(10))
             except struct.error:
                 raise EOFError from None
-            self._sampwidth = (sampwidth + 7) // 8
-            if not self._sampwidth:
-                raise Error('bad sample width')
-        else:
-            raise Error('unknown format: %r' % (wFormatTag,))
+            if SubFormatFmt != WAVE_FORMAT_PCM:
+                raise Error(f'unknown format: {SubFormatFmt}')
+        self._sampwidth = (sampwidth + 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
diff --git a/Misc/NEWS.d/next/Library/2018-09-23-07-47-29.bpo-32990.2FVVTU.rst b/Misc/NEWS.d/next/Library/2018-09-23-07-47-29.bpo-32990.2FVVTU.rst
new file mode 100644 (file)
index 0000000..7d7f7e3
--- /dev/null
@@ -0,0 +1,2 @@
+Support reading wave files with the ``WAVE_FORMAT_EXTENSIBLE`` format in the
+:mod:`wave` module.