PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/sunaudio.py

http://unladen-swallow.googlecode.com/
Python | 49 lines | 39 code | 7 blank | 3 comment | 3 complexity | 5a93cd60fd96604a74c61c23b12de71c MD5 | raw file
 1"""Interpret sun audio headers."""
 2from warnings import warnpy3k
 3warnpy3k("the sunaudio module has been removed in Python 3.0; "
 4         "use the sunau module instead", stacklevel=2)
 5del warnpy3k
 6
 7
 8MAGIC = '.snd'
 9
10class error(Exception):
11    pass
12
13
14def 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
19def 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
38def 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)