blob: 86a5b4190eb721ee6a1081ff793b738d2af35ee3 [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Interpret sun audio headers."""
Guido van Rossum217a5fa1990-12-26 15:40:07 +00002
Guido van Rossum217a5fa1990-12-26 15:40:07 +00003MAGIC = '.snd'
4
Fred Drake9b8d8012000-08-17 04:45:13 +00005class error(Exception):
Tim Peters495ad3c2001-01-15 01:36:40 +00006 pass
Guido van Rossum217a5fa1990-12-26 15:40:07 +00007
8
Guido van Rossumd482e8a1992-06-03 16:47:49 +00009def get_long_be(s):
Tim Peters495ad3c2001-01-15 01:36:40 +000010 """Convert a 4-char value to integer."""
11 return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
Guido van Rossum217a5fa1990-12-26 15:40:07 +000012
13
Guido van Rossum217a5fa1990-12-26 15:40:07 +000014def gethdr(fp):
Tim Peters495ad3c2001-01-15 01:36:40 +000015 """Read a sound header from an open file."""
16 if fp.read(4) != MAGIC:
17 raise error, 'gethdr: bad magic word'
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:
25 raise error, 'gethdr: bad hdr_size'
26 if excess > 0:
27 info = fp.read(excess)
28 else:
29 info = ''
30 return (data_size, encoding, sample_rate, channels, info)
Guido van Rossum217a5fa1990-12-26 15:40:07 +000031
32
Guido van Rossum217a5fa1990-12-26 15:40:07 +000033def printhdr(file):
Tim Peters495ad3c2001-01-15 01:36:40 +000034 """Read and print the sound header of a named file."""
35 hdr = gethdr(open(file, 'r'))
36 data_size, encoding, sample_rate, channels, info = hdr
37 while info[-1:] == '\0':
38 info = info[:-1]
39 print 'File name: ', file
40 print 'Data size: ', data_size
41 print 'Encoding: ', encoding
42 print 'Sample rate:', sample_rate
43 print 'Channels: ', channels
44 print 'Info: ', `info`