blob: a9b3205322d7d29d640b726b4ecabdc25a994112 [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
43 the :keyword:`with` block completes, the :meth:`Wave_read.close()
44 <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
50.. function:: openfp(file, mode)
51
Georg Brandl502d9a52009-07-26 15:02:41 +000052 A synonym for :func:`.open`, maintained for backwards compatibility.
Georg Brandl116aa622007-08-15 14:28:22 +000053
54
55.. exception:: Error
56
57 An error raised when something is impossible because it violates the WAV
58 specification or hits an implementation deficiency.
59
60
61.. _wave-read-objects:
62
63Wave_read Objects
64-----------------
65
Georg Brandl502d9a52009-07-26 15:02:41 +000066Wave_read objects, as returned by :func:`.open`, have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000067
68
69.. method:: Wave_read.close()
70
Georg Brandld97b7b52011-01-08 09:45:43 +000071 Close the stream if it was opened by :mod:`wave`, and make the instance
72 unusable. This is called automatically on object collection.
Georg Brandl116aa622007-08-15 14:28:22 +000073
74
75.. method:: Wave_read.getnchannels()
76
77 Returns number of audio channels (``1`` for mono, ``2`` for stereo).
78
79
80.. method:: Wave_read.getsampwidth()
81
82 Returns sample width in bytes.
83
84
85.. method:: Wave_read.getframerate()
86
87 Returns sampling frequency.
88
89
90.. method:: Wave_read.getnframes()
91
92 Returns number of audio frames.
93
94
95.. method:: Wave_read.getcomptype()
96
97 Returns compression type (``'NONE'`` is the only supported type).
98
99
100.. method:: Wave_read.getcompname()
101
102 Human-readable version of :meth:`getcomptype`. Usually ``'not compressed'``
103 parallels ``'NONE'``.
104
105
106.. method:: Wave_read.getparams()
107
R David Murray671cd322013-04-10 12:31:43 -0400108 Returns a :func:`~collections.namedtuple` ``(nchannels, sampwidth,
109 framerate, nframes, comptype, compname)``, equivalent to output of the
110 :meth:`get\*` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112
113.. method:: Wave_read.readframes(n)
114
R David Murray48de2822016-08-23 20:43:56 -0400115 Reads and returns at most *n* frames of audio, as a :class:`bytes` object.
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117
118.. method:: Wave_read.rewind()
119
120 Rewind the file pointer to the beginning of the audio stream.
121
122The following two methods are defined for compatibility with the :mod:`aifc`
123module, and don't do anything interesting.
124
125
126.. method:: Wave_read.getmarkers()
127
128 Returns ``None``.
129
130
131.. method:: Wave_read.getmark(id)
132
133 Raise an error.
134
135The following two methods define a term "position" which is compatible between
136them, and is otherwise implementation dependent.
137
138
139.. method:: Wave_read.setpos(pos)
140
141 Set the file pointer to the specified position.
142
143
144.. method:: Wave_read.tell()
145
146 Return current file pointer position.
147
148
149.. _wave-write-objects:
150
151Wave_write Objects
152------------------
153
R David Murray5d6240e2014-03-08 11:14:29 -0500154For seekable output streams, the ``wave`` header will automatically be updated
155to 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
157accurate *nframes* value can be achieved either by calling
158:meth:`~Wave_write.setnframes` or :meth:`~Wave_write.setparams` with the number
159of frames that will be written before :meth:`~Wave_write.close` is called and
160then using :meth:`~Wave_write.writeframesraw` to write the frame data, or by
161calling :meth:`~Wave_write.writeframes` with all of the frame data to be
162written. In the latter case :meth:`~Wave_write.writeframes` will calculate
163the number of frames in the data and set *nframes* accordingly before writing
164the frame data.
165
Georg Brandl502d9a52009-07-26 15:02:41 +0000166Wave_write objects, as returned by :func:`.open`, have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000167
R David Murray5d6240e2014-03-08 11:14:29 -0500168.. versionchanged:: 3.4
169 Added support for unseekable files.
170
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172.. method:: Wave_write.close()
173
Georg Brandld97b7b52011-01-08 09:45:43 +0000174 Make sure *nframes* is correct, and close the file if it was opened by
R David Murray5d6240e2014-03-08 11:14:29 -0500175 :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.
Georg Brandl116aa622007-08-15 14:28:22 +0000178
179
180.. method:: Wave_write.setnchannels(n)
181
182 Set the number of channels.
183
184
185.. method:: Wave_write.setsampwidth(n)
186
187 Set the sample width to *n* bytes.
188
189
190.. method:: Wave_write.setframerate(n)
191
192 Set the frame rate to *n*.
193
Mark Dickinson64a38c02010-08-28 17:22:16 +0000194 .. versionchanged:: 3.2
195 A non-integral input to this method is rounded to the nearest
196 integer.
197
Georg Brandl116aa622007-08-15 14:28:22 +0000198
199.. method:: Wave_write.setnframes(n)
200
R David Murray5d6240e2014-03-08 11:14:29 -0500201 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).
Georg Brandl116aa622007-08-15 14:28:22 +0000204
205
206.. method:: Wave_write.setcomptype(type, name)
207
208 Set the compression type and description. At the moment, only compression type
209 ``NONE`` is supported, meaning no compression.
210
211
212.. method:: Wave_write.setparams(tuple)
213
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
218
219.. method:: Wave_write.tell()
220
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
224
225.. method:: Wave_write.writeframesraw(data)
226
227 Write audio frames, without correcting *nframes*.
228
Serhiy Storchaka452bab42013-11-16 14:01:31 +0200229 .. versionchanged:: 3.4
R David Murraycbf479a2014-03-08 11:46:05 -0500230 Any :term:`bytes-like object` is now accepted.
Serhiy Storchaka452bab42013-11-16 14:01:31 +0200231
Georg Brandl116aa622007-08-15 14:28:22 +0000232
233.. method:: Wave_write.writeframes(data)
234
R David Murray5d6240e2014-03-08 11:14:29 -0500235 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*.
Georg Brandl116aa622007-08-15 14:28:22 +0000239
Serhiy Storchaka452bab42013-11-16 14:01:31 +0200240 .. versionchanged:: 3.4
R David Murraycbf479a2014-03-08 11:46:05 -0500241 Any :term:`bytes-like object` is now accepted.
Serhiy Storchaka452bab42013-11-16 14:01:31 +0200242
Georg Brandld97b7b52011-01-08 09:45:43 +0000243
Georg Brandl116aa622007-08-15 14:28:22 +0000244Note that it is invalid to set any parameters after calling :meth:`writeframes`
245or :meth:`writeframesraw`, and any attempt to do so will raise
246:exc:`wave.Error`.
247