]> granicus.if.org Git - python/commitdiff
#5024: whichhdr now returns the frame count for WAV files.
authorR David Murray <rdmurray@bitdance.com>
Mon, 18 Mar 2013 21:42:42 +0000 (17:42 -0400)
committerR David Murray <rdmurray@bitdance.com>
Mon, 18 Mar 2013 21:42:42 +0000 (17:42 -0400)
Patch by Ned Jackson Lovely based on a suggestion by Robert Pyle.

Lib/sndhdr.py
Lib/test/test_sndhdr.py
Misc/ACKS
Misc/NEWS

index 0a2fa8dca279f1ec6049fe44a20c3d0491bccf8a..240e5072f8fdf383279c98905ecbe7816f3dee0e 100644 (file)
@@ -137,14 +137,17 @@ tests.append(test_voc)
 
 
 def test_wav(h, f):
+    import wave
     # 'RIFF' <len> 'WAVE' 'fmt ' <len>
     if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
         return None
-    style = get_short_le(h[20:22])
-    nchannels = get_short_le(h[22:24])
-    rate = get_long_le(h[24:28])
-    sample_bits = get_short_le(h[34:36])
-    return 'wav', rate, nchannels, -1, sample_bits
+    f.seek(0)
+    try:
+        w = wave.openfp(f, 'r')
+    except (EOFError, wave.Error):
+        return None
+    return ('wav', w.getframerate(), w.getnchannels(),
+                   w.getnframes(), 8*w.getsampwidth())
 
 tests.append(test_wav)
 
index 10046887d7059d0aa4bfaaf53c92588730ff8b71..5e0abe0b363d80df975450739384e2531eab0c55 100644 (file)
@@ -12,7 +12,7 @@ class TestFormats(unittest.TestCase):
             ('sndhdr.hcom', ('hcom', 22050.0, 1, -1, 8)),
             ('sndhdr.sndt', ('sndt', 44100, 1, 5, 8)),
             ('sndhdr.voc', ('voc', 0, 1, -1, 8)),
-            ('sndhdr.wav', ('wav', 44100, 2, -1, 16)),
+            ('sndhdr.wav', ('wav', 44100, 2, 5, 16)),
         ):
             filename = findfile(filename, subdir="sndhdrdata")
             what = sndhdr.what(filename)
index 30face88e3f1a971b6ebba8c61d9a4f6ca28f071..edd736d2b0a05b85471c9f37362112d6fb7b1a29 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -746,6 +746,7 @@ Hugo Lopes Tavares
 Anne Lord
 Tom Loredo
 Justin Love
+Ned Jackson Lovely
 Jason Lowe
 Tony Lownds
 Ray Loyzaga
index 1f3c68a6467b12a055bbbdcb2195144146253e71..8a82385a090122ea43c9451b35f371caa0fdbac6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -287,6 +287,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #5024: sndhdr.whichhdr now returns the frame count for WAV files
+  rather than -1.
+
 - Issue #17460: Remove the strict argument of HTTPConnection and removing the
   DeprecationWarning being issued from 3.2 onwards.