Back to index

python3.2  3.2.2
Functions | Variables
sndhdr Namespace Reference

Functions

def what
def whathdr
def test_aifc
def test_au
def test_hcom
def test_voc
def test_wav
def test_8svx
def test_sndt
def test_sndr
def get_long_be
def get_long_le
def get_short_be
def get_short_le
def test
def testall

Variables

list __all__ = ['what', 'whathdr']
list tests = []

Detailed Description

Routines to help recognizing sound files.

Function whathdr() recognizes various types of sound file headers.
It understands almost all headers that SOX can decode.

The return tuple contains the following items, in this order:
- file type (as SOX understands it)
- sampling rate (0 if unknown or hard to decode)
- number of channels (0 if unknown or hard to decode)
- number of frames in the file (-1 if unknown or hard to decode)
- number of bits/sample, or 'U' for U-LAW, or 'A' for A-LAW

If the file doesn't have a recognizable type, it returns None.
If the file can't be opened, IOError is raised.

To compute the total time, divide the number of frames by the
sampling rate (a frame contains a sample for each channel).

Function what() calls whathdr().  (It used to also use some
heuristics for raw data, but this doesn't work very well.)

Finally, the function test() is a simple main program that calls
what() for all files mentioned on the argument list.  For directory
arguments it calls what() for all files in that directory.  Default
argument is "." (testing all files in the current directory).  The
option -r tells it to recurse down directories found inside
explicitly given directories.

Function Documentation

def sndhdr.get_long_be (   b)

Definition at line 183 of file sndhdr.py.

00183 
00184 def get_long_be(b):
00185     return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]

Here is the caller graph for this function:

def sndhdr.get_long_le (   b)

Definition at line 186 of file sndhdr.py.

00186 
00187 def get_long_le(b):
00188     return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]

Here is the caller graph for this function:

def sndhdr.get_short_be (   b)

Definition at line 189 of file sndhdr.py.

00189 
00190 def get_short_be(b):
00191     return (b[0] << 8) | b[1]

def sndhdr.get_short_le (   b)

Definition at line 192 of file sndhdr.py.

00192 
00193 def get_short_le(b):
00194     return (b[1] << 8) | b[0]
00195 
00196 
00197 #--------------------#
00198 # Small test program #
00199 #--------------------#

Here is the caller graph for this function:

def sndhdr.test ( )

Definition at line 200 of file sndhdr.py.

00200 
00201 def test():
00202     import sys
00203     recursive = 0
00204     if sys.argv[1:] and sys.argv[1] == '-r':
00205         del sys.argv[1:2]
00206         recursive = 1
00207     try:
00208         if sys.argv[1:]:
00209             testall(sys.argv[1:], recursive, 1)
00210         else:
00211             testall(['.'], recursive, 1)
00212     except KeyboardInterrupt:
00213         sys.stderr.write('\n[Interrupted]\n')
00214         sys.exit(1)

Here is the call graph for this function:

def sndhdr.test_8svx (   h,
  f 
)

Definition at line 152 of file sndhdr.py.

00152 
00153 def test_8svx(h, f):
00154     if not h.startswith(b'FORM') or h[8:12] != b'8SVX':
00155         return None
00156     # Should decode it to get #channels -- assume always 1
00157     return '8svx', 0, 1, 0, 8
00158 
00159 tests.append(test_8svx)
00160 

def sndhdr.test_aifc (   h,
  f 
)

Definition at line 58 of file sndhdr.py.

00058 
00059 def test_aifc(h, f):
00060     import aifc
00061     if not h.startswith(b'FORM'):
00062         return None
00063     if h[8:12] == b'AIFC':
00064         fmt = 'aifc'
00065     elif h[8:12] == b'AIFF':
00066         fmt = 'aiff'
00067     else:
00068         return None
00069     f.seek(0)
00070     try:
00071         a = aifc.open(f, 'r')
00072     except (EOFError, aifc.Error):
00073         return None
00074     return (fmt, a.getframerate(), a.getnchannels(),
00075             a.getnframes(), 8 * a.getsampwidth())
00076 
00077 tests.append(test_aifc)
00078 

Here is the call graph for this function:

def sndhdr.test_au (   h,
  f 
)

Definition at line 79 of file sndhdr.py.

00079 
00080 def test_au(h, f):
00081     if h.startswith(b'.snd'):
00082         func = get_long_be
00083     elif h[:4] in (b'\0ds.', b'dns.'):
00084         func = get_long_le
00085     else:
00086         return None
00087     filetype = 'au'
00088     hdr_size = func(h[4:8])
00089     data_size = func(h[8:12])
00090     encoding = func(h[12:16])
00091     rate = func(h[16:20])
00092     nchannels = func(h[20:24])
00093     sample_size = 1 # default
00094     if encoding == 1:
00095         sample_bits = 'U'
00096     elif encoding == 2:
00097         sample_bits = 8
00098     elif encoding == 3:
00099         sample_bits = 16
00100         sample_size = 2
00101     else:
00102         sample_bits = '?'
00103     frame_size = sample_size * nchannels
00104     if frame_size:
00105         nframe = data_size / frame_size
00106     else:
00107         nframe = -1
00108     return filetype, rate, nchannels, nframe, sample_bits
00109 
00110 tests.append(test_au)
00111 

