@@ -168,6 +168,10 @@ class Error(Exception):
168
168
169
169
170
170
class Wave_read :
171
+ """Used for wave files opened in read mode.
172
+
173
+ Do not construct directly, but call `open` instead."""
174
+
171
175
def initfp (self , file ):
172
176
self ._convert = None
173
177
self ._soundpos = 0
@@ -225,63 +229,51 @@ def __exit__(self, *args):
225
229
# User visible methods.
226
230
#
227
231
def getfp (self ):
232
+ """Get the underlying file object"""
228
233
return self ._file
229
234
230
235
def rewind (self ):
236
+ """Go back to the start of the audio data"""
231
237
self ._data_seek_needed = 1
232
238
self ._soundpos = 0
233
239
234
240
def close (self ):
241
+ """Close the file"""
235
242
self ._file = None
236
243
file = self ._i_opened_the_file
237
244
if file :
238
245
self ._i_opened_the_file = None
239
246
file .close ()
240
247
241
248
def tell (self ):
249
+ """Get the current position in the audio data"""
242
250
return self ._soundpos
243
251
244
252
def getnchannels (self ):
253
+ """Get the number of channels (1 for mono, 2 for stereo)"""
245
254
return self ._nchannels
246
255
247
256
def getnframes (self ):
257
+ """Get the number of frames"""
248
258
return self ._nframes
249
259
250
260
def getsampwidth (self ):
261
+ """Get the sample width in bytes"""
251
262
return self ._sampwidth
252
263
253
264
def getframerate (self ):
265
+ """Get the sample rate in Hz"""
254
266
return self ._framerate
255
267
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
-
278
268
def setpos (self , pos ):
269
+ """Seek to a particular position in the audio data"""
279
270
if pos < 0 or pos > self ._nframes :
280
271
raise Error ("position not in range" )
281
272
self ._soundpos = pos
282
273
self ._data_seek_needed = 1
283
274
284
275
def readframes (self , nframes ):
276
+ """Read frames of audio data"""
285
277
if self ._data_seek_needed :
286
278
self ._data_chunk .seek (0 , 0 )
287
279
pos = self ._soundpos * self ._framesize
@@ -331,6 +323,10 @@ def _read_fmt_chunk(self, chunk):
331
323
332
324
333
325
class Wave_write :
326
+ """Used for wave files opened in write mode.
327
+
328
+ Do not construct directly, but call `open` instead."""
329
+
334
330
def __init__ (self , f ):
335
331
self ._i_opened_the_file = None
336
332
if isinstance (f , str ):
@@ -368,37 +364,43 @@ def __exit__(self, *args):
368
364
# User visible methods.
369
365
#
370
366
def setnchannels (self , nchannels ):
367
+ """Set the number of channels (1 for mono, 2 for stereo)"""
371
368
if self ._datawritten :
372
369
raise Error ("cannot change parameters after starting to write" )
373
370
if nchannels < 1 :
374
371
raise Error ("bad # of channels" )
375
372
self ._nchannels = nchannels
376
373
377
374
def getnchannels (self ):
375
+ """Get the number of channels (1 for mono, 2 for stereo)"""
378
376
if not self ._nchannels :
379
377
raise Error ("number of channels not set" )
380
378
return self ._nchannels
381
379
382
380
def setsampwidth (self , sampwidth ):
381
+ """Set the sample width in bytes"""
383
382
if self ._datawritten :
384
383
raise Error ("cannot change parameters after starting to write" )
385
384
if sampwidth < 1 or sampwidth > 4 :
386
385
raise Error ("bad sample width" )
387
386
self ._sampwidth = sampwidth
388
387
389
388
def getsampwidth (self ):
389
+ """Get the sample width in bytes"""
390
390
if not self ._sampwidth :
391
391
raise Error ("sample width not set" )
392
392
return self ._sampwidth
393
393
394
394
def setframerate (self , framerate ):
395
+ """Set the sample rate in Hz"""
395
396
if self ._datawritten :
396
397
raise Error ("cannot change parameters after starting to write" )
397
398
if framerate <= 0 :
398
399
raise Error ("bad frame rate" )
399
400
self ._framerate = int (round (framerate ))
400
401
401
402
def getframerate (self ):
403
+ """Get the sample rate in Hz"""
402
404
if not self ._framerate :
403
405
raise Error ("frame rate not set" )
404
406
return self ._framerate
@@ -411,55 +413,22 @@ def setnframes(self, nframes):
411
413
def getnframes (self ):
412
414
return self ._nframeswritten
413
415
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
-
428
416
def setparams (self , params ):
417
+ """Set all parameters at once"""
429
418
nchannels , sampwidth , framerate , nframes , comptype , compname = params
430
419
if self ._datawritten :
431
420
raise Error ("cannot change parameters after starting to write" )
432
421
self .setnchannels (nchannels )
433
422
self .setsampwidth (sampwidth )
434
423
self .setframerate (framerate )
435
424
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
458
425
459
426
def tell (self ):
427
+ """Get the current position in the audio data"""
460
428
return self ._nframeswritten
461
429
462
430
def writeframesraw (self , data ):
431
+ """Write data to the file without updating the header"""
463
432
if not isinstance (data , (bytes , bytearray )):
464
433
data = memoryview (data ).cast ("B" )
465
434
self ._ensure_header_written (len (data ))
@@ -471,11 +440,13 @@ def writeframesraw(self, data):
471
440
self ._nframeswritten = self ._nframeswritten + nframes
472
441
473
442
def writeframes (self , data ):
443
+ """Write data to the file and update the header if needed"""
474
444
self .writeframesraw (data )
475
445
if self ._datalength != self ._datawritten :
476
446
self ._patchheader ()
477
447
478
448
def close (self ):
449
+ """Close the file"""
479
450
try :
480
451
if self ._file :
481
452
self ._ensure_header_written (0 )
@@ -548,6 +519,13 @@ def _patchheader(self):
548
519
549
520
550
521
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
+ """
551
529
if mode is None :
552
530
if hasattr (f , "mode" ):
553
531
mode = f .mode
0 commit comments