11
11
12
12
--------------
13
13
14
- The :mod: `wave ` module provides a convenient interface to the WAV sound format.
14
+ The :mod: `wave ` module provides a convenient interface to the Waveform Audio
15
+ "WAVE" (or "WAV") file format.
15
16
Only files using ``WAVE_FORMAT_PCM `` are supported. Note that this does not
16
17
include files using ``WAVE_FORMAT_EXTENSIBLE `` even if the subformat is PCM.
17
18
@@ -37,13 +38,12 @@ The :mod:`wave` module defines the following function and exception:
37
38
value for *mode *.
38
39
39
40
If you pass in a file-like object, the wave object will not close it when its
40
- :meth: ` close ` method is called; it is the caller's responsibility to close
41
+ `` close() ` ` method is called; it is the caller's responsibility to close
41
42
the file object.
42
43
43
44
The :func: `.open ` function may be used in a :keyword: `with ` statement. When
44
- the :keyword: `!with ` block completes, the :meth: `Wave_read.close()
45
- <wave.Wave_read.close> ` or :meth: `Wave_write.close()
46
- <wave.Wave_write.close()> ` method is called.
45
+ the :keyword: `!with ` block completes, the :meth: `Wave_read.close() ` or
46
+ :meth: `Wave_write.close() ` method is called.
47
47
48
48
.. versionchanged :: 3.4
49
49
Added support for unseekable files.
@@ -59,185 +59,192 @@ The :mod:`wave` module defines the following function and exception:
59
59
Wave_read Objects
60
60
-----------------
61
61
62
- Wave_read objects, as returned by :func: ` .open `, have the following methods:
62
+ .. class :: Wave_read
63
63
64
+ Read a WAV file.
64
65
65
- .. method :: Wave_read.close()
66
+ Wave_read objects, as returned by :func: ` .open `, have the following methods:
66
67
67
- Close the stream if it was opened by :mod: `wave `, and make the instance
68
- unusable. This is called automatically on object collection.
69
68
69
+ .. method :: close()
70
70
71
- .. method :: Wave_read.getnchannels()
71
+ Close the stream if it was opened by :mod: `wave `, and make the instance
72
+ unusable. This is called automatically on object collection.
72
73
73
- Returns number of audio channels (``1 `` for mono, ``2 `` for stereo).
74
74
75
+ .. method :: getnchannels()
75
76
76
- .. method :: Wave_read.getsampwidth()
77
+ Returns number of audio channels (`` 1 `` for mono, `` 2 `` for stereo).
77
78
78
- Returns sample width in bytes.
79
79
80
+ .. method :: getsampwidth()
80
81
81
- .. method :: Wave_read.getframerate()
82
+ Returns sample width in bytes.
82
83
83
- Returns sampling frequency.
84
84
85
+ .. method :: getframerate()
85
86
86
- .. method :: Wave_read.getnframes()
87
+ Returns sampling frequency.
87
88
88
- Returns number of audio frames.
89
89
90
+ .. method :: getnframes()
90
91
91
- .. method :: Wave_read.getcomptype()
92
+ Returns number of audio frames.
92
93
93
- Returns compression type (``'NONE' `` is the only supported type).
94
94
95
+ .. method :: getcomptype()
95
96
96
- .. method :: Wave_read.getcompname()
97
+ Returns compression type (`` 'NONE' `` is the only supported type).
97
98
98
- Human-readable version of :meth: `getcomptype `. Usually ``'not compressed' ``
99
- parallels ``'NONE' ``.
100
99
100
+ .. method :: getcompname()
101
101
102
- .. method :: Wave_read.getparams()
102
+ Human-readable version of :meth: `getcomptype `. Usually ``'not compressed' ``
103
+ parallels ``'NONE' ``.
103
104
104
- Returns a :func: `~collections.namedtuple ` ``(nchannels, sampwidth,
105
- framerate, nframes, comptype, compname) ``, equivalent to output of the
106
- :meth: `get\* ` methods.
107
105
106
+ .. method :: getparams()
108
107
109
- .. method :: Wave_read.readframes(n)
108
+ Returns a :func: `~collections.namedtuple ` ``(nchannels, sampwidth,
109
+ framerate, nframes, comptype, compname) ``, equivalent to output of the
110
+ ``get*() `` methods.
110
111
111
- Reads and returns at most *n * frames of audio, as a :class: `bytes ` object.
112
112
113
+ .. method :: readframes(n)
113
114
114
- .. method :: Wave_read.rewind()
115
+ Reads and returns at most * n * frames of audio, as a :class: ` bytes ` object.
115
116
116
- Rewind the file pointer to the beginning of the audio stream.
117
117
118
- The following two methods are defined for compatibility with the :mod: `aifc `
119
- module, and don't do anything interesting.
118
+ .. method :: rewind()
120
119
120
+ Rewind the file pointer to the beginning of the audio stream.
121
121
122
- .. method :: Wave_read.getmarkers()
122
+ The following two methods are defined for compatibility with the :mod: `aifc `
123
+ module, and don't do anything interesting.
123
124
124
- Returns ``None ``.
125
125
126
+ .. method :: getmarkers()
126
127
127
- .. method :: Wave_read.getmark(id)
128
+ Returns `` None ``.
128
129
129
- Raise an error.
130
130
131
- The following two methods define a term "position" which is compatible between
132
- them, and is otherwise implementation dependent.
131
+ .. method :: getmark(id)
133
132
133
+ Raise an error.
134
134
135
- .. method :: Wave_read.setpos(pos)
135
+ The following two methods define a term "position" which is compatible between
136
+ them, and is otherwise implementation dependent.
136
137
137
- Set the file pointer to the specified position.
138
138
139
+ .. method :: setpos(pos)
139
140
140
- .. method :: Wave_read.tell()
141
+ Set the file pointer to the specified position.
141
142
142
- Return current file pointer position.
143
+
144
+ .. method :: tell()
145
+
146
+ Return current file pointer position.
143
147
144
148
145
149
.. _wave-write-objects :
146
150
147
151
Wave_write Objects
148
152
------------------
149
153
150
- For seekable output streams, the ``wave `` header will automatically be updated
151
- to reflect the number of frames actually written. For unseekable streams, the
152
- *nframes * value must be accurate when the first frame data is written. An
153
- accurate *nframes * value can be achieved either by calling
154
- :meth: `~Wave_write.setnframes ` or :meth: `~Wave_write.setparams ` with the number
155
- of frames that will be written before :meth: `~Wave_write.close ` is called and
156
- then using :meth: `~Wave_write.writeframesraw ` to write the frame data, or by
157
- calling :meth: `~Wave_write.writeframes ` with all of the frame data to be
158
- written. In the latter case :meth: `~Wave_write.writeframes ` will calculate
159
- the number of frames in the data and set *nframes * accordingly before writing
160
- the frame data.
154
+ .. class :: Wave_write
161
155
162
- Wave_write objects, as returned by :func: ` .open `, have the following methods:
156
+ Write a WAV file.
163
157
164
- .. versionchanged :: 3.4
165
- Added support for unseekable files.
158
+ Wave_write objects, as returned by :func: `.open `.
166
159
160
+ For seekable output streams, the ``wave `` header will automatically be updated
161
+ to reflect the number of frames actually written. For unseekable streams, the
162
+ *nframes * value must be accurate when the first frame data is written. An
163
+ accurate *nframes * value can be achieved either by calling
164
+ :meth: `setnframes ` or :meth: `setparams ` with the number
165
+ of frames that will be written before :meth: `close ` is called and
166
+ then using :meth: `writeframesraw ` to write the frame data, or by
167
+ calling :meth: `writeframes ` with all of the frame data to be
168
+ written. In the latter case :meth: `writeframes ` will calculate
169
+ the number of frames in the data and set *nframes * accordingly before writing
170
+ the frame data.
167
171
168
- .. method :: Wave_write.close()
172
+ .. versionchanged :: 3.4
173
+ Added support for unseekable files.
169
174
170
- Make sure *nframes * is correct, and close the file if it was opened by
171
- :mod: `wave `. This method is called upon object collection. It will raise
172
- an exception if the output stream is not seekable and *nframes * does not
173
- match the number of frames actually written.
175
+ Wave_write objects have the following methods:
174
176
177
+ .. method :: close()
175
178
176
- .. method :: Wave_write.setnchannels(n)
179
+ Make sure *nframes * is correct, and close the file if it was opened by
180
+ :mod: `wave `. This method is called upon object collection. It will raise
181
+ an exception if the output stream is not seekable and *nframes * does not
182
+ match the number of frames actually written.
177
183
178
- Set the number of channels.
179
184
185
+ .. method :: setnchannels(n)
180
186
181
- .. method :: Wave_write.setsampwidth(n)
187
+ Set the number of channels.
182
188
183
- Set the sample width to *n * bytes.
184
189
190
+ .. method :: setsampwidth(n)
185
191
186
- .. method :: Wave_write.setframerate(n)
192
+ Set the sample width to * n * bytes.
187
193
188
- Set the frame rate to *n *.
189
194
190
- .. versionchanged :: 3.2
191
- A non-integral input to this method is rounded to the nearest
192
- integer.
195
+ .. method :: setframerate(n)
193
196
197
+ Set the frame rate to *n *.
194
198
195
- .. method :: Wave_write.setnframes(n)
199
+ .. versionchanged :: 3.2
200
+ A non-integral input to this method is rounded to the nearest
201
+ integer.
196
202
197
- Set the number of frames to *n *. This will be changed later if the number
198
- of frames actually written is different (this update attempt will
199
- raise an error if the output stream is not seekable).
200
203
204
+ .. method :: setnframes(n)
201
205
202
- .. method :: Wave_write.setcomptype(type, name)
206
+ Set the number of frames to *n *. This will be changed later if the number
207
+ of frames actually written is different (this update attempt will
208
+ raise an error if the output stream is not seekable).
203
209
204
- Set the compression type and description. At the moment, only compression type
205
- ``NONE `` is supported, meaning no compression.
206
210
211
+ .. method :: setcomptype(type, name)
207
212
208
- .. method :: Wave_write.setparams(tuple)
213
+ Set the compression type and description. At the moment, only compression type
214
+ ``NONE `` is supported, meaning no compression.
209
215
210
- The *tuple * should be ``(nchannels, sampwidth, framerate, nframes, comptype,
211
- compname) ``, with values valid for the :meth: `set\* ` methods. Sets all
212
- parameters.
213
216
217
+ .. method :: setparams(tuple)
214
218
215
- .. method :: Wave_write.tell()
219
+ The *tuple * should be ``(nchannels, sampwidth, framerate, nframes, comptype,
220
+ compname) ``, with values valid for the ``set*() `` methods. Sets all
221
+ parameters.
216
222
217
- Return current position in the file, with the same disclaimer for the
218
- :meth: `Wave_read.tell ` and :meth: `Wave_read.setpos ` methods.
219
223
224
+ .. method :: tell()
220
225
221
- .. method :: Wave_write.writeframesraw(data)
226
+ Return current position in the file, with the same disclaimer for the
227
+ :meth: `Wave_read.tell ` and :meth: `Wave_read.setpos ` methods.
222
228
223
- Write audio frames, without correcting *nframes *.
224
229
225
- .. versionchanged :: 3.4
226
- Any :term: `bytes-like object ` is now accepted.
230
+ .. method :: writeframesraw(data)
227
231
232
+ Write audio frames, without correcting *nframes *.
228
233
229
- .. method :: Wave_write.writeframes(data)
234
+ .. versionchanged :: 3.4
235
+ Any :term: `bytes-like object ` is now accepted.
230
236
231
- Write audio frames and make sure *nframes * is correct. It will raise an
232
- error if the output stream is not seekable and the total number of frames
233
- that have been written after *data * has been written does not match the
234
- previously set value for *nframes *.
235
237
236
- .. versionchanged :: 3.4
237
- Any :term: `bytes-like object ` is now accepted.
238
+ .. method :: writeframes(data)
238
239
240
+ Write audio frames and make sure *nframes * is correct. It will raise an
241
+ error if the output stream is not seekable and the total number of frames
242
+ that have been written after *data * has been written does not match the
243
+ previously set value for *nframes *.
239
244
240
- Note that it is invalid to set any parameters after calling :meth: `writeframes `
241
- or :meth: `writeframesraw `, and any attempt to do so will raise
242
- :exc: `wave.Error `.
245
+ .. versionchanged :: 3.4
246
+ Any :term: `bytes-like object ` is now accepted.
243
247
248
+ Note that it is invalid to set any parameters after calling :meth: `writeframes `
249
+ or :meth: `writeframesraw `, and any attempt to do so will raise
250
+ :exc: `wave.Error `.
0 commit comments