blob: d7433e70f2e7eb799bab642f1d711dafb4b00f45 [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Standard Module \module{aifc}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{aifc}
3
4\modulesynopsis{Read and write audio files in AIFF or AIFC format.}
5
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +00006
7This module provides support for reading and writing AIFF and AIFF-C
8files. AIFF is Audio Interchange File Format, a format for storing
9digital audio samples in a file. AIFF-C is a newer version of the
10format that includes the ability to compress the audio data.
Fred Drakefc576191998-04-04 07:15:02 +000011\index{Audio Interchange File Format}
12\index{AIFF}
13\index{AIFF-C}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000014
15Audio files have a number of parameters that describe the audio data.
16The sampling rate or frame rate is the number of times per second the
17sound is sampled. The number of channels indicate if the audio is
18mono, stereo, or quadro. Each frame consists of one sample per
19channel. The sample size is the size in bytes of each sample. Thus a
Guido van Rossumecde7811995-03-28 13:35:14 +000020frame consists of \var{nchannels}*\var{samplesize} bytes, and a
21second's worth of audio consists of
22\var{nchannels}*\var{samplesize}*\var{framerate} bytes.
23
24For example, CD quality audio has a sample size of two bytes (16
25bits), uses two channels (stereo) and has a frame rate of 44,100
26frames/second. This gives a frame size of 4 bytes (2*2), and a
27second's worth occupies 2*2*44100 bytes, i.e.\ 176,400 bytes.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000028
Fred Drakefc576191998-04-04 07:15:02 +000029Module \module{aifc} defines the following function:
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000030
Fred Drakecce10901998-03-17 06:33:25 +000031\begin{funcdesc}{open}{file, mode}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000032Open an AIFF or AIFF-C file and return an object instance with
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000033methods that are described below. The argument file is either a
34string naming a file or a file object. The mode is either the string
Guido van Rossum470be141995-03-17 16:07:09 +000035\code{'r'} when the file must be opened for reading, or \code{'w'}
36when the file must be opened for writing. When used for writing, the
37file object should be seekable, unless you know ahead of time how many
38samples you are going to write in total and use
Fred Drakefc576191998-04-04 07:15:02 +000039\method{writeframesraw()} and \method{setnframes()}.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000040\end{funcdesc}
41
Fred Drakefc576191998-04-04 07:15:02 +000042Objects returned by \function{open()} when a file is opened for
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000043reading have the following methods:
44
Fred Drakefc576191998-04-04 07:15:02 +000045\begin{methoddesc}[aifc]{getnchannels}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000046Return the number of audio channels (1 for mono, 2 for stereo).
Fred Drakefc576191998-04-04 07:15:02 +000047\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000048
Fred Drakefc576191998-04-04 07:15:02 +000049\begin{methoddesc}[aifc]{getsampwidth}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000050Return the size in bytes of individual samples.
Fred Drakefc576191998-04-04 07:15:02 +000051\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000052
Fred Drakefc576191998-04-04 07:15:02 +000053\begin{methoddesc}[aifc]{getframerate}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000054Return the sampling rate (number of audio frames per second).
Fred Drakefc576191998-04-04 07:15:02 +000055\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000056
Fred Drakefc576191998-04-04 07:15:02 +000057\begin{methoddesc}[aifc]{getnframes}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000058Return the number of audio frames in the file.
Fred Drakefc576191998-04-04 07:15:02 +000059\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000060
Fred Drakefc576191998-04-04 07:15:02 +000061\begin{methoddesc}[aifc]{getcomptype}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000062Return a four-character string describing the type of compression used
63in the audio file. For AIFF files, the returned value is
64\code{'NONE'}.
Fred Drakefc576191998-04-04 07:15:02 +000065\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000066
Fred Drakefc576191998-04-04 07:15:02 +000067\begin{methoddesc}[aifc]{getcompname}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000068Return a human-readable description of the type of compression used in
69the audio file. For AIFF files, the returned value is \code{'not
70compressed'}.
Fred Drakefc576191998-04-04 07:15:02 +000071\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000072
Fred Drakefc576191998-04-04 07:15:02 +000073\begin{methoddesc}[aifc]{getparams}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000074Return a tuple consisting of all of the above values in the above
75order.
Fred Drakefc576191998-04-04 07:15:02 +000076\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000077
Fred Drakefc576191998-04-04 07:15:02 +000078\begin{methoddesc}[aifc]{getmarkers}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000079Return a list of markers in the audio file. A marker consists of a
80tuple of three elements. The first is the mark ID (an integer), the
81second is the mark position in frames from the beginning of the data
82(an integer), the third is the name of the mark (a string).
Fred Drakefc576191998-04-04 07:15:02 +000083\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000084
Fred Drakefc576191998-04-04 07:15:02 +000085\begin{methoddesc}[aifc]{getmark}{id}
86Return the tuple as described in \method{getmarkers()} for the mark
87with the given \var{id}.
88\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000089
Fred Drakefc576191998-04-04 07:15:02 +000090\begin{methoddesc}[aifc]{readframes}{nframes}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000091Read and return the next \var{nframes} frames from the audio file. The
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000092returned data is a string containing for each frame the uncompressed
93samples of all channels.
Fred Drakefc576191998-04-04 07:15:02 +000094\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000095
Fred Drakefc576191998-04-04 07:15:02 +000096\begin{methoddesc}[aifc]{rewind}{}
97Rewind the read pointer. The next \method{readframes()} will start from
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000098the beginning.
Fred Drakefc576191998-04-04 07:15:02 +000099\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000100
Fred Drakefc576191998-04-04 07:15:02 +0000101\begin{methoddesc}[aifc]{setpos}{pos}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000102Seek to the specified frame number.
Fred Drakefc576191998-04-04 07:15:02 +0000103\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000104
Fred Drakefc576191998-04-04 07:15:02 +0000105\begin{methoddesc}[aifc]{tell}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000106Return the current frame number.
Fred Drakefc576191998-04-04 07:15:02 +0000107\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000108
Fred Drakefc576191998-04-04 07:15:02 +0000109\begin{methoddesc}[aifc]{close}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000110Close the AIFF file. After calling this method, the object can no
111longer be used.
Fred Drakefc576191998-04-04 07:15:02 +0000112\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000113
Fred Drakefc576191998-04-04 07:15:02 +0000114Objects returned by \function{open()} when a file is opened for
115writing have all the above methods, except for \method{readframes()} and
116\method{setpos()}. In addition the following methods exist. The
117\method{get*()} methods can only be called after the corresponding
118\method{set*()} methods have been called. Before the first
119\method{writeframes()} or \method{writeframesraw()}, all parameters
120except for the number of frames must be filled in.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000121
Fred Drakefc576191998-04-04 07:15:02 +0000122\begin{methoddesc}[aifc]{aiff}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000123Create an AIFF file. The default is that an AIFF-C file is created,
Fred Drakeb666c151998-02-13 22:22:36 +0000124unless the name of the file ends in \code{'.aiff'} in which case the
125default is an AIFF file.
Fred Drakefc576191998-04-04 07:15:02 +0000126\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000127
Fred Drakefc576191998-04-04 07:15:02 +0000128\begin{methoddesc}[aifc]{aifc}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000129Create an AIFF-C file. The default is that an AIFF-C file is created,
Fred Drakeb666c151998-02-13 22:22:36 +0000130unless the name of the file ends in \code{'.aiff'} in which case the
131default is an AIFF file.
Fred Drakefc576191998-04-04 07:15:02 +0000132\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000133
Fred Drakefc576191998-04-04 07:15:02 +0000134\begin{methoddesc}[aifc]{setnchannels}{nchannels}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000135Specify the number of channels in the audio file.
Fred Drakefc576191998-04-04 07:15:02 +0000136\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000137
Fred Drakefc576191998-04-04 07:15:02 +0000138\begin{methoddesc}[aifc]{setsampwidth}{width}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000139Specify the size in bytes of audio samples.
Fred Drakefc576191998-04-04 07:15:02 +0000140\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000141
Fred Drakefc576191998-04-04 07:15:02 +0000142\begin{methoddesc}[aifc]{setframerate}{rate}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000143Specify the sampling frequency in frames per second.
Fred Drakefc576191998-04-04 07:15:02 +0000144\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000145
Fred Drakefc576191998-04-04 07:15:02 +0000146\begin{methoddesc}[aifc]{setnframes}{nframes}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000147Specify the number of frames that are to be written to the audio file.
148If this parameter is not set, or not set correctly, the file needs to
149support seeking.
Fred Drakefc576191998-04-04 07:15:02 +0000150\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000151
Fred Drakefc576191998-04-04 07:15:02 +0000152\begin{methoddesc}[aifc]{setcomptype}{type, name}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000153Specify the compression type. If not specified, the audio data will
154not be compressed. In AIFF files, compression is not possible. The
155name parameter should be a human-readable description of the
156compression type, the type parameter should be a four-character
157string. Currently the following compression types are supported:
158NONE, ULAW, ALAW, G722.
Fred Drakefc576191998-04-04 07:15:02 +0000159\index{u-LAW}
160\index{A-LAW}
161\index{G.722}
162\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000163
Fred Drakefc576191998-04-04 07:15:02 +0000164\begin{methoddesc}[aifc]{setparams}{nchannels, sampwidth, framerate, comptype, compname}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000165Set all the above parameters at once. The argument is a tuple
166consisting of the various parameters. This means that it is possible
Fred Drakefc576191998-04-04 07:15:02 +0000167to use the result of a \method{getparams()} call as argument to
168\method{setparams()}.
169\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000170
Fred Drakefc576191998-04-04 07:15:02 +0000171\begin{methoddesc}[aifc]{setmark}{id, pos, name}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000172Add a mark with the given id (larger than 0), and the given name at
173the given position. This method can be called at any time before
Fred Drakefc576191998-04-04 07:15:02 +0000174\method{close()}.
175\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000176
Fred Drakefc576191998-04-04 07:15:02 +0000177\begin{methoddesc}[aifc]{tell}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000178Return the current write position in the output file. Useful in
Fred Drakefc576191998-04-04 07:15:02 +0000179combination with \method{setmark()}.
180\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000181
Fred Drakefc576191998-04-04 07:15:02 +0000182\begin{methoddesc}[aifc]{writeframes}{data}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000183Write data to the output file. This method can only be called after
184the audio file parameters have been set.
Fred Drakefc576191998-04-04 07:15:02 +0000185\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000186
Fred Drakefc576191998-04-04 07:15:02 +0000187\begin{methoddesc}[aifc]{writeframesraw}{data}
188Like \method{writeframes()}, except that the header of the audio file
189is not updated.
190\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000191
Fred Drakefc576191998-04-04 07:15:02 +0000192\begin{methoddesc}[aifc]{close}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000193Close the AIFF file. The header of the file is updated to reflect the
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000194actual size of the audio data. After calling this method, the object
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000195can no longer be used.
Fred Drakefc576191998-04-04 07:15:02 +0000196\end{methoddesc}