Mercurial > cpython-lru_cache-weakref
annotate Lib/wave.py @ 12357:5b59ea41db4e legacy-trunk
Install the docs (with fewer rules).
Add descriptions for the system variables.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 10 Apr 2000 19:36:27 +0000 |
| parents | 6f4146846337 |
| children | 5b727ab37092 |
| rev | line source |
|---|---|
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
1 """Stuff to parse WAVE files. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
2 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
3 Usage. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
4 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
5 Reading WAVE files: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
6 f = wave.open(file, 'r') |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
7 where file is either the name of a file or an open file pointer. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
8 The open file pointer must have methods read(), seek(), and close(). |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
9 When the setpos() and rewind() methods are not used, the seek() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
10 method is not necessary. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
11 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
12 This returns an instance of a class with the following public methods: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
13 getnchannels() -- returns number of audio channels (1 for |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
14 mono, 2 for stereo) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
15 getsampwidth() -- returns sample width in bytes |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
16 getframerate() -- returns sampling frequency |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
17 getnframes() -- returns number of audio frames |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
18 getcomptype() -- returns compression type ('NONE' for linear samples) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
19 getcompname() -- returns human-readable version of |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
20 compression type ('not compressed' linear samples) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
21 getparams() -- returns a tuple consisting of all of the |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
22 above in the above order |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
23 getmarkers() -- returns None (for compatibility with the |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
24 aifc module) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
25 getmark(id) -- raises an error since the mark does not |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
26 exist (for compatibility with the aifc module) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
27 readframes(n) -- returns at most n frames of audio |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
28 rewind() -- rewind to the beginning of the audio stream |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
29 setpos(pos) -- seek to the specified position |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
30 tell() -- return the current position |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
31 close() -- close the instance (make it unusable) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
32 The position returned by tell() and the position given to setpos() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
33 are compatible and have nothing to do with the actual postion in the |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
34 file. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
35 The close() method is called automatically when the class instance |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
36 is destroyed. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
37 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
38 Writing WAVE files: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
39 f = wave.open(file, 'w') |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
40 where file is either the name of a file or an open file pointer. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
41 The open file pointer must have methods write(), tell(), seek(), and |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
42 close(). |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
43 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
44 This returns an instance of a class with the following public methods: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
45 setnchannels(n) -- set the number of channels |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
46 setsampwidth(n) -- set the sample width |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
47 setframerate(n) -- set the frame rate |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
48 setnframes(n) -- set the number of frames |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
49 setcomptype(type, name) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
50 -- set the compression type and the |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
51 human-readable compression type |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
52 setparams(tuple) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
53 -- set all parameters at once |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
54 tell() -- return current position in output file |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
55 writeframesraw(data) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
56 -- write audio frames without pathing up the |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
57 file header |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
58 writeframes(data) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
59 -- write audio frames and patch up the file header |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
60 close() -- patch up the file header and close the |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
61 output file |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
62 You should set the parameters before the first writeframesraw or |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
63 writeframes. The total number of frames does not need to be set, |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
64 but when it is set to the correct value, the header does not have to |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
65 be patched up. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
66 It is best to first set all parameters, perhaps possibly the |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
67 compression type, and then write audio frames using writeframesraw. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
68 When all frames have been written, either call writeframes('') or |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
69 close() to patch up the sizes in the header. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
70 The close() method is called automatically when the class instance |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
71 is destroyed. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
72 """ |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 import __builtin__ |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 Error = 'wave.Error' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 WAVE_FORMAT_PCM = 0x0001 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 _array_fmts = None, 'b', 'h', None, 'l' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 |
| 10106 | 82 # Determine endian-ness |
| 83 import struct | |
| 84 if struct.pack("h", 1) == "\000\001": | |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
85 big_endian = 1 |
| 10106 | 86 else: |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
87 big_endian = 0 |
| 10106 | 88 |
|
11227
e773d2f0741d
Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents:
10999
diff
changeset
|
89 from chunk import Chunk |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 class Wave_read: |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
92 """Variables used in this class: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
93 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
94 These variables are available to the user though appropriate |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
95 methods of this class: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
96 _file -- the open file with methods read(), close(), and seek() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
97 set through the __init__() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
98 _nchannels -- the number of audio channels |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
99 available through the getnchannels() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
100 _nframes -- the number of audio frames |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
101 available through the getnframes() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
102 _sampwidth -- the number of bytes per audio sample |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
103 available through the getsampwidth() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
104 _framerate -- the sampling frequency |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
105 available through the getframerate() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
106 _comptype -- the AIFF-C compression type ('NONE' if AIFF) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
107 available through the getcomptype() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
108 _compname -- the human-readable AIFF-C compression type |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
109 available through the getcomptype() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
110 _soundpos -- the position in the audio stream |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
111 available through the tell() method, set through the |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
112 setpos() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
113 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
114 These variables are used internally only: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
115 _fmt_chunk_read -- 1 iff the FMT chunk has been read |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
116 _data_seek_needed -- 1 iff positioned correctly in audio |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
117 file for readframes() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
118 _data_chunk -- instantiation of a chunk class for the DATA chunk |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
119 _framesize -- size of one frame in the file |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
120 """ |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
122 def initfp(self, file): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
123 self._convert = None |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
124 self._soundpos = 0 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
125 self._file = Chunk(file, bigendian = 0) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
126 if self._file.getname() != 'RIFF': |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
127 raise Error, 'file does not start with RIFF id' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
128 if self._file.read(4) != 'WAVE': |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
129 raise Error, 'not a WAVE file' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
130 self._fmt_chunk_read = 0 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
131 self._data_chunk = None |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
132 while 1: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
133 self._data_seek_needed = 1 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
134 try: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
135 chunk = Chunk(self._file, bigendian = 0) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
136 except EOFError: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
137 break |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
138 chunkname = chunk.getname() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
139 if chunkname == 'fmt ': |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
140 self._read_fmt_chunk(chunk) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
141 self._fmt_chunk_read = 1 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
142 elif chunkname == 'data': |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
143 if not self._fmt_chunk_read: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
144 raise Error, 'data chunk before fmt chunk' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
145 self._data_chunk = chunk |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
146 self._nframes = chunk.chunksize / self._framesize |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
147 self._data_seek_needed = 0 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
148 break |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
149 chunk.skip() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
150 if not self._fmt_chunk_read or not self._data_chunk: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
151 raise Error, 'fmt chunk and/or data chunk missing' |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
153 def __init__(self, f): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
154 if type(f) == type(''): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
155 f = __builtin__.open(f, 'rb') |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
156 # else, assume it is an open file object already |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
157 self.initfp(f) |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
159 # |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
160 # User visible methods. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
161 # |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
162 def getfp(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
163 return self._file |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
165 def rewind(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
166 self._data_seek_needed = 1 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
167 self._soundpos = 0 |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
169 def close(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
170 self._file = None |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
172 def tell(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
173 return self._soundpos |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
175 def getnchannels(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
176 return self._nchannels |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
178 def getnframes(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
179 return self._nframes |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
181 def getsampwidth(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
182 return self._sampwidth |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
184 def getframerate(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
185 return self._framerate |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
187 def getcomptype(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
188 return self._comptype |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
190 def getcompname(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
191 return self._compname |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
193 def getparams(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
194 return self.getnchannels(), self.getsampwidth(), \ |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
195 self.getframerate(), self.getnframes(), \ |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
196 self.getcomptype(), self.getcompname() |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
198 def getmarkers(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
199 return None |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
201 def getmark(self, id): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
202 raise Error, 'no marks' |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
204 def setpos(self, pos): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
205 if pos < 0 or pos > self._nframes: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
206 raise Error, 'position not in range' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
207 self._soundpos = pos |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
208 self._data_seek_needed = 1 |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
210 def readframes(self, nframes): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
211 if self._data_seek_needed: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
212 self._data_chunk.seek(0, 0) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
213 pos = self._soundpos * self._framesize |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
214 if pos: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
215 self._data_chunk.seek(pos, 0) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
216 self._data_seek_needed = 0 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
217 if nframes == 0: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
218 return '' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
219 if self._sampwidth > 1 and big_endian: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
220 # unfortunately the fromfile() method does not take |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
221 # something that only looks like a file object, so |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
222 # we have to reach into the innards of the chunk object |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
223 import array |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
224 chunk = self._data_chunk |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
225 data = array.array(_array_fmts[self._sampwidth]) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
226 nitems = nframes * self._nchannels |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
227 if nitems * self._sampwidth > chunk.chunksize - chunk.size_read: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
228 nitems = (chunk.chunksize - chunk.size_read) / self._sampwidth |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
229 data.fromfile(chunk.file.file, nitems) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
230 # "tell" data chunk how much was read |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
231 chunk.size_read = chunk.size_read + nitems * self._sampwidth |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
232 # do the same for the outermost chunk |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
233 chunk = chunk.file |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
234 chunk.size_read = chunk.size_read + nitems * self._sampwidth |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
235 data.byteswap() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
236 data = data.tostring() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
237 else: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
238 data = self._data_chunk.read(nframes * self._framesize) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
239 if self._convert and data: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
240 data = self._convert(data) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
241 self._soundpos = self._soundpos + len(data) / (self._nchannels * self._sampwidth) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
242 return data |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
244 # |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
245 # Internal methods. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
246 # |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
248 def _read_fmt_chunk(self, chunk): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
249 wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack('<hhllh', chunk.read(14)) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
250 if wFormatTag == WAVE_FORMAT_PCM: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
251 sampwidth = struct.unpack('<h', chunk.read(2))[0] |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
252 self._sampwidth = (sampwidth + 7) / 8 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
253 else: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
254 raise Error, 'unknown format: ' + `wFormatTag` |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
255 self._framesize = self._nchannels * self._sampwidth |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
256 self._comptype = 'NONE' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
257 self._compname = 'not compressed' |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 class Wave_write: |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
260 """Variables used in this class: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
261 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
262 These variables are user settable through appropriate methods |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
263 of this class: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
264 _file -- the open file with methods write(), close(), tell(), seek() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
265 set through the __init__() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
266 _comptype -- the AIFF-C compression type ('NONE' in AIFF) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
267 set through the setcomptype() or setparams() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
268 _compname -- the human-readable AIFF-C compression type |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
269 set through the setcomptype() or setparams() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
270 _nchannels -- the number of audio channels |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
271 set through the setnchannels() or setparams() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
272 _sampwidth -- the number of bytes per audio sample |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
273 set through the setsampwidth() or setparams() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
274 _framerate -- the sampling frequency |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
275 set through the setframerate() or setparams() method |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
276 _nframes -- the number of audio frames written to the header |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
277 set through the setnframes() or setparams() method |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
279 These variables are used internally only: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
280 _datalength -- the size of the audio samples written to the header |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
281 _nframeswritten -- the number of frames actually written |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
282 _datawritten -- the size of the audio samples actually written |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
283 """ |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
284 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
285 def __init__(self, f): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
286 if type(f) == type(''): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
287 f = __builtin__.open(f, 'wb') |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
288 self.initfp(f) |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
290 def initfp(self, file): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
291 self._file = file |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
292 self._convert = None |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
293 self._nchannels = 0 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
294 self._sampwidth = 0 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
295 self._framerate = 0 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
296 self._nframes = 0 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
297 self._nframeswritten = 0 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
298 self._datawritten = 0 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
299 self._datalength = 0 |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
301 def __del__(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
302 if self._file: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
303 self.close() |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
305 # |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
306 # User visible methods. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
307 # |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
308 def setnchannels(self, nchannels): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
309 if self._datawritten: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
310 raise Error, 'cannot change parameters after starting to write' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
311 if nchannels < 1: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
312 raise Error, 'bad # of channels' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
313 self._nchannels = nchannels |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
315 def getnchannels(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
316 if not self._nchannels: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
317 raise Error, 'number of channels not set' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
318 return self._nchannels |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
320 def setsampwidth(self, sampwidth): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
321 if self._datawritten: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
322 raise Error, 'cannot change parameters after starting to write' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
323 if sampwidth < 1 or sampwidth > 4: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
324 raise Error, 'bad sample width' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
325 self._sampwidth = sampwidth |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
327 def getsampwidth(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
328 if not self._sampwidth: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
329 raise Error, 'sample width not set' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
330 return self._sampwidth |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
332 def setframerate(self, framerate): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
333 if self._datawritten: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
334 raise Error, 'cannot change parameters after starting to write' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
335 if framerate <= 0: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
336 raise Error, 'bad frame rate' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
337 self._framerate = framerate |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
339 def getframerate(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
340 if not self._framerate: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
341 raise Error, 'frame rate not set' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
342 return self._framerate |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
344 def setnframes(self, nframes): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
345 if self._datawritten: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
346 raise Error, 'cannot change parameters after starting to write' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
347 self._nframes = nframes |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
348 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
349 def getnframes(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
350 return self._nframeswritten |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
352 def setcomptype(self, comptype, compname): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
353 if self._datawritten: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
354 raise Error, 'cannot change parameters after starting to write' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
355 if comptype not in ('NONE',): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
356 raise Error, 'unsupported compression type' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
357 self._comptype = comptype |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
358 self._compname = compname |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
360 def getcomptype(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
361 return self._comptype |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
363 def getcompname(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
364 return self._compname |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
365 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
366 def setparams(self, (nchannels, sampwidth, framerate, nframes, comptype, compname)): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
367 if self._datawritten: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
368 raise Error, 'cannot change parameters after starting to write' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
369 self.setnchannels(nchannels) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
370 self.setsampwidth(sampwidth) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
371 self.setframerate(framerate) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
372 self.setnframes(nframes) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
373 self.setcomptype(comptype, compname) |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
374 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
375 def getparams(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
376 if not self._nchannels or not self._sampwidth or not self._framerate: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
377 raise Error, 'not all parameters set' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
378 return self._nchannels, self._sampwidth, self._framerate, \ |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
379 self._nframes, self._comptype, self._compname |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
381 def setmark(self, id, pos, name): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
382 raise Error, 'setmark() not supported' |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
384 def getmark(self, id): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
385 raise Error, 'no marks' |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
387 def getmarkers(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
388 return None |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
389 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
390 def tell(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
391 return self._nframeswritten |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
393 def writeframesraw(self, data): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
394 self._ensure_header_written(len(data)) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
395 nframes = len(data) / (self._sampwidth * self._nchannels) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
396 if self._convert: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
397 data = self._convert(data) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
398 if self._sampwidth > 1 and big_endian: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
399 import array |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
400 data = array.array(_array_fmts[self._sampwidth], data) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
401 data.byteswap() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
402 data.tofile(self._file) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
403 self._datawritten = self._datawritten + len(data) * self._sampwidth |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
404 else: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
405 self._file.write(data) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
406 self._datawritten = self._datawritten + len(data) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
407 self._nframeswritten = self._nframeswritten + nframes |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
408 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
409 def writeframes(self, data): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
410 self.writeframesraw(data) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
411 if self._datalength != self._datawritten: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
412 self._patchheader() |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
414 def close(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
415 self._ensure_header_written(0) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
416 if self._datalength != self._datawritten: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
417 self._patchheader() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
418 self._file.flush() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
419 self._file = None |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
420 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
421 # |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
422 # Internal methods. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
423 # |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
425 def _ensure_header_written(self, datasize): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
426 if not self._datawritten: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
427 if not self._nchannels: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
428 raise Error, '# channels not specified' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
429 if not self._sampwidth: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
430 raise Error, 'sample width not specified' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
431 if not self._framerate: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
432 raise Error, 'sampling rate not specified' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
433 self._write_header(datasize) |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
435 def _write_header(self, initlength): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
436 self._file.write('RIFF') |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
437 if not self._nframes: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
438 self._nframes = initlength / (self._nchannels * self._sampwidth) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
439 self._datalength = self._nframes * self._nchannels * self._sampwidth |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
440 self._form_length_pos = self._file.tell() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
441 self._file.write(struct.pack('<lsslhhllhhs', |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
442 36 + self._datalength, 'WAVE', 'fmt ', 16, |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
443 WAVE_FORMAT_PCM, self._nchannels, self._framerate, |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
444 self._nchannels * self._framerate * self._sampwidth, |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
445 self._nchannels * self._sampwidth, |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
446 self._sampwidth * 8, 'data')) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
447 self._data_length_pos = self._file.tell() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
448 self._file.write(struct.pack('<l', self._datalength)) |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
449 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
450 def _patchheader(self): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
451 if self._datawritten == self._datalength: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
452 return |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
453 curpos = self._file.tell() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
454 self._file.seek(self._form_length_pos, 0) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
455 self._file.write(struct.pack('<l', 36 + self._datawritten)) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
456 self._file.seek(self._data_length_pos, 0) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
457 self._file.write(struct.pack('<l', self._datawritten)) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
458 self._file.seek(curpos, 0) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
459 self._datalength = self._datawritten |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
460 |
|
10999
8a4fc4da768d
open(): Make the mode parameter optional; if omitted or None, use the
Fred Drake <fdrake@acm.org>
parents:
10107
diff
changeset
|
461 def open(f, mode=None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
462 if mode is None: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
463 if hasattr(f, 'mode'): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
464 mode = f.mode |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
465 else: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
466 mode = 'rb' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
467 if mode in ('r', 'rb'): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
468 return Wave_read(f) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
469 elif mode in ('w', 'wb'): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
470 return Wave_write(f) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
471 else: |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11227
diff
changeset
|
472 raise Error, "mode must be 'r', 'rb', 'w', or 'wb'" |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
473 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
474 openfp = open # B/W compatibility |
