Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Interpret sun audio headers.""" |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 23cfc98 | 2007-08-21 22:49:52 +0000 | [diff] [blame] | 3 | MAGIC = b'.snd' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 4 | |
Fred Drake | 9b8d801 | 2000-08-17 04:45:13 +0000 | [diff] [blame] | 5 | class error(Exception): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 6 | pass |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 7 | |
| 8 | |
Guido van Rossum | d482e8a | 1992-06-03 16:47:49 +0000 | [diff] [blame] | 9 | def get_long_be(s): |
Guido van Rossum | 23cfc98 | 2007-08-21 22:49:52 +0000 | [diff] [blame] | 10 | """Convert a 4-byte value to integer.""" |
| 11 | return (s[0]<<24) | (s[1]<<16) | (s[2]<<8) | s[3] |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 12 | |
| 13 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 14 | def gethdr(fp): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 15 | """Read a sound header from an open file.""" |
| 16 | if fp.read(4) != MAGIC: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 17 | raise error('gethdr: bad magic word') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 18 | hdr_size = get_long_be(fp.read(4)) |
| 19 | data_size = get_long_be(fp.read(4)) |
| 20 | encoding = get_long_be(fp.read(4)) |
| 21 | sample_rate = get_long_be(fp.read(4)) |
| 22 | channels = get_long_be(fp.read(4)) |
| 23 | excess = hdr_size - 24 |
| 24 | if excess < 0: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 25 | raise error('gethdr: bad hdr_size') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 26 | if excess > 0: |
| 27 | info = fp.read(excess) |
| 28 | else: |
Guido van Rossum | 23cfc98 | 2007-08-21 22:49:52 +0000 | [diff] [blame] | 29 | info = b'' |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 30 | return (data_size, encoding, sample_rate, channels, info) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 31 | |
| 32 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 33 | def printhdr(file): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 34 | """Read and print the sound header of a named file.""" |
Guido van Rossum | 23cfc98 | 2007-08-21 22:49:52 +0000 | [diff] [blame] | 35 | f = open(file, 'rb') |
| 36 | try: |
| 37 | hdr = gethdr(f) |
| 38 | finally: |
| 39 | f.close() |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 40 | data_size, encoding, sample_rate, channels, info = hdr |
Guido van Rossum | 23cfc98 | 2007-08-21 22:49:52 +0000 | [diff] [blame] | 41 | while info.endswith(b'\0'): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 42 | info = info[:-1] |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 43 | print('File name: ', file) |
| 44 | print('Data size: ', data_size) |
| 45 | print('Encoding: ', encoding) |
| 46 | print('Sample rate:', sample_rate) |
| 47 | print('Channels: ', channels) |
| 48 | print('Info: ', repr(info)) |