1 :mod:`wave` --- Read and write WAV files
2 ========================================
5 :synopsis: Provide an interface to the WAV sound format.
6 .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
7 .. Documentations stolen from comments in file.
9 The :mod:`wave` module provides a convenient interface to the WAV sound format.
10 It does not support compression/decompression, but it does support mono/stereo.
12 The :mod:`wave` module defines the following function and exception:
15 .. function:: open(file[, mode])
17 If *file* is a string, open the file by that name, other treat it as a seekable
18 file-like object. *mode* can be any of
26 Note that it does not allow read/write WAV files.
28 A *mode* of ``'r'`` or ``'rb'`` returns a :class:`Wave_read` object, while a
29 *mode* of ``'w'`` or ``'wb'`` returns a :class:`Wave_write` object. If *mode*
30 is omitted and a file-like object is passed as *file*, ``file.mode`` is used as
31 the default value for *mode* (the ``'b'`` flag is still added if necessary).
34 .. function:: openfp(file, mode)
36 A synonym for :func:`.open`, maintained for backwards compatibility.
41 An error raised when something is impossible because it violates the WAV
42 specification or hits an implementation deficiency.
45 .. _wave-read-objects:
50 Wave_read objects, as returned by :func:`.open`, have the following methods:
53 .. method:: Wave_read.close()
55 Close the stream, and make the instance unusable. This is called automatically
59 .. method:: Wave_read.getnchannels()
61 Returns number of audio channels (``1`` for mono, ``2`` for stereo).
64 .. method:: Wave_read.getsampwidth()
66 Returns sample width in bytes.
69 .. method:: Wave_read.getframerate()
71 Returns sampling frequency.
74 .. method:: Wave_read.getnframes()
76 Returns number of audio frames.
79 .. method:: Wave_read.getcomptype()
81 Returns compression type (``'NONE'`` is the only supported type).
84 .. method:: Wave_read.getcompname()
86 Human-readable version of :meth:`getcomptype`. Usually ``'not compressed'``
90 .. method:: Wave_read.getparams()
92 Returns a tuple ``(nchannels, sampwidth, framerate, nframes, comptype,
93 compname)``, equivalent to output of the :meth:`get\*` methods.
96 .. method:: Wave_read.readframes(n)
98 Reads and returns at most *n* frames of audio, as a string of bytes.
101 .. method:: Wave_read.rewind()
103 Rewind the file pointer to the beginning of the audio stream.
105 The following two methods are defined for compatibility with the :mod:`aifc`
106 module, and don't do anything interesting.
109 .. method:: Wave_read.getmarkers()
114 .. method:: Wave_read.getmark(id)
118 The following two methods define a term "position" which is compatible between
119 them, and is otherwise implementation dependent.
122 .. method:: Wave_read.setpos(pos)
124 Set the file pointer to the specified position.
127 .. method:: Wave_read.tell()
129 Return current file pointer position.
132 .. _wave-write-objects:
137 Wave_write objects, as returned by :func:`.open`, have the following methods:
140 .. method:: Wave_write.close()
142 Make sure *nframes* is correct, and close the file. This method is called upon
146 .. method:: Wave_write.setnchannels(n)
148 Set the number of channels.
151 .. method:: Wave_write.setsampwidth(n)
153 Set the sample width to *n* bytes.
156 .. method:: Wave_write.setframerate(n)
158 Set the frame rate to *n*.
161 .. method:: Wave_write.setnframes(n)
163 Set the number of frames to *n*. This will be changed later if more frames are
167 .. method:: Wave_write.setcomptype(type, name)
169 Set the compression type and description. At the moment, only compression type
170 ``NONE`` is supported, meaning no compression.
173 .. method:: Wave_write.setparams(tuple)
175 The *tuple* should be ``(nchannels, sampwidth, framerate, nframes, comptype,
176 compname)``, with values valid for the :meth:`set\*` methods. Sets all
180 .. method:: Wave_write.tell()
182 Return current position in the file, with the same disclaimer for the
183 :meth:`Wave_read.tell` and :meth:`Wave_read.setpos` methods.
186 .. method:: Wave_write.writeframesraw(data)
188 Write audio frames, without correcting *nframes*.
191 .. method:: Wave_write.writeframes(data)
193 Write audio frames and make sure *nframes* is correct.
195 Note that it is invalid to set any parameters after calling :meth:`writeframes`
196 or :meth:`writeframesraw`, and any attempt to do so will raise