2007-08-15 11:28:22 -03:00
|
|
|
:mod:`wave` --- Read and write WAV files
|
|
|
|
========================================
|
|
|
|
|
|
|
|
.. module:: wave
|
|
|
|
:synopsis: Provide an interface to the WAV sound format.
|
2016-06-11 16:02:54 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
|
Merged revisions 59605-59624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59606 | georg.brandl | 2007-12-29 11:57:00 +0100 (Sat, 29 Dec 2007) | 2 lines
Some cleanup in the docs.
........
r59611 | martin.v.loewis | 2007-12-29 19:49:21 +0100 (Sat, 29 Dec 2007) | 2 lines
Bug #1699: Define _BSD_SOURCE only on OpenBSD.
........
r59612 | raymond.hettinger | 2007-12-29 23:09:34 +0100 (Sat, 29 Dec 2007) | 1 line
Simpler documentation for itertools.tee(). Should be backported.
........
r59613 | raymond.hettinger | 2007-12-29 23:16:24 +0100 (Sat, 29 Dec 2007) | 1 line
Improve docs for itertools.groupby(). The use of xrange(0) to create a unique object is less obvious than object().
........
r59620 | christian.heimes | 2007-12-31 15:47:07 +0100 (Mon, 31 Dec 2007) | 3 lines
Added wininst-9.0.exe executable for VS 2008
Integrated bdist_wininst into PCBuild9 directory
........
r59621 | christian.heimes | 2007-12-31 15:51:18 +0100 (Mon, 31 Dec 2007) | 1 line
Moved PCbuild directory to PC/VS7.1
........
r59622 | christian.heimes | 2007-12-31 15:59:26 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot
........
r59623 | christian.heimes | 2007-12-31 16:02:41 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot, part 2
........
r59624 | christian.heimes | 2007-12-31 16:18:55 +0100 (Mon, 31 Dec 2007) | 1 line
Renamed PCBuild9 directory to PCBuild
........
2007-12-31 12:14:33 -04:00
|
|
|
.. Documentations stolen from comments in file.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-01-27 16:38:46 -04:00
|
|
|
**Source code:** :source:`Lib/wave.py`
|
|
|
|
|
|
|
|
--------------
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
The :mod:`wave` module provides a convenient interface to the Waveform Audio
|
|
|
|
"WAVE" (or "WAV") file format. Only uncompressed PCM encoded wave files are
|
|
|
|
supported.
|
2022-09-23 12:08:21 -03:00
|
|
|
|
|
|
|
.. versionchanged:: 3.12
|
|
|
|
|
|
|
|
Support for ``WAVE_FORMAT_EXTENSIBLE`` headers was added, provided that the
|
|
|
|
extended format is ``KSDATAFORMAT_SUBTYPE_PCM``.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
The :mod:`wave` module defines the following function and exception:
|
|
|
|
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. function:: open(file, mode=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-01-08 05:45:43 -04:00
|
|
|
If *file* is a string, open the file by that name, otherwise treat it as a
|
2013-11-16 07:04:00 -04:00
|
|
|
file-like object. *mode* can be:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-07-31 21:48:26 -03:00
|
|
|
``'rb'``
|
2007-08-15 11:28:22 -03:00
|
|
|
Read only mode.
|
|
|
|
|
2013-07-31 21:48:26 -03:00
|
|
|
``'wb'``
|
2007-08-15 11:28:22 -03:00
|
|
|
Write only mode.
|
|
|
|
|
|
|
|
Note that it does not allow read/write WAV files.
|
|
|
|
|
2013-07-31 21:48:26 -03:00
|
|
|
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*.
|
2011-01-08 05:45:43 -04:00
|
|
|
|
|
|
|
If you pass in a file-like object, the wave object will not close it when its
|
2023-05-31 08:29:10 -03:00
|
|
|
``close()`` method is called; it is the caller's responsibility to close
|
2011-01-08 05:45:43 -04:00
|
|
|
the file object.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-07-31 14:46:08 -03:00
|
|
|
The :func:`.open` function may be used in a :keyword:`with` statement. When
|
2023-05-31 08:29:10 -03:00
|
|
|
the :keyword:`!with` block completes, the :meth:`Wave_read.close()` or
|
|
|
|
:meth:`Wave_write.close()` method is called.
|
2013-07-31 14:46:08 -03:00
|
|
|
|
2013-11-16 07:04:00 -04:00
|
|
|
.. versionchanged:: 3.4
|
|
|
|
Added support for unseekable files.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. 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
|
|
|
|
-----------------
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. class:: Wave_read
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Read a WAV file.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Wave_read objects, as returned by :func:`.open`, have the following methods:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: close()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Close the stream if it was opened by :mod:`wave`, and make the instance
|
|
|
|
unusable. This is called automatically on object collection.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: getnchannels()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Returns number of audio channels (``1`` for mono, ``2`` for stereo).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: getsampwidth()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Returns sample width in bytes.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: getframerate()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Returns sampling frequency.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: getnframes()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Returns number of audio frames.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: getcomptype()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Returns compression type (``'NONE'`` is the only supported type).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: getcompname()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Human-readable version of :meth:`getcomptype`. Usually ``'not compressed'``
|
|
|
|
parallels ``'NONE'``.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: getparams()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Returns a :func:`~collections.namedtuple` ``(nchannels, sampwidth,
|
|
|
|
framerate, nframes, comptype, compname)``, equivalent to output of the
|
|
|
|
``get*()`` methods.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: readframes(n)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Reads and returns at most *n* frames of audio, as a :class:`bytes` object.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: rewind()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Rewind the file pointer to the beginning of the audio stream.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
The following two methods are defined for compatibility with the old :mod:`!aifc`
|
|
|
|
module, and don't do anything interesting.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: getmarkers()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Returns ``None``.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 09:09:41 -03:00
|
|
|
.. deprecated-removed:: 3.13 3.15
|
|
|
|
The method only existed for compatibility with the :mod:`!aifc` module
|
|
|
|
which has been removed in Python 3.13.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: getmark(id)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Raise an error.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 09:09:41 -03:00
|
|
|
.. deprecated-removed:: 3.13 3.15
|
|
|
|
The method only existed for compatibility with the :mod:`!aifc` module
|
|
|
|
which has been removed in Python 3.13.
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
The following two methods define a term "position" which is compatible between
|
|
|
|
them, and is otherwise implementation dependent.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: setpos(pos)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Set the file pointer to the specified position.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
|
|
|
|
.. method:: tell()
|
|
|
|
|
|
|
|
Return current file pointer position.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _wave-write-objects:
|
|
|
|
|
|
|
|
Wave_write Objects
|
|
|
|
------------------
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. class:: Wave_write
|
2014-03-08 12:14:29 -04:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Write a WAV file.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Wave_write objects, as returned by :func:`.open`.
|
2014-03-08 12:14:29 -04:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
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:`setnframes` or :meth:`setparams` with the number
|
|
|
|
of frames that will be written before :meth:`close` is called and
|
|
|
|
then using :meth:`writeframesraw` to write the frame data, or by
|
|
|
|
calling :meth:`writeframes` with all of the frame data to be
|
|
|
|
written. In the latter case :meth:`writeframes` will calculate
|
|
|
|
the number of frames in the data and set *nframes* accordingly before writing
|
|
|
|
the frame data.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. versionchanged:: 3.4
|
|
|
|
Added support for unseekable files.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Wave_write objects have the following methods:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: close()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Make sure *nframes* is correct, and close the file if it was opened by
|
|
|
|
: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.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: setnchannels(n)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Set the number of channels.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: setsampwidth(n)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Set the sample width to *n* bytes.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: setframerate(n)
|
2010-08-28 14:22:16 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Set the frame rate to *n*.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
A non-integral input to this method is rounded to the nearest
|
|
|
|
integer.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: setnframes(n)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
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).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: setcomptype(type, name)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Set the compression type and description. At the moment, only compression type
|
|
|
|
``NONE`` is supported, meaning no compression.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: setparams(tuple)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
The *tuple* should be ``(nchannels, sampwidth, framerate, nframes, comptype,
|
|
|
|
compname)``, with values valid for the ``set*()`` methods. Sets all
|
|
|
|
parameters.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: tell()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Return current position in the file, with the same disclaimer for the
|
|
|
|
:meth:`Wave_read.tell` and :meth:`Wave_read.setpos` methods.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: writeframesraw(data)
|
2013-11-16 08:01:31 -04:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
Write audio frames, without correcting *nframes*.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. versionchanged:: 3.4
|
|
|
|
Any :term:`bytes-like object` is now accepted.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. method:: writeframes(data)
|
2013-11-16 08:01:31 -04:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
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*.
|
2011-01-08 05:45:43 -04:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
.. versionchanged:: 3.4
|
|
|
|
Any :term:`bytes-like object` is now accepted.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-31 08:29:10 -03:00
|
|
|
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`.
|