sunau — Read and write Sun AU files¶
Source code:Lib/sunau.py
Thesunau module provides a convenient interface to the Sun AU soundformat. Note that this module is interface-compatible with the modulesaifc andwave.
An audio file consists of a header followed by the data. The fields of theheader are:
Field | Contents |
|---|---|
magic word | The four bytes |
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 theaudio file (padded with null bytes). |
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.
Thesunau module defines the following functions:
sunau.open(file,mode)¶Iffile is a string, open the file by that name, otherwise treat it as aseekable file-like object.mode can be any of
'r'Read only mode.
'w'Write only mode.
Note that it does not allow read/write files.
Amode of
'r'returns anAU_readobject, while amode of'w'or'wb'returns anAU_writeobject.
sunau.openfp(file,mode)¶A synonym for
open(), maintained for backwards compatibility.Deprecated since version 3.7, will be removed in version 3.9.
Thesunau module defines the following exception:
- exception
sunau.Error¶ An error raised when something is impossible because of Sun AU specs orimplementation deficiency.
Thesunau 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
.sndinterpreted 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 Objects¶
AU_read objects, as returned byopen() 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()¶Returns sample width in bytes.
AU_read.getframerate()¶Returns sampling frequency.
AU_read.getnframes()¶Returns number of audio frames.
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'CCITTG.711u-law','CCITTG.711A-law'and'notcompressed'.
AU_read.getparams()¶Returns a
namedtuple()(nchannels,sampwidth,framerate,nframes,comptype,compname), equivalent to output of theget*()methods.
AU_read.readframes(n)¶Reads and returns at mostn frames of audio, as a
bytesobject. The datawill be returned in linear format. If the original data is in u-LAW format, itwill be converted.
AU_read.rewind()¶Rewind the file pointer to the beginning of the audio stream.
The following two methods define a term “position” which is compatible betweenthem, and is otherwise implementation dependent.
AU_read.setpos(pos)¶Set the file pointer to the specified position. Only values returned from
tell()should be used forpos.
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 theaifc,and don’t do anything interesting.
AU_read.getmarkers()¶Returns
None.
AU_read.getmark(id)¶Raise an error.
AU_write Objects¶
AU_write objects, as returned byopen() above, have the following methods:
AU_write.setnchannels(n)¶Set the number of channels.
AU_write.setsampwidth(n)¶Set the sample width (in bytes.)
Changed in version 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 framesare written.
AU_write.setcomptype(type,name)¶Set the compression type and description. Only
'NONE'and'ULAW'aresupported on output.
AU_write.setparams(tuple)¶Thetuple should be
(nchannels,sampwidth,framerate,nframes,comptype,compname), with values valid for theset*()methods. Set allparameters.
AU_write.tell()¶Return current position in the file, with the same disclaimer for the
AU_read.tell()andAU_read.setpos()methods.
AU_write.writeframesraw(data)¶Write audio frames, without correctingnframes.
Changed in version 3.4:Anybytes-like object is now accepted.
AU_write.writeframes(data)¶Write audio frames and make surenframes is correct.
Changed in version 3.4:Anybytes-like object is now accepted.
AU_write.close()¶Make surenframes is correct, and close the file.
This method is called upon deletion.
Note that it is invalid to set any parameters after callingwriteframes()orwriteframesraw().