waveRead and write WAV files读取和写入WAV文件

Source code: Lib/wave.py


The wave module provides a convenient interface to the WAV sound format. wave模块为WAV声音格式提供了方便的接口。It does not support compression/decompression, but it does support mono/stereo.它不支持压缩/解压缩,但支持单声道/立体声。

The wave module defines the following function and exception:wave模块定义了以下功能和异常:

wave.open(file, mode=None)

If file is a string, open the file by that name, otherwise treat it as a file-like object. 如果file是字符串,请按该名称打开文件,否则将其视为类似文件的对象。mode can be:可以是:

'rb'

Read only mode.只读模式。

'wb'

Write only mode.仅写模式。

Note that it does not allow read/write WAV files.请注意,它不允许读/写WAV文件。

A mode of 'rb' returns a Wave_read object, while a mode of 'wb' returns a Wave_write object. 'rb'模式返回一个Wave_read对象,而'wb'模式则返回一个Wave_write对象。If mode is omitted and a file-like object is passed as file, file.mode is used as the default value for mode.如果省略mode,并将类似文件的对象作为file传递,则file.mode将用作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.如果传入类似文件的对象,则在调用其close()方法时,wave对象不会关闭它;调用者有责任关闭文件对象。

The open() function may be used in a with statement. open()函数可以在with语句中使用。When the with block completes, the Wave_read.close() or Wave_write.close() method is called.with块完成后,将调用Wave_read.close()Wave_write.close()方法。

Changed in version 3.4:版本3.4中更改: Added support for unseekable files.增加了对不可查看文件的支持。

exceptionwave.Error

An error raised when something is impossible because it violates the WAV specification or hits an implementation deficiency.由于违反WAV规范或遇到实现缺陷而无法执行某项操作时引发的错误。

Wave_read ObjectsWave_read对象

Wave_read objects, as returned by open(), have the following methods:open()返回的Wave_read对象具有以下方法:

Wave_read.close()

Close the stream if it was opened by wave, and make the instance unusable. 如果流是由wave打开的,请关闭该流,并使实例不可用。This is called automatically on object collection.这在对象集合上自动调用。

Wave_read.getnchannels()

Returns number of audio channels (1 for mono, 2 for stereo).返回音频通道数(单声道为1,立体声为2)。

Wave_read.getsampwidth()

Returns sample width in bytes.返回以字节为单位的样本宽度。

Wave_read.getframerate()

Returns sampling frequency.返回采样频率。

Wave_read.getnframes()

Returns number of audio frames.返回音频帧数。

Wave_read.getcomptype()

Returns compression type ('NONE' is the only supported type).返回压缩类型('NONE'是唯一受支持的类型)。

Wave_read.getcompname()

Human-readable version of getcomptype(). 人类可读版本的getcomptype()Usually 'not compressed' parallels 'NONE'.'not compressed'通常与'NONE'平行。

Wave_read.getparams()

Returns a namedtuple() (nchannels, sampwidth, framerate, nframes, comptype, compname), equivalent to output of the get*() methods.返回namedtuple()(nchannels, sampwidth, framerate, nframes, comptype, compname),相当于get*()方法的输出。

Wave_read.readframes(n)

Reads and returns at most n frames of audio, as a bytes object.bytes对象的形式读取并返回最多n帧音频。

Wave_read.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.以下两种方法是为了与aifc模块兼容而定义的,没有做任何有趣的事情。

Wave_read.getmarkers()

Returns None.返回None

Wave_read.getmark(id)

Raise an error.引发错误。

The following two methods define a term “position” which is compatible between them, and is otherwise implementation dependent.以下两种方法定义了一个术语“位置”,该术语在它们之间是兼容的,否则依赖于实现。

Wave_read.setpos(pos)

Set the file pointer to the specified position.将文件指针设置到指定位置。

Wave_read.tell()

Return current file pointer position.返回当前文件指针位置。

Wave_write Objects对象

For seekable output streams, the wave header will automatically be updated to reflect the number of frames actually written. 对于可搜索的输出流,wave头将自动更新以反映实际写入的帧数。For unseekable streams, the nframes value must be accurate when the first frame data is written. 对于不可观看的流,当写入第一帧数据时,nframes值必须准确。An accurate nframes value can be achieved either by calling setnframes() or setparams() with the number of frames that will be written before close() is called and then using writeframesraw() to write the frame data, or by calling writeframes() with all of the frame data to be written. 通过调用setnframes()setparams()以及调用close()之前将要写入的帧数,然后使用writeframesraw()写入帧数据,或者使用所有要写入的帧数据调用writeframes(),可以实现精确的nframes值。In the latter case writeframes() will calculate the number of frames in the data and set nframes accordingly before writing the frame data.在后一种情况下,writeframes()将计算数据中的帧数,并在写入帧数据之前相应地设置nframes

Wave_write objects, as returned by open(), have the following methods:open()返回的Wave_write对象具有以下方法:

Changed in version 3.4:版本3.4中更改: Added support for unseekable files.增加了对不可查看文件的支持。

Wave_write.close()

Make sure nframes is correct, and close the file if it was opened by wave. 确保nframes是正确的,如果文件是由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.如果输出流不可查找且nframes与实际写入的帧数不匹配,则会引发异常。

Wave_write.setnchannels(n)

Set the number of channels.设置通道数。

Wave_write.setsampwidth(n)

Set the sample width to n bytes.将样本宽度设置为n字节。

Wave_write.setframerate(n)

Set the frame rate to n.将帧速率设置为n

Changed in version 3.2:版本3.2中更改: A non-integral input to this method is rounded to the nearest integer.此方法的非整数输入四舍五入为最接近的整数。

Wave_write.setnframes(n)

Set the number of frames to n. 将帧数设置为nThis 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).如果实际写入的帧数不同,这将在稍后更改(如果输出流不可查找,则此更新尝试将引发错误)。

Wave_write.setcomptype(type, name)

Set the compression type and description. 设置压缩类型和描述。At the moment, only compression type NONE is supported, meaning no compression.目前,仅支持压缩类型NONE,即不支持压缩。

Wave_write.setparams(tuple)

The tuple should be (nchannels, sampwidth, framerate, nframes, comptype, compname), with values valid for the set*() methods. Sets all parameters.tuple应为(nchannels, sampwidth, framerate, nframes, comptype, compname),其值对set*()方法有效。设置所有参数。

Wave_write.tell()

Return current position in the file, with the same disclaimer for the Wave_read.tell() and Wave_read.setpos() methods.返回文件中的当前位置,对Wave_read.tell()Wave_read.setpos()方法使用相同的免责声明。

Wave_write.writeframesraw(data)

Write audio frames, without correcting nframes.写入音频帧,而不校正nframes

Changed in version 3.4:版本3.4中更改: Any bytes-like object is now accepted.现在接受任何类似字节的对象

Wave_write.writeframes(data)

Write audio frames and make sure nframes is correct. 写入音频帧并确保nframes正确。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.如果输出流不可查找,并且data写入后写入的总帧数与之前设置的nframes值不匹配,则会引发错误。

Changed in version 3.4:版本3.4中更改: Any bytes-like object is now accepted.现在接受任何类似字节的对象

Note that it is invalid to set any parameters after calling writeframes() or writeframesraw(), and any attempt to do so will raise wave.Error.请注意,在调用writeframes()writeframesraw()后设置任何参数都是无效的,任何这样做的尝试都会引发wave.Error