blob: 97d7332c4856ac9b0ba2f0a63c8f6ea68f9620d8 [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Interpret sun audio headers."""
Brett Cannon1e8fba72008-07-18 19:30:22 +00002from warnings import warnpy3k
3warnpy3k("the sunaudio module has been removed in Python 3.0; "
4 "use the sunau module instead", stacklevel=2)
5del warnpy3k
6
Guido van Rossum217a5fa1990-12-26 15:40:07 +00007
Guido van Rossum217a5fa1990-12-26 15:40:07 +00008MAGIC = '.snd'
9
Fred Drake9b8d8012000-08-17 04:45:13 +000010class error(Exception):
Tim Peters495ad3c2001-01-15 01:36:40 +000011 pass
Guido van Rossum217a5fa1990-12-26 15:40:07 +000012
13
Guido van Rossumd482e8a1992-06-03 16:47:49 +000014def get_long_be(s):
Tim Peters495ad3c2001-01-15 01:36:40 +000015 """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 Rossum217a5fa1990-12-26 15:40:07 +000017
18
Guido van Rossum217a5fa1990-12-26 15:40:07 +000019def gethdr(fp):
Tim Peters495ad3c2001-01-15 01:36:40 +000020 """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 Rossum217a5fa1990-12-26 15:40:07 +000036
37
Guido van Rossum217a5fa1990-12-26 15:40:07 +000038def printhdr(file):
Tim Peters495ad3c2001-01-15 01:36:40 +000039 """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örwald70a6b492004-02-12 17:35:32 +000049 print 'Info: ', repr(info)