1 """Interpret sun audio headers.""" 2 from warnings import warnpy3k 3 warnpy3k("the sunaudio module has been removed in Python 3.0; " 4 "use the sunau module instead", stacklevel=2) 5 del warnpy3k 6 7 8 MAGIC = '.snd' 9 10 class error(Exception): 11 pass 12 13 14 def get_long_be(s): 15 """Convert a 4-char value to integer.""" 16 return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3]) 17 18 19 def gethdr(fp): 20 """Read a sound header from an open file.""" 21 if fp.read(4) != MAGIC: 22 raise error, 'gethdr: bad magic word' 23 hdr_size = get_long_be(fp.read(4)) 24 data_size = get_long_be(fp.read(4)) 25 encoding = get_long_be(fp.read(4)) 26 sample_rate = get_long_be(fp.read(4)) 27 channels = get_long_be(fp.read(4)) 28 excess = hdr_size - 24 29 if excess < 0: 30 raise error, 'gethdr: bad hdr_size' 31 if excess > 0: 32 info = fp.read(excess) 33 else: 34 info = '' 35 return (data_size, encoding, sample_rate, channels, info) 36 37 38 def printhdr(file): 39 """Read and print the sound header of a named file.""" 40 hdr = gethdr(open(file, 'r')) 41 data_size, encoding, sample_rate, channels, info = hdr 42 while info[-1:] == '\0': 43 info = info[:-1] 44 print 'File name: ', file 45 print 'Data size: ', data_size 46 print 'Encoding: ', encoding 47 print 'Sample rate:', sample_rate 48 print 'Channels: ', channels 49 print 'Info: ', repr(info) 50