• sunau —- 读写 Sun AU 文件
    • AU_read 对象
    • AU_write 对象

    sunau —- 读写 Sun AU 文件

    源代码:Lib/sunau.py


    sunau 模拟提供了一个处理 Sun AU 声音格式的便利接口。请注意此模块与 aifcwave 是兼容接口的。

    音频文件由标头和数据组成。标头的字段为:

    内容
    magic word四个字节 .snd
    header size标头的大小,包括信息,以字节为单位。
    data size数据的物理大小,以字节为单位。
    编码指示音频样本的编码方式。
    sample rate采样率
    # of channels采样中的通道数。
    info提供音频文件描述的ASCII字符串(用空字节填充)。

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

    The sunau module defines the following functions:

    • sunau.open(file, mode)
    • If file is a string, open the file by that name, otherwise treat it as aseekable file-like object. mode can be any of

      • 'r'
      • 只读模式。

      • 'w'

      • 只写模式。

    Note that it does not allow read/write files.

    A mode of 'r' returns an AUread object, while a _mode of 'w'or 'wb' returns an AU_write object.

    • sunau.openfp(file, mode)
    • open(),用于向后兼容。

    Deprecated since version 3.7, will be removed in version 3.9.

    The sunau module defines the following exception:

    • exception sunau.Error
    • An error raised when something is impossible because of Sun AU specs orimplementation deficiency.

    The sunau module defines the following data items:

    • sunau.AUDIO_FILE_MAGIC
    • An integer every valid Sun AU file begins with, stored in big-endian form. Thisis 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 thismodule.

    • 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 arenot supported by this module.

    AU_read 对象

    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 automaticallyon deletion.)

    • AU_read.getnchannels()

    • Returns number of audio channels (1 for mono, 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()

    • Human-readable version of getcomptype(). The supported types have therespective names 'CCITT G.711 u-law', 'CCITT G.711 A-law' and 'notcompressed'.

    • AU_read.getparams()

    • 返回一个 namedtuple() (nchannels, sampwidth, framerate, nframes, comptype, compname),与 get*() 方法的输出相同。

    • AUread.readframes(_n)

    • Reads and returns at most n frames of audio, as a bytes object. The datawill be returned in linear format. If the original data is in u-LAW format, itwill be converted.

    • AU_read.rewind()

    • 设置当前文件指针位置。

    以下两个方法都使用指针,具体实现由其底层决定。

    • AUread.setpos(_pos)
    • Set the file pointer to the specified position. Only values returned fromtell() should be used for pos.

    • AU_read.tell()

    • Return current file pointer position. Note that the returned value has nothingto 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

    • AUread.getmark(_id)

    • 引发错误异常。

    AU_write 对象

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

    • AUwrite.setnchannels(_n)
    • 设置声道数。

    • AUwrite.setsampwidth(_n)

    • Set the sample width (in bytes.)

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

    • AUwrite.setframerate(_n)
    • Set the frame rate.

    • AUwrite.setnframes(_n)

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

    • AUwrite.setcomptype(_type, name)

    • Set the compression type and description. Only 'NONE' and 'ULAW' aresupported on output.

    • AUwrite.setparams(_tuple)

    • The tuple should be (nchannels, sampwidth, framerate, nframes, comptype,compname), with values valid for the set*() methods. Set allparameters.

    • AU_write.tell()

    • Return current position in the file, with the same disclaimer for theAU_read.tell() and AU_read.setpos() methods.

    • AUwrite.writeframesraw(_data)

    • 写入音频数据但不更新 nframes

    在 3.4 版更改: 现在可接受任意 bytes-like object。

    • AUwrite.writeframes(_data)
    • 写入音频数据并更新 nframes

    在 3.4 版更改: 现在可接受任意 bytes-like object。

    • AU_write.close()
    • Make sure nframes is correct, and close the file.

    This method is called upon deletion.

    Note that it is invalid to set any parameters after calling writeframes()or writeframesraw().