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