22.3. sunau — 读写 Sun AU 文件

源代码: Lib/sunau.py


The sunau 模块为 Sun AU 声音格式提供方便接口。 注意,此模块的接口兼容模块 aifc and wave .

音频文件由 Header 头及之后的数据组成。头字段:

字段 内容
magic word 4 字节 .snd .
header size Size of the header, including info, in bytes.
data size Physical size of the data, in bytes.
encoding Indicates how the audio samples are encoded.
sample rate The sampling rate.
# of channels The number of channels in the samples.
info ASCII string giving a description of the audio file (padded with null bytes).

Apart from the info field, all header fields are 4 bytes in size. They are all 32-bit unsigned integers encoded in big-endian byte order.

The sunau 模块定义了下列函数:

sunau. 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'
只读模式。
'w'
只写模式。

注意,它不允许读/写文件。

A mode of 'r' 返回 AU_read 对象,而 mode of 'w' or 'wb' 返回 AU_write 对象。

sunau. openfp ( file , mode )

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

The sunau 模块定义以下异常:

exception sunau. Error

An error raised when something is impossible because of Sun AU specs or implementation deficiency.

The sunau 模块定义了下列数据项:

sunau. AUDIO_FILE_MAGIC

An integer every valid Sun AU file begins with, stored in big-endian form. This is the string .snd interpreted as an integer.

sunau. AUDIO_FILE_ENCODING_MULAW_8
sunau. AUDIO_FILE_ENCODING_LINEAR_8
sunau. AUDIO_FILE_ENCODING_LINEAR_16
sunau. AUDIO_FILE_ENCODING_LINEAR_24
sunau. AUDIO_FILE_ENCODING_LINEAR_32
sunau. AUDIO_FILE_ENCODING_ALAW_8

Values of the encoding field from the AU header which are supported by this module.

sunau. AUDIO_FILE_ENCODING_FLOAT
sunau. AUDIO_FILE_ENCODING_DOUBLE
sunau. AUDIO_FILE_ENCODING_ADPCM_G721
sunau. AUDIO_FILE_ENCODING_ADPCM_G722
sunau. AUDIO_FILE_ENCODING_ADPCM_G723_3
sunau. AUDIO_FILE_ENCODING_ADPCM_G723_5

Additional known values of the encoding field from the AU header, but which are not supported by this module.

22.3.1. AU_read Objects

AU_read objects, as returned by open() above, have the following methods:

AU_read. close ( )

Close the stream, and make the instance unusable. (This is called automatically on deletion.)

AU_read. getnchannels ( )

Returns number of audio channels (1 for mone, 2 for stereo).

AU_read. getsampwidth ( )

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

AU_read. getframerate ( )

返回采样频率。

AU_read. getnframes ( )

返回音频帧数。

AU_read. getcomptype ( )

Returns compression type. Supported compression types are 'ULAW' , 'ALAW' and 'NONE' .

AU_read. getcompname ( )

人性化可读版本的 getcomptype() . The supported types have the respective names 'CCITT G.711 u-law' , 'CCITT G.711 A-law' and 'not compressed' .

AU_read. getparams ( )

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

AU_read. readframes ( n )

读取并返回最多 n frames of audio, as a string of bytes. The data will be returned in linear format. If the original data is in u-LAW format, it will be converted.

AU_read. rewind ( )

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

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

AU_read. setpos ( pos )

Set the file pointer to the specified position. Only values returned from tell() should be used for pos .

AU_read. tell ( )

Return current file pointer position. Note that the returned value has nothing to do with the actual position in the file.

The following two functions are defined for compatibility with the aifc , and don’t do anything interesting.

AU_read. getmarkers ( )

返回 None .

AU_read. getmark ( id )

引发错误。

22.3.2. AU_write Objects

AU_write objects, as returned by open() above, have the following methods:

AU_write. setnchannels ( n )

设置通道数。

AU_write. setsampwidth ( n )

Set the sample width (in bytes.)

3.4 版改变: Added support for 24-bit samples.

AU_write. setframerate ( n )

Set the frame rate.

AU_write. setnframes ( n )

Set the number of frames. This can be later changed, when and if more frames are written.

AU_write. setcomptype ( type , name )

Set the compression type and description. Only 'NONE' and 'ULAW' are supported on output.

AU_write. setparams ( tuple )

The tuple 应该为 (nchannels, sampwidth, framerate, nframes, comptype, compname) ,具有有效值对于 set*() methods. Set all parameters.

AU_write. tell ( )

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

AU_write. writeframesraw ( data )

写入音频帧,不校正 nframes .

3.4 版改变: 任何 像字节对象 现接受。

AU_write. writeframes ( data )

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

3.4 版改变: 任何 像字节对象 现接受。

AU_write. close ( )

确保 nframes is correct, and close the file.

This method is called upon deletion.

注意:设置任何参数无效,之前有调用 writeframes() or writeframesraw() .