Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 1 | # Module 'sunaudio' -- interpret sun audio headers |
| 2 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 3 | MAGIC = '.snd' |
| 4 | |
| 5 | error = 'sunaudio sound header conversion error' |
| 6 | |
| 7 | |
| 8 | # convert a 4-char value to integer |
| 9 | |
Guido van Rossum | d482e8a | 1992-06-03 16:47:49 +0000 | [diff] [blame] | 10 | def get_long_be(s): |
| 11 | 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] | 12 | |
| 13 | |
| 14 | # read a sound header from an open file |
| 15 | |
| 16 | def gethdr(fp): |
| 17 | if fp.read(4) <> MAGIC: |
| 18 | raise error, 'gethdr: bad magic word' |
Guido van Rossum | d482e8a | 1992-06-03 16:47:49 +0000 | [diff] [blame] | 19 | hdr_size = get_long_be(fp.read(4)) |
| 20 | data_size = get_long_be(fp.read(4)) |
| 21 | encoding = get_long_be(fp.read(4)) |
| 22 | sample_rate = get_long_be(fp.read(4)) |
| 23 | channels = get_long_be(fp.read(4)) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 24 | excess = hdr_size - 24 |
| 25 | if excess < 0: |
| 26 | raise error, 'gethdr: bad hdr_size' |
| 27 | if excess > 0: |
| 28 | info = fp.read(excess) |
| 29 | else: |
| 30 | info = '' |
| 31 | return (data_size, encoding, sample_rate, channels, info) |
| 32 | |
| 33 | |
| 34 | # read and print the sound header of a named file |
| 35 | |
| 36 | def printhdr(file): |
| 37 | hdr = gethdr(open(file, 'r')) |
| 38 | data_size, encoding, sample_rate, channels, info = hdr |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 39 | while info[-1:] == '\0': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 40 | info = info[:-1] |
| 41 | print 'File name: ', file |
| 42 | print 'Data size: ', data_size |
| 43 | print 'Encoding: ', encoding |
| 44 | print 'Sample rate:', sample_rate |
| 45 | print 'Channels: ', channels |
| 46 | print 'Info: ', `info` |