wave.rst 6.74 KB
Newer Older
1 2 3 4 5
:mod:`wave` --- Read and write WAV files
========================================

.. module:: wave
   :synopsis: Provide an interface to the WAV sound format.
6

7
.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
8
.. Documentations stolen from comments in file.
9

Raymond Hettinger's avatar
Raymond Hettinger committed
10 11 12 13
**Source code:** :source:`Lib/wave.py`

--------------

14 15 16 17 18 19
The :mod:`wave` module provides a convenient interface to the WAV sound format.
It does not support compression/decompression, but it does support mono/stereo.

The :mod:`wave` module defines the following function and exception:


20
.. function:: open(file, mode=None)
21

22
   If *file* is a string, open the file by that name, otherwise treat it as a
23
   file-like object.  *mode* can be:
24

25
   ``'rb'``
26 27
      Read only mode.

28
   ``'wb'``
29 30 31 32
      Write only mode.

   Note that it does not allow read/write WAV files.

33 34 35 36
   A *mode* of ``'rb'`` returns a :class:`Wave_read` object, while a *mode* of
   ``'wb'`` returns a :class:`Wave_write` object.  If *mode* is omitted and a
   file-like object is passed as *file*, ``file.mode`` is used as the default
   value for *mode*.
37 38 39 40

   If you pass in a file-like object, the wave object will not close it when its
   :meth:`close` method is called; it is the caller's responsibility to close
   the file object.
41

42 43 44 45 46
   The :func:`.open` function may be used in a :keyword:`with` statement.  When
   the :keyword:`with` block completes, the :meth:`Wave_read.close()
   <wave.Wave_read.close>` or :meth:`Wave_write.close()
   <wave.Wave_write.close()>` method is called.

47 48
   .. versionchanged:: 3.4
      Added support for unseekable files.
49 50 51

.. function:: openfp(file, mode)

52
   A synonym for :func:`.open`, maintained for backwards compatibility.
53

54 55
   .. deprecated-removed:: 3.7 3.9

56 57 58 59 60 61 62 63 64 65 66 67

.. exception:: Error

   An error raised when something is impossible because it violates the WAV
   specification or hits an implementation deficiency.


.. _wave-read-objects:

Wave_read Objects
-----------------

68
Wave_read objects, as returned by :func:`.open`, have the following methods:
69 70 71 72


.. method:: Wave_read.close()

73 74
   Close the stream if it was opened by :mod:`wave`, and make the instance
   unusable.  This is called automatically on object collection.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109


.. method:: Wave_read.getnchannels()

   Returns number of audio channels (``1`` for mono, ``2`` for stereo).


.. method:: Wave_read.getsampwidth()

   Returns sample width in bytes.


.. method:: Wave_read.getframerate()

   Returns sampling frequency.


.. method:: Wave_read.getnframes()

   Returns number of audio frames.


.. method:: Wave_read.getcomptype()

   Returns compression type (``'NONE'`` is the only supported type).


.. method:: Wave_read.getcompname()

   Human-readable version of :meth:`getcomptype`. Usually ``'not compressed'``
   parallels ``'NONE'``.


.. method:: Wave_read.getparams()

110 111 112
   Returns a :func:`~collections.namedtuple` ``(nchannels, sampwidth,
   framerate, nframes, comptype, compname)``, equivalent to output of the
   :meth:`get\*` methods.
113 114 115 116


.. method:: Wave_read.readframes(n)

117
   Reads and returns at most *n* frames of audio, as a :class:`bytes` object.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155


.. method:: Wave_read.rewind()

   Rewind the file pointer to the beginning of the audio stream.

The following two methods are defined for compatibility with the :mod:`aifc`
module, and don't do anything interesting.


.. method:: Wave_read.getmarkers()

   Returns ``None``.


.. method:: Wave_read.getmark(id)

   Raise an error.

The following two methods define a term "position" which is compatible between
them, and is otherwise implementation dependent.


.. method:: Wave_read.setpos(pos)

   Set the file pointer to the specified position.


.. method:: Wave_read.tell()

   Return current file pointer position.


.. _wave-write-objects:

Wave_write Objects
------------------

156 157 158 159 160 161 162 163 164 165 166 167
For seekable output streams, the ``wave`` header will automatically be updated
to reflect the number of frames actually written.  For unseekable streams, the
*nframes* value must be accurate when the first frame data is written.  An
accurate *nframes* value can be achieved either by calling
:meth:`~Wave_write.setnframes` or :meth:`~Wave_write.setparams` with the number
of frames that will be written before :meth:`~Wave_write.close` is called and
then using :meth:`~Wave_write.writeframesraw` to write the frame data, or by
calling :meth:`~Wave_write.writeframes` with all of the frame data to be
written.  In the latter case :meth:`~Wave_write.writeframes` will calculate
the number of frames in the data and set *nframes* accordingly before writing
the frame data.

168
Wave_write objects, as returned by :func:`.open`, have the following methods:
169

170 171 172
.. versionchanged:: 3.4
   Added support for unseekable files.

173 174 175

.. method:: Wave_write.close()

176
   Make sure *nframes* is correct, and close the file if it was opened by
177 178 179
   :mod:`wave`.  This method is called upon object collection.  It will raise
   an exception if the output stream is not seekable and *nframes* does not
   match the number of frames actually written.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195


.. method:: Wave_write.setnchannels(n)

   Set the number of channels.


.. method:: Wave_write.setsampwidth(n)

   Set the sample width to *n* bytes.


.. method:: Wave_write.setframerate(n)

   Set the frame rate to *n*.

196 197 198 199
   .. versionchanged:: 3.2
      A non-integral input to this method is rounded to the nearest
      integer.

200 201 202

.. method:: Wave_write.setnframes(n)

203 204 205
   Set the number of frames to *n*.  This will be changed later if the number
   of frames actually written is different (this update attempt will
   raise an error if the output stream is not seekable).
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230


.. method:: Wave_write.setcomptype(type, name)

   Set the compression type and description. At the moment, only compression type
   ``NONE`` is supported, meaning no compression.


.. method:: Wave_write.setparams(tuple)

   The *tuple* should be ``(nchannels, sampwidth, framerate, nframes, comptype,
   compname)``, with values valid for the :meth:`set\*` methods.  Sets all
   parameters.


.. method:: Wave_write.tell()

   Return current position in the file, with the same disclaimer for the
   :meth:`Wave_read.tell` and :meth:`Wave_read.setpos` methods.


.. method:: Wave_write.writeframesraw(data)

   Write audio frames, without correcting *nframes*.

231
   .. versionchanged:: 3.4
232
      Any :term:`bytes-like object` is now accepted.
233

234 235 236

.. method:: Wave_write.writeframes(data)

237 238 239 240
   Write audio frames and make sure *nframes* is correct.  It will raise an
   error if the output stream is not seekable and the total number of frames
   that have been written after *data* has been written does not match the
   previously set value for *nframes*.
241

242
   .. versionchanged:: 3.4
243
      Any :term:`bytes-like object` is now accepted.
244

245

246 247 248 249
Note that it is invalid to set any parameters after calling :meth:`writeframes`
or :meth:`writeframesraw`, and any attempt to do so will raise
:exc:`wave.Error`.