Skip to content

Commit 8eb0f94

Browse files
committed
Improve docs
1 parent fbc18d3 commit 8eb0f94

File tree

2 files changed

+45
-60
lines changed

2 files changed

+45
-60
lines changed

adafruit_wave.py

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ class Error(Exception):
168168

169169

170170
class Wave_read:
171+
"""Used for wave files opened in read mode.
172+
173+
Do not construct directly, but call `open` instead."""
174+
171175
def initfp(self, file):
172176
self._convert = None
173177
self._soundpos = 0
@@ -225,63 +229,51 @@ def __exit__(self, *args):
225229
# User visible methods.
226230
#
227231
def getfp(self):
232+
"""Get the underlying file object"""
228233
return self._file
229234

230235
def rewind(self):
236+
"""Go back to the start of the audio data"""
231237
self._data_seek_needed = 1
232238
self._soundpos = 0
233239

234240
def close(self):
241+
"""Close the file"""
235242
self._file = None
236243
file = self._i_opened_the_file
237244
if file:
238245
self._i_opened_the_file = None
239246
file.close()
240247

241248
def tell(self):
249+
"""Get the current position in the audio data"""
242250
return self._soundpos
243251

244252
def getnchannels(self):
253+
"""Get the number of channels (1 for mono, 2 for stereo)"""
245254
return self._nchannels
246255

247256
def getnframes(self):
257+
"""Get the number of frames"""
248258
return self._nframes
249259

250260
def getsampwidth(self):
261+
"""Get the sample width in bytes"""
251262
return self._sampwidth
252263

253264
def getframerate(self):
265+
"""Get the sample rate in Hz"""
254266
return self._framerate
255267

256-
def getcomptype(self):
257-
return self._comptype
258-
259-
def getcompname(self):
260-
return self._compname
261-
262-
def getparams(self):
263-
return _wave_params(
264-
self.getnchannels(),
265-
self.getsampwidth(),
266-
self.getframerate(),
267-
self.getnframes(),
268-
self.getcomptype(),
269-
self.getcompname(),
270-
)
271-
272-
def getmarkers(self):
273-
return None
274-
275-
def getmark(self, id):
276-
raise Error("no marks")
277-
278268
def setpos(self, pos):
269+
"""Seek to a particular position in the audio data"""
279270
if pos < 0 or pos > self._nframes:
280271
raise Error("position not in range")
281272
self._soundpos = pos
282273
self._data_seek_needed = 1
283274

284275
def readframes(self, nframes):
276+
"""Read frames of audio data"""
285277
if self._data_seek_needed:
286278
self._data_chunk.seek(0, 0)
287279
pos = self._soundpos * self._framesize
@@ -331,6 +323,10 @@ def _read_fmt_chunk(self, chunk):
331323

332324

333325
class Wave_write:
326+
"""Used for wave files opened in write mode.
327+
328+
Do not construct directly, but call `open` instead."""
329+
334330
def __init__(self, f):
335331
self._i_opened_the_file = None
336332
if isinstance(f, str):
@@ -368,37 +364,43 @@ def __exit__(self, *args):
368364
# User visible methods.
369365
#
370366
def setnchannels(self, nchannels):
367+
"""Set the number of channels (1 for mono, 2 for stereo)"""
371368
if self._datawritten:
372369
raise Error("cannot change parameters after starting to write")
373370
if nchannels < 1:
374371
raise Error("bad # of channels")
375372
self._nchannels = nchannels
376373

377374
def getnchannels(self):
375+
"""Get the number of channels (1 for mono, 2 for stereo)"""
378376
if not self._nchannels:
379377
raise Error("number of channels not set")
380378
return self._nchannels
381379

382380
def setsampwidth(self, sampwidth):
381+
"""Set the sample width in bytes"""
383382
if self._datawritten:
384383
raise Error("cannot change parameters after starting to write")
385384
if sampwidth < 1 or sampwidth > 4:
386385
raise Error("bad sample width")
387386
self._sampwidth = sampwidth
388387

