源代码: Lib/wave.py
The wave 模块为 WAV 声音格式提供方便接口。它不支持压缩/解压,但支持单声道/立体声。
The wave 模块定义了以下函数和异常:
若 file is a string, open the file by that name, otherwise treat it as a seekable file-like object. mode can be any of
注意:它不允许读/写 WAV 文件。
A mode of 'r' or 'rb' 返回 Wave_read 对象,而 mode of 'w' or 'wb' 返回 Wave_write 对象。若 mode 被省略且像文件对象被传递作为 file , file.mode 被用作默认值对于 mode ( 'b' flag is still added if necessary).
若传入像文件对象,wave 对象将不关闭它当它的 close() 方法被调用;关闭文件对象是调用者的责任。
错误被引发,当某些东西因违反 WAV 规范或命中实现缺陷而不可实现时。
Wave_read 对象,如返回通过 open() ,拥有下列方法:
返回音频通道数 ( 1 为单声道, 2 为立体声)。
返回采样宽度,以字节为单位。
返回采样频率。
返回音频帧数。
返回压缩类型 ( 'NONE' 是唯一支持的类型)。
人性化可读版本的 getcomptype() 。通常 'not compressed' 平行 'NONE' .
返回元组 (nchannels, sampwidth, framerate, nframes, comptype, compname) ,相当于输出对于 get*() 方法。
读取并返回最多 n frames of audio, as a string of bytes.
将文件指针倒带到音频流的开头。
定义以下 2 方法是为了兼容 aifc 模块,且不会做任何有趣的事情。
返回 None .
引发错误。
以下 2 种法定义了在它们之间兼容的 position 术语,否则从属实现。
将文件指针设置到指定位置。
返回当前文件指针位置。
Wave_write 对象,如返回通过 open() ,拥有下列方法:
确保 nframes 正确,并关闭文件若它的打开是通过 wave . This method is called upon object collection.
设置通道数。
将采样宽度设为 n 字节。
将帧速率设为 n .
将帧数设为 n . This will be changed later if more frames are written.
设置压缩类型和描述。目前,仅压缩类型 NONE 受支持,意味着不压缩。
The tuple 应该为 (nchannels, sampwidth, framerate, nframes, comptype, compname) ,具有有效值对于 set*() 方法。设置所有参数。
返回文件的当前位置,具有相同免责声明对于 Wave_read.tell() and Wave_read.setpos() 方法。
写入音频帧,不校正 nframes .
写入音频帧并确保 nframes is correct.
注意:设置任何参数无效,之前有调用 writeframes() or writeframesraw() ,且任何试图这样做会引发 wave.Error .