sunaudio.py revision d482e8ad4a11c7cbe9374f449da036ad21a5ee55
1# Module 'sunaudio' -- interpret sun audio headers 2 3MAGIC = '.snd' 4 5error = 'sunaudio sound header conversion error' 6 7 8# convert a 4-char value to integer 9 10def get_long_be(s): 11 return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3]) 12 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' 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) 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 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` 47