]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """Routines to help recognizing sound files.\r |
2 | \r | |
3 | Function whathdr() recognizes various types of sound file headers.\r | |
4 | It understands almost all headers that SOX can decode.\r | |
5 | \r | |
6 | The return tuple contains the following items, in this order:\r | |
7 | - file type (as SOX understands it)\r | |
8 | - sampling rate (0 if unknown or hard to decode)\r | |
9 | - number of channels (0 if unknown or hard to decode)\r | |
10 | - number of frames in the file (-1 if unknown or hard to decode)\r | |
11 | - number of bits/sample, or 'U' for U-LAW, or 'A' for A-LAW\r | |
12 | \r | |
13 | If the file doesn't have a recognizable type, it returns None.\r | |
14 | If the file can't be opened, IOError is raised.\r | |
15 | \r | |
16 | To compute the total time, divide the number of frames by the\r | |
17 | sampling rate (a frame contains a sample for each channel).\r | |
18 | \r | |
19 | Function what() calls whathdr(). (It used to also use some\r | |
20 | heuristics for raw data, but this doesn't work very well.)\r | |
21 | \r | |
22 | Finally, the function test() is a simple main program that calls\r | |
23 | what() for all files mentioned on the argument list. For directory\r | |
24 | arguments it calls what() for all files in that directory. Default\r | |
25 | argument is "." (testing all files in the current directory). The\r | |
26 | option -r tells it to recurse down directories found inside\r | |
27 | explicitly given directories.\r | |
28 | """\r | |
29 | \r | |
30 | # The file structure is top-down except that the test program and its\r | |
31 | # subroutine come last.\r | |
32 | \r | |
33 | __all__ = ["what","whathdr"]\r | |
34 | \r | |
35 | def what(filename):\r | |
36 | """Guess the type of a sound file"""\r | |
37 | res = whathdr(filename)\r | |
38 | return res\r | |
39 | \r | |
40 | \r | |
41 | def whathdr(filename):\r | |
42 | """Recognize sound headers"""\r | |
43 | f = open(filename, 'rb')\r | |
44 | h = f.read(512)\r | |
45 | for tf in tests:\r | |
46 | res = tf(h, f)\r | |
47 | if res:\r | |
48 | return res\r | |
49 | return None\r | |
50 | \r | |
51 | \r | |
52 | #-----------------------------------#\r | |
53 | # Subroutines per sound header type #\r | |
54 | #-----------------------------------#\r | |
55 | \r | |
56 | tests = []\r | |
57 | \r | |
58 | def test_aifc(h, f):\r | |
59 | import aifc\r | |
60 | if h[:4] != 'FORM':\r | |
61 | return None\r | |
62 | if h[8:12] == 'AIFC':\r | |
63 | fmt = 'aifc'\r | |
64 | elif h[8:12] == 'AIFF':\r | |
65 | fmt = 'aiff'\r | |
66 | else:\r | |
67 | return None\r | |
68 | f.seek(0)\r | |
69 | try:\r | |
70 | a = aifc.openfp(f, 'r')\r | |
71 | except (EOFError, aifc.Error):\r | |
72 | return None\r | |
73 | return (fmt, a.getframerate(), a.getnchannels(), \\r | |
74 | a.getnframes(), 8*a.getsampwidth())\r | |
75 | \r | |
76 | tests.append(test_aifc)\r | |
77 | \r | |
78 | \r | |
79 | def test_au(h, f):\r | |
80 | if h[:4] == '.snd':\r | |
81 | f = get_long_be\r | |
82 | elif h[:4] in ('\0ds.', 'dns.'):\r | |
83 | f = get_long_le\r | |
84 | else:\r | |
85 | return None\r | |
86 | type = 'au'\r | |
87 | hdr_size = f(h[4:8])\r | |
88 | data_size = f(h[8:12])\r | |
89 | encoding = f(h[12:16])\r | |
90 | rate = f(h[16:20])\r | |
91 | nchannels = f(h[20:24])\r | |
92 | sample_size = 1 # default\r | |
93 | if encoding == 1:\r | |
94 | sample_bits = 'U'\r | |
95 | elif encoding == 2:\r | |
96 | sample_bits = 8\r | |
97 | elif encoding == 3:\r | |
98 | sample_bits = 16\r | |
99 | sample_size = 2\r | |
100 | else:\r | |
101 | sample_bits = '?'\r | |
102 | frame_size = sample_size * nchannels\r | |
103 | return type, rate, nchannels, data_size//frame_size, sample_bits\r | |
104 | \r | |
105 | tests.append(test_au)\r | |
106 | \r | |
107 | \r | |
108 | def test_hcom(h, f):\r | |
109 | if h[65:69] != 'FSSD' or h[128:132] != 'HCOM':\r | |
110 | return None\r | |
111 | divisor = get_long_be(h[128+16:128+20])\r | |
112 | return 'hcom', 22050//divisor, 1, -1, 8\r | |
113 | \r | |
114 | tests.append(test_hcom)\r | |
115 | \r | |
116 | \r | |
117 | def test_voc(h, f):\r | |
118 | if h[:20] != 'Creative Voice File\032':\r | |
119 | return None\r | |
120 | sbseek = get_short_le(h[20:22])\r | |
121 | rate = 0\r | |
122 | if 0 <= sbseek < 500 and h[sbseek] == '\1':\r | |
123 | ratecode = ord(h[sbseek+4])\r | |
124 | rate = int(1000000.0 / (256 - ratecode))\r | |
125 | return 'voc', rate, 1, -1, 8\r | |
126 | \r | |
127 | tests.append(test_voc)\r | |
128 | \r | |
129 | \r | |
130 | def test_wav(h, f):\r | |
131 | # 'RIFF' <len> 'WAVE' 'fmt ' <len>\r | |
132 | if h[:4] != 'RIFF' or h[8:12] != 'WAVE' or h[12:16] != 'fmt ':\r | |
133 | return None\r | |
134 | style = get_short_le(h[20:22])\r | |
135 | nchannels = get_short_le(h[22:24])\r | |
136 | rate = get_long_le(h[24:28])\r | |
137 | sample_bits = get_short_le(h[34:36])\r | |
138 | return 'wav', rate, nchannels, -1, sample_bits\r | |
139 | \r | |
140 | tests.append(test_wav)\r | |
141 | \r | |
142 | \r | |
143 | def test_8svx(h, f):\r | |
144 | if h[:4] != 'FORM' or h[8:12] != '8SVX':\r | |
145 | return None\r | |
146 | # Should decode it to get #channels -- assume always 1\r | |
147 | return '8svx', 0, 1, 0, 8\r | |
148 | \r | |
149 | tests.append(test_8svx)\r | |
150 | \r | |
151 | \r | |
152 | def test_sndt(h, f):\r | |
153 | if h[:5] == 'SOUND':\r | |
154 | nsamples = get_long_le(h[8:12])\r | |
155 | rate = get_short_le(h[20:22])\r | |
156 | return 'sndt', rate, 1, nsamples, 8\r | |
157 | \r | |
158 | tests.append(test_sndt)\r | |
159 | \r | |
160 | \r | |
161 | def test_sndr(h, f):\r | |
162 | if h[:2] == '\0\0':\r | |
163 | rate = get_short_le(h[2:4])\r | |
164 | if 4000 <= rate <= 25000:\r | |
165 | return 'sndr', rate, 1, -1, 8\r | |
166 | \r | |
167 | tests.append(test_sndr)\r | |
168 | \r | |
169 | \r | |
170 | #---------------------------------------------#\r | |
171 | # Subroutines to extract numbers from strings #\r | |
172 | #---------------------------------------------#\r | |
173 | \r | |
174 | def get_long_be(s):\r | |
175 | return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])\r | |
176 | \r | |
177 | def get_long_le(s):\r | |
178 | return (ord(s[3])<<24) | (ord(s[2])<<16) | (ord(s[1])<<8) | ord(s[0])\r | |
179 | \r | |
180 | def get_short_be(s):\r | |
181 | return (ord(s[0])<<8) | ord(s[1])\r | |
182 | \r | |
183 | def get_short_le(s):\r | |
184 | return (ord(s[1])<<8) | ord(s[0])\r | |
185 | \r | |
186 | \r | |
187 | #--------------------#\r | |
188 | # Small test program #\r | |
189 | #--------------------#\r | |
190 | \r | |
191 | def test():\r | |
192 | import sys\r | |
193 | recursive = 0\r | |
194 | if sys.argv[1:] and sys.argv[1] == '-r':\r | |
195 | del sys.argv[1:2]\r | |
196 | recursive = 1\r | |
197 | try:\r | |
198 | if sys.argv[1:]:\r | |
199 | testall(sys.argv[1:], recursive, 1)\r | |
200 | else:\r | |
201 | testall(['.'], recursive, 1)\r | |
202 | except KeyboardInterrupt:\r | |
203 | sys.stderr.write('\n[Interrupted]\n')\r | |
204 | sys.exit(1)\r | |
205 | \r | |
206 | def testall(list, recursive, toplevel):\r | |
207 | import sys\r | |
208 | import os\r | |
209 | for filename in list:\r | |
210 | if os.path.isdir(filename):\r | |
211 | print filename + '/:',\r | |
212 | if recursive or toplevel:\r | |
213 | print 'recursing down:'\r | |
214 | import glob\r | |
215 | names = glob.glob(os.path.join(filename, '*'))\r | |
216 | testall(names, recursive, 0)\r | |
217 | else:\r | |
218 | print '*** directory (use -r) ***'\r | |
219 | else:\r | |
220 | print filename + ':',\r | |
221 | sys.stdout.flush()\r | |
222 | try:\r | |
223 | print what(filename)\r | |
224 | except IOError:\r | |
225 | print '*** not found ***'\r | |
226 | \r | |
227 | if __name__ == '__main__':\r | |
228 | test()\r |