blob: 2e64d00305d800440372892eefd82b9d16a02321 [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
22 seekable file-like object. *mode* can be any of
Georg Brandl116aa622007-08-15 14:28:22 +000023
24 ``'r'``, ``'rb'``
25 Read only mode.
26
27 ``'w'``, ``'wb'``
28 Write only mode.
29
30 Note that it does not allow read/write WAV files.
31
32 A *mode* of ``'r'`` or ``'rb'`` returns a :class:`Wave_read` object, while a
Georg Brandld97b7b52011-01-08 09:45:43 +000033 *mode* of ``'w'`` or ``'wb'`` returns a :class:`Wave_write` object. If
34 *mode* is omitted and a file-like object is passed as *file*, ``file.mode``
35 is used as the default value for *mode* (the ``'b'`` flag is still added if
36 necessary).
37
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
42
43.. function:: openfp(file, mode)
44
Georg Brandl502d9a52009-07-26 15:02:41 +000045 A synonym for :func:`.open`, maintained for backwards compatibility.
Georg Brandl116aa622007-08-15 14:28:22 +000046
47
48.. exception:: Error
49
50 An error raised when something is impossible because it violates the WAV
51 specification or hits an implementation deficiency.
52
53
54.. _wave-read-objects:
55
56Wave_read Objects
57-----------------
58
Georg Brandl502d9a52009-07-26 15:02:41 +000059Wave_read objects, as returned by :func:`.open`, have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000060
61
62.. method:: Wave_read.close()
63
Georg Brandld97b7b52011-01-08 09:45:43 +000064 Close the stream if it was opened by :mod:`wave`, and make the instance
65 unusable. This is called automatically on object collection.
Georg Brandl116aa622007-08-15 14:28:22 +000066
67
68.. method:: Wave_read.getnchannels()
69
70 Returns number of audio channels (``1`` for mono, ``2`` for stereo).
71
72
73.. method:: Wave_read.getsampwidth()
74
75 Returns sample width in bytes.
76
77
78.. method:: Wave_read.getframerate()
79
80 Returns sampling frequency.
81
82
83.. method:: Wave_read.getnframes()
84
85 Returns number of audio frames.
86
87
88.. method:: Wave_read.getcomptype()
89
90 Returns compression type (``'NONE'`` is the only supported type).
91
92
93.. method:: Wave_read.getcompname()
94
95 Human-readable version of :meth:`getcomptype`. Usually ``'not compressed'``
96 parallels ``'NONE'``.
97
98
99.. method:: Wave_read.getparams()
100
R David Murray671cd322013-04-10 12:31:43 -0400101 Returns a :func:`~collections.namedtuple` ``(nchannels, sampwidth,
102 framerate, nframes, comptype, compname)``, equivalent to output of the
103 :meth:`get\*` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105
106.. method:: Wave_read.readframes(n)
107
108 Reads and returns at most *n* frames of audio, as a string of bytes.
109
110
111.. method:: Wave_read.rewind()
112
113 Rewind the file pointer to the beginning of the audio stream.
114
115The following two methods are defined for compatibility with the :mod:`aifc`
116module, and don't do anything interesting.
117
118
119.. method:: Wave_read.getmarkers()
120
121 Returns ``None``.
122
123
124.. method:: Wave_read.getmark(id)
125
126 Raise an error.
127
128The following two methods define a term "position" which is compatible between
129them, and is otherwise implementation dependent.
130
131
132.. method:: Wave_read.setpos(pos)
133
134 Set the file pointer to the specified position.
135
136
137.. method:: Wave_read.tell()
138
139 Return current file pointer position.
140
141
142.. _wave-write-objects:
143
144Wave_write Objects
145------------------
146
Georg Brandl502d9a52009-07-26 15:02:41 +0000147Wave_write objects, as returned by :func:`.open`, have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000148
149
150.. method:: Wave_write.close()
151
Georg Brandld97b7b52011-01-08 09:45:43 +0000152 Make sure *nframes* is correct, and close the file if it was opened by
153 :mod:`wave`. This method is called upon object collection.
Georg Brandl116aa622007-08-15 14:28:22 +0000154
155
156.. method:: Wave_write.setnchannels(n)
157
158 Set the number of channels.
159
160
161.. method:: Wave_write.setsampwidth(n)
162
163 Set the sample width to *n* bytes.
164
165
166.. method:: Wave_write.setframerate(n)
167
168 Set the frame rate to *n*.
169
Mark Dickinson64a38c02010-08-28 17:22:16 +0000170 .. versionchanged:: 3.2
171 A non-integral input to this method is rounded to the nearest
172 integer.
173
Georg Brandl116aa622007-08-15 14:28:22 +0000174
175.. method:: Wave_write.setnframes(n)
176
177 Set the number of frames to *n*. This will be changed later if more frames are
178 written.
179
180
181.. method:: Wave_write.setcomptype(type, name)
182
183 Set the compression type and description. At the moment, only compression type
184 ``NONE`` is supported, meaning no compression.
185
186
187.. method:: Wave_write.setparams(tuple)
188
189 The *tuple* should be ``(nchannels, sampwidth, framerate, nframes, comptype,
190 compname)``, with values valid for the :meth:`set\*` methods. Sets all
191 parameters.
192
193
194.. method:: Wave_write.tell()
195
196 Return current position in the file, with the same disclaimer for the
197 :meth:`Wave_read.tell` and :meth:`Wave_read.setpos` methods.
198
199
200.. method:: Wave_write.writeframesraw(data)
201
202 Write audio frames, without correcting *nframes*.
203
204
205.. method:: Wave_write.writeframes(data)
206
207 Write audio frames and make sure *nframes* is correct.
208
Georg Brandld97b7b52011-01-08 09:45:43 +0000209
Georg Brandl116aa622007-08-15 14:28:22 +0000210Note that it is invalid to set any parameters after calling :meth:`writeframes`
211or :meth:`writeframesraw`, and any attempt to do so will raise
212:exc:`wave.Error`.
213