Skip to content

Commit 05c042e

Browse files
gh-85525: Indicate supported sound header formats (#21575)
* Indicate supported sound header formats * modify file names Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 3c4cbd1 commit 05c042e

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Doc/library/sndhdr.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,52 @@ be the sample size in bits or ``'A'`` for A-LAW or ``'U'`` for u-LAW.
5454
.. versionchanged:: 3.5
5555
Result changed from a tuple to a namedtuple.
5656

57+
The following sound header types are recognized, as listed below with the return value
58+
from :func:`whathdr`: and :func:`what`:
59+
60+
+------------+------------------------------------+
61+
| Value | Sound header format |
62+
+============+====================================+
63+
| ``'aifc'`` | Compressed Audio Interchange Files |
64+
+------------+------------------------------------+
65+
| ``'aiff'`` | Audio Interchange Files |
66+
+------------+------------------------------------+
67+
| ``'au'`` | Au Files |
68+
+------------+------------------------------------+
69+
| ``'hcom'`` | HCOM Files |
70+
+------------+------------------------------------+
71+
+------------+------------------------------------+
72+
| ``'sndt'`` | Sndtool Sound Files |
73+
+------------+------------------------------------+
74+
| ``'voc'`` | Creative Labs Audio Files |
75+
+------------+------------------------------------+
76+
| ``'wav'`` | Waveform Audio File Format Files |
77+
+------------+------------------------------------+
78+
| ``'8svx'`` | 8-Bit Sampled Voice Files |
79+
+------------+------------------------------------+
80+
| ``'sb'`` | Signed Byte Audio Data Files |
81+
+------------+------------------------------------+
82+
| ``'ub'`` | UB Files |
83+
+------------+------------------------------------+
84+
| ``'ul'`` | uLAW Audio Files |
85+
+------------+------------------------------------+
86+
87+
.. data:: tests
88+
89+
A list of functions performing the individual tests. Each function takes two
90+
arguments: the byte-stream and an open file-like object. When :func:`what` is
91+
called with a byte-stream, the file-like object will be ``None``.
92+
93+
The test function should return a string describing the image type if the test
94+
succeeded, or ``None`` if it failed.
95+
96+
Example:
97+
98+
.. code-block:: pycon
99+
100+
>>> import sndhdr
101+
>>> imghdr.what('bass.wav')
102+
'wav'
103+
>>> imghdr.whathdr('bass.wav')
104+
'wav'
105+

Lib/sndhdr.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def whathdr(filename):
7777
tests = []
7878

7979
def test_aifc(h, f):
80+
"""AIFC and AIFF files"""
8081
with warnings.catch_warnings():
8182
warnings.simplefilter('ignore', category=DeprecationWarning)
8283
import aifc
@@ -100,6 +101,7 @@ def test_aifc(h, f):
100101

101102

102103
def test_au(h, f):
104+
"""AU and SND files"""
103105
if h.startswith(b'.snd'):
104106
func = get_long_be
105107
elif h[:4] in (b'\0ds.', b'dns.'):
@@ -133,6 +135,7 @@ def test_au(h, f):
133135

134136

135137
def test_hcom(h, f):
138+
"""HCOM file"""
136139
if h[65:69] != b'FSSD' or h[128:132] != b'HCOM':
137140
return None
138141
divisor = get_long_be(h[144:148])
@@ -146,6 +149,7 @@ def test_hcom(h, f):
146149

147150

148151
def test_voc(h, f):
152+
"""VOC file"""
149153
if not h.startswith(b'Creative Voice File\032'):
150154
return None
151155
sbseek = get_short_le(h[20:22])
@@ -160,6 +164,7 @@ def test_voc(h, f):
160164

161165

162166
def test_wav(h, f):
167+
"""WAV file"""
163168
import wave
164169
# 'RIFF' <len> 'WAVE' 'fmt ' <len>
165170
if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
@@ -176,6 +181,7 @@ def test_wav(h, f):
176181

177182

178183
def test_8svx(h, f):
184+
"""8SVX file"""
179185
if not h.startswith(b'FORM') or h[8:12] != b'8SVX':
180186
return None
181187
# Should decode it to get #channels -- assume always 1
@@ -185,6 +191,7 @@ def test_8svx(h, f):
185191

186192

187193
def test_sndt(h, f):
194+
"""SNDT file"""
188195
if h.startswith(b'SOUND'):
189196
nsamples = get_long_le(h[8:12])
190197
rate = get_short_le(h[20:22])
@@ -194,6 +201,7 @@ def test_sndt(h, f):
194201

195202

196203
def test_sndr(h, f):
204+
"""SNDR file"""
197205
if h.startswith(b'\0\0'):
198206
rate = get_short_le(h[2:4])
199207
if 4000 <= rate <= 25000:

0 commit comments

Comments
 (0)