Fred Drake | b742a42 | 1999-06-23 13:33:40 +0000 | [diff] [blame] | 1 | \section{\module{sunau} --- |
| 2 | Read and write Sun AU files} |
| 3 | |
| 4 | \declaremodule{standard}{sunau} |
| 5 | \sectionauthor{Moshe Zadka}{mzadka@geocities.com} |
| 6 | \modulesynopsis{Provide an interface to the Sun AU sound format.} |
| 7 | |
Fred Drake | c2bac87 | 2000-10-06 21:07:14 +0000 | [diff] [blame] | 8 | The \module{sunau} module provides a convenient interface to the Sun |
| 9 | AU sound format. Note that this module is interface-compatible with |
| 10 | the modules \refmodule{aifc} and \refmodule{wave}. |
| 11 | |
| 12 | An audio file consists of a header followed by the data. The fields |
| 13 | of the header are: |
| 14 | |
| 15 | \begin{tableii}{l|l}{textrm}{Field}{Contents} |
| 16 | \lineii{magic word}{The four bytes \samp{.snd}.} |
| 17 | \lineii{header size}{Size of the header, including info, in bytes.} |
| 18 | \lineii{data size}{Physical size of the data, in bytes.} |
| 19 | \lineii{encoding}{Indicates how the audio samples are encoded.} |
| 20 | \lineii{sample rate}{The sampling rate.} |
| 21 | \lineii{\# of channels}{The number of channels in the samples.} |
| 22 | \lineii{info}{\ASCII{} string giving a description of the audio |
| 23 | file (padded with null bytes).} |
| 24 | \end{tableii} |
| 25 | |
| 26 | Apart from the info field, all header fields are 4 bytes in size. |
| 27 | They are all 32-bit unsigned integers encoded in big-endian byte |
| 28 | order. |
| 29 | |
Fred Drake | b742a42 | 1999-06-23 13:33:40 +0000 | [diff] [blame] | 30 | |
| 31 | The \module{sunau} module defines the following functions: |
| 32 | |
| 33 | \begin{funcdesc}{open}{file, mode} |
| 34 | If \var{file} is a string, open the file by that name, otherwise treat it |
| 35 | as a seekable file-like object. \var{mode} can be any of |
| 36 | \begin{description} |
| 37 | \item[\code{'r'}] Read only mode. |
| 38 | \item[\code{'w'}] Write only mode. |
| 39 | \end{description} |
| 40 | Note that it does not allow read/write files. |
| 41 | |
| 42 | A \var{mode} of \code{'r'} returns a \class{AU_read} |
| 43 | object, while a \var{mode} of \code{'w'} or \code{'wb'} returns |
| 44 | a \class{AU_write} object. |
| 45 | \end{funcdesc} |
| 46 | |
| 47 | \begin{funcdesc}{openfp}{file, mode} |
| 48 | A synonym for \function{open}, maintained for backwards compatibility. |
| 49 | \end{funcdesc} |
| 50 | |
| 51 | The \module{sunau} module defines the following exception: |
| 52 | |
| 53 | \begin{excdesc}{Error} |
| 54 | An error raised when something is impossible because of Sun AU specs or |
| 55 | implementation deficiency. |
| 56 | \end{excdesc} |
| 57 | |
Fred Drake | c2bac87 | 2000-10-06 21:07:14 +0000 | [diff] [blame] | 58 | The \module{sunau} module defines the following data items: |
Fred Drake | b742a42 | 1999-06-23 13:33:40 +0000 | [diff] [blame] | 59 | |
| 60 | \begin{datadesc}{AUDIO_FILE_MAGIC} |
Fred Drake | c2bac87 | 2000-10-06 21:07:14 +0000 | [diff] [blame] | 61 | An integer every valid Sun AU file begins with, stored in big-endian |
| 62 | form. This is the string \samp{.snd} interpreted as an integer. |
| 63 | \end{datadesc} |
| 64 | |
| 65 | \begin{datadesc}{AUDIO_FILE_ENCODING_MULAW_8} |
| 66 | \dataline{AUDIO_FILE_ENCODING_LINEAR_8} |
| 67 | \dataline{AUDIO_FILE_ENCODING_LINEAR_16} |
| 68 | \dataline{AUDIO_FILE_ENCODING_LINEAR_24} |
| 69 | \dataline{AUDIO_FILE_ENCODING_LINEAR_32} |
| 70 | \dataline{AUDIO_FILE_ENCODING_ALAW_8} |
| 71 | Values of the encoding field from the AU header which are supported by |
| 72 | this module. |
| 73 | \end{datadesc} |
| 74 | |
| 75 | \begin{datadesc}{AUDIO_FILE_ENCODING_FLOAT} |
| 76 | \dataline{AUDIO_FILE_ENCODING_DOUBLE} |
| 77 | \dataline{AUDIO_FILE_ENCODING_ADPCM_G721} |
| 78 | \dataline{AUDIO_FILE_ENCODING_ADPCM_G722} |
| 79 | \dataline{AUDIO_FILE_ENCODING_ADPCM_G723_3} |
| 80 | \dataline{AUDIO_FILE_ENCODING_ADPCM_G723_5} |
| 81 | Additional known values of the encoding field from the AU header, but |
| 82 | which are not supported by this module. |
Fred Drake | b742a42 | 1999-06-23 13:33:40 +0000 | [diff] [blame] | 83 | \end{datadesc} |
| 84 | |
| 85 | |
| 86 | \subsection{AU_read Objects \label{au-read-objects}} |
| 87 | |
| 88 | AU_read objects, as returned by \function{open()} above, have the |
| 89 | following methods: |
| 90 | |
| 91 | \begin{methoddesc}[AU_read]{close}{} |
| 92 | Close the stream, and make the instance unusable. (This is |
| 93 | called automatically on deletion.) |
| 94 | \end{methoddesc} |
| 95 | |
| 96 | \begin{methoddesc}[AU_read]{getnchannels}{} |
| 97 | Returns number of audio channels (1 for mone, 2 for stereo). |
| 98 | \end{methoddesc} |
| 99 | |
| 100 | \begin{methoddesc}[AU_read]{getsampwidth}{} |
| 101 | Returns sample width in bytes. |
| 102 | \end{methoddesc} |
| 103 | |
| 104 | \begin{methoddesc}[AU_read]{getframerate}{} |
| 105 | Returns sampling frequency. |
| 106 | \end{methoddesc} |
| 107 | |
| 108 | \begin{methoddesc}[AU_read]{getnframes}{} |
| 109 | Returns number of audio frames. |
| 110 | \end{methoddesc} |
| 111 | |
| 112 | \begin{methoddesc}[AU_read]{getcomptype}{} |
| 113 | Returns compression type. |
| 114 | Supported compression types are \code{'ULAW'}, \code{'ALAW'} and \code{'NONE'}. |
| 115 | \end{methoddesc} |
| 116 | |
| 117 | \begin{methoddesc}[AU_read]{getcompname}{} |
| 118 | Human-readable version of \method{getcomptype()}. |
| 119 | The supported types have the respective names \code{'CCITT G.711 |
| 120 | u-law'}, \code{'CCITT G.711 A-law'} and \code{'not compressed'}. |
| 121 | \end{methoddesc} |
| 122 | |
| 123 | \begin{methoddesc}[AU_read]{getparams}{} |
| 124 | Returns a tuple \code{(\var{nchannels}, \var{sampwidth}, |
| 125 | \var{framerate}, \var{nframes}, \var{comptype}, \var{compname})}, |
| 126 | equivalent to output of the \method{get*()} methods. |
| 127 | \end{methoddesc} |
| 128 | |
| 129 | \begin{methoddesc}[AU_read]{readframes}{n} |
| 130 | Reads and returns at most \var{n} frames of audio, as a string of bytes. |
| 131 | \end{methoddesc} |
| 132 | |
| 133 | \begin{methoddesc}[AU_read]{rewind}{} |
| 134 | Rewind the file pointer to the beginning of the audio stream. |
| 135 | \end{methoddesc} |
| 136 | |
| 137 | The following two methods define a term ``position'' which is compatible |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 138 | between them, and is otherwise implementation dependent. |
Fred Drake | b742a42 | 1999-06-23 13:33:40 +0000 | [diff] [blame] | 139 | |
| 140 | \begin{methoddesc}[AU_read]{setpos}{pos} |
Fred Drake | c2bac87 | 2000-10-06 21:07:14 +0000 | [diff] [blame] | 141 | Set the file pointer to the specified position. Only values returned |
| 142 | from \method{tell()} should be used for \var{pos}. |
Fred Drake | b742a42 | 1999-06-23 13:33:40 +0000 | [diff] [blame] | 143 | \end{methoddesc} |
| 144 | |
| 145 | \begin{methoddesc}[AU_read]{tell}{} |
Fred Drake | c2bac87 | 2000-10-06 21:07:14 +0000 | [diff] [blame] | 146 | Return current file pointer position. Note that the returned value |
| 147 | has nothing to do with the actual position in the file. |
Fred Drake | b742a42 | 1999-06-23 13:33:40 +0000 | [diff] [blame] | 148 | \end{methoddesc} |
| 149 | |
| 150 | The following two functions are defined for compatibility with the |
| 151 | \refmodule{aifc}, and don't do anything interesting. |
| 152 | |
| 153 | \begin{methoddesc}[AU_read]{getmarkers}{} |
| 154 | Returns \code{None}. |
| 155 | \end{methoddesc} |
| 156 | |
| 157 | \begin{methoddesc}[AU_read]{getmark}{id} |
| 158 | Raise an error. |
| 159 | \end{methoddesc} |
| 160 | |
| 161 | |
| 162 | \subsection{AU_write Objects \label{au-write-objects}} |
| 163 | |
| 164 | AU_write objects, as returned by \function{open()} above, have the |
| 165 | following methods: |
| 166 | |
| 167 | \begin{methoddesc}[AU_write]{setnchannels}{n} |
| 168 | Set the number of channels. |
| 169 | \end{methoddesc} |
| 170 | |
| 171 | \begin{methoddesc}[AU_write]{setsampwidth}{n} |
| 172 | Set the sample width (in bytes.) |
| 173 | \end{methoddesc} |
| 174 | |
| 175 | \begin{methoddesc}[AU_write]{setframerate}{n} |
| 176 | Set the frame rate. |
| 177 | \end{methoddesc} |
| 178 | |
| 179 | \begin{methoddesc}[AU_write]{setnframes}{n} |
| 180 | Set the number of frames. This can be later changed, when and if more |
| 181 | frames are written. |
| 182 | \end{methoddesc} |
| 183 | |
| 184 | |
| 185 | \begin{methoddesc}[AU_write]{setcomptype}{type, name} |
| 186 | Set the compression type and description. |
| 187 | Only \code{'NONE'} and \code{'ULAW'} are supported on output. |
| 188 | \end{methoddesc} |
| 189 | |
| 190 | \begin{methoddesc}[AU_write]{setparams}{tuple} |
| 191 | The \var{tuple} should be \code{(\var{nchannels}, \var{sampwidth}, |
| 192 | \var{framerate}, \var{nframes}, \var{comptype}, \var{compname})}, with |
| 193 | values valid for the \method{set*()} methods. Set all parameters. |
| 194 | \end{methoddesc} |
| 195 | |
| 196 | \begin{methoddesc}[AU_write]{tell}{} |
| 197 | Return current position in the file, with the same disclaimer for |
| 198 | the \method{AU_read.tell()} and \method{AU_read.setpos()} methods. |
| 199 | \end{methoddesc} |
| 200 | |
| 201 | \begin{methoddesc}[AU_write]{writeframesraw}{data} |
| 202 | Write audio frames, without correcting \var{nframes}. |
| 203 | \end{methoddesc} |
| 204 | |
| 205 | \begin{methoddesc}[AU_write]{writeframes}{data} |
| 206 | Write audio frames and make sure \var{nframes} is correct. |
| 207 | \end{methoddesc} |
| 208 | |
| 209 | \begin{methoddesc}[AU_write]{close}{} |
| 210 | Make sure \var{nframes} is correct, and close the file. |
| 211 | |
| 212 | This method is called upon deletion. |
| 213 | \end{methoddesc} |
| 214 | |
| 215 | Note that it is invalid to set any parameters after calling |
| 216 | \method{writeframes()} or \method{writeframesraw()}. |