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