def sndhdr.test_hcom (   h,
  f 
)

Definition at line 112 of file sndhdr.py.

00112 
00113 def test_hcom(h, f):
00114     if h[65:69] != b'FSSD' or h[128:132] != b'HCOM':
00115         return None
00116     divisor = get_long_be(h[144:148])
00117     if divisor:
00118         rate = 22050 / divisor
00119     else:
00120         rate = 0
00121     return 'hcom', rate, 1, -1, 8
00122 
00123 tests.append(test_hcom)
00124 

Here is the call graph for this function:

def sndhdr.test_sndr (   h,
  f 
)

Definition at line 170 of file sndhdr.py.

00170 
00171 def test_sndr(h, f):
00172     if h.startswith(b'\0\0'):
00173         rate = get_short_le(h[2:4])
00174         if 4000 <= rate <= 25000:
00175             return 'sndr', rate, 1, -1, 8
00176 
00177 tests.append(test_sndr)
00178 
00179 
00180 #-------------------------------------------#
00181 # Subroutines to extract numbers from bytes #
00182 #-------------------------------------------#

Here is the call graph for this function:

def sndhdr.test_sndt (   h,
  f 
)

Definition at line 161 of file sndhdr.py.

00161 
00162 def test_sndt(h, f):
00163     if h.startswith(b'SOUND'):
00164         nsamples = get_long_le(h[8:12])
00165         rate = get_short_le(h[20:22])
00166         return 'sndt', rate, 1, nsamples, 8
00167 
00168 tests.append(test_sndt)
00169 

Here is the call graph for this function:

def sndhdr.test_voc (   h,
  f 
)

Definition at line 125 of file sndhdr.py.

00125 
00126 def test_voc(h, f):
00127     if not h.startswith(b'Creative Voice File\032'):
00128         return None
00129     sbseek = get_short_le(h[20:22])
00130     rate = 0
00131     if 0 <= sbseek < 500 and h[sbseek] == 1:
00132         ratecode = 256 - h[sbseek+4]
00133         if ratecode:
00134             rate = int(1000000.0 / ratecode)
00135     return 'voc', rate, 1, -1, 8
00136 
00137 tests.append(test_voc)
00138 

Here is the call graph for this function:

def sndhdr.test_wav (   h,
  f 
)

Definition at line 139 of file sndhdr.py.

00139 
00140 def test_wav(h, f):
00141     # 'RIFF' <len> 'WAVE' 'fmt ' <len>
00142     if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
00143         return None
00144     style = get_short_le(h[20:22])
00145     nchannels = get_short_le(h[22:24])
00146     rate = get_long_le(h[24:28])
00147     sample_bits = get_short_le(h[34:36])
00148     return 'wav', rate, nchannels, -1, sample_bits
00149 
00150 tests.append(test_wav)
00151 

Here is the call graph for this function:

def sndhdr.testall (   list,
  recursive,
  toplevel 
)

Definition at line 215 of file sndhdr.py.

00215 
00216 def testall(list, recursive, toplevel):
00217     import sys
00218     import os
00219     for filename in list:
00220         if os.path.isdir(filename):
00221             print(filename + '/:', end=' ')
00222             if recursive or toplevel:
00223                 print('recursing down:')
00224                 import glob
00225                 names = glob.glob(os.path.join(filename, '*'))
00226                 testall(names, recursive, 0)
00227             else:
00228                 print('*** directory (use -r) ***')
00229         else:
00230             print(filename + ':', end=' ')
00231             sys.stdout.flush()
00232             try:
00233                 print(what(filename))
00234             except IOError:
00235                 print('*** not found ***')

Here is the call graph for this function:

Here is the caller graph for this function:

def sndhdr.what (   filename)
Guess the type of a sound file.

Definition at line 35 of file sndhdr.py.

00035 
00036 def what(filename):
00037     """Guess the type of a sound file."""
00038     res = whathdr(filename)
00039     return res
00040 

Here is the call graph for this function:

Here is the caller graph for this function:

def sndhdr.whathdr (   filename)
Recognize sound headers.

Definition at line 41 of file sndhdr.py.

00041 
00042 def whathdr(filename):
00043     """Recognize sound headers."""
00044     with open(filename, 'rb') as f:
00045         h = f.read(512)
00046         for tf in tests:
00047             res = tf(h, f)
00048             if res:
00049                 return res
00050         return None
00051 
00052 
00053 #-----------------------------------#
00054 # Subroutines per sound header type #
00055 #-----------------------------------#

Here is the caller graph for this function:


Variable Documentation

list sndhdr.__all__ = ['what', 'whathdr']

Definition at line 33 of file sndhdr.py.

list sndhdr.tests = []

Definition at line 56 of file sndhdr.py.