Skip to content

Commit 5de6ca5

Browse files
authored
[3.11] gh-105096: Reformat wave documentation (#105136) (#105138) (#105155)
[3.12] gh-105096: Reformat wave documentation (#105136) (#105138) gh-105096: Reformat wave documentation (#105136) Add ".. class::" markups in the wave documentation. * Reformat also wave.py (minor PEP 8 changes). * Remove redundant "import struct": it's already imported at top level. * Remove wave.rst from .nitignore (cherry picked from commit 85e5d03) (cherry picked from commit 01b42f9)
1 parent 67a8469 commit 5de6ca5

File tree

2 files changed

+108
-100
lines changed

2 files changed

+108
-100
lines changed

Doc/library/wave.rst

+105-98
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
--------------
1313

14-
The :mod:`wave` module provides a convenient interface to the WAV sound format.
14+
The :mod:`wave` module provides a convenient interface to the Waveform Audio
15+
"WAVE" (or "WAV") file format.
1516
Only files using ``WAVE_FORMAT_PCM`` are supported. Note that this does not
1617
include files using ``WAVE_FORMAT_EXTENSIBLE`` even if the subformat is PCM.
1718

@@ -37,13 +38,12 @@ The :mod:`wave` module defines the following function and exception:
3738
value for *mode*.
3839

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

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

4848
.. versionchanged:: 3.4
4949
Added support for unseekable files.
@@ -59,185 +59,192 @@ The :mod:`wave` module defines the following function and exception:
5959
Wave_read Objects
6060
-----------------
6161

62-
Wave_read objects, as returned by :func:`.open`, have the following methods:
62+
.. class:: Wave_read
6363

64+
Read a WAV file.
6465

65-
.. method:: Wave_read.close()
66+
Wave_read objects, as returned by :func:`.open`, have the following methods:
6667

67-
Close the stream if it was opened by :mod:`wave`, and make the instance
68-
unusable. This is called automatically on object collection.
6968

69+
.. method:: close()
7070

71-
.. method:: Wave_read.getnchannels()
71+
Close the stream if it was opened by :mod:`wave`, and make the instance
72+
unusable. This is called automatically on object collection.
7273

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

75+
.. method:: getnchannels()
7576

76-
.. method:: Wave_read.getsampwidth()
77+
Returns number of audio channels (``1`` for mono, ``2`` for stereo).
7778

78-
Returns sample width in bytes.
7979

80+
.. method:: getsampwidth()
8081

81-
.. method:: Wave_read.getframerate()
82+
Returns sample width in bytes.
8283

83-
Returns sampling frequency.
8484

85+
.. method:: getframerate()
8586

86-
.. method:: Wave_read.getnframes()
87+
Returns sampling frequency.
8788

88-
Returns number of audio frames.
8989

90+
.. method:: getnframes()
9091

91-
.. method:: Wave_read.getcomptype()
92+
Returns number of audio frames.
9293

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

95+
.. method:: getcomptype()
9596

96-
.. method:: Wave_read.getcompname()
97+
Returns compression type (``'NONE'`` is the only supported type).
9798

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

100+
.. method:: getcompname()
101101

102-
.. method:: Wave_read.getparams()
102+
Human-readable version of :meth:`getcomptype`. Usually ``'not compressed'``
103+
parallels ``'NONE'``.
103104

104-
Returns a :func:`~collections.namedtuple` ``(nchannels, sampwidth,
105-
framerate, nframes, comptype, compname)``, equivalent to output of the
106-
:meth:`get\*` methods.
107105

106+
.. method:: getparams()
108107

109-
.. method:: Wave_read.readframes(n)
108+
Returns a :func:`~collections.namedtuple` ``(nchannels, sampwidth,
109+
framerate, nframes, comptype, compname)``, equivalent to output of the
110+
``get*()`` methods.
110111

111-
Reads and returns at most *n* frames of audio, as a :class:`bytes` object.
112112

113+
.. method:: readframes(n)
113114

114-
.. method:: Wave_read.rewind()
115+
Reads and returns at most *n* frames of audio, as a :class:`bytes` object.
115116

116-
Rewind the file pointer to the beginning of the audio stream.
117117

118-
The following two methods are defined for compatibility with the :mod:`aifc`
119-
module, and don't do anything interesting.
118+
.. method:: rewind()
120119

120+
Rewind the file pointer to the beginning of the audio stream.
121121

122-
.. method:: Wave_read.getmarkers()
122+
The following two methods are defined for compatibility with the :mod:`aifc`
123+
module, and don't do anything interesting.
123124

124-
Returns ``None``.
125125

126+
.. method:: getmarkers()
126127

127-
.. method:: Wave_read.getmark(id)
128+
Returns ``None``.
128129

129-
Raise an error.
130130

131-
The following two methods define a term "position" which is compatible between
132-
them, and is otherwise implementation dependent.
131+
.. method:: getmark(id)
133132

133+
Raise an error.
134134

135-
.. method:: Wave_read.setpos(pos)
135+
The following two methods define a term "position" which is compatible between
136+
them, and is otherwise implementation dependent.
136137

137-
Set the file pointer to the specified position.
138138

139+
.. method:: setpos(pos)
139140

140-
.. method:: Wave_read.tell()
141+
Set the file pointer to the specified position.
141142

142-
Return current file pointer position.
143+
144+
.. method:: tell()
145+
146+
Return current file pointer position.
143147

144148

145149
.. _wave-write-objects:
146150

147151
Wave_write Objects
148152
------------------
149153

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

162-
Wave_write objects, as returned by :func:`.open`, have the following methods:
156+
Write a WAV file.
163157

164-
.. versionchanged:: 3.4
165-
Added support for unseekable files.
158+
Wave_write objects, as returned by :func:`.open`.
166159

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

168-
.. method:: Wave_write.close()
172+
.. versionchanged:: 3.4
173+
Added support for unseekable files.
169174

170-
Make sure *nframes* is correct, and close the file if it was opened by
171-
:mod:`wave`. This method is called upon object collection. It will raise
172-
an exception if the output stream is not seekable and *nframes* does not
173-
match the number of frames actually written.
175+
Wave_write objects have the following methods:
174176

177+
.. method:: close()
175178

176-
.. method:: Wave_write.setnchannels(n)
179+
Make sure *nframes* is correct, and close the file if it was opened by
180+
:mod:`wave`. This method is called upon object collection. It will raise
181+
an exception if the output stream is not seekable and *nframes* does not
182+
match the number of frames actually written.
177183

178-
Set the number of channels.
179184

185+
.. method:: setnchannels(n)
180186

181-
.. method:: Wave_write.setsampwidth(n)
187+
Set the number of channels.
182188

183-
Set the sample width to *n* bytes.
184189

190+
.. method:: setsampwidth(n)
185191

186-
.. method:: Wave_write.setframerate(n)
192+
Set the sample width to *n* bytes.
187193

188-
Set the frame rate to *n*.
189194

190-
.. versionchanged:: 3.2
191-
A non-integral input to this method is rounded to the nearest
192-
integer.
195+
.. method:: setframerate(n)
193196

197+
Set the frame rate to *n*.
194198

195-
.. method:: Wave_write.setnframes(n)
199+
.. versionchanged:: 3.2
200+
A non-integral input to this method is rounded to the nearest
201+
integer.
196202

197-
Set the number of frames to *n*. This will be changed later if the number
198-
of frames actually written is different (this update attempt will
199-
raise an error if the output stream is not seekable).
200203

204+
.. method:: setnframes(n)
201205

202-
.. method:: Wave_write.setcomptype(type, name)
206+
Set the number of frames to *n*. This will be changed later if the number
207+
of frames actually written is different (this update attempt will
208+
raise an error if the output stream is not seekable).
203209

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

211+
.. method:: setcomptype(type, name)
207212

208-
.. method:: Wave_write.setparams(tuple)
213+
Set the compression type and description. At the moment, only compression type
214+
``NONE`` is supported, meaning no compression.
209215

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

217+
.. method:: setparams(tuple)
214218

215-
.. method:: Wave_write.tell()
219+
The *tuple* should be ``(nchannels, sampwidth, framerate, nframes, comptype,
220+
compname)``, with values valid for the ``set*()`` methods. Sets all
221+
parameters.
216222

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

224+
.. method:: tell()
220225

221-
.. method:: Wave_write.writeframesraw(data)
226+
Return current position in the file, with the same disclaimer for the
227+
:meth:`Wave_read.tell` and :meth:`Wave_read.setpos` methods.
222228

223-
Write audio frames, without correcting *nframes*.
224229

225-
.. versionchanged:: 3.4
226-
Any :term:`bytes-like object` is now accepted.
230+
.. method:: writeframesraw(data)
227231

232+
Write audio frames, without correcting *nframes*.
228233

229-
.. method:: Wave_write.writeframes(data)
234+
.. versionchanged:: 3.4
235+
Any :term:`bytes-like object` is now accepted.
230236

231-
Write audio frames and make sure *nframes* is correct. It will raise an
232-
error if the output stream is not seekable and the total number of frames
233-
that have been written after *data* has been written does not match the
234-
previously set value for *nframes*.
235237

236-
.. versionchanged:: 3.4
237-
Any :term:`bytes-like object` is now accepted.
238+
.. method:: writeframes(data)
238239

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

240-
Note that it is invalid to set any parameters after calling :meth:`writeframes`
241-
or :meth:`writeframesraw`, and any attempt to do so will raise
242-
:exc:`wave.Error`.
245+
.. versionchanged:: 3.4
246+
Any :term:`bytes-like object` is now accepted.
243247

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

Lib/wave.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class Error(Exception):
8989
_wave_params = namedtuple('_wave_params',
9090
'nchannels sampwidth framerate nframes comptype compname')
9191

92+
9293
def _byteswap(data, width):
9394
swapped_data = bytearray(len(data))
9495

@@ -101,7 +102,6 @@ def _byteswap(data, width):
101102

102103
class _Chunk:
103104
def __init__(self, file, align=True, bigendian=True, inclheader=False):
104-
import struct
105105
self.closed = False
106106
self.align = align # whether to align to word (2-byte) boundaries
107107
if bigendian:
@@ -211,7 +211,6 @@ def skip(self):
211211
raise EOFError
212212

213213

214-
215214
class Wave_read:
216215
"""Variables used in this class:
217216
@@ -393,6 +392,7 @@ def _read_fmt_chunk(self, chunk):
393392
self._comptype = 'NONE'
394393
self._compname = 'not compressed'
395394

395+
396396
class Wave_write:
397397
"""Variables used in this class:
398398
@@ -620,6 +620,7 @@ def _patchheader(self):
620620
self._file.seek(curpos, 0)
621621
self._datalength = self._datawritten
622622

623+
623624
def open(f, mode=None):
624625
if mode is None:
625626
if hasattr(f, 'mode'):

0 commit comments

Comments
 (0)