aifc
--- 读写 AIFF 和 AIFC 文件¶
源代码: Lib/aifc.py
本模块提供读写 AIFF 和 AIFF-C 文件的支持。AIFF 是音频交换文件格式 (Audio Interchange File Format),一种用于在文件中存储数字音频采样的格式。AIFF-C 是该格式的更新版本,其中包括压缩音频数据的功能。
音频文件内有许多参数,用于描述音频数据。采样率或帧率是每秒对声音采样的次数。通道数表示音频是单声道,双声道还是四声道。每个通道的每个帧包含一次采样。采样大小是以字节表示的每次采样的大小。因此,一帧由 nchannels * samplesize
(通道数*采样大小)字节组成,而一秒钟的音频包含 nchannels * samplesize * framerate
(通道数*采样大小*帧率)字节。
例如,CD 质量的音频采样大小为 2 字节(16位),使用 2 个声道(立体声),且帧速率为 44,100 帧/秒。这表示帧大小为 4 字节 (2*2),一秒钟占用 2*2*44100 字节(176,400 字节)。
aifc
模块定义了以下函数:
-
aifc.
open
(file, mode=None)¶ 打开一个 AIFF 或 AIFF-C 文件并返回一个对象实例,该实例具有下方描述的方法。参数 file 是文件名称字符串或 文件对象。当打开文件用于读取时,mode 必须为
'r'
或'rb'
,当打开文件用于写入时,mode 必须为'w'
或'wb'
。如果该参数省略,则使用file.mode
的值(如果有),否则使用'rb'
。当文件用于写入时,文件对象应该支持 seek 操作,除非提前获知写入的采样总数,并使用writeframesraw()
和setnframes()
。open()
函数可以在with
语句中使用。当with
块执行完毕,将调用close()
方法。在 3.4 版更改: 支持了
with
语句。
当打开文件用于读取时,由 open()
返回的对象具有以下几种方法:
-
aifc.
getnchannels
()¶ 返回音频的通道数(单声道为 1,立体声为 2)。
-
aifc.
getsampwidth
()¶ 返回以字节表示的单个采样的大小。
-
aifc.
getframerate
()¶ 返回采样率(每秒的音频帧数)。
-
aifc.
getnframes
()¶ 返回文件中的音频帧总数。
-
aifc.
getcomptype
()¶ 返回一个长度为 4 的字节数组,描述了音频文件中使用的压缩类型。对于 AIFF 文件,返回值为
b'NONE'
。
-
aifc.
getcompname
()¶ 返回一个字节数组,可转换为人类可读的描述,描述的是音频文件中使用的压缩类型。对于 AIFF 文件,返回值为
b'not compressed'
。
-
aifc.
getparams
()¶ 返回一个
namedtuple()
(nchannels, sampwidth, framerate, nframes, comptype, compname)
,与get*()
方法的输出相同。
-
aifc.
getmarkers
()¶ 返回一个列表,包含音频文件中的所有标记。标记由一个 3 元素的元组组成。第一个元素是标记 ID(整数),第二个是标记位置,从数据开头算起的帧数(整数),第三个是标记的名称(字符串)。
-
aifc.
getmark
(id)¶ 根据传入的标记 id 返回元组,元组与
getmarkers()
中描述的一致。
-
aifc.
readframes
(nframes)¶ 从音频文件读取并返回后续 nframes 个帧。返回的数据是一个字符串,包含每个帧所有通道的未压缩采样值。
-
aifc.
rewind
()¶ 倒回读取指针。下一次
readframes()
将从头开始。
-
aifc.
setpos
(pos)¶ 移动读取指针到指定的帧上。
-
aifc.
tell
()¶ 返回当前的帧号。
-
aifc.
close
()¶ 关闭 AIFF 文件。调用此方法后,对象将无法再使用。
打开文件用于写入时,open()
返回的对象具有上述所有方法,但 readframes()
和 setpos()
除外,并额外具备了以下方法。只有调用了 set*()
方法之后,才能调用相应的 get*()
方法。在首次调用 writeframes()
或 writeframesraw()
之前,必须填写除帧数以外的所有参数。
-
aifc.
aiff
()¶ Create an AIFF file. The default is that an AIFF-C file is created, unless the name of the file ends in
'.aiff'
in which case the default is an AIFF file.
-
aifc.
aifc
()¶ Create an AIFF-C file. The default is that an AIFF-C file is created, unless the name of the file ends in
'.aiff'
in which case the default is an AIFF file.
-
aifc.
setnchannels
(nchannels)¶ Specify the number of channels in the audio file.
-
aifc.
setsampwidth
(width)¶ Specify the size in bytes of audio samples.
-
aifc.
setframerate
(rate)¶ Specify the sampling frequency in frames per second.
-
aifc.
setnframes
(nframes)¶ Specify the number of frames that are to be written to the audio file. If this parameter is not set, or not set correctly, the file needs to support seeking.
-
aifc.
setcomptype
(type, name)¶ Specify the compression type. If not specified, the audio data will not be compressed. In AIFF files, compression is not possible. The name parameter should be a human-readable description of the compression type as a bytes array, the type parameter should be a bytes array of length 4. Currently the following compression types are supported:
b'NONE'
,b'ULAW'
,b'ALAW'
,b'G722'
.
-
aifc.
setparams
(nchannels, sampwidth, framerate, comptype, compname)¶ Set all the above parameters at once. The argument is a tuple consisting of the various parameters. This means that it is possible to use the result of a
getparams()
call as argument tosetparams()
.
-
aifc.
setmark
(id, pos, name)¶ Add a mark with the given id (larger than 0), and the given name at the given position. This method can be called at any time before
close()
.
-
aifc.
tell
() Return the current write position in the output file. Useful in combination with
setmark()
.
-
aifc.
writeframes
(data)¶ Write data to the output file. This method can only be called after the audio file parameters have been set.
在 3.4 版更改: 现在可接受任意 bytes-like object。
-
aifc.
writeframesraw
(data)¶ Like
writeframes()
, except that the header of the audio file is not updated.在 3.4 版更改: 现在可接受任意 bytes-like object。
-
aifc.
close
() Close the AIFF file. The header of the file is updated to reflect the actual size of the audio data. After calling this method, the object can no longer be used.