Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
toaiff.py
Go to the documentation of this file.
1 """Convert "arbitrary" sound files to AIFF (Apple and SGI's audio format).
2 
3 Input may be compressed.
4 Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others.
5 An exception is raised if the file is not of a recognized type.
6 Returned filename is either the input filename or a temporary filename;
7 in the latter case the caller must ensure that it is removed.
8 Other temporary files used are removed by the function.
9 """
10 
11 import os
12 import tempfile
13 import pipes
14 import sndhdr
15 
16 __all__ = ["error", "toaiff"]
17 
18 table = {}
19 
21 t.append('sox -t au - -t aiff -r 8000 -', '--')
22 table['au'] = t
23 
24 # XXX The following is actually sub-optimal.
25 # XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4.
26 # XXX We must force the output sampling rate else the SGI won't play
27 # XXX files sampled at 5.5k or 7.333k; however this means that files
28 # XXX sampled at 11k are unnecessarily expanded.
29 # XXX Similar comments apply to some other file types.
30 t = pipes.Template()
31 t.append('sox -t hcom - -t aiff -r 22050 -', '--')
32 table['hcom'] = t
33 
34 t = pipes.Template()
35 t.append('sox -t voc - -t aiff -r 11025 -', '--')
36 table['voc'] = t
37 
38 t = pipes.Template()
39 t.append('sox -t wav - -t aiff -', '--')
40 table['wav'] = t
41 
42 t = pipes.Template()
43 t.append('sox -t 8svx - -t aiff -r 16000 -', '--')
44 table['8svx'] = t
45 
46 t = pipes.Template()
47 t.append('sox -t sndt - -t aiff -r 16000 -', '--')
48 table['sndt'] = t
49 
50 t = pipes.Template()
51 t.append('sox -t sndr - -t aiff -r 16000 -', '--')
52 table['sndr'] = t
53 
54 uncompress = pipes.Template()
55 uncompress.append('uncompress', '--')
56 
57 
58 class error(Exception):
59  pass
60 
61 def toaiff(filename):
62  temps = []
63  ret = None
64  try:
65  ret = _toaiff(filename, temps)
66  finally:
67  for temp in temps[:]:
68  if temp != ret:
69  try:
70  os.unlink(temp)
71  except os.error:
72  pass
73  temps.remove(temp)
74  return ret
75 
76 def _toaiff(filename, temps):
77  if filename[-2:] == '.Z':
78  fname = tempfile.mktemp()
79  temps.append(fname)
80  sts = uncompress.copy(filename, fname)
81  if sts:
82  raise error, filename + ': uncompress failed'
83  else:
84  fname = filename
85  try:
86  ftype = sndhdr.whathdr(fname)
87  if ftype:
88  ftype = ftype[0] # All we're interested in
89  except IOError, msg:
90  if type(msg) == type(()) and len(msg) == 2 and \
91  type(msg[0]) == type(0) and type(msg[1]) == type(''):
92  msg = msg[1]
93  if type(msg) != type(''):
94  msg = `msg`
95  raise error, filename + ': ' + msg
96  if ftype == 'aiff':
97  return fname
98  if ftype is None or not table.has_key(ftype):
99  raise error, \
100  filename + ': unsupported audio file type ' + `ftype`
101  temp = tempfile.mktemp()
102  temps.append(temp)
103  sts = table[ftype].copy(fname, temp)
104  if sts:
105  raise error, filename + ': conversion to aiff failed'
106  return temp