Mercurial > cpython-lru_cache-weakref
annotate Lib/sunaudio.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | f9040d107d66 |
| children |
| rev | line source |
|---|---|
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
879
diff
changeset
|
1 """Interpret sun audio headers.""" |
|
48020
f9040d107d66
Deprecate the sunaudio module for removal in Python 3.0. The sunau module can provide similar functionality.
Brett Cannon <bcannon@gmail.com>
parents:
31515
diff
changeset
|
2 from warnings import warnpy3k |
|
f9040d107d66
Deprecate the sunaudio module for removal in Python 3.0. The sunau module can provide similar functionality.
Brett Cannon <bcannon@gmail.com>
parents:
31515
diff
changeset
|
3 warnpy3k("the sunaudio module has been removed in Python 3.0; " |
|
f9040d107d66
Deprecate the sunaudio module for removal in Python 3.0. The sunau module can provide similar functionality.
Brett Cannon <bcannon@gmail.com>
parents:
31515
diff
changeset
|
4 "use the sunau module instead", stacklevel=2) |
|
f9040d107d66
Deprecate the sunaudio module for removal in Python 3.0. The sunau module can provide similar functionality.
Brett Cannon <bcannon@gmail.com>
parents:
31515
diff
changeset
|
5 del warnpy3k |
|
f9040d107d66
Deprecate the sunaudio module for removal in Python 3.0. The sunau module can provide similar functionality.
Brett Cannon <bcannon@gmail.com>
parents:
31515
diff
changeset
|
6 |
| 103 | 7 |
| 8 MAGIC = '.snd' | |
| 9 | |
|
14021
5b34b70ecf22
Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents:
11707
diff
changeset
|
10 class error(Exception): |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
11 pass |
| 103 | 12 |
| 13 | |
|
879
f9481aa5e776
Remove reference to obsolete module 'audio'
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
14 def get_long_be(s): |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
15 """Convert a 4-char value to integer.""" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
16 return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3]) |
| 103 | 17 |
| 18 | |
| 19 def gethdr(fp): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
20 """Read a sound header from an open file.""" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
21 if fp.read(4) != MAGIC: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
22 raise error, 'gethdr: bad magic word' |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
23 hdr_size = get_long_be(fp.read(4)) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
24 data_size = get_long_be(fp.read(4)) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
25 encoding = get_long_be(fp.read(4)) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
26 sample_rate = get_long_be(fp.read(4)) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
27 channels = get_long_be(fp.read(4)) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
28 excess = hdr_size - 24 |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
29 if excess < 0: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
30 raise error, 'gethdr: bad hdr_size' |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
31 if excess > 0: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
32 info = fp.read(excess) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
33 else: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
34 info = '' |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
35 return (data_size, encoding, sample_rate, channels, info) |
| 103 | 36 |
| 37 | |
| 38 def printhdr(file): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
39 """Read and print the sound header of a named file.""" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
40 hdr = gethdr(open(file, 'r')) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
41 data_size, encoding, sample_rate, channels, info = hdr |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
42 while info[-1:] == '\0': |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
43 info = info[:-1] |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
44 print 'File name: ', file |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
45 print 'Data size: ', data_size |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
46 print 'Encoding: ', encoding |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
47 print 'Sample rate:', sample_rate |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
48 print 'Channels: ', channels |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
15872
diff
changeset
|
49 print 'Info: ', repr(info) |
