PyXR

c:\python24\lib \ sunaudio.py



0001 """Interpret sun audio headers."""
0002 
0003 MAGIC = '.snd'
0004 
0005 class error(Exception):
0006     pass
0007 
0008 
0009 def get_long_be(s):
0010     """Convert a 4-char value to integer."""
0011     return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
0012 
0013 
0014 def gethdr(fp):
0015     """Read a sound header from an open file."""
0016     if fp.read(4) != MAGIC:
0017         raise error, 'gethdr: bad magic word'
0018     hdr_size = get_long_be(fp.read(4))
0019     data_size = get_long_be(fp.read(4))
0020     encoding = get_long_be(fp.read(4))
0021     sample_rate = get_long_be(fp.read(4))
0022     channels = get_long_be(fp.read(4))
0023     excess = hdr_size - 24
0024     if excess < 0:
0025         raise error, 'gethdr: bad hdr_size'
0026     if excess > 0:
0027         info = fp.read(excess)
0028     else:
0029         info = ''
0030     return (data_size, encoding, sample_rate, channels, info)
0031 
0032 
0033 def printhdr(file):
0034     """Read and print the sound header of a named file."""
0035     hdr = gethdr(open(file, 'r'))
0036     data_size, encoding, sample_rate, channels, info = hdr
0037     while info[-1:] == '\0':
0038         info = info[:-1]
0039     print 'File name:  ', file
0040     print 'Data size:  ', data_size
0041     print 'Encoding:   ', encoding
0042     print 'Sample rate:', sample_rate
0043     print 'Channels:   ', channels
0044     print 'Info:       ', repr(info)
0045 

Generated by PyXR 0.9.4
SourceForge.net Logo