aifcRead and write AIFF and AIFC files读取和写入AIFF和AIFC文件

Source code: Lib/aifc.py

Deprecated since version 3.11: 自3.11版起已弃用:The aifc module is deprecated (see PEP 594 for details).aifc模块已弃用(详见PEP 594)。


This module provides support for reading and writing AIFF and AIFF-C files. 该模块支持读取和写入AIFF和AIFF-C文件。AIFF is Audio Interchange File Format, a format for storing digital audio samples in a file. AIFF是音频交换文件格式,一种用于在文件中存储数字音频样本的格式。AIFF-C is a newer version of the format that includes the ability to compress the audio data.AIFF-C是该格式的更新版本,包括压缩音频数据的能力。

Audio files have a number of parameters that describe the audio data. 音频文件具有描述音频数据的多个参数。The sampling rate or frame rate is the number of times per second the sound is sampled. 采样率或帧速率是每秒对声音进行采样的次数。The number of channels indicate if the audio is mono, stereo, or quadro. 通道数指示音频是单声道、立体声还是四声道。Each frame consists of one sample per channel. 每个帧由每个通道一个样本组成。The sample size is the size in bytes of each sample. 样本大小是每个样本的字节大小。Thus a frame consists of nchannels * samplesize bytes, and a second’s worth of audio consists of nchannels * samplesize * framerate bytes.因此,一帧由nchannels*samplesize字节组成,而一秒钟的音频由nchannels * samplesize * framerate字节组成。

For example, CD quality audio has a sample size of two bytes (16 bits), uses two channels (stereo) and has a frame rate of 44,100 frames/second. 例如,CD质量音频的样本大小为两个字节(16位),使用两个通道(立体声),帧速率为44100帧/秒。This gives a frame size of 4 bytes (2*2), and a second’s worth occupies 2*2*44100 bytes (176,400 bytes).这给出了一个4字节(2*2)的帧大小,而一秒钟的大小占用了2*2*44100字节(176400字节)。

Module aifc defines the following function:模块aifc定义以下功能:

aifc.open(file, mode=None)

Open an AIFF or AIFF-C file and return an object instance with methods that are described below. 打开AIFF或AIFF-C文件并返回一个对象实例,方法如下所述。The argument file is either a string naming a file or a file object. 参数文件是命名file文件对象的字符串。mode must be 'r' or 'rb' when the file must be opened for reading, or 'w' or 'wb' when the file must be opened for writing. 当必须打开文件进行读取时,mode必须为'r''rb';当必须打开该文件进行写入时,模式为'w''wb'If omitted, file.mode is used if it exists, otherwise 'rb' is used. 如果省略,则使用file.mode(如果存在),否则使用'rb'When used for writing, the file object should be seekable, unless you know ahead of time how many samples you are going to write in total and use writeframesraw() and setnframes(). 当用于写入时,文件对象应该是可查找的,除非事先知道总共要写入多少个样本,并使用writeframesraw()setnframes()The open() function may be used in a with statement. open()函数可以在with语句中使用。When the with block completes, the close() method is called.with块完成时,将调用close()方法。

Changed in version 3.4:版本3.4中更改: Support for the with statement was added.添加了对with语句的支持。

Objects returned by open() when a file is opened for reading have the following methods:打开文件进行读取时,open()返回的对象具有以下方法:

aifc.getnchannels()

Return the number of audio channels (1 for mono, 2 for stereo).返回音频通道数(1个用于单声道,2个用于立体声)。

aifc.getsampwidth()

Return the size in bytes of individual samples.返回单个样本的字节大小。

aifc.getframerate()

Return the sampling rate (number of audio frames per second).返回采样率(每秒音频帧数)。

aifc.getnframes()

Return the number of audio frames in the file.返回文件中的音频帧数。

aifc.getcomptype()

