wave
— Read and write WAV files¶
Código-fonte: Lib/wave.py
The wave
module provides a convenient interface to the Waveform Audio
“WAVE” (or “WAV”) file format. Only uncompressed PCM encoded wave files are
supported.
Alterado na versão 3.12: Support for WAVE_FORMAT_EXTENSIBLE
headers was added, provided that the
extended format is KSDATAFORMAT_SUBTYPE_PCM
.
The wave
module defines the following function and exception:
- wave.open(file, mode=None)¶
If file is a string, open the file by that name, otherwise treat it as a file-like object. mode can be:
'rb'
Modo somente para leitura.
'wb'
Modo somente para escrita.
Note that it does not allow read/write WAV files.
A mode of
'rb'
returns aWave_read
object, while a mode of'wb'
returns aWave_write
object. If mode is omitted and a file-like object is passed as file,file.mode
is used as the default value for mode.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 close the file object.The
open()
function may be used in awith
statement. When thewith
block completes, theWave_read.close()
orWave_write.close()
method is called.Alterado na versão 3.4: Added support for unseekable files.
- exception wave.Error¶
An error raised when something is impossible because it violates the WAV specification or hits an implementation deficiency.
Objetos Wave_read¶
- class wave.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 instance unusable. This is called automatically on object collection.
- getnchannels()¶
Returns number of audio channels (
1
for mono,2
for stereo).
- getsampwidth()¶
Retorna a largura da amostra em bytes.
- getframerate()¶
Retorna a frequência de amostragem.
- getnframes()¶
Retorna o número de quadros de áudio.
- getcomptype()¶
Returns compression type (
'NONE'
is the only supported type).
- getcompname()¶
Human-readable version of
getcomptype()
. Usually'not compressed'
parallels'NONE'
.
- getparams()¶
Returns a
namedtuple()
(nchannels, sampwidth, framerate, nframes, comptype, compname)
, equivalent to output of theget*()
methods.
- rewind()¶
Volta o ponteiro do arquivo para o início do fluxo de áudio.
The following two methods are defined for compatibility with the old
aifc
module, and don’t do anything interesting.- getmarkers()¶
Retorna
None
.Deprecated since version 3.13, will be removed in version 3.15: The method only existed for compatibility with the
aifc
module which has been removed in Python 3.13.
- getmark(id)¶
Levanta um erro.
Deprecated since version 3.13, will be removed in version 3.15: The method only existed for compatibility with the
aifc
module which has been removed in Python 3.13.
Os dois métodos a seguir definem um termo “posição” que é compatível entre eles e é dependente da implementação.
- setpos(pos)¶
Set the file pointer to the specified position.
- tell()¶
Return current file pointer position.
Objetos Wave_write¶
- class wave.Wave_write¶
Write a WAV file.
Wave_write objects, as returned by
open()
.For seekable output streams, the
wave
header will automatically be updated to reflect the number of frames actually written. For unseekable streams, the nframes value must be accurate when the first frame data is written. An accurate nframes value can be achieved either by callingsetnframes()
orsetparams()
with the number of frames that will be written beforeclose()
is called and then usingwriteframesraw()
to write the frame data, or by callingwriteframes()
with all of the frame data to be written. In the latter casewriteframes()
will calculate the number of frames in the data and set nframes accordingly before writing the frame data.Alterado na versão 3.4: Added support for unseekable files.
Wave_write objects have the following methods:
- close()¶
Make sure nframes is correct, and close the file if it was opened by
wave
. This method is called upon object collection. It will raise an exception if the output stream is not seekable and nframes does not match the number of frames actually written.
- setnchannels(n)¶
Define o número de canais.
- setsampwidth(n)¶
Set the sample width to n bytes.
- setframerate(n)¶
Set the frame rate to n.
Alterado na versão 3.2: A non-integral input to this method is rounded to the nearest integer.
- setnframes(n)¶
Set the number of frames to n. This will be changed later if the number of frames actually written is different (this update attempt will raise 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)¶
The tuple should be
(nchannels, sampwidth, framerate, nframes, comptype, compname)
, with values valid for theset*()
methods. Sets all parameters.
- tell()¶
Return current position in the file, with the same disclaimer for the
Wave_read.tell()
andWave_read.setpos()
methods.
- writeframesraw(data)¶
Escreve quadros de áudio, sem corrigir nframes.
Alterado na versão 3.4: Todo objeto byte ou similar agora é aceito.
- writeframes(data)¶
Write audio frames and make sure nframes is correct. It will raise an error if the output stream is not seekable and the total number of frames that have been written after data has been written does not match the previously set value for nframes.
Alterado na versão 3.4: Todo objeto byte ou similar agora é aceito.
Note that it is invalid to set any parameters after calling
writeframes()
orwriteframesraw()
, and any attempt to do so will raisewave.Error
.