22.3. sunau — Read and write Sun AU files

Code source : Lib/sunau.py

The sunau module provides a convenient interface to the Sun AU sound format. Note that this module is interface-compatible with the modules aifc and wave.

An audio file consists of a header followed by the data. The fields of the header are:

Field Sommaire
magic word The four bytes .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 module defines the following functions:

sunau.open(file, mode)

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

Mode lecture seule.
Mode écriture seule.

Note that it does not allow read/write files.

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

sunau.openfp(file, mode)

Un synonyme de open(), maintenu pour la rétrocompatibilité.

The sunau module defines the following exception:

exception sunau.Error

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

The sunau module defines the following data items:


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


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


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:


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


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


Renvoie la largeur de l’échantillon en octets.


Renvoie la fréquence d’échantillonnage.


Renvoie le nombre de trames audio.


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


Human-readable version of getcomptype(). The supported types have the respective names 'CCITT G.711 u-law', 'CCITT G.711 A-law' and 'not compressed'.


Renvoie une namedtuple() (nchannels, sampwidth, framerate, nframes, comptype, compname), équivalent à la sortie des méthodes get*().


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


Remet le pointeur de fichier au début du flux audio.

Les deux fonctions suivantes utilisent le vocabulaire « position ». Ces positions sont compatible entre elles, la « position » de l’un est compatible avec la « position » de l’autre. Cette position est dépendante de l’implémentation.


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


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.


Renvoie None.


Lève une erreur.

22.3.2. AU_write Objects

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


Définit le nombre de canaux.


Set the sample width (in bytes.)

Modifié dans la version 3.4: Added support for 24-bit samples.


Set the frame rate.


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.


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


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


Écrit les trames audio sans corriger nframes.

Modifié dans la version 3.4: N’importe quel bytes-like object est maintenant accepté.


Write audio frames and make sure nframes is correct.

Modifié dans la version 3.4: N’importe quel bytes-like object est maintenant accepté.


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().