blob: 9d124550b17153727bedf8c1d187dd4a6dee7c9f [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.
6.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
Christian Heimes5b5e81c2007-12-31 16:14:33 +00007.. Documentations stolen from comments in file.
Georg Brandl116aa622007-08-15 14:28:22 +00008
Raymond Hettinger469271d2011-01-27 20:38:46 +00009**Source code:** :source:`Lib/wave.py`
10
11--------------
12
Georg Brandl116aa622007-08-15 14:28:22 +000013The :mod:`wave` module provides a convenient interface to the WAV sound format.
14It does not support compression/decompression, but it does support mono/stereo.
15
16The :mod:`wave` module defines the following function and exception:
17
18
Georg Brandl7f01a132009-09-16 15:58:14 +000019.. function:: open(file, mode=None)
Georg Brandl116aa622007-08-15 14:28:22 +000020
Georg Brandld97b7b52011-01-08 09:45:43 +000021 If *file* is a string, open the file by that name, otherwise treat it as a
R David Murray536ffe12013-07-31 20:48:26 -040022 seekable file-like object. *mode* can be:
Georg Brandl116aa622007-08-15 14:28:22 +000023
R David Murray536ffe12013-07-31 20:48:26 -040024 ``'rb'``
Georg Brandl116aa622007-08-15 14:28:22 +000025 Read only mode.
26
R David Murray536ffe12013-07-31 20:48:26 -040027 ``'wb'``
Georg Brandl116aa622007-08-15 14:28:22 +000028 Write only mode.
29
30 Note that it does not allow read/write WAV files.
31
R David Murray536ffe12013-07-31 20:48:26 -040032 A *mode* of ``'rb'`` returns a :class:`Wave_read` object, while a *mode* of
33 ``'wb'`` returns a :class:`Wave_write` object. If *mode* is omitted and a
34 file-like object is passed as *file*, ``file.mode`` is used as the default
35 value for *mode*.
Georg Brandld97b7b52011-01-08 09:45:43 +000036
37 If you pass in a file-like object, the wave object will not close it when its
38 :meth:`close` method is called; it is the caller's responsibility to close
39 the file object.
Georg Brandl116aa622007-08-15 14:28:22 +000040
R David Murrayc91d5ee2013-07-31 13:46:08 -040041 The :func:`.open` function may be used in a :keyword:`with` statement. When
42 the :keyword:`with` block completes, the :meth:`Wave_read.close()
43 <wave.Wave_read.close>` or :meth:`Wave_write.close()
44 <wave.Wave_write.close()>` method is called.
45
Georg Brandl116aa622007-08-15 14:28:22 +000046
47.. function:: openfp(file, mode)
48
Georg Brandl502d9a52009-07-26 15:02:41 +000049 A synonym for :func:`.open`, maintained for backwards compatibility.
Georg Brandl116aa622007-08-15 14:28:22 +000050
51
52.. exception:: Error
53
54 An error raised when something is impossible because it violates the WAV
55 specification or hits an implementation deficiency.
56
57
58.. _wave-read-objects:
59
60Wave_read Objects
61-----------------
62
Georg Brandl502d9a52009-07-26 15:02:41 +000063Wave_read objects, as returned by :func:`.open`, have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000064
65
66.. method:: Wave_read.close()
67
Georg Brandld97b7b52011-01-08 09:45:43 +000068 Close the stream if it was opened by :mod:`wave`, and make the instance
69 unusable. This is called automatically on object collection.
Georg Brandl116aa622007-08-15 14:28:22 +000070
71
72.. method:: Wave_read.getnchannels()
73
74 Returns number of audio channels (``1`` for mono, ``2`` for stereo).
75
76
77.. method:: Wave_read.getsampwidth()
78
79 Returns sample width in bytes.
80
81
82.. method:: Wave_read.getframerate()
83
84 Returns sampling frequency.
85
86
87.. method:: Wave_read.getnframes()
88
89 Returns number of audio frames.
90
91
92.. method:: Wave_read.getcomptype()
93
94 Returns compression type (``'NONE'`` is the only supported type).
95
96
97.. method:: Wave_read.getcompname()
98
99 Human-readable version of :meth:`getcomptype`. Usually ``'not compressed'``
100 parallels ``'NONE'``.
101
102
103.. method:: Wave_read.getparams()
104
R David Murray671cd322013-04-10 12:31:43 -0400105 Returns a :func:`~collections.namedtuple` ``(nchannels, sampwidth,
106 framerate, nframes, comptype, compname)``, equivalent to output of the
107 :meth:`get\*` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109
110.. method:: Wave_read.readframes(n)
111
112 Reads and returns at most *n* frames of audio, as a string of bytes.
113
114
115.. method:: Wave_read.rewind()
116
117 Rewind the file pointer to the beginning of the audio stream.
118
119The following two methods are defined for compatibility with the :mod:`aifc`
120module, and don't do anything interesting.
121
122
123.. method:: Wave_read.getmarkers()
124
125 Returns ``None``.
126
127
128.. method:: Wave_read.getmark(id)
129
130 Raise an error.
131
132The following two methods define a term "position" which is compatible between
133them, and is otherwise implementation dependent.
134
135
136.. method:: Wave_read.setpos(pos)
137
138 Set the file pointer to the specified position.
139
140
141.. method:: Wave_read.tell()
142
143 Return current file pointer position.
144
145
146.. _wave-write-objects:
147
148Wave_write Objects
149------------------
150
Georg Brandl502d9a52009-07-26 15:02:41 +0000151Wave_write objects, as returned by :func:`.open`, have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153
154.. method:: Wave_write.close()
155
Georg Brandld97b7b52011-01-08 09:45:43 +0000156 Make sure *nframes* is correct, and close the file if it was opened by
157 :mod:`wave`. This method is called upon object collection.
Georg Brandl116aa622007-08-15 14:28:22 +0000158
159
160.. method:: Wave_write.setnchannels(n)
161
162 Set the number of channels.
163
164
165.. method:: Wave_write.setsampwidth(n)
166
167 Set the sample width to *n* bytes.
168
169
170.. method:: Wave_write.setframerate(n)
171
172 Set the frame rate to *n*.
173
Mark Dickinson64a38c02010-08-28 17:22:16 +0000174 .. versionchanged:: 3.2
175 A non-integral input to this method is rounded to the nearest
176 integer.
177
Georg Brandl116aa622007-08-15 14:28:22 +0000178
179.. method:: Wave_write.setnframes(n)
180
181 Set the number of frames to *n*. This will be changed later if more frames are
182 written.
183
184
185.. method:: Wave_write.setcomptype(type, name)
186
187 Set the compression type and description. At the moment, only compression type
188 ``NONE`` is supported, meaning no compression.
189
190
191.. method:: Wave_write.setparams(tuple)
192
193 The *tuple* should be ``(nchannels, sampwidth, framerate, nframes, comptype,
194 compname)``, with values valid for the :meth:`set\*` methods. Sets all
195 parameters.
196
197
198.. method:: Wave_write.tell()
199
200 Return current position in the file, with the same disclaimer for the
201 :meth:`Wave_read.tell` and :meth:`Wave_read.setpos` methods.
202
203
204.. method:: Wave_write.writeframesraw(data)
205
206 Write audio frames, without correcting *nframes*.
207
208
209.. method:: Wave_write.writeframes(data)
210
211 Write audio frames and make sure *nframes* is correct.
212
Georg Brandld97b7b52011-01-08 09:45:43 +0000213
Georg Brandl116aa622007-08-15 14:28:22 +0000214Note that it is invalid to set any parameters after calling :meth:`writeframes`
215or :meth:`writeframesraw`, and any attempt to do so will raise
216:exc:`wave.Error`.
217