389388
def getsampwidth(self):
389+
"""Get the sample width in bytes"""
390390
if not self._sampwidth:
391391
raise Error("sample width not set")
392392
return self._sampwidth
393393

394394
def setframerate(self, framerate):
395+
"""Set the sample rate in Hz"""
395396
if self._datawritten:
396397
raise Error("cannot change parameters after starting to write")
397398
if framerate <= 0:
398399
raise Error("bad frame rate")
399400
self._framerate = int(round(framerate))
400401

401402
def getframerate(self):
403+
"""Get the sample rate in Hz"""
402404
if not self._framerate:
403405
raise Error("frame rate not set")
404406
return self._framerate
@@ -411,55 +413,22 @@ def setnframes(self, nframes):
411413
def getnframes(self):
412414
return self._nframeswritten
413415

414-
def setcomptype(self, comptype, compname):
415-
if self._datawritten:
416-
raise Error("cannot change parameters after starting to write")
417-
if comptype not in ("NONE",):
418-
raise Error("unsupported compression type")
419-
self._comptype = comptype
420-
self._compname = compname
421-
422-
def getcomptype(self):
423-
return self._comptype
424-
425-
def getcompname(self):
426-
return self._compname
427-
428416
def setparams(self, params):
417+
"""Set all parameters at once"""
429418
nchannels, sampwidth, framerate, nframes, comptype, compname = params
430419
if self._datawritten:
431420
raise Error("cannot change parameters after starting to write")
432421
self.setnchannels(nchannels)
433422
self.setsampwidth(sampwidth)
434423
self.setframerate(framerate)
435424
self.setnframes(nframes)
436-
self.setcomptype(comptype, compname)
437-
438-
def getparams(self):
439-
if not self._nchannels or not self._sampwidth or not self._framerate:
440-
raise Error("not all parameters set")
441-
return _wave_params(
442-
self._nchannels,
443-
self._sampwidth,
444-
self._framerate,
445-
self._nframes,
446-
self._comptype,
447-
self._compname,
448-
)
449-
450-
def setmark(self, id, pos, name):
451-
raise Error("setmark() not supported")
452-
453-
def getmark(self, id):
454-
raise Error("no marks")
455-
456-
def getmarkers(self):
457-
return None
458425

459426
def tell(self):
427+
"""Get the current position in the audio data"""
460428
return self._nframeswritten
461429

462430
def writeframesraw(self, data):
431+
"""Write data to the file without updating the header"""
463432
if not isinstance(data, (bytes, bytearray)):
464433
data = memoryview(data).cast("B")
465434
self._ensure_header_written(len(data))
@@ -471,11 +440,13 @@ def writeframesraw(self, data):
471440
self._nframeswritten = self._nframeswritten + nframes
472441

473442
def writeframes(self, data):
443+
"""Write data to the file and update the header if needed"""
474444
self.writeframesraw(data)
475445
if self._datalength != self._datawritten:
476446
self._patchheader()
477447

478448
def close(self):
449+
"""Close the file"""
479450
try:
480451
if self._file:
481452
self._ensure_header_written(0)
@@ -548,6 +519,13 @@ def _patchheader(self):
548519

549520

550521
def open(f, mode=None): # pylint: disable=redefined-builtin
522+
"""Open a wave file in reading (default) or writing (``mode="w"``) mode.
523+
524+
The argument may be a filename or an open file.
525+
526+
In reading mode, returns a `Wave_read` object.
527+
In writing mode, returns a `Wave_write` object.
528+
"""
551529
if mode is None:
552530
if hasattr(f, "mode"):
553531
mode = f.mode

docs/api.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@
44
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
55
.. use this format as the module name: "adafruit_foo.foo"
66
7-
.. automodule:: adafruit_wave
8-
:members:
7+
.. module:: adafruit_wave
8+
9+
.. autofunction:: open
10+
.. autoclass:: Wave_read
11+
:members:
12+
13+
14+
.. autoclass:: Wave_write
15+
:members:

0 commit comments

Comments
 (0)