blob: f63e0d3dce19c6383828cf2bedbc587f88f2888b [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`wave` --- Read and write WAV files
2========================================
3
4.. module:: wave
5 :synopsis: Provide an interface to the WAV sound format.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
Christian Heimes5b5e81c2007-12-31 16:14:33 +00008.. Documentations stolen from comments in file.
Georg Brandl116aa622007-08-15 14:28:22 +00009
Raymond Hettinger469271d2011-01-27 20:38:46 +000010**Source code:** :source:`Lib/wave.py`
11
12--------------
13
Georg Brandl116aa622007-08-15 14:28:22 +000014The :mod:`wave` module provides a convenient interface to the WAV sound format.
15It does not support compression/decompression, but it does support mono/stereo.
16
17The :mod:`wave` module defines the following function and exception:
18
19
Georg Brandl7f01a132009-09-16 15:58:14 +000020.. function:: open(file, mode=None)
Georg Brandl116aa622007-08-15 14:28:22 +000021
Georg Brandld97b7b52011-01-08 09:45:43 +000022 If *file* is a string, open the file by that name, otherwise treat it as a
Serhiy Storchaka7714ebb2013-11-16 13:04:00 +020023 file-like object. *mode* can be:
Georg Brandl116aa622007-08-15 14:28:22 +000024
R David Murray536ffe12013-07-31 20:48:26 -040025 ``'rb'``
Georg Brandl116aa622007-08-15 14:28:22 +000026 Read only mode.
27
R David Murray536ffe12013-07-31 20:48:26 -040028 ``'wb'``
Georg Brandl116aa622007-08-15 14:28:22 +000029 Write only mode.
30
31 Note that it does not allow read/write WAV files.
32
R David Murray536ffe12013-07-31 20:48:26 -040033 A *mode* of ``'rb'`` returns a :class:`Wave_read` object, while a *mode* of
34 ``'wb'`` returns a :class:`Wave_write` object. If *mode* is omitted and a
35 file-like object is passed as *file*, ``file.mode`` is used as the default
36 value for *mode*.
Georg Brandld97b7b52011-01-08 09:45:43 +000037
38 If you pass in a file-like object, the wave object will not close it when its
39 :meth:`close` method is called; it is the caller's responsibility to close
40 the file object.
Georg Brandl116aa622007-08-15 14:28:22 +000041
R David Murrayc91d5ee2013-07-31 13:46:08 -040042 The :func:`.open` function may be used in a :keyword:`with` statement. When
Serhiy Storchaka2b57c432018-12-19 08:09:46 +020043 the :keyword:`!with` block completes, the :meth:`Wave_read.close()
R David Murrayc91d5ee2013-07-31 13:46:08 -040044 <wave.Wave_read.close>` or :meth:`Wave_write.close()
45 <wave.Wave_write.close()>` method is called.
46
Serhiy Storchaka7714ebb2013-11-16 13:04:00 +020047 .. versionchanged:: 3.4
48 Added support for unseekable files.
Georg Brandl116aa622007-08-15 14:28:22 +000049
Georg Brandl116aa622007-08-15 14:28:22 +000050.. exception:: Error
51
52 An error raised when something is impossible because it violates the WAV
53 specification or hits an implementation deficiency.
54
55
56.. _wave-read-objects:
57
58Wave_read Objects
59-----------------
60
Georg Brandl502d9a52009-07-26 15:02:41 +000061Wave_read objects, as returned by :func:`.open`, have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000062
63
64.. method:: Wave_read.close()
65
Georg Brandld97b7b52011-01-08 09:45:43 +000066 Close the stream if it was opened by :mod:`wave`, and make the instance
67 unusable. This is called automatically on object collection.
Georg Brandl116aa622007-08-15 14:28:22 +000068
69
70.. method:: Wave_read.getnchannels()
71
72 Returns number of audio channels (``1`` for mono, ``2`` for stereo).
73
74
75.. method:: Wave_read.getsampwidth()
76
77 Returns sample width in bytes.
78
79
80.. method:: Wave_read.getframerate()
81
82 Returns sampling frequency.
83
84
85.. method:: Wave_read.getnframes()
86
87 Returns number of audio frames.
88
89
90.. method:: Wave_read.getcomptype()
91
92 Returns compression type (``'NONE'`` is the only supported type).
93
94
95.. method:: Wave_read.getcompname()
96
97 Human-readable version of :meth:`getcomptype`. Usually ``'not compressed'``
98 parallels ``'NONE'``.
99
100
101.. method:: Wave_read.getparams()
102
R David Murray671cd322013-04-10 12:31:43 -0400103 Returns a :func:`~collections.namedtuple` ``(nchannels, sampwidth,
104 framerate, nframes, comptype, compname)``, equivalent to output of the
105 :meth:`get\*` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107
108.. method:: Wave_read.readframes(n)
109
R David Murray48de2822016-08-23 20:43:56 -0400110 Reads and returns at most *n* frames of audio, as a :class:`bytes` object.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112
113.. method:: Wave_read.rewind()
114
115 Rewind the file pointer to the beginning of the audio stream.
116
117The following two methods are defined for compatibility with the :mod:`aifc`
118module, and don't do anything interesting.
119
120
121.. method:: Wave_read.getmarkers()
122
123 Returns ``None``.
124
125
126.. method:: Wave_read.getmark(id)
127
128 Raise an error.
129
130The following two methods define a term "position" which is compatible between
131them, and is otherwise implementation dependent.
132
133
134.. method:: Wave_read.setpos(pos)
135
136 Set the file pointer to the specified position.
137
138
139.. method:: Wave_read.tell()
140
141 Return current file pointer position.
142
143
144.. _wave-write-objects:
145
146Wave_write Objects
147------------------
148
R David Murray5d6240e2014-03-08 11:14:29 -0500149For seekable output streams, the ``wave`` header will automatically be updated
150to reflect the number of frames actually written. For unseekable streams, the
151*nframes* value must be accurate when the first frame data is written. An
152accurate *nframes* value can be achieved either by calling
153:meth:`~Wave_write.setnframes` or :meth:`~Wave_write.setparams` with the number
154of frames that will be written before :meth:`~Wave_write.close` is called and
155then using :meth:`~Wave_write.writeframesraw` to write the frame data, or by
156calling :meth:`~Wave_write.writeframes` with all of the frame data to be
157written. In the latter case :meth:`~Wave_write.writeframes` will calculate
158the number of frames in the data and set *nframes* accordingly before writing
159the frame data.
160
Georg Brandl502d9a52009-07-26 15:02:41 +0000161Wave_write objects, as returned by :func:`.open`, have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000162
R David Murray5d6240e2014-03-08 11:14:29 -0500163.. versionchanged:: 3.4
164 Added support for unseekable files.
165
Georg Brandl116aa622007-08-15 14:28:22 +0000166
167.. method:: Wave_write.close()
168
Georg Brandld97b7b52011-01-08 09:45:43 +0000169 Make sure *nframes* is correct, and close the file if it was opened by
R David Murray5d6240e2014-03-08 11:14:29 -0500170 :mod:`wave`. This method is called upon object collection. It will raise
171 an exception if the output stream is not seekable and *nframes* does not
172 match the number of frames actually written.
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174
175.. method:: Wave_write.setnchannels(n)
176
177 Set the number of channels.
178
179
180.. method:: Wave_write.setsampwidth(n)
181
182 Set the sample width to *n* bytes.
183
184
185.. method:: Wave_write.setframerate(n)
186
187 Set the frame rate to *n*.
188
Mark Dickinson64a38c02010-08-28 17:22:16 +0000189 .. versionchanged:: 3.2
190 A non-integral input to this method is rounded to the nearest
191 integer.
192
Georg Brandl116aa622007-08-15 14:28:22 +0000193
194.. method:: Wave_write.setnframes(n)
195
R David Murray5d6240e2014-03-08 11:14:29 -0500196 Set the number of frames to *n*. This will be changed later if the number
197 of frames actually written is different (this update attempt will
198 raise an error if the output stream is not seekable).
Georg Brandl116aa622007-08-15 14:28:22 +0000199
200
201.. method:: Wave_write.setcomptype(type, name)
202
203 Set the compression type and description. At the moment, only compression type
204 ``NONE`` is supported, meaning no compression.
205
206
207.. method:: Wave_write.setparams(tuple)
208
209 The *tuple* should be ``(nchannels, sampwidth, framerate, nframes, comptype,
210 compname)``, with values valid for the :meth:`set\*` methods. Sets all
211 parameters.
212
213
214.. method:: Wave_write.tell()
215
216 Return current position in the file, with the same disclaimer for the
217 :meth:`Wave_read.tell` and :meth:`Wave_read.setpos` methods.
218
219
220.. method:: Wave_write.writeframesraw(data)
221
222 Write audio frames, without correcting *nframes*.
223
Serhiy Storchaka452bab42013-11-16 14:01:31 +0200224 .. versionchanged:: 3.4
R David Murraycbf479a2014-03-08 11:46:05 -0500225 Any :term:`bytes-like object` is now accepted.
Serhiy Storchaka452bab42013-11-16 14:01:31 +0200226
Georg Brandl116aa622007-08-15 14:28:22 +0000227
228.. method:: Wave_write.writeframes(data)
229
R David Murray5d6240e2014-03-08 11:14:29 -0500230 Write audio frames and make sure *nframes* is correct. It will raise an
231 error if the output stream is not seekable and the total number of frames
232 that have been written after *data* has been written does not match the
233 previously set value for *nframes*.
Georg Brandl116aa622007-08-15 14:28:22 +0000234
Serhiy Storchaka452bab42013-11-16 14:01:31 +0200235 .. versionchanged:: 3.4
R David Murraycbf479a2014-03-08 11:46:05 -0500236 Any :term:`bytes-like object` is now accepted.
Serhiy Storchaka452bab42013-11-16 14:01:31 +0200237
Georg Brandld97b7b52011-01-08 09:45:43 +0000238
Georg Brandl116aa622007-08-15 14:28:22 +0000239Note that it is invalid to set any parameters after calling :meth:`writeframes`
240or :meth:`writeframesraw`, and any attempt to do so will raise
241:exc:`wave.Error`.
242