wave
— Read and write WAV files¶
Source code:Lib/wave.py
Thewave
module provides a convenient interface to the Waveform Audio«WAVE» (or «WAV») file format. Only uncompressed PCM encoded wave files aresupported.
Άλλαξε στην έκδοση 3.12:Support forWAVE_FORMAT_EXTENSIBLE
headers was added, provided that theextended format isKSDATAFORMAT_SUBTYPE_PCM
.
Thewave
module defines the following function and exception:
- wave.open(file,mode=None)¶
Iffile is a string, open the file by that name, otherwise treat it as afile-like object.mode can be:
'rb'
Read only mode.
'wb'
Write only mode.
Note that it does not allow read/write WAV files.
Amode of
'rb'
returns aWave_read
object, while amode of'wb'
returns aWave_write
object. Ifmode is omitted and afile-like object is passed asfile,file.mode
is used as the defaultvalue formode.If you pass in a file-like object, the wave object will not close it when its
close()
method is called; it is the caller’s responsibility to closethe file object.The
open()
function may be used in awith
statement. Whenthewith
block completes, theWave_read.close()
orWave_write.close()
method is called.Άλλαξε στην έκδοση 3.4:Added support for unseekable files.
- exceptionwave.Error¶
An error raised when something is impossible because it violates the WAVspecification or hits an implementation deficiency.
Wave_read Objects¶
- classwave.Wave_read¶
Read a WAV file.
Wave_read objects, as returned by
open()
, have the following methods:- close()¶
Close the stream if it was opened by
wave
, and make the instanceunusable. This is called automatically on object collection.
- getnchannels()¶
Returns number of audio channels (
1
for mono,2
for stereo).
- getsampwidth()¶
Returns sample width in bytes.
- getframerate()¶
Returns sampling frequency.
- getnframes()¶
Returns number of audio frames.
- getcomptype()¶
Returns compression type (
'NONE'
is the only supported type).
- getcompname()¶
Human-readable version of
getcomptype()
. Usually'notcompressed'
parallels'NONE'
.
- getparams()¶
Returns a
namedtuple()
(nchannels,sampwidth,framerate,nframes,comptype,compname)
, equivalent to output of theget*()
methods.
- rewind()¶
Rewind the file pointer to the beginning of the audio stream.
The following two methods are defined for compatibility with the
aifc
module, and don’t do anything interesting.- getmarkers()¶
Returns
None
.
- getmark(id)¶
Raise an error.
The following two methods define a term «position» which is compatible betweenthem, and is otherwise implementation dependent.
- setpos(pos)¶
Set the file pointer to the specified position.
- tell()¶
Return current file pointer position.
Wave_write Objects¶
- classwave.Wave_write¶
Write a WAV file.
Wave_write objects, as returned by
open()
.For seekable output streams, the
wave
header will automatically be updatedto reflect the number of frames actually written. For unseekable streams, thenframes value must be accurate when the first frame data is written. Anaccuratenframes value can be achieved either by callingsetnframes()
orsetparams()
with the numberof frames that will be written beforeclose()
is called andthen usingwriteframesraw()
to write the frame data, or bycallingwriteframes()
with all of the frame data to bewritten. In the latter casewriteframes()
will calculatethe number of frames in the data and setnframes accordingly before writingthe frame data.Άλλαξε στην έκδοση 3.4:Added support for unseekable files.
Wave_write objects have the following methods:
- close()¶
Make surenframes is correct, and close the file if it was opened by
wave
. This method is called upon object collection. It will raisean exception if the output stream is not seekable andnframes does notmatch the number of frames actually written.
- setnchannels(n)¶
Set the number of channels.
- setsampwidth(n)¶
Set the sample width ton bytes.
- setframerate(n)¶
Set the frame rate ton.
Άλλαξε στην έκδοση 3.2:A non-integral input to this method is rounded to the nearestinteger.
- setnframes(n)¶
Set the number of frames ton. This will be changed later if the numberof frames actually written is different (this update attempt willraise an error if the output stream is not seekable).
- setcomptype(type,name)¶
Set the compression type and description. At the moment, only compression type
NONE
is supported, meaning no compression.
- setparams(tuple)¶
Thetuple should be
(nchannels,sampwidth,framerate,nframes,comptype,compname)
, with values valid for theset*()
methods. Sets allparameters.
- tell()¶
Return current position in the file, with the same disclaimer for the
Wave_read.tell()
andWave_read.setpos()
methods.
- writeframesraw(data)¶
Write audio frames, without correctingnframes.
Άλλαξε στην έκδοση 3.4:Anybytes-like object is now accepted.
- writeframes(data)¶
Write audio frames and make surenframes is correct. It will raise anerror if the output stream is not seekable and the total number of framesthat have been written afterdata has been written does not match thepreviously set value fornframes.
Άλλαξε στην έκδοση 3.4:Anybytes-like object is now accepted.
Note that it is invalid to set any parameters after calling
writeframes()
orwriteframesraw()
, and any attempt to do so will raisewave.Error
.