1:mod:`wave` --- Read and write WAV files 2======================================== 3 4.. module:: wave 5 :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 10**Source code:** :source:`Lib/wave.py` 11 12-------------- 13 14The :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 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 Read only mode. 27 28 ``'wb'`` 29 Write only mode. 30 31 Note that it does not allow read/write WAV files. 32 33 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*. 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. 41 42 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 47 .. versionchanged:: 3.4 48 Added support for unseekable files. 49 50.. exception:: Error 51 52 An error raised when something is impossible because it violates the WAV 53 specification or hits an implementation deficiency. 54 55 56.. _wave-read-objects: 57 58Wave_read Objects 59----------------- 60 61Wave_read objects, as returned by :func:`.open`, have the following methods: 62 63 64.. method:: Wave_read.close() 65 66 Close the stream if it was opened by :mod:`wave`, and make the instance 67 unusable. This is called automatically on object collection. 68 69 70.. method:: Wave_read.getnchannels() 71 72 Returns number of audio channels (``1`` for mono, ``2`` for stereo). 73 74 75.. method:: Wave_read.getsampwidth() 76 77 Returns sample width in bytes. 78 79 80.. method:: Wave_read.getframerate() 81 82 Returns sampling frequency. 83 84 85.. method:: Wave_read.getnframes() 86 87 Returns number of audio frames. 88 89 90.. method:: Wave_read.getcomptype() 91 92 Returns compression type (``'NONE'`` is the only supported type). 93 94 95.. method:: Wave_read.getcompname() 96 97 Human-readable version of :meth:`getcomptype`. Usually ``'not compressed'`` 98 parallels ``'NONE'``. 99 100 101.. method:: Wave_read.getparams() 102 103 Returns a :func:`~collections.namedtuple` ``(nchannels, sampwidth, 104 framerate, nframes, comptype, compname)``, equivalent to output of the 105 :meth:`get\*` methods. 106 107 108.. method:: Wave_read.readframes(n) 109 110 Reads and returns at most *n* frames of audio, as a :class:`bytes` object. 111 112 113.. method:: Wave_read.rewind() 114 115 Rewind the file pointer to the beginning of the audio stream. 116 117The following two methods are defined for compatibility with the :mod:`aifc` 118module, and don't do anything interesting. 119 120 121.. method:: Wave_read.getmarkers() 122 123 Returns ``None``. 124 125 126.. method:: Wave_read.getmark(id) 127 128 Raise an error. 129 130The following two methods define a term "position" which is compatible between 131them, and is otherwise implementation dependent. 132 133 134.. method:: Wave_read.setpos(pos) 135 136 Set the file pointer to the specified position. 137 138 139.. method:: Wave_read.tell() 140 141 Return current file pointer position. 142 143 144.. _wave-write-objects: 145 146Wave_write Objects 147------------------ 148 149For seekable output streams, the ``wave`` header will automatically be updated 150to reflect the number of frames actually written. For unseekable streams, the 151*nframes* value must be accurate when the first frame data is written. An 152accurate *nframes* value can be achieved either by calling 153:meth:`~Wave_write.setnframes` or :meth:`~Wave_write.setparams` with the number 154of frames that will be written before :meth:`~Wave_write.close` is called and 155then using :meth:`~Wave_write.writeframesraw` to write the frame data, or by 156calling :meth:`~Wave_write.writeframes` with all of the frame data to be 157written. In the latter case :meth:`~Wave_write.writeframes` will calculate 158the number of frames in the data and set *nframes* accordingly before writing 159the frame data. 160 161Wave_write objects, as returned by :func:`.open`, have the following methods: 162 163.. versionchanged:: 3.4 164 Added support for unseekable files. 165 166 167.. method:: Wave_write.close() 168 169 Make sure *nframes* is correct, and close the file if it was opened by 170 :mod:`wave`. This method is called upon object collection. It will raise 171 an exception if the output stream is not seekable and *nframes* does not 172 match the number of frames actually written. 173 174 175.. method:: Wave_write.setnchannels(n) 176 177 Set the number of channels. 178 179 180.. method:: Wave_write.setsampwidth(n) 181 182 Set the sample width to *n* bytes. 183 184 185.. method:: Wave_write.setframerate(n) 186 187 Set the frame rate to *n*. 188 189 .. versionchanged:: 3.2 190 A non-integral input to this method is rounded to the nearest 191 integer. 192 193 194.. method:: Wave_write.setnframes(n) 195 196 Set the number of frames to *n*. This will be changed later if the number 197 of frames actually written is different (this update attempt will 198 raise an error if the output stream is not seekable). 199 200 201.. method:: Wave_write.setcomptype(type, name) 202 203 Set the compression type and description. At the moment, only compression type 204 ``NONE`` is supported, meaning no compression. 205 206 207.. method:: Wave_write.setparams(tuple) 208 209 The *tuple* should be ``(nchannels, sampwidth, framerate, nframes, comptype, 210 compname)``, with values valid for the :meth:`set\*` methods. Sets all 211 parameters. 212 213 214.. method:: Wave_write.tell() 215 216 Return current position in the file, with the same disclaimer for the 217 :meth:`Wave_read.tell` and :meth:`Wave_read.setpos` methods. 218 219 220.. method:: Wave_write.writeframesraw(data) 221 222 Write audio frames, without correcting *nframes*. 223 224 .. versionchanged:: 3.4 225 Any :term:`bytes-like object` is now accepted. 226 227 228.. method:: Wave_write.writeframes(data) 229 230 Write audio frames and make sure *nframes* is correct. It will raise an 231 error if the output stream is not seekable and the total number of frames 232 that have been written after *data* has been written does not match the 233 previously set value for *nframes*. 234 235 .. versionchanged:: 3.4 236 Any :term:`bytes-like object` is now accepted. 237 238 239Note that it is invalid to set any parameters after calling :meth:`writeframes` 240or :meth:`writeframesraw`, and any attempt to do so will raise 241:exc:`wave.Error`. 242 243