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