sunaudio.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. MAGIC = '.snd'
  7. class error(Exception):
  8. pass
  9. def get_long_be(s):
  10. """Convert a 4-char value to integer."""
  11. return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
  12. def gethdr(fp):
  13. """Read a sound header from an open file."""
  14. if fp.read(4) != MAGIC:
  15. raise error, 'gethdr: bad magic word'
  16. hdr_size = get_long_be(fp.read(4))
  17. data_size = get_long_be(fp.read(4))
  18. encoding = get_long_be(fp.read(4))
  19. sample_rate = get_long_be(fp.read(4))
  20. channels = get_long_be(fp.read(4))
  21. excess = hdr_size - 24
  22. if excess < 0:
  23. raise error, 'gethdr: bad hdr_size'
  24. if excess > 0:
  25. info = fp.read(excess)
  26. else:
  27. info = ''
  28. return (data_size, encoding, sample_rate, channels, info)
  29. def printhdr(file):
  30. """Read and print the sound header of a named file."""
  31. hdr = gethdr(open(file, 'r'))
  32. data_size, encoding, sample_rate, channels, info = hdr
  33. while info[-1:] == '\0':
  34. info = info[:-1]
  35. print 'File name: ', file
  36. print 'Data size: ', data_size
  37. print 'Encoding: ', encoding
  38. print 'Sample rate:', sample_rate
  39. print 'Channels: ', channels
  40. print 'Info: ', repr(info)