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