blob: f8b5d8b2460ccbc42cd86e54752e436db28780a6 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`sndhdr` --- Determine type of sound file
2==============================================
3
4.. module:: sndhdr
5 :synopsis: Determine type of a sound file.
6.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
Christian Heimes5b5e81c2007-12-31 16:14:33 +00007.. Based on comments in the module source file.
Georg Brandl116aa622007-08-15 14:28:22 +00008
9.. index::
10 single: A-LAW
11 single: u-LAW
12
Raymond Hettinger469271d2011-01-27 20:38:46 +000013**Source code:** :source:`Lib/sndhdr.py`
14
15--------------
16
Georg Brandl116aa622007-08-15 14:28:22 +000017The :mod:`sndhdr` provides utility functions which attempt to determine the type
18of sound data which is in a file. When these functions are able to determine
R David Murray4487dd02014-10-09 16:59:30 -040019what type of sound data is stored in a file, they return a
20:func:`~collections.namedtuple`, containing five attributes: (``filetype``,
21``framerate``, ``nchannels``, ``nframes``, ``sampwidth``). The value for *type*
Georg Brandl116aa622007-08-15 14:28:22 +000022indicates the data type and will be one of the strings ``'aifc'``, ``'aiff'``,
23``'au'``, ``'hcom'``, ``'sndr'``, ``'sndt'``, ``'voc'``, ``'wav'``, ``'8svx'``,
24``'sb'``, ``'ub'``, or ``'ul'``. The *sampling_rate* will be either the actual
25value or ``0`` if unknown or difficult to decode. Similarly, *channels* will be
26either the number of channels or ``0`` if it cannot be determined or if the
27value is difficult to decode. The value for *frames* will be either the number
28of frames or ``-1``. The last item in the tuple, *bits_per_sample*, will either
29be the sample size in bits or ``'A'`` for A-LAW or ``'U'`` for u-LAW.
30
31
32.. function:: what(filename)
33
34 Determines the type of sound data stored in the file *filename* using
R David Murray4487dd02014-10-09 16:59:30 -040035 :func:`whathdr`. If it succeeds, returns a namedtuple as described above, otherwise
Georg Brandl116aa622007-08-15 14:28:22 +000036 ``None`` is returned.
37
R David Murray4487dd02014-10-09 16:59:30 -040038 .. versionchanged:: 3.5
39 Result changed from a tuple to a namedtuple.
40
Georg Brandl116aa622007-08-15 14:28:22 +000041
42.. function:: whathdr(filename)
43
44 Determines the type of sound data stored in a file based on the file header.
R David Murray4487dd02014-10-09 16:59:30 -040045 The name of the file is given by *filename*. This function returns a namedtuple as
Georg Brandl116aa622007-08-15 14:28:22 +000046 described above on success, or ``None``.
47
R David Murray4487dd02014-10-09 16:59:30 -040048 .. versionchanged:: 3.5
49 Result changed from a tuple to a namedtuple.
50