From c5224866831864f85a0c5c59ad8f0467201a5833 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye Date: Tue, 21 Jul 2020 01:56:16 +0000 Subject: [PATCH 1/2] Indicate supported sound header formats --- Doc/library/sndhdr.rst | 48 ++++++++++++++++++++++++++++++++++++++++++ Lib/sndhdr.py | 8 +++++++ 2 files changed, 56 insertions(+) diff --git a/Doc/library/sndhdr.rst b/Doc/library/sndhdr.rst index 6bfa9a9fd210be..b5db489671eb4e 100644 --- a/Doc/library/sndhdr.rst +++ b/Doc/library/sndhdr.rst @@ -49,3 +49,51 @@ be the sample size in bits or ``'A'`` for A-LAW or ``'U'`` for u-LAW. .. versionchanged:: 3.5 Result changed from a tuple to a namedtuple. +The following sound header types are recognized, as listed below with the return value +from :func:`whathdr`: and :func:`what`: + ++------------+------------------------------------+ +| Value | Sound header format | ++============+====================================+ +| ``'aifc'`` | Compressed Audio Interchange Files | ++------------+------------------------------------+ +| ``'aiff'`` | Audio Interchange Files | ++------------+------------------------------------+ +| ``'au'`` | AU Files | ++------------+------------------------------------+ +| ``'hcom'`` | HCOM Files | ++------------+------------------------------------+ +| ``'sndr'`` | SNDR Files | ++------------+------------------------------------+ +| ``'sndt'`` | SNDT Files | ++------------+------------------------------------+ +| ``'voc'`` | VOC Files | ++------------+------------------------------------+ +| ``'wav'`` | WAV Files | ++------------+------------------------------------+ +| ``'8svx'`` | 8SVX Files | ++------------+------------------------------------+ +| ``'sb'`` | SB Files | ++------------+------------------------------------+ +| ``'ub'`` | UB Files | ++------------+------------------------------------+ +| ``'ul'`` | uLAW Audio Files | ++------------+------------------------------------+ + +.. data:: tests + + A list of functions performing the individual tests. Each function takes two + arguments: the byte-stream and an open file-like object. When :func:`what` is + called with a byte-stream, the file-like object will be ``None``. + + The test function should return a string describing the image type if the test + succeeded, or ``None`` if it failed. + +Example:: + + >>> import sndhdr + >>> imghdr.what('bass.wav') + 'wav' + >>> imghdr.whathdr('bass.wav') + 'wav' + diff --git a/Lib/sndhdr.py b/Lib/sndhdr.py index 96595c69744682..38ee7a0c7ee7bb 100644 --- a/Lib/sndhdr.py +++ b/Lib/sndhdr.py @@ -73,6 +73,7 @@ def whathdr(filename): tests = [] def test_aifc(h, f): + """AIFC and AIFF files""" import aifc if not h.startswith(b'FORM'): return None @@ -94,6 +95,7 @@ def test_aifc(h, f): def test_au(h, f): + """AU and SND files""" if h.startswith(b'.snd'): func = get_long_be elif h[:4] in (b'\0ds.', b'dns.'): @@ -127,6 +129,7 @@ def test_au(h, f): def test_hcom(h, f): + """HCOM file""" if h[65:69] != b'FSSD' or h[128:132] != b'HCOM': return None divisor = get_long_be(h[144:148]) @@ -140,6 +143,7 @@ def test_hcom(h, f): def test_voc(h, f): + """VOC file""" if not h.startswith(b'Creative Voice File\032'): return None sbseek = get_short_le(h[20:22]) @@ -154,6 +158,7 @@ def test_voc(h, f): def test_wav(h, f): + """WAV file""" import wave # 'RIFF' 'WAVE' 'fmt ' if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ': @@ -170,6 +175,7 @@ def test_wav(h, f): def test_8svx(h, f): + """8SVX file""" if not h.startswith(b'FORM') or h[8:12] != b'8SVX': return None # Should decode it to get #channels -- assume always 1 @@ -179,6 +185,7 @@ def test_8svx(h, f): def test_sndt(h, f): + """SNDT file""" if h.startswith(b'SOUND'): nsamples = get_long_le(h[8:12]) rate = get_short_le(h[20:22]) @@ -188,6 +195,7 @@ def test_sndt(h, f): def test_sndr(h, f): + """SNDR file""" if h.startswith(b'\0\0'): rate = get_short_le(h[2:4]) if 4000 <= rate <= 25000: From 7514e4fdcf42bda75b4f94be0b851a4bad856d0e Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye Date: Sat, 15 Oct 2022 08:29:05 -0400 Subject: [PATCH 2/2] modify file names --- Doc/library/sndhdr.rst | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Doc/library/sndhdr.rst b/Doc/library/sndhdr.rst index 6abf4543f295df..3fc61d4ef18080 100644 --- a/Doc/library/sndhdr.rst +++ b/Doc/library/sndhdr.rst @@ -63,21 +63,20 @@ from :func:`whathdr`: and :func:`what`: +------------+------------------------------------+ | ``'aiff'`` | Audio Interchange Files | +------------+------------------------------------+ -| ``'au'`` | AU Files | +| ``'au'`` | Au Files | +------------+------------------------------------+ | ``'hcom'`` | HCOM Files | +------------+------------------------------------+ -| ``'sndr'`` | SNDR Files | +------------+------------------------------------+ -| ``'sndt'`` | SNDT Files | +| ``'sndt'`` | Sndtool Sound Files | +------------+------------------------------------+ -| ``'voc'`` | VOC Files | +| ``'voc'`` | Creative Labs Audio Files | +------------+------------------------------------+ -| ``'wav'`` | WAV Files | +| ``'wav'`` | Waveform Audio File Format Files | +------------+------------------------------------+ -| ``'8svx'`` | 8SVX Files | +| ``'8svx'`` | 8-Bit Sampled Voice Files | +------------+------------------------------------+ -| ``'sb'`` | SB Files | +| ``'sb'`` | Signed Byte Audio Data Files | +------------+------------------------------------+ | ``'ub'`` | UB Files | +------------+------------------------------------+ @@ -93,7 +92,9 @@ from :func:`whathdr`: and :func:`what`: The test function should return a string describing the image type if the test succeeded, or ``None`` if it failed. -Example:: +Example: + +.. code-block:: pycon >>> import sndhdr >>> imghdr.what('bass.wav')