Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Interpret sun audio headers.""" |
Brett Cannon | 1e8fba7 | 2008-07-18 19:30:22 +0000 | [diff] [blame] | 2 | from warnings import warnpy3k |
| 3 | warnpy3k("the sunaudio module has been removed in Python 3.0; " |
| 4 | "use the sunau module instead", stacklevel=2) |
| 5 | del warnpy3k |
| 6 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 8 | MAGIC = '.snd' |
| 9 | |
Fred Drake | 9b8d801 | 2000-08-17 04:45:13 +0000 | [diff] [blame] | 10 | class error(Exception): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 11 | pass |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 12 | |
| 13 | |
Guido van Rossum | d482e8a | 1992-06-03 16:47:49 +0000 | [diff] [blame] | 14 | def get_long_be(s): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 15 | """Convert a 4-char value to integer.""" |
| 16 | return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3]) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 17 | |
| 18 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 19 | def gethdr(fp): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 20 | """Read a sound header from an open file.""" |
| 21 | if fp.read(4) != MAGIC: |
| 22 | raise error, 'gethdr: bad magic word' |
| 23 | hdr_size = get_long_be(fp.read(4)) |
| 24 | data_size = get_long_be(fp.read(4)) |
| 25 | encoding = get_long_be(fp.read(4)) |
| 26 | sample_rate = get_long_be(fp.read(4)) |
| 27 | channels = get_long_be(fp.read(4)) |
| 28 | excess = hdr_size - 24 |
| 29 | if excess < 0: |
| 30 | raise error, 'gethdr: bad hdr_size' |
| 31 | if excess > 0: |
| 32 | info = fp.read(excess) |
| 33 | else: |
| 34 | info = '' |
| 35 | return (data_size, encoding, sample_rate, channels, info) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 36 | |
| 37 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 38 | def printhdr(file): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 39 | """Read and print the sound header of a named file.""" |
| 40 | hdr = gethdr(open(file, 'r')) |
| 41 | data_size, encoding, sample_rate, channels, info = hdr |
| 42 | while info[-1:] == '\0': |
| 43 | info = info[:-1] |
| 44 | print 'File name: ', file |
| 45 | print 'Data size: ', data_size |
| 46 | print 'Encoding: ', encoding |
| 47 | print 'Sample rate:', sample_rate |
| 48 | print 'Channels: ', channels |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 49 | print 'Info: ', repr(info) |