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