blob: f86b1d8682ad728fc2fc92702288b9dccadc7850 [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Standard Module \module{aifc}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-aifc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +00003\stmodindex{aifc}
4
5This module provides support for reading and writing AIFF and AIFF-C
6files. AIFF is Audio Interchange File Format, a format for storing
7digital audio samples in a file. AIFF-C is a newer version of the
8format that includes the ability to compress the audio data.
Fred Drakefc576191998-04-04 07:15:02 +00009\index{Audio Interchange File Format}
10\index{AIFF}
11\index{AIFF-C}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000012
13Audio files have a number of parameters that describe the audio data.
14The sampling rate or frame rate is the number of times per second the
15sound is sampled. The number of channels indicate if the audio is
16mono, stereo, or quadro. Each frame consists of one sample per
17channel. The sample size is the size in bytes of each sample. Thus a
Guido van Rossumecde7811995-03-28 13:35:14 +000018frame consists of \var{nchannels}*\var{samplesize} bytes, and a
19second's worth of audio consists of
20\var{nchannels}*\var{samplesize}*\var{framerate} bytes.
21
22For example, CD quality audio has a sample size of two bytes (16
23bits), uses two channels (stereo) and has a frame rate of 44,100
24frames/second. This gives a frame size of 4 bytes (2*2), and a
25second's worth occupies 2*2*44100 bytes, i.e.\ 176,400 bytes.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000026
Fred Drakefc576191998-04-04 07:15:02 +000027Module \module{aifc} defines the following function:
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000028
Fred Drakecce10901998-03-17 06:33:25 +000029\begin{funcdesc}{open}{file, mode}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000030Open an AIFF or AIFF-C file and return an object instance with
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000031methods that are described below. The argument file is either a
32string naming a file or a file object. The mode is either the string
Guido van Rossum470be141995-03-17 16:07:09 +000033\code{'r'} when the file must be opened for reading, or \code{'w'}
34when the file must be opened for writing. When used for writing, the
35file object should be seekable, unless you know ahead of time how many
36samples you are going to write in total and use
Fred Drakefc576191998-04-04 07:15:02 +000037\method{writeframesraw()} and \method{setnframes()}.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000038\end{funcdesc}
39
Fred Drakefc576191998-04-04 07:15:02 +000040Objects returned by \function{open()} when a file is opened for
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000041reading have the following methods:
42
Fred Drakefc576191998-04-04 07:15:02 +000043\begin{methoddesc}[aifc]{getnchannels}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000044Return the number of audio channels (1 for mono, 2 for stereo).
Fred Drakefc576191998-04-04 07:15:02 +000045\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000046
Fred Drakefc576191998-04-04 07:15:02 +000047\begin{methoddesc}[aifc]{getsampwidth}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000048Return the size in bytes of individual samples.
Fred Drakefc576191998-04-04 07:15:02 +000049\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000050
Fred Drakefc576191998-04-04 07:15:02 +000051\begin{methoddesc}[aifc]{getframerate}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000052Return the sampling rate (number of audio frames per second).
Fred Drakefc576191998-04-04 07:15:02 +000053\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000054
Fred Drakefc576191998-04-04 07:15:02 +000055\begin{methoddesc}[aifc]{getnframes}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000056Return the number of audio frames in the file.
Fred Drakefc576191998-04-04 07:15:02 +000057\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000058
Fred Drakefc576191998-04-04 07:15:02 +000059\begin{methoddesc}[aifc]{getcomptype}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000060Return a four-character string describing the type of compression used
61in the audio file. For AIFF files, the returned value is
62\code{'NONE'}.
Fred Drakefc576191998-04-04 07:15:02 +000063\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000064
Fred Drakefc576191998-04-04 07:15:02 +000065\begin{methoddesc}[aifc]{getcompname}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000066Return a human-readable description of the type of compression used in
67the audio file. For AIFF files, the returned value is \code{'not
68compressed'}.
Fred Drakefc576191998-04-04 07:15:02 +000069\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000070
Fred Drakefc576191998-04-04 07:15:02 +000071\begin{methoddesc}[aifc]{getparams}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000072Return a tuple consisting of all of the above values in the above
73order.
Fred Drakefc576191998-04-04 07:15:02 +000074\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000075
Fred Drakefc576191998-04-04 07:15:02 +000076\begin{methoddesc}[aifc]{getmarkers}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000077Return a list of markers in the audio file. A marker consists of a
78tuple of three elements. The first is the mark ID (an integer), the
79second 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 Drakefc576191998-04-04 07:15:02 +000081\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000082
Fred Drakefc576191998-04-04 07:15:02 +000083\begin{methoddesc}[aifc]{getmark}{id}
84Return the tuple as described in \method{getmarkers()} for the mark
85with the given \var{id}.
86\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000087
Fred Drakefc576191998-04-04 07:15:02 +000088\begin{methoddesc}[aifc]{readframes}{nframes}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000089Read and return the next \var{nframes} frames from the audio file. The
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000090returned data is a string containing for each frame the uncompressed
91samples of all channels.
Fred Drakefc576191998-04-04 07:15:02 +000092\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000093
Fred Drakefc576191998-04-04 07:15:02 +000094\begin{methoddesc}[aifc]{rewind}{}
95Rewind the read pointer. The next \method{readframes()} will start from
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000096the beginning.
Fred Drakefc576191998-04-04 07:15:02 +000097\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000098
Fred Drakefc576191998-04-04 07:15:02 +000099\begin{methoddesc}[aifc]{setpos}{pos}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000100Seek to the specified frame number.
Fred Drakefc576191998-04-04 07:15:02 +0000101\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000102
Fred Drakefc576191998-04-04 07:15:02 +0000103\begin{methoddesc}[aifc]{tell}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000104Return the current frame number.
Fred Drakefc576191998-04-04 07:15:02 +0000105\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000106
Fred Drakefc576191998-04-04 07:15:02 +0000107\begin{methoddesc}[aifc]{close}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000108Close the AIFF file. After calling this method, the object can no
109longer be used.
Fred Drakefc576191998-04-04 07:15:02 +0000110\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000111
Fred Drakefc576191998-04-04 07:15:02 +0000112Objects returned by \function{open()} when a file is opened for
113writing 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
118except for the number of frames must be filled in.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000119
Fred Drakefc576191998-04-04 07:15:02 +0000120\begin{methoddesc}[aifc]{aiff}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000121Create an AIFF file. The default is that an AIFF-C file is created,
Fred Drakeb666c151998-02-13 22:22:36 +0000122unless the name of the file ends in \code{'.aiff'} in which case the
123default is an AIFF file.
Fred Drakefc576191998-04-04 07:15:02 +0000124\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000125
Fred Drakefc576191998-04-04 07:15:02 +0000126\begin{methoddesc}[aifc]{aifc}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000127Create an AIFF-C file. The default is that an AIFF-C file is created,
Fred Drakeb666c151998-02-13 22:22:36 +0000128unless the name of the file ends in \code{'.aiff'} in which case the
129default is an AIFF file.
Fred Drakefc576191998-04-04 07:15:02 +0000130\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000131
Fred Drakefc576191998-04-04 07:15:02 +0000132\begin{methoddesc}[aifc]{setnchannels}{nchannels}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000133Specify the number of channels in the audio file.
Fred Drakefc576191998-04-04 07:15:02 +0000134\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000135
Fred Drakefc576191998-04-04 07:15:02 +0000136\begin{methoddesc}[aifc]{setsampwidth}{width}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000137Specify the size in bytes of audio samples.
Fred Drakefc576191998-04-04 07:15:02 +0000138\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000139
Fred Drakefc576191998-04-04 07:15:02 +0000140\begin{methoddesc}[aifc]{setframerate}{rate}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000141Specify the sampling frequency in frames per second.
Fred Drakefc576191998-04-04 07:15:02 +0000142\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000143
Fred Drakefc576191998-04-04 07:15:02 +0000144\begin{methoddesc}[aifc]{setnframes}{nframes}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000145Specify the number of frames that are to be written to the audio file.
146If this parameter is not set, or not set correctly, the file needs to
147support seeking.
Fred Drakefc576191998-04-04 07:15:02 +0000148\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000149
Fred Drakefc576191998-04-04 07:15:02 +0000150\begin{methoddesc}[aifc]{setcomptype}{type, name}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000151Specify the compression type. If not specified, the audio data will
152not be compressed. In AIFF files, compression is not possible. The
153name parameter should be a human-readable description of the
154compression type, the type parameter should be a four-character
155string. Currently the following compression types are supported:
156NONE, ULAW, ALAW, G722.
Fred Drakefc576191998-04-04 07:15:02 +0000157\index{u-LAW}
158\index{A-LAW}
159\index{G.722}
160\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000161
Fred Drakefc576191998-04-04 07:15:02 +0000162\begin{methoddesc}[aifc]{setparams}{nchannels, sampwidth, framerate, comptype, compname}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000163Set all the above parameters at once. The argument is a tuple
164consisting of the various parameters. This means that it is possible
Fred Drakefc576191998-04-04 07:15:02 +0000165to use the result of a \method{getparams()} call as argument to
166\method{setparams()}.
167\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000168
Fred Drakefc576191998-04-04 07:15:02 +0000169\begin{methoddesc}[aifc]{setmark}{id, pos, name}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000170Add a mark with the given id (larger than 0), and the given name at
171the given position. This method can be called at any time before
Fred Drakefc576191998-04-04 07:15:02 +0000172\method{close()}.
173\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000174
Fred Drakefc576191998-04-04 07:15:02 +0000175\begin{methoddesc}[aifc]{tell}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000176Return the current write position in the output file. Useful in
Fred Drakefc576191998-04-04 07:15:02 +0000177combination with \method{setmark()}.
178\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000179
Fred Drakefc576191998-04-04 07:15:02 +0000180\begin{methoddesc}[aifc]{writeframes}{data}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000181Write data to the output file. This method can only be called after
182the audio file parameters have been set.
Fred Drakefc576191998-04-04 07:15:02 +0000183\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000184
Fred Drakefc576191998-04-04 07:15:02 +0000185\begin{methoddesc}[aifc]{writeframesraw}{data}
186Like \method{writeframes()}, except that the header of the audio file
187is not updated.
188\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000189
Fred Drakefc576191998-04-04 07:15:02 +0000190\begin{methoddesc}[aifc]{close}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000191Close the AIFF file. The header of the file is updated to reflect the
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000192actual size of the audio data. After calling this method, the object
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000193can no longer be used.
Fred Drakefc576191998-04-04 07:15:02 +0000194\end{methoddesc}