Oops -- Lib/Test should be Lib/test, of course!
[python/dscho.git] / Doc / libaifc.tex
blobf86b1d8682ad728fc2fc92702288b9dccadc7850
1 \section{Standard Module \module{aifc}}
2 \label{module-aifc}
3 \stmodindex{aifc}
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 \index{Audio Interchange File Format}
10 \index{AIFF}
11 \index{AIFF-C}
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
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.
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.
27 Module \module{aifc} defines the following function:
29 \begin{funcdesc}{open}{file, mode}
30 Open an AIFF or AIFF-C file and return an object instance with
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
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
37 \method{writeframesraw()} and \method{setnframes()}.
38 \end{funcdesc}
40 Objects returned by \function{open()} when a file is opened for
41 reading have the following methods:
43 \begin{methoddesc}[aifc]{getnchannels}{}
44 Return the number of audio channels (1 for mono, 2 for stereo).
45 \end{methoddesc}
47 \begin{methoddesc}[aifc]{getsampwidth}{}
48 Return the size in bytes of individual samples.
49 \end{methoddesc}
51 \begin{methoddesc}[aifc]{getframerate}{}
52 Return the sampling rate (number of audio frames per second).
53 \end{methoddesc}
55 \begin{methoddesc}[aifc]{getnframes}{}
56 Return the number of audio frames in the file.
57 \end{methoddesc}
59 \begin{methoddesc}[aifc]{getcomptype}{}
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'}.
63 \end{methoddesc}
65 \begin{methoddesc}[aifc]{getcompname}{}
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'}.
69 \end{methoddesc}
71 \begin{methoddesc}[aifc]{getparams}{}
72 Return a tuple consisting of all of the above values in the above
73 order.
74 \end{methoddesc}
76 \begin{methoddesc}[aifc]{getmarkers}{}
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).
81 \end{methoddesc}
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}
88 \begin{methoddesc}[aifc]{readframes}{nframes}
89 Read and return the next \var{nframes} frames from the audio file. The
90 returned data is a string containing for each frame the uncompressed
91 samples of all channels.
92 \end{methoddesc}
94 \begin{methoddesc}[aifc]{rewind}{}
95 Rewind the read pointer. The next \method{readframes()} will start from
96 the beginning.
97 \end{methoddesc}
99 \begin{methoddesc}[aifc]{setpos}{pos}
100 Seek to the specified frame number.
101 \end{methoddesc}
103 \begin{methoddesc}[aifc]{tell}{}
104 Return the current frame number.
105 \end{methoddesc}
107 \begin{methoddesc}[aifc]{close}{}
108 Close the AIFF file. After calling this method, the object can no
109 longer be used.
110 \end{methoddesc}
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.
120 \begin{methoddesc}[aifc]{aiff}{}
121 Create an AIFF file. The default is that an AIFF-C file is created,
122 unless the name of the file ends in \code{'.aiff'} in which case the
123 default is an AIFF file.
124 \end{methoddesc}
126 \begin{methoddesc}[aifc]{aifc}{}
127 Create an AIFF-C file. The default is that an AIFF-C file is created,
128 unless the name of the file ends in \code{'.aiff'} in which case the
129 default is an AIFF file.
130 \end{methoddesc}
132 \begin{methoddesc}[aifc]{setnchannels}{nchannels}
133 Specify the number of channels in the audio file.
134 \end{methoddesc}
136 \begin{methoddesc}[aifc]{setsampwidth}{width}
137 Specify the size in bytes of audio samples.
138 \end{methoddesc}
140 \begin{methoddesc}[aifc]{setframerate}{rate}
141 Specify the sampling frequency in frames per second.
142 \end{methoddesc}
144 \begin{methoddesc}[aifc]{setnframes}{nframes}
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.
148 \end{methoddesc}
150 \begin{methoddesc}[aifc]{setcomptype}{type, name}
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.
157 \index{u-LAW}
158 \index{A-LAW}
159 \index{G.722}
160 \end{methoddesc}
162 \begin{methoddesc}[aifc]{setparams}{nchannels, sampwidth, framerate, comptype, compname}
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
165 to use the result of a \method{getparams()} call as argument to
166 \method{setparams()}.
167 \end{methoddesc}
169 \begin{methoddesc}[aifc]{setmark}{id, pos, name}
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
172 \method{close()}.
173 \end{methoddesc}
175 \begin{methoddesc}[aifc]{tell}{}
176 Return the current write position in the output file. Useful in
177 combination with \method{setmark()}.
178 \end{methoddesc}
180 \begin{methoddesc}[aifc]{writeframes}{data}
181 Write data to the output file. This method can only be called after
182 the audio file parameters have been set.
183 \end{methoddesc}
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}
190 \begin{methoddesc}[aifc]{close}{}
191 Close the AIFF file. The header of the file is updated to reflect the
192 actual size of the audio data. After calling this method, the object
193 can no longer be used.
194 \end{methoddesc}