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