blob: b6e5047198ce0d9fbeba87ee581658c579096f0b [file] [log] [blame]
Guido van Rossum217a5fa1990-12-26 15:40:07 +00001# Module 'sunaudio' -- interpret sun audio headers
2
3import audio
4
5MAGIC = '.snd'
6
7error = 'sunaudio sound header conversion error'
8
9
10# convert a 4-char value to integer
11
12def c2i(data):
13 if type(data) <> type('') or len(data) <> 4:
14 raise error, 'c2i: bad arg (not string[4])'
15 bytes = audio.chr2num(data)
16 for i in (1, 2, 3):
17 if bytes[i] < 0:
18 bytes[i] = bytes[i] + 256
19 return ((bytes[0]*256 + bytes[1])*256 + bytes[2])*256 + bytes[3]
20
21
22# read a sound header from an open file
23
24def gethdr(fp):
25 if fp.read(4) <> MAGIC:
26 raise error, 'gethdr: bad magic word'
27 hdr_size = c2i(fp.read(4))
28 data_size = c2i(fp.read(4))
29 encoding = c2i(fp.read(4))
30 sample_rate = c2i(fp.read(4))
31 channels = c2i(fp.read(4))
32 excess = hdr_size - 24
33 if excess < 0:
34 raise error, 'gethdr: bad hdr_size'
35 if excess > 0:
36 info = fp.read(excess)
37 else:
38 info = ''
39 return (data_size, encoding, sample_rate, channels, info)
40
41
42# read and print the sound header of a named file
43
44def printhdr(file):
45 hdr = gethdr(open(file, 'r'))
46 data_size, encoding, sample_rate, channels, info = hdr
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000047 while info[-1:] == '\0':
Guido van Rossum217a5fa1990-12-26 15:40:07 +000048 info = info[:-1]
49 print 'File name: ', file
50 print 'Data size: ', data_size
51 print 'Encoding: ', encoding
52 print 'Sample rate:', sample_rate
53 print 'Channels: ', channels
54 print 'Info: ', `info`