blob: d03f091826302c0df4496666f8dba6bd372358b7 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. % Documentations stolen and LaTeX'ed from comments in file.
2
3
4:mod:`wave` --- Read and write WAV files
5========================================
6
7.. module:: wave
8 :synopsis: Provide an interface to the WAV sound format.
9.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
10
11
12The :mod:`wave` module provides a convenient interface to the WAV sound format.
13It does not support compression/decompression, but it does support mono/stereo.
14
15The :mod:`wave` module defines the following function and exception:
16
17
18.. function:: open(file[, mode])
19
20 If *file* is a string, open the file by that name, other treat it as a seekable
21 file-like object. *mode* can be any of
22
23 ``'r'``, ``'rb'``
24 Read only mode.
25
26 ``'w'``, ``'wb'``
27 Write only mode.
28
29 Note that it does not allow read/write WAV files.
30
31 A *mode* of ``'r'`` or ``'rb'`` returns a :class:`Wave_read` object, while a
32 *mode* of ``'w'`` or ``'wb'`` returns a :class:`Wave_write` object. If *mode*
33 is omitted and a file-like object is passed as *file*, ``file.mode`` is used as
34 the default value for *mode* (the ``'b'`` flag is still added if necessary).
35
36
37.. function:: openfp(file, mode)
38
39 A synonym for :func:`open`, maintained for backwards compatibility.
40
41
42.. exception:: Error
43
44 An error raised when something is impossible because it violates the WAV
45 specification or hits an implementation deficiency.
46
47
48.. _wave-read-objects:
49
50Wave_read Objects
51-----------------
52
53Wave_read objects, as returned by :func:`open`, have the following methods:
54
55
56.. method:: Wave_read.close()
57
58 Close the stream, and make the instance unusable. This is called automatically
59 on object collection.
60
61
62.. method:: Wave_read.getnchannels()
63
64 Returns number of audio channels (``1`` for mono, ``2`` for stereo).
65
66
67.. method:: Wave_read.getsampwidth()
68
69 Returns sample width in bytes.
70
71
72.. method:: Wave_read.getframerate()
73
74 Returns sampling frequency.
75
76
77.. method:: Wave_read.getnframes()
78
79 Returns number of audio frames.
80
81
82.. method:: Wave_read.getcomptype()
83
84 Returns compression type (``'NONE'`` is the only supported type).
85
86
87.. method:: Wave_read.getcompname()
88
89 Human-readable version of :meth:`getcomptype`. Usually ``'not compressed'``
90 parallels ``'NONE'``.
91
92
93.. method:: Wave_read.getparams()
94
95 Returns a tuple ``(nchannels, sampwidth, framerate, nframes, comptype,
96 compname)``, equivalent to output of the :meth:`get\*` methods.
97
98
99.. method:: Wave_read.readframes(n)
100
101 Reads and returns at most *n* frames of audio, as a string of bytes.
102
103
104.. method:: Wave_read.rewind()
105
106 Rewind the file pointer to the beginning of the audio stream.
107
108The following two methods are defined for compatibility with the :mod:`aifc`
109module, and don't do anything interesting.
110
111
112.. method:: Wave_read.getmarkers()
113
114 Returns ``None``.
115
116
117.. method:: Wave_read.getmark(id)
118
119 Raise an error.
120
121The following two methods define a term "position" which is compatible between
122them, and is otherwise implementation dependent.
123
124
125.. method:: Wave_read.setpos(pos)
126
127 Set the file pointer to the specified position.
128
129
130.. method:: Wave_read.tell()
131
132 Return current file pointer position.
133
134
135.. _wave-write-objects:
136
137Wave_write Objects
138------------------
139
140Wave_write objects, as returned by :func:`open`, have the following methods:
141
142
143.. method:: Wave_write.close()
144
145 Make sure *nframes* is correct, and close the file. This method is called upon
146 deletion.
147
148
149.. method:: Wave_write.setnchannels(n)
150
151 Set the number of channels.
152
153
154.. method:: Wave_write.setsampwidth(n)
155
156 Set the sample width to *n* bytes.
157
158
159.. method:: Wave_write.setframerate(n)
160
161 Set the frame rate to *n*.
162
163
164.. method:: Wave_write.setnframes(n)
165
166 Set the number of frames to *n*. This will be changed later if more frames are
167 written.
168
169
170.. method:: Wave_write.setcomptype(type, name)
171
172 Set the compression type and description. At the moment, only compression type
173 ``NONE`` is supported, meaning no compression.
174
175
176.. method:: Wave_write.setparams(tuple)
177
178 The *tuple* should be ``(nchannels, sampwidth, framerate, nframes, comptype,
179 compname)``, with values valid for the :meth:`set\*` methods. Sets all
180 parameters.
181
182
183.. method:: Wave_write.tell()
184
185 Return current position in the file, with the same disclaimer for the
186 :meth:`Wave_read.tell` and :meth:`Wave_read.setpos` methods.
187
188
189.. method:: Wave_write.writeframesraw(data)
190
191 Write audio frames, without correcting *nframes*.
192
193
194.. method:: Wave_write.writeframes(data)
195
196 Write audio frames and make sure *nframes* is correct.
197
198Note that it is invalid to set any parameters after calling :meth:`writeframes`
199or :meth:`writeframesraw`, and any attempt to do so will raise
200:exc:`wave.Error`.
201