blob: b7df71c97efc996db86fbb2753f6ed41000bcf4e [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Interpret sun audio headers."""
Guido van Rossum217a5fa1990-12-26 15:40:07 +00002
Guido van Rossum217a5fa1990-12-26 15:40:07 +00003MAGIC = '.snd'
4
5error = 'sunaudio sound header conversion error'
6
7
Guido van Rossumd482e8a1992-06-03 16:47:49 +00008def get_long_be(s):
Guido van Rossume7b146f2000-02-04 15:28:42 +00009 """Convert a 4-char value to integer."""
Guido van Rossumd482e8a1992-06-03 16:47:49 +000010 return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
Guido van Rossum217a5fa1990-12-26 15:40:07 +000011
12
Guido van Rossum217a5fa1990-12-26 15:40:07 +000013def gethdr(fp):
Guido van Rossume7b146f2000-02-04 15:28:42 +000014 """Read a sound header from an open file."""
Guido van Rossum217a5fa1990-12-26 15:40:07 +000015 if fp.read(4) <> MAGIC:
16 raise error, 'gethdr: bad magic word'
Guido van Rossumd482e8a1992-06-03 16:47:49 +000017 hdr_size = get_long_be(fp.read(4))
18 data_size = get_long_be(fp.read(4))
19 encoding = get_long_be(fp.read(4))
20 sample_rate = get_long_be(fp.read(4))
21 channels = get_long_be(fp.read(4))
Guido van Rossum217a5fa1990-12-26 15:40:07 +000022 excess = hdr_size - 24
23 if excess < 0:
24 raise error, 'gethdr: bad hdr_size'
25 if excess > 0:
26 info = fp.read(excess)
27 else:
28 info = ''
29 return (data_size, encoding, sample_rate, channels, info)
30
31
Guido van Rossum217a5fa1990-12-26 15:40:07 +000032def printhdr(file):
Guido van Rossume7b146f2000-02-04 15:28:42 +000033 """Read and print the sound header of a named file."""
Guido van Rossum217a5fa1990-12-26 15:40:07 +000034 hdr = gethdr(open(file, 'r'))
35 data_size, encoding, sample_rate, channels, info = hdr
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000036 while info[-1:] == '\0':
Guido van Rossum217a5fa1990-12-26 15:40:07 +000037 info = info[:-1]
38 print 'File name: ', file
39 print 'Data size: ', data_size
40 print 'Encoding: ', encoding
41 print 'Sample rate:', sample_rate
42 print 'Channels: ', channels
43 print 'Info: ', `info`