Return a bytes array of length 4 describing the type of compression used in the audio file. 返回长度为4的字节数组,描述音频文件中使用的压缩类型。For AIFF files, the returned value is b'NONE'.对于AIFF文件,返回值为b'NONE'

aifc.getcompname()

Return a bytes array convertible to a human-readable description of the type of compression used in the audio file. 返回可转换为音频文件中使用的压缩类型的可读描述的字节数组。For AIFF files, the returned value is b'not compressed'.对于AIFF文件,返回值为b'not compressed'

aifc.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*()方法的输出。

aifc.getmarkers()

Return a list of markers in the audio file. 返回音频文件中的标记列表。A marker consists of a tuple of three elements. 标记由三个元素的元组组成。The first is the mark ID (an integer), the second is the mark position in frames from the beginning of the data (an integer), the third is the name of the mark (a string).第一个是标记ID(整数),第二个是从数据开始的帧中的标记位置(整数);第三个是标记的名称(字符串)。

aifc.getmark(id)

Return the tuple as described in getmarkers() for the mark with the given id.返回具有给定id的标记的元组,如getmarkers()中所述。

aifc.readframes(nframes)

Read and return the next nframes frames from the audio file. 读取并返回音频文件中的下一个nframes帧。The returned data is a string containing for each frame the uncompressed samples of all channels.返回的数据是一个字符串,每个帧包含所有通道的未压缩样本。

aifc.rewind()

Rewind the read pointer. 回放读取游标。The next readframes() will start from the beginning.下一个readframes()将从头开始。

aifc.setpos(pos)

Seek to the specified frame number.查找指定的帧数。

aifc.tell()

Return the current frame number.返回当前帧号。

aifc.close()

Close the AIFF file. After calling this method, the object can no longer be used.关闭AIFF文件。调用此方法后,无法再使用该对象。

Objects returned by open() when a file is opened for writing have all the above methods, except for readframes() and setpos(). 打开文件进行写入时,open()返回的对象具有上述所有方法,readframes()setpos()除外。In addition the following methods exist. 此外,还存在以下方法。The get*() methods can only be called after the corresponding set*() methods have been called. 只有在调用了相应的set*()方法之后,才能调用get*()Before the first writeframes() or writeframesraw(), all parameters except for the number of frames must be filled in.在第一个writeframes()writeframesraw()之前,必须填写除帧数之外的所有参数。

aifc.aiff()

Create an AIFF file. 创建AIFF文件。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.默认值是创建AIFF-C文件,除非文件名以'.aiff'结尾,在这种情况下,默认值是AIFF文件。

aifc.aifc()

Create an AIFF-C file. 创建AIFF-C文件。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.默认值是创建AIFF-C文件,除非文件名以'.aiff'结尾,在这种情况下,默认值是AIFF文件。

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. 在AIFF文件中,压缩是不可能的。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. name参数应该是压缩类型的可读描述,如字节数组,type参数应该是长度为4的字节数组。Currently the following compression types are supported: b'NONE', b'ULAW', b'ALAW', b'G722'.目前支持以下压缩类型: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 to setparams().这意味着可以将getparams()调用的结果用作setparams()的参数。

aifc.setmark(id, pos, name)

Add a mark with the given id (larger than 0), and the given name at the given position. 添加具有给定id(大于0)的标记,并在给定位置添加给定名称。This method can be called at any time before close().close()之前,可以随时调用此方法。

aifc.tell()

Return the current write position in the output file. 返回输出文件中的当前写入位置。Useful in combination with setmark().setmark()结合使用非常有用。

aifc.writeframes(data)

Write data to the output file. 将数据写入输出文件。This method can only be called after the audio file parameters have been set.只有在设置音频文件参数后才能调用此方法。

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

aifc.writeframesraw(data)

Like writeframes(), except that the header of the audio file is not updated.writeframes()类似,只是音频文件的头未更新。

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

aifc.close()

Close the AIFF file. 关闭AIFF文件。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.调用此方法后,无法再使用该对象。