blob: 96def15a19a40d39c5e7ec2664ceb79409c051c3 [file] [log] [blame]
Guido van Rossum217a5fa1990-12-26 15:40:07 +00001# Module 'sunaudio' -- interpret sun audio headers
2
Guido van Rossum217a5fa1990-12-26 15:40:07 +00003MAGIC = '.snd'
4
5error = 'sunaudio sound header conversion error'
6
7
8# convert a 4-char value to integer
9
Guido van Rossumd482e8a1992-06-03 16:47:49 +000010def get_long_be(s):
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
14# read a sound header from an open file
15
16def gethdr(fp):
17 if fp.read(4) <> MAGIC:
18 raise error, 'gethdr: bad magic word'
Guido van Rossumd482e8a1992-06-03 16:47:49 +000019 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 Rossum217a5fa1990-12-26 15:40:07 +000024 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
36def printhdr(file):
37 hdr = gethdr(open(file, 'r'))
38 data_size, encoding, sample_rate, channels, info = hdr
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000039 while info[-1:] == '\0':
Guido van Rossum217a5fa1990-12-26 15:40:07 +000040 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`