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 | |
R David Murray | 4487dd0 | 2014-10-09 16:59:30 -0400 | [diff] [blame] | 35 | from collections import namedtuple |
| 36 | |
| 37 | SndHeaders = namedtuple('SndHeaders', |
| 38 | 'filetype framerate nchannels nframes sampwidth') |
| 39 | |
Raymond Hettinger | 5b798ab | 2015-08-17 22:04:45 -0700 | [diff] [blame] | 40 | SndHeaders.filetype.__doc__ = ("""The value for type indicates the data type |
| 41 | and will be one of the strings 'aifc', 'aiff', 'au','hcom', |
| 42 | 'sndr', 'sndt', 'voc', 'wav', '8svx', 'sb', 'ub', or 'ul'.""") |
| 43 | SndHeaders.framerate.__doc__ = ("""The sampling_rate will be either the actual |
| 44 | value or 0 if unknown or difficult to decode.""") |
| 45 | SndHeaders.nchannels.__doc__ = ("""The number of channels or 0 if it cannot be |
| 46 | determined or if the value is difficult to decode.""") |
| 47 | SndHeaders.nframes.__doc__ = ("""The value for frames will be either the number |
| 48 | of frames or -1.""") |
| 49 | SndHeaders.sampwidth.__doc__ = ("""Either the sample size in bits or |
| 50 | 'A' for A-LAW or 'U' for u-LAW.""") |
| 51 | |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 52 | def what(filename): |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 53 | """Guess the type of a sound file.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 54 | res = whathdr(filename) |
| 55 | return res |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 56 | |
| 57 | |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 58 | def whathdr(filename): |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 59 | """Recognize sound headers.""" |
| 60 | with open(filename, 'rb') as f: |
| 61 | h = f.read(512) |
| 62 | for tf in tests: |
| 63 | res = tf(h, f) |
| 64 | if res: |
R David Murray | 4487dd0 | 2014-10-09 16:59:30 -0400 | [diff] [blame] | 65 | return SndHeaders(*res) |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 66 | return None |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 67 | |
| 68 | |
| 69 | #-----------------------------------# |
| 70 | # Subroutines per sound header type # |
| 71 | #-----------------------------------# |
| 72 | |
| 73 | tests = [] |
| 74 | |
| 75 | def test_aifc(h, f): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 76 | import aifc |
Victor Stinner | e9ebde4 | 2010-07-13 23:04:56 +0000 | [diff] [blame] | 77 | if not h.startswith(b'FORM'): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 78 | return None |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 79 | if h[8:12] == b'AIFC': |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 80 | fmt = 'aifc' |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 81 | elif h[8:12] == b'AIFF': |
Victor Stinner | e9ebde4 | 2010-07-13 23:04:56 +0000 | [diff] [blame] | 82 | fmt = 'aiff' |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 83 | else: |
| 84 | return None |
| 85 | f.seek(0) |
| 86 | try: |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 87 | a = aifc.open(f, 'r') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 88 | except (EOFError, aifc.Error): |
| 89 | return None |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 90 | return (fmt, a.getframerate(), a.getnchannels(), |
| 91 | a.getnframes(), 8 * a.getsampwidth()) |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 92 | |
| 93 | tests.append(test_aifc) |
| 94 | |
| 95 | |
| 96 | def test_au(h, f): |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 97 | if h.startswith(b'.snd'): |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 98 | func = get_long_be |
| 99 | elif h[:4] in (b'\0ds.', b'dns.'): |
| 100 | func = get_long_le |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 101 | else: |
| 102 | return None |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 103 | filetype = 'au' |
| 104 | hdr_size = func(h[4:8]) |
| 105 | data_size = func(h[8:12]) |
| 106 | encoding = func(h[12:16]) |
| 107 | rate = func(h[16:20]) |
| 108 | nchannels = func(h[20:24]) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 109 | sample_size = 1 # default |
| 110 | if encoding == 1: |
| 111 | sample_bits = 'U' |
| 112 | elif encoding == 2: |
| 113 | sample_bits = 8 |
| 114 | elif encoding == 3: |
| 115 | sample_bits = 16 |
| 116 | sample_size = 2 |
| 117 | else: |
| 118 | sample_bits = '?' |
| 119 | frame_size = sample_size * nchannels |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 120 | if frame_size: |
| 121 | nframe = data_size / frame_size |
| 122 | else: |
| 123 | nframe = -1 |
| 124 | return filetype, rate, nchannels, nframe, sample_bits |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 125 | |
| 126 | tests.append(test_au) |
| 127 | |
| 128 | |
| 129 | def test_hcom(h, f): |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 130 | if h[65:69] != b'FSSD' or h[128:132] != b'HCOM': |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 131 | return None |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 132 | divisor = get_long_be(h[144:148]) |
| 133 | if divisor: |
| 134 | rate = 22050 / divisor |
| 135 | else: |
| 136 | rate = 0 |
| 137 | return 'hcom', rate, 1, -1, 8 |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 138 | |
| 139 | tests.append(test_hcom) |
| 140 | |
| 141 | |
| 142 | def test_voc(h, f): |
Victor Stinner | e9ebde4 | 2010-07-13 23:04:56 +0000 | [diff] [blame] | 143 | if not h.startswith(b'Creative Voice File\032'): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 144 | return None |
| 145 | sbseek = get_short_le(h[20:22]) |
| 146 | rate = 0 |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 147 | if 0 <= sbseek < 500 and h[sbseek] == 1: |
| 148 | ratecode = 256 - h[sbseek+4] |
| 149 | if ratecode: |
| 150 | rate = int(1000000.0 / ratecode) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 151 | return 'voc', rate, 1, -1, 8 |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 152 | |
| 153 | tests.append(test_voc) |
| 154 | |
| 155 | |
| 156 | def test_wav(h, f): |
R David Murray | 1a48b9d | 2013-03-18 17:42:42 -0400 | [diff] [blame] | 157 | import wave |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 158 | # 'RIFF' <len> 'WAVE' 'fmt ' <len> |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 159 | 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] | 160 | return None |
R David Murray | 1a48b9d | 2013-03-18 17:42:42 -0400 | [diff] [blame] | 161 | f.seek(0) |
| 162 | try: |
| 163 | w = wave.openfp(f, 'r') |
| 164 | except (EOFError, wave.Error): |
| 165 | return None |
| 166 | return ('wav', w.getframerate(), w.getnchannels(), |
| 167 | w.getnframes(), 8*w.getsampwidth()) |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 168 | |
| 169 | tests.append(test_wav) |
| 170 | |
| 171 | |
| 172 | def test_8svx(h, f): |
Victor Stinner | e9ebde4 | 2010-07-13 23:04:56 +0000 | [diff] [blame] | 173 | if not h.startswith(b'FORM') or h[8:12] != b'8SVX': |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 174 | return None |
| 175 | # Should decode it to get #channels -- assume always 1 |
| 176 | return '8svx', 0, 1, 0, 8 |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 177 | |
| 178 | tests.append(test_8svx) |
| 179 | |
| 180 | |
| 181 | def test_sndt(h, f): |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 182 | if h.startswith(b'SOUND'): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 183 | nsamples = get_long_le(h[8:12]) |
| 184 | rate = get_short_le(h[20:22]) |
| 185 | return 'sndt', rate, 1, nsamples, 8 |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 186 | |
| 187 | tests.append(test_sndt) |
| 188 | |
| 189 | |
| 190 | def test_sndr(h, f): |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 191 | if h.startswith(b'\0\0'): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 192 | rate = get_short_le(h[2:4]) |
| 193 | if 4000 <= rate <= 25000: |
| 194 | return 'sndr', rate, 1, -1, 8 |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 195 | |
| 196 | tests.append(test_sndr) |
| 197 | |
| 198 | |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 199 | #-------------------------------------------# |
| 200 | # Subroutines to extract numbers from bytes # |
| 201 | #-------------------------------------------# |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 202 | |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 203 | def get_long_be(b): |
| 204 | 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] | 205 | |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 206 | def get_long_le(b): |
| 207 | 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] | 208 | |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 209 | def get_short_be(b): |
| 210 | return (b[0] << 8) | b[1] |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 211 | |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 212 | def get_short_le(b): |
| 213 | return (b[1] << 8) | b[0] |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 214 | |
| 215 | |
| 216 | #--------------------# |
| 217 | # Small test program # |
| 218 | #--------------------# |
| 219 | |
| 220 | def test(): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 221 | import sys |
| 222 | recursive = 0 |
| 223 | if sys.argv[1:] and sys.argv[1] == '-r': |
| 224 | del sys.argv[1:2] |
| 225 | recursive = 1 |
| 226 | try: |
| 227 | if sys.argv[1:]: |
| 228 | testall(sys.argv[1:], recursive, 1) |
| 229 | else: |
| 230 | testall(['.'], recursive, 1) |
| 231 | except KeyboardInterrupt: |
| 232 | sys.stderr.write('\n[Interrupted]\n') |
| 233 | sys.exit(1) |
Guido van Rossum | b90bdeb | 1994-01-14 16:46:14 +0000 | [diff] [blame] | 234 | |
| 235 | def testall(list, recursive, toplevel): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 236 | import sys |
| 237 | import os |
| 238 | for filename in list: |
| 239 | if os.path.isdir(filename): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 240 | print(filename + '/:', end=' ') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 241 | if recursive or toplevel: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 242 | print('recursing down:') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 243 | import glob |
| 244 | names = glob.glob(os.path.join(filename, '*')) |
| 245 | testall(names, recursive, 0) |
| 246 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 247 | print('*** directory (use -r) ***') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 248 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 249 | print(filename + ':', end=' ') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 250 | sys.stdout.flush() |
| 251 | try: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 252 | print(what(filename)) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 253 | except OSError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 254 | print('*** not found ***') |
Guido van Rossum | f4bf044 | 1999-01-06 13:05:58 +0000 | [diff] [blame] | 255 | |
| 256 | if __name__ == '__main__': |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 257 | test() |