Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a48b9d

Browse files
committed
#5024: whichhdr now returns the frame count for WAV files.
Patch by Ned Jackson Lovely based on a suggestion by Robert Pyle.
1 parent 052ddb0 commit 1a48b9d

4 files changed

Lines changed: 13 additions & 6 deletions

File tree

Lib/sndhdr.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,17 @@ def test_voc(h, f):
137137

138138

139139
def test_wav(h, f):
140+
import wave
140141
# 'RIFF' <len> 'WAVE' 'fmt ' <len>
141142
if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
142143
return None
143-
style = get_short_le(h[20:22])
144-
nchannels = get_short_le(h[22:24])
145-
rate = get_long_le(h[24:28])
146-
sample_bits = get_short_le(h[34:36])
147-
return 'wav', rate, nchannels, -1, sample_bits
144+
f.seek(0)
145+
try:
146+
w = wave.openfp(f, 'r')
147+
except (EOFError, wave.Error):
148+
return None
149+
return ('wav', w.getframerate(), w.getnchannels(),
150+
w.getnframes(), 8*w.getsampwidth())
148151

149152
tests.append(test_wav)
150153

Lib/test/test_sndhdr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_data(self):
1212
('sndhdr.hcom', ('hcom', 22050.0, 1, -1, 8)),
1313
('sndhdr.sndt', ('sndt', 44100, 1, 5, 8)),
1414
('sndhdr.voc', ('voc', 0, 1, -1, 8)),
15-
('sndhdr.wav', ('wav', 44100, 2, -1, 16)),
15+
('sndhdr.wav', ('wav', 44100, 2, 5, 16)),
1616
):
1717
filename = findfile(filename, subdir="sndhdrdata")
1818
what = sndhdr.what(filename)

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ Hugo Lopes Tavares
746746
Anne Lord
747747
Tom Loredo
748748
Justin Love
749+
Ned Jackson Lovely
749750
Jason Lowe
750751
Tony Lownds
751752
Ray Loyzaga

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ Core and Builtins
287287
Library
288288
-------
289289

290+
- Issue #5024: sndhdr.whichhdr now returns the frame count for WAV files
291+
rather than -1.
292+
290293
- Issue #17460: Remove the strict argument of HTTPConnection and removing the
291294
DeprecationWarning being issued from 3.2 onwards.
292295

0 commit comments

Comments
 (0)