Clarify portability and main program.
[python/dscho.git] / Lib / sunaudio.py
blob96def15a19a40d39c5e7ec2664ceb79409c051c3
1 # Module 'sunaudio' -- interpret sun audio headers
3 MAGIC = '.snd'
5 error = 'sunaudio sound header conversion error'
8 # convert a 4-char value to integer
10 def get_long_be(s):
11 return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
14 # read a sound header from an open file
16 def gethdr(fp):
17 if fp.read(4) <> MAGIC:
18 raise error, 'gethdr: bad magic word'
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))
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)
34 # read and print the sound header of a named file
36 def printhdr(file):
37 hdr = gethdr(open(file, 'r'))
38 data_size, encoding, sample_rate, channels, info = hdr
39 while info[-1:] == '\0':
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`