home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Lib / sunaudio.py < prev    next >
Text File  |  1993-12-29  |  1KB  |  47 lines

  1. # Module 'sunaudio' -- interpret sun audio headers
  2.  
  3. MAGIC = '.snd'
  4.  
  5. error = 'sunaudio sound header conversion error'
  6.  
  7.  
  8. # convert a 4-char value to integer
  9.  
  10. def 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.  
  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)
  32.  
  33.  
  34. # read and print the sound header of a named file
  35.  
  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`
  47.