Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files 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 (   s)

Definition at line 174 of file sndhdr.py.

175 def get_long_be(s):
176  return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
def sndhdr.get_long_le (   s)

Definition at line 177 of file sndhdr.py.

178 def get_long_le(s):
179  return (ord(s[3])<<24) | (ord(s[2])<<16) | (ord(s[1])<<8) | ord(s[0])
def sndhdr.get_short_be (   s)

Definition at line 180 of file sndhdr.py.

181 def get_short_be(s):
182  return (ord(s[0])<<8) | ord(s[1])
def sndhdr.get_short_le (   s)

Definition at line 183 of file sndhdr.py.

184 def get_short_le(s):
185  return (ord(s[1])<<8) | ord(s[0])
186 
187 
188 #--------------------#
189 # Small test program #
190 #--------------------#
def sndhdr.test ( )

Definition at line 191 of file sndhdr.py.

References testall().

192 def test():
193  import sys
194  recursive = 0
195  if sys.argv[1:] and sys.argv[1] == '-r':
196  del sys.argv[1:2]
197  recursive = 1
198  try:
199  if sys.argv[1:]:
200  testall(sys.argv[1:], recursive, 1)
201  else:
202  testall(['.'], recursive, 1)
203  except KeyboardInterrupt:
204  sys.stderr.write('\n[Interrupted]\n')
205  sys.exit(1)
def sndhdr.test_8svx (   h,
  f 
)

Definition at line 143 of file sndhdr.py.

144 def test_8svx(h, f):
145  if h[:4] != 'FORM' or h[8:12] != '8SVX':
146  return None
147  # Should decode it to get #channels -- assume always 1
148  return '8svx', 0, 1, 0, 8
149 
150 tests.append(test_8svx)
151 
def sndhdr.test_aifc (   h,
  f 
)

Definition at line 58 of file sndhdr.py.

58 
59 def test_aifc(h, f):
60  import aifc
61  if h[:4] != 'FORM':
62  return None
63  if h[8:12] == 'AIFC':
64  fmt = 'aifc'
65  elif h[8:12] == 'AIFF':
66  fmt = 'aiff'
67  else:
68  return None
69  f.seek(0)
70  try:
71  a = aifc.openfp(f, 'r')
72  except (EOFError, aifc.Error):
73  return None
74  return (fmt, a.getframerate(), a.getnchannels(), \
75  a.getnframes(), 8*a.getsampwidth())
76 
77 tests.append(test_aifc)
78 
def sndhdr.test_au (   h,
  f 
)

Definition at line 79 of file sndhdr.py.

References aifc.f.

79 
80 def test_au(h, f):
81  if h[:4] == '.snd':
82  f = get_long_be
83  elif h[:4] in ('\0ds.', 'dns.'):
84  f = get_long_le
85  else:
86  return None
87  type = 'au'
88  hdr_size = f(h[4:8])
89  data_size = f(h[8:12])
90  encoding = f(h[12:16])
91  rate = f(h[16:20])
92  nchannels = f(h[20:24])
93  sample_size = 1 # default
94  if encoding == 1:
95  sample_bits = 'U'
96  elif encoding == 2:
97  sample_bits = 8
98  elif encoding == 3:
99  sample_bits = 16
100  sample_size = 2
101  else:
102  sample_bits = '?'
103  frame_size = sample_size * nchannels
104  return type, rate, nchannels, data_size/frame_size, sample_bits
105 
106 tests.append(test_au)
107 
def sndhdr.test_hcom (   h,
  f 
)

Definition at line 108 of file sndhdr.py.

References get_long_be().

109 def test_hcom(h, f):
110  if h[65:69] != 'FSSD' or h[128:132] != 'HCOM':
111  return None
112  divisor = get_long_be(h[128+16:128+20])
113  return 'hcom', 22050/divisor, 1, -1, 8
114 
115 tests.append(test_hcom)
116 
def sndhdr.test_sndr (   h,
  f 
)

Definition at line 161 of file sndhdr.py.

References get_short_le().

162 def test_sndr(h, f):
163  if h[:2] == '\0\0':
164  rate = get_short_le(h[2:4])
165  if 4000 <= rate <= 25000:
166  return 'sndr', rate, 1, -1, 8
167 
168 tests.append(test_sndr)
169 
170 
171 #---------------------------------------------#
172 # Subroutines to extract numbers from strings #
173 #---------------------------------------------#
def sndhdr.test_sndt (   h,
  f 
)

Definition at line 152 of file sndhdr.py.

References get_long_le(), and get_short_le().

153 def test_sndt(h, f):
154  if h[:5] == 'SOUND':
155  nsamples = get_long_le(h[8:12])
156  rate = get_short_le(h[20:22])
157  return 'sndt', rate, 1, nsamples, 8
158 
159 tests.append(test_sndt)
160 
def sndhdr.test_voc (   h,
  f 
)

Definition at line 117 of file sndhdr.py.

References get_short_le().

118 def test_voc(h, f):
119  if h[:20] != 'Creative Voice File\032':
120  return None
121  sbseek = get_short_le(h[20:22])
122  rate = 0
123  if 0 <= sbseek < 500 and h[sbseek] == '\1':
124  ratecode = ord(h[sbseek+4])
125  rate = int(1000000.0 / (256 - ratecode))
126  return 'voc', rate, 1, -1, 8
127 
128 tests.append(test_voc)
129 
def sndhdr.test_wav (   h,
  f 
)

Definition at line 130 of file sndhdr.py.

References get_long_le(), and get_short_le().

131 def test_wav(h, f):
132  # 'RIFF' <len> 'WAVE' 'fmt ' <len>
133  if h[:4] != 'RIFF' or h[8:12] != 'WAVE' or h[12:16] != 'fmt ':
134  return None
135  style = get_short_le(h[20:22])
136  nchannels = get_short_le(h[22:24])
137  rate = get_long_le(h[24:28])
138  sample_bits = get_short_le(h[34:36])
139  return 'wav', rate, nchannels, -1, sample_bits
140 
141 tests.append(test_wav)
142 
def sndhdr.testall (   list,
  recursive,
  toplevel 
)

Definition at line 206 of file sndhdr.py.

References glob.glob(), test(), and what().

207 def testall(list, recursive, toplevel):
208  import sys
209  import os
210  for filename in list:
211  if os.path.isdir(filename):
212  print filename + '/:',
213  if recursive or toplevel:
214  print 'recursing down:'
215  import glob
216  names = glob.glob(os.path.join(filename, '*'))
217  testall(names, recursive, 0)
218  else:
219  print '*** directory (use -r) ***'
220  else:
221  print filename + ':',
222  sys.stdout.flush()
223  try:
224  print what(filename)
225  except IOError:
226  print '*** not found ***'
def sndhdr.what (   filename)
Guess the type of a sound file

Definition at line 35 of file sndhdr.py.

35 
36 def what(filename):
37  """Guess the type of a sound file"""
38  res = whathdr(filename)
39  return res
40 
def sndhdr.whathdr (   filename)
Recognize sound headers

Definition at line 41 of file sndhdr.py.

41 
42 def whathdr(filename):
43  """Recognize sound headers"""
44  f = open(filename, 'rb')
45  h = f.read(512)
46  for tf in tests:
47  res = tf(h, f)
48  if res:
49  return res
50  return None
51 
52 
53 #-----------------------------------#
54 # Subroutines per sound header type #
55 #-----------------------------------#

Variable Documentation

list __all__ = ["what","whathdr"]

Definition at line 33 of file sndhdr.py.

list tests = []

Definition at line 56 of file sndhdr.py.