21.5. wave — 读写 WAV 文件

源代码: Lib/wave.py


The wave 模块为 WAV 声音格式提供方便接口。它不支持压缩/解压,但支持单声道/立体声。

The wave 模块定义了以下函数和异常:

wave. open ( file [ , mode ] )

file is a string, open the file by that name, otherwise treat it as a seekable file-like object. mode can be any of

'r' , 'rb'

只读模式。

'w' , 'wb'

只写模式。

注意:它不允许读/写 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() 方法被调用;关闭文件对象是调用者的责任。

wave. openfp ( file , mode )

同义词 open() , maintained for backwards compatibility.

exception wave. Error

错误被引发,当某些东西因违反 WAV 规范或命中实现缺陷而不可实现时。

21.5.1. Wave_read 对象

Wave_read 对象,如返回通过 open() ,拥有下列方法:

Wave_read. close ( )

关闭流若它被打开通过 wave ,并使实例不可用。这是在对象集合上自动调用的。

Wave_read. getnchannels ( )

返回音频通道数 ( 1 为单声道, 2 为立体声)。

Wave_read. getsampwidth ( )

返回采样宽度,以字节为单位。

Wave_read. getframerate ( )

返回采样频率。

Wave_read. getnframes ( )

返回音频帧数。

Wave_read. getcomptype ( )

返回压缩类型 ( 'NONE' 是唯一支持的类型)。

Wave_read. getcompname ( )

人性化可读版本的 getcomptype() 。通常 'not compressed' 平行 'NONE' .

Wave_read. getparams ( )

返回元组 (nchannels, sampwidth, framerate, nframes, comptype, compname) ,相当于输出对于 get*() 方法。

Wave_read. readframes ( n )

读取并返回最多 n frames of audio, as a string of bytes.

Wave_read. rewind ( )

将文件指针倒带到音频流的开头。

定义以下 2 方法是为了兼容 aifc 模块,且不会做任何有趣的事情。

Wave_read. getmarkers ( )

返回 None .

Wave_read. getmark ( id )

引发错误。

以下 2 种法定义了在它们之间兼容的 position 术语,否则从属实现。

Wave_read. setpos ( pos )

将文件指针设置到指定位置。

Wave_read. tell ( )

返回当前文件指针位置。

21.5.2. Wave_write 对象

Wave_write 对象,如返回通过 open() ,拥有下列方法:

Wave_write. close ( )

确保 nframes 正确,并关闭文件若它的打开是通过 wave . This method is called upon object collection.

Wave_write. setnchannels ( n )

设置通道数。

Wave_write. setsampwidth ( n )

将采样宽度设为 n 字节。

Wave_write. setframerate ( n )

将帧速率设为 n .

Wave_write. setnframes ( n )

将帧数设为 n . This will be changed later if more frames are written.

Wave_write. setcomptype ( type , 名称 )

设置压缩类型和描述。目前,仅压缩类型 NONE 受支持,意味着不压缩。

Wave_write. setparams ( tuple )

The tuple 应该为 (nchannels, sampwidth, framerate, nframes, comptype, compname) ,具有有效值对于 set*() 方法。设置所有参数。

Wave_write. tell ( )

返回文件的当前位置,具有相同免责声明对于 Wave_read.tell() and Wave_read.setpos() 方法。

Wave_write. writeframesraw ( data )

写入音频帧,不校正 nframes .

Wave_write. writeframes ( data )

写入音频帧并确保 nframes is correct.

注意:设置任何参数无效,之前有调用 writeframes() or writeframesraw() ,且任何试图这样做会引发 wave.Error .

内容表

上一话题

21.4. sunau — 读写 Sun AU 文件

下一话题

21.6. chunk — 读取 IFF 分块数据

本页

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1