Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{aifc} --- |
Fred Drake | 67d229e | 1999-02-20 04:51:16 +0000 | [diff] [blame] | 2 | Read and write AIFF and AIFC files} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 67d229e | 1999-02-20 04:51:16 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{aifc} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Read and write audio files in AIFF or AIFC format.} |
| 6 | |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 7 | |
| 8 | This module provides support for reading and writing AIFF and AIFF-C |
| 9 | files. AIFF is Audio Interchange File Format, a format for storing |
| 10 | digital audio samples in a file. AIFF-C is a newer version of the |
| 11 | format that includes the ability to compress the audio data. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 12 | \index{Audio Interchange File Format} |
| 13 | \index{AIFF} |
| 14 | \index{AIFF-C} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 15 | |
Fred Drake | 19a0dba | 1999-01-20 16:26:09 +0000 | [diff] [blame] | 16 | \strong{Caveat:} Some operations may only work under IRIX; these will |
| 17 | raise \exception{ImportError} when attempting to import the |
| 18 | \module{cl} module, which is only available on IRIX. |
| 19 | |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 20 | Audio files have a number of parameters that describe the audio data. |
| 21 | The sampling rate or frame rate is the number of times per second the |
| 22 | sound is sampled. The number of channels indicate if the audio is |
| 23 | mono, stereo, or quadro. Each frame consists of one sample per |
| 24 | channel. The sample size is the size in bytes of each sample. Thus a |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 25 | frame consists of \var{nchannels}*\var{samplesize} bytes, and a |
| 26 | second's worth of audio consists of |
| 27 | \var{nchannels}*\var{samplesize}*\var{framerate} bytes. |
| 28 | |
| 29 | For example, CD quality audio has a sample size of two bytes (16 |
| 30 | bits), uses two channels (stereo) and has a frame rate of 44,100 |
| 31 | frames/second. This gives a frame size of 4 bytes (2*2), and a |
| 32 | second's worth occupies 2*2*44100 bytes, i.e.\ 176,400 bytes. |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 33 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 34 | Module \module{aifc} defines the following function: |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 35 | |
Fred Drake | 89ab163 | 1999-12-21 18:31:30 +0000 | [diff] [blame] | 36 | \begin{funcdesc}{open}{file\optional{, mode}} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 37 | Open an AIFF or AIFF-C file and return an object instance with |
Fred Drake | 89ab163 | 1999-12-21 18:31:30 +0000 | [diff] [blame] | 38 | methods that are described below. The argument \var{file} is either a |
| 39 | string naming a file or a file object. \var{mode} must be \code{'r'} |
| 40 | or \code{'rb'} when the file must be opened for reading, or \code{'w'} |
| 41 | or \code{'wb'} when the file must be opened for writing. If omitted, |
| 42 | \code{\var{file}.mode} is used if it exists, otherwise \code{'rb'} is |
| 43 | used. When used for writing, the file object should be seekable, |
| 44 | unless you know ahead of time how many samples you are going to write |
| 45 | in total and use \method{writeframesraw()} and \method{setnframes()}. |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 46 | \end{funcdesc} |
| 47 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 48 | Objects returned by \function{open()} when a file is opened for |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 49 | reading have the following methods: |
| 50 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 51 | \begin{methoddesc}[aifc]{getnchannels}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 52 | Return the number of audio channels (1 for mono, 2 for stereo). |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 53 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 54 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 55 | \begin{methoddesc}[aifc]{getsampwidth}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 56 | Return the size in bytes of individual samples. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 57 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 58 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 59 | \begin{methoddesc}[aifc]{getframerate}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 60 | Return the sampling rate (number of audio frames per second). |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 61 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 62 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 63 | \begin{methoddesc}[aifc]{getnframes}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 64 | Return the number of audio frames in the file. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 65 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 66 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 67 | \begin{methoddesc}[aifc]{getcomptype}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 68 | Return a four-character string describing the type of compression used |
| 69 | in the audio file. For AIFF files, the returned value is |
| 70 | \code{'NONE'}. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 71 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 72 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 73 | \begin{methoddesc}[aifc]{getcompname}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 74 | Return a human-readable description of the type of compression used in |
| 75 | the audio file. For AIFF files, the returned value is \code{'not |
| 76 | compressed'}. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 77 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 78 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 79 | \begin{methoddesc}[aifc]{getparams}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 80 | Return a tuple consisting of all of the above values in the above |
| 81 | order. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 82 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 83 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 84 | \begin{methoddesc}[aifc]{getmarkers}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 85 | Return a list of markers in the audio file. A marker consists of a |
| 86 | tuple of three elements. The first is the mark ID (an integer), the |
| 87 | second is the mark position in frames from the beginning of the data |
| 88 | (an integer), the third is the name of the mark (a string). |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 89 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 90 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 91 | \begin{methoddesc}[aifc]{getmark}{id} |
| 92 | Return the tuple as described in \method{getmarkers()} for the mark |
| 93 | with the given \var{id}. |
| 94 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 95 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 96 | \begin{methoddesc}[aifc]{readframes}{nframes} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 97 | Read and return the next \var{nframes} frames from the audio file. The |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 98 | returned data is a string containing for each frame the uncompressed |
| 99 | samples of all channels. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 100 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 101 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 102 | \begin{methoddesc}[aifc]{rewind}{} |
| 103 | Rewind the read pointer. The next \method{readframes()} will start from |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 104 | the beginning. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 105 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 106 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 107 | \begin{methoddesc}[aifc]{setpos}{pos} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 108 | Seek to the specified frame number. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 109 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 110 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 111 | \begin{methoddesc}[aifc]{tell}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 112 | Return the current frame number. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 113 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 114 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 115 | \begin{methoddesc}[aifc]{close}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 116 | Close the AIFF file. After calling this method, the object can no |
| 117 | longer be used. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 118 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 119 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 120 | Objects returned by \function{open()} when a file is opened for |
| 121 | writing have all the above methods, except for \method{readframes()} and |
| 122 | \method{setpos()}. In addition the following methods exist. The |
| 123 | \method{get*()} methods can only be called after the corresponding |
| 124 | \method{set*()} methods have been called. Before the first |
| 125 | \method{writeframes()} or \method{writeframesraw()}, all parameters |
| 126 | except for the number of frames must be filled in. |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 127 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 128 | \begin{methoddesc}[aifc]{aiff}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 129 | Create an AIFF file. The default is that an AIFF-C file is created, |
Fred Drake | b666c15 | 1998-02-13 22:22:36 +0000 | [diff] [blame] | 130 | unless the name of the file ends in \code{'.aiff'} in which case the |
| 131 | default is an AIFF file. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 132 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 133 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 134 | \begin{methoddesc}[aifc]{aifc}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 135 | Create an AIFF-C file. The default is that an AIFF-C file is created, |
Fred Drake | b666c15 | 1998-02-13 22:22:36 +0000 | [diff] [blame] | 136 | unless the name of the file ends in \code{'.aiff'} in which case the |
| 137 | default is an AIFF file. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 138 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 139 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 140 | \begin{methoddesc}[aifc]{setnchannels}{nchannels} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 141 | Specify the number of channels in the audio file. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 142 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 143 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 144 | \begin{methoddesc}[aifc]{setsampwidth}{width} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 145 | Specify the size in bytes of audio samples. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 146 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 147 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 148 | \begin{methoddesc}[aifc]{setframerate}{rate} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 149 | Specify the sampling frequency in frames per second. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 150 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 151 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 152 | \begin{methoddesc}[aifc]{setnframes}{nframes} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 153 | Specify the number of frames that are to be written to the audio file. |
| 154 | If this parameter is not set, or not set correctly, the file needs to |
| 155 | support seeking. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 156 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 157 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 158 | \begin{methoddesc}[aifc]{setcomptype}{type, name} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 159 | Specify the compression type. If not specified, the audio data will |
| 160 | not be compressed. In AIFF files, compression is not possible. The |
| 161 | name parameter should be a human-readable description of the |
| 162 | compression type, the type parameter should be a four-character |
| 163 | string. Currently the following compression types are supported: |
| 164 | NONE, ULAW, ALAW, G722. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 165 | \index{u-LAW} |
| 166 | \index{A-LAW} |
| 167 | \index{G.722} |
| 168 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 169 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 170 | \begin{methoddesc}[aifc]{setparams}{nchannels, sampwidth, framerate, comptype, compname} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 171 | Set all the above parameters at once. The argument is a tuple |
| 172 | consisting of the various parameters. This means that it is possible |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 173 | to use the result of a \method{getparams()} call as argument to |
| 174 | \method{setparams()}. |
| 175 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 176 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 177 | \begin{methoddesc}[aifc]{setmark}{id, pos, name} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 178 | Add a mark with the given id (larger than 0), and the given name at |
| 179 | the given position. This method can be called at any time before |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 180 | \method{close()}. |
| 181 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 182 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 183 | \begin{methoddesc}[aifc]{tell}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 184 | Return the current write position in the output file. Useful in |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 185 | combination with \method{setmark()}. |
| 186 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 187 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 188 | \begin{methoddesc}[aifc]{writeframes}{data} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 189 | Write data to the output file. This method can only be called after |
| 190 | the audio file parameters have been set. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 191 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 192 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 193 | \begin{methoddesc}[aifc]{writeframesraw}{data} |
| 194 | Like \method{writeframes()}, except that the header of the audio file |
| 195 | is not updated. |
| 196 | \end{methoddesc} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 197 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 198 | \begin{methoddesc}[aifc]{close}{} |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 199 | Close the AIFF file. The header of the file is updated to reflect the |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 200 | actual size of the audio data. After calling this method, the object |
Sjoerd Mullender | cd57dc3 | 1994-09-29 16:46:42 +0000 | [diff] [blame] | 201 | can no longer be used. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 202 | \end{methoddesc} |