blob: 65abe84844263896db791b7224f3e2c8fc915d35 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{aifc} ---
Fred Drake67d229e1999-02-20 04:51:16 +00002 Read and write AIFF and AIFC files}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake67d229e1999-02-20 04:51:16 +00004\declaremodule{standard}{aifc}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Read and write audio files in AIFF or AIFC format.}
6
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +00007
8This module provides support for reading and writing AIFF and AIFF-C
9files. AIFF is Audio Interchange File Format, a format for storing
10digital audio samples in a file. AIFF-C is a newer version of the
11format that includes the ability to compress the audio data.
Fred Drakefc576191998-04-04 07:15:02 +000012\index{Audio Interchange File Format}
13\index{AIFF}
14\index{AIFF-C}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000015
Fred Drake19a0dba1999-01-20 16:26:09 +000016\strong{Caveat:} Some operations may only work under IRIX; these will
17raise \exception{ImportError} when attempting to import the
18\module{cl} module, which is only available on IRIX.
19
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000020Audio files have a number of parameters that describe the audio data.
21The sampling rate or frame rate is the number of times per second the
22sound is sampled. The number of channels indicate if the audio is
23mono, stereo, or quadro. Each frame consists of one sample per
24channel. The sample size is the size in bytes of each sample. Thus a
Guido van Rossumecde7811995-03-28 13:35:14 +000025frame consists of \var{nchannels}*\var{samplesize} bytes, and a
26second's worth of audio consists of
27\var{nchannels}*\var{samplesize}*\var{framerate} bytes.
28
29For example, CD quality audio has a sample size of two bytes (16
30bits), uses two channels (stereo) and has a frame rate of 44,100
31frames/second. This gives a frame size of 4 bytes (2*2), and a
Fred Drake91f2f262001-07-06 19:28:48 +000032second's worth occupies 2*2*44100 bytes (176,400 bytes).
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000033
Fred Drakefc576191998-04-04 07:15:02 +000034Module \module{aifc} defines the following function:
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000035
Fred Drake89ab1631999-12-21 18:31:30 +000036\begin{funcdesc}{open}{file\optional{, mode}}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000037Open an AIFF or AIFF-C file and return an object instance with
Fred Drake89ab1631999-12-21 18:31:30 +000038methods that are described below. The argument \var{file} is either a
39string naming a file or a file object. \var{mode} must be \code{'r'}
40or \code{'rb'} when the file must be opened for reading, or \code{'w'}
41or \code{'wb'} when the file must be opened for writing. If omitted,
42\code{\var{file}.mode} is used if it exists, otherwise \code{'rb'} is
43used. When used for writing, the file object should be seekable,
44unless you know ahead of time how many samples you are going to write
45in total and use \method{writeframesraw()} and \method{setnframes()}.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000046\end{funcdesc}
47
Fred Drakefc576191998-04-04 07:15:02 +000048Objects returned by \function{open()} when a file is opened for
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000049reading have the following methods:
50
Fred Drakefc576191998-04-04 07:15:02 +000051\begin{methoddesc}[aifc]{getnchannels}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000052Return the number of audio channels (1 for mono, 2 for stereo).
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]{getsampwidth}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000056Return the size in bytes of individual samples.
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]{getframerate}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000060Return the sampling rate (number of audio frames per second).
Fred Drakefc576191998-04-04 07:15:02 +000061\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000062
Fred Drakefc576191998-04-04 07:15:02 +000063\begin{methoddesc}[aifc]{getnframes}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000064Return the number of audio frames in the file.
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]{getcomptype}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000068Return a four-character string describing the type of compression used
69in the audio file. For AIFF files, the returned value is
70\code{'NONE'}.
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]{getcompname}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000074Return a human-readable description of the type of compression used in
75the audio file. For AIFF files, the returned value is \code{'not
76compressed'}.
Fred Drakefc576191998-04-04 07:15:02 +000077\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000078
Fred Drakefc576191998-04-04 07:15:02 +000079\begin{methoddesc}[aifc]{getparams}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000080Return a tuple consisting of all of the above values in the above
81order.
Fred Drakefc576191998-04-04 07:15:02 +000082\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000083
Fred Drakefc576191998-04-04 07:15:02 +000084\begin{methoddesc}[aifc]{getmarkers}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000085Return a list of markers in the audio file. A marker consists of a
86tuple of three elements. The first is the mark ID (an integer), the
87second is the mark position in frames from the beginning of the data
88(an integer), the third is the name of the mark (a string).
Fred Drakefc576191998-04-04 07:15:02 +000089\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000090
Fred Drakefc576191998-04-04 07:15:02 +000091\begin{methoddesc}[aifc]{getmark}{id}
92Return the tuple as described in \method{getmarkers()} for the mark
93with the given \var{id}.
94\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000095
Fred Drakefc576191998-04-04 07:15:02 +000096\begin{methoddesc}[aifc]{readframes}{nframes}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000097Read and return the next \var{nframes} frames from the audio file. The
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000098returned data is a string containing for each frame the uncompressed
99samples of all channels.
Fred Drakefc576191998-04-04 07:15:02 +0000100\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000101
Fred Drakefc576191998-04-04 07:15:02 +0000102\begin{methoddesc}[aifc]{rewind}{}
103Rewind the read pointer. The next \method{readframes()} will start from
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000104the beginning.
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]{setpos}{pos}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000108Seek to the specified frame number.
Fred Drakefc576191998-04-04 07:15:02 +0000109\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000110
Fred Drakefc576191998-04-04 07:15:02 +0000111\begin{methoddesc}[aifc]{tell}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000112Return the current frame number.
Fred Drakefc576191998-04-04 07:15:02 +0000113\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000114
Fred Drakefc576191998-04-04 07:15:02 +0000115\begin{methoddesc}[aifc]{close}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000116Close the AIFF file. After calling this method, the object can no
117longer be used.
Fred Drakefc576191998-04-04 07:15:02 +0000118\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000119
Fred Drakefc576191998-04-04 07:15:02 +0000120Objects returned by \function{open()} when a file is opened for
121writing have all the above methods, except for \method{readframes()} and
122\method{setpos()}. In addition the following methods exist. The
123\method{get*()} methods can only be called after the corresponding
124\method{set*()} methods have been called. Before the first
125\method{writeframes()} or \method{writeframesraw()}, all parameters
126except for the number of frames must be filled in.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000127
Fred Drakefc576191998-04-04 07:15:02 +0000128\begin{methoddesc}[aifc]{aiff}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000129Create an AIFF 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]{aifc}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000135Create an AIFF-C file. The default is that an AIFF-C file is created,
Fred Drakeb666c151998-02-13 22:22:36 +0000136unless the name of the file ends in \code{'.aiff'} in which case the
137default is an AIFF file.
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]{setnchannels}{nchannels}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000141Specify the number of channels in the audio file.
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]{setsampwidth}{width}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000145Specify the size in bytes of audio samples.
Fred Drakefc576191998-04-04 07:15:02 +0000146\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000147
Fred Drakefc576191998-04-04 07:15:02 +0000148\begin{methoddesc}[aifc]{setframerate}{rate}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000149Specify the sampling frequency in frames per second.
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]{setnframes}{nframes}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000153Specify the number of frames that are to be written to the audio file.
154If this parameter is not set, or not set correctly, the file needs to
155support seeking.
Fred Drakefc576191998-04-04 07:15:02 +0000156\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000157
Fred Drakefc576191998-04-04 07:15:02 +0000158\begin{methoddesc}[aifc]{setcomptype}{type, name}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000159Specify the compression type. If not specified, the audio data will
160not be compressed. In AIFF files, compression is not possible. The
161name parameter should be a human-readable description of the
162compression type, the type parameter should be a four-character
163string. Currently the following compression types are supported:
164NONE, ULAW, ALAW, G722.
Fred Drakefc576191998-04-04 07:15:02 +0000165\index{u-LAW}
166\index{A-LAW}
167\index{G.722}
168\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000169
Fred Drakefc576191998-04-04 07:15:02 +0000170\begin{methoddesc}[aifc]{setparams}{nchannels, sampwidth, framerate, comptype, compname}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000171Set all the above parameters at once. The argument is a tuple
172consisting of the various parameters. This means that it is possible
Fred Drakefc576191998-04-04 07:15:02 +0000173to use the result of a \method{getparams()} call as argument to
174\method{setparams()}.
175\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000176
Fred Drakefc576191998-04-04 07:15:02 +0000177\begin{methoddesc}[aifc]{setmark}{id, pos, name}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000178Add a mark with the given id (larger than 0), and the given name at
179the given position. This method can be called at any time before
Fred Drakefc576191998-04-04 07:15:02 +0000180\method{close()}.
181\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000182
Fred Drakefc576191998-04-04 07:15:02 +0000183\begin{methoddesc}[aifc]{tell}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000184Return the current write position in the output file. Useful in
Fred Drakefc576191998-04-04 07:15:02 +0000185combination with \method{setmark()}.
186\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000187
Fred Drakefc576191998-04-04 07:15:02 +0000188\begin{methoddesc}[aifc]{writeframes}{data}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000189Write data to the output file. This method can only be called after
190the audio file parameters have been set.
Fred Drakefc576191998-04-04 07:15:02 +0000191\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000192
Fred Drakefc576191998-04-04 07:15:02 +0000193\begin{methoddesc}[aifc]{writeframesraw}{data}
194Like \method{writeframes()}, except that the header of the audio file
195is not updated.
196\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000197
Fred Drakefc576191998-04-04 07:15:02 +0000198\begin{methoddesc}[aifc]{close}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000199Close the AIFF file. The header of the file is updated to reflect the
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000200actual size of the audio data. After calling this method, the object
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000201can no longer be used.
Fred Drakefc576191998-04-04 07:15:02 +0000202\end{methoddesc}