cpython/Doc/library/wave.rst
Georg Brandl b044b2a701 Merged revisions 74821,74828-74831,74833,74835 via svnmerge from
svn+ssh://svn.python.org/python/branches/py3k

................
  r74821 | georg.brandl | 2009-09-16 11:42:19 +0200 (Mi, 16 Sep 2009) | 1 line

  #6885: run python 3 as python3.
................
  r74828 | georg.brandl | 2009-09-16 16:23:20 +0200 (Mi, 16 Sep 2009) | 1 line

  Use true booleans.
................
  r74829 | georg.brandl | 2009-09-16 16:24:29 +0200 (Mi, 16 Sep 2009) | 1 line

  Small PEP8 correction.
................
  r74830 | georg.brandl | 2009-09-16 16:36:22 +0200 (Mi, 16 Sep 2009) | 1 line

  Use true booleans.
................
  r74831 | georg.brandl | 2009-09-16 17:54:04 +0200 (Mi, 16 Sep 2009) | 1 line

  Use true booleans and PEP8 for argdefaults.
................
  r74833 | georg.brandl | 2009-09-16 17:58:14 +0200 (Mi, 16 Sep 2009) | 1 line

  Last round of adapting style of documenting argument default values.
................
  r74835 | georg.brandl | 2009-09-16 18:00:31 +0200 (Mi, 16 Sep 2009) | 33 lines

  Merged revisions 74817-74820,74822-74824 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r74817 | georg.brandl | 2009-09-16 11:05:11 +0200 (Mi, 16 Sep 2009) | 1 line

    Make deprecation notices as visible as warnings are right now.
  ........
    r74818 | georg.brandl | 2009-09-16 11:23:04 +0200 (Mi, 16 Sep 2009) | 1 line

    #6880: add reference to classes section in exceptions section, which comes earlier.
  ........
    r74819 | georg.brandl | 2009-09-16 11:24:57 +0200 (Mi, 16 Sep 2009) | 1 line

    #6876: fix base class constructor invocation in example.
  ........
    r74820 | georg.brandl | 2009-09-16 11:30:48 +0200 (Mi, 16 Sep 2009) | 1 line

    #6891: comment out dead link to Unicode article.
  ........
    r74822 | georg.brandl | 2009-09-16 12:12:06 +0200 (Mi, 16 Sep 2009) | 1 line

    #5621: refactor description of how class/instance attributes interact on a.x=a.x+1 or augassign.
  ........
    r74823 | georg.brandl | 2009-09-16 15:06:22 +0200 (Mi, 16 Sep 2009) | 1 line

    Remove strange trailing commas.
  ........
    r74824 | georg.brandl | 2009-09-16 15:11:06 +0200 (Mi, 16 Sep 2009) | 1 line

    #6892: fix optparse example involving help option.
  ........
................
2009-09-16 16:05:59 +00:00

198 lines
4.6 KiB
ReStructuredText

:mod:`wave` --- Read and write WAV files
========================================
.. module:: wave
:synopsis: Provide an interface to the WAV sound format.
.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
.. Documentations stolen from comments in file.
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:
.. function:: open(file, mode=None)
If *file* is a string, open the file by that name, other treat it as a seekable
file-like object. *mode* can be any of
``'r'``, ``'rb'``
Read only mode.
``'w'``, ``'wb'``
Write only mode.
Note that it does not allow read/write WAV files.
A *mode* of ``'r'`` or ``'rb'`` returns a :class:`Wave_read` object, while a
*mode* of ``'w'`` or ``'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* (the ``'b'`` flag is still added if necessary).
.. function:: openfp(file, mode)
A synonym for :func:`.open`, maintained for backwards compatibility.
.. 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
-----------------
Wave_read objects, as returned by :func:`.open`, have the following methods:
.. method:: Wave_read.close()
Close the stream, and make the instance unusable. This is called automatically
on object collection.
.. 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()
Returns a tuple ``(nchannels, sampwidth, framerate, nframes, comptype,
compname)``, equivalent to output of the :meth:`get\*` methods.
.. method:: Wave_read.readframes(n)
Reads and returns at most *n* frames of audio, as a string of bytes.
.. 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
------------------
Wave_write objects, as returned by :func:`.open`, have the following methods:
.. method:: Wave_write.close()
Make sure *nframes* is correct, and close the file. This method is called upon
deletion.
.. 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*.
.. method:: Wave_write.setnframes(n)
Set the number of frames to *n*. This will be changed later if more frames are
written.
.. 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*.
.. method:: Wave_write.writeframes(data)
Write audio frames and make sure *nframes* is correct.
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`.