| 1 | 1 | """Interpret sun audio headers.""" |
|---|
| 2 | 1 | from warnings import warnpy3k |
|---|
| 3 | 1 | warnpy3k("the sunaudio module has been removed in Python 3.0; " |
|---|
| 4 | 1 | "use the sunau module instead", stacklevel=2) |
|---|
| 5 | 1 | del warnpy3k |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | |
|---|
| 8 | 1 | MAGIC = '.snd' |
|---|
| 9 | n/a | |
|---|
| 10 | 2 | class error(Exception): |
|---|
| 11 | 1 | pass |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | |
|---|
| 14 | 1 | def get_long_be(s): |
|---|
| 15 | n/a | """Convert a 4-char value to integer.""" |
|---|
| 16 | 0 | return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3]) |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | 1 | def gethdr(fp): |
|---|
| 20 | n/a | """Read a sound header from an open file.""" |
|---|
| 21 | 0 | if fp.read(4) != MAGIC: |
|---|
| 22 | 0 | raise error, 'gethdr: bad magic word' |
|---|
| 23 | 0 | hdr_size = get_long_be(fp.read(4)) |
|---|
| 24 | 0 | data_size = get_long_be(fp.read(4)) |
|---|
| 25 | 0 | encoding = get_long_be(fp.read(4)) |
|---|
| 26 | 0 | sample_rate = get_long_be(fp.read(4)) |
|---|
| 27 | 0 | channels = get_long_be(fp.read(4)) |
|---|
| 28 | 0 | excess = hdr_size - 24 |
|---|
| 29 | 0 | if excess < 0: |
|---|
| 30 | 0 | raise error, 'gethdr: bad hdr_size' |
|---|
| 31 | 0 | if excess > 0: |
|---|
| 32 | 0 | info = fp.read(excess) |
|---|
| 33 | n/a | else: |
|---|
| 34 | 0 | info = '' |
|---|
| 35 | 0 | return (data_size, encoding, sample_rate, channels, info) |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | |
|---|
| 38 | 1 | def printhdr(file): |
|---|
| 39 | n/a | """Read and print the sound header of a named file.""" |
|---|
| 40 | 0 | hdr = gethdr(open(file, 'r')) |
|---|
| 41 | 0 | data_size, encoding, sample_rate, channels, info = hdr |
|---|
| 42 | 0 | while info[-1:] == '\0': |
|---|
| 43 | 0 | info = info[:-1] |
|---|
| 44 | 0 | print 'File name: ', file |
|---|
| 45 | 0 | print 'Data size: ', data_size |
|---|
| 46 | 0 | print 'Encoding: ', encoding |
|---|
| 47 | 0 | print 'Sample rate:', sample_rate |
|---|
| 48 | 0 | print 'Channels: ', channels |
|---|
| 49 | 0 | print 'Info: ', repr(info) |
|---|