blob: 3b34b4a7a734a80703b7141a1a8e5c8029fff1f9 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{aifc} ---
2 Read and write audio files in AIFF or AIFC format.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{aifc}
4
5\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
32second's worth occupies 2*2*44100 bytes, i.e.\ 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 Drakecce10901998-03-17 06:33:25 +000036\begin{funcdesc}{open}{file, mode}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000037Open an AIFF or AIFF-C file and return an object instance with
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000038methods that are described below. The argument file is either a
39string naming a file or a file object. The mode is either the string
Guido van Rossum470be141995-03-17 16:07:09 +000040\code{'r'} when the file must be opened for reading, or \code{'w'}
41when the file must be opened for writing. When used for writing, the
42file object should be seekable, unless you know ahead of time how many
43samples you are going to write in total and use
Fred Drakefc576191998-04-04 07:15:02 +000044\method{writeframesraw()} and \method{setnframes()}.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000045\end{funcdesc}
46
Fred Drakefc576191998-04-04 07:15:02 +000047Objects returned by \function{open()} when a file is opened for
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000048reading have the following methods:
49
Fred Drakefc576191998-04-04 07:15:02 +000050\begin{methoddesc}[aifc]{getnchannels}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000051Return the number of audio channels (1 for mono, 2 for stereo).
Fred Drakefc576191998-04-04 07:15:02 +000052\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000053
Fred Drakefc576191998-04-04 07:15:02 +000054\begin{methoddesc}[aifc]{getsampwidth}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000055Return the size in bytes of individual samples.
Fred Drakefc576191998-04-04 07:15:02 +000056\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000057
Fred Drakefc576191998-04-04 07:15:02 +000058\begin{methoddesc}[aifc]{getframerate}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000059Return the sampling rate (number of audio frames per second).
Fred Drakefc576191998-04-04 07:15:02 +000060\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000061
Fred Drakefc576191998-04-04 07:15:02 +000062\begin{methoddesc}[aifc]{getnframes}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000063Return the number of audio frames in the file.
Fred Drakefc576191998-04-04 07:15:02 +000064\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000065
Fred Drakefc576191998-04-04 07:15:02 +000066\begin{methoddesc}[aifc]{getcomptype}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000067Return a four-character string describing the type of compression used
68in the audio file. For AIFF files, the returned value is
69\code{'NONE'}.
Fred Drakefc576191998-04-04 07:15:02 +000070\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000071
Fred Drakefc576191998-04-04 07:15:02 +000072\begin{methoddesc}[aifc]{getcompname}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000073Return a human-readable description of the type of compression used in
74the audio file. For AIFF files, the returned value is \code{'not
75compressed'}.
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]{getparams}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000079Return a tuple consisting of all of the above values in the above
80order.
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]{getmarkers}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000084Return a list of markers in the audio file. A marker consists of a
85tuple of three elements. The first is the mark ID (an integer), the
86second is the mark position in frames from the beginning of the data
87(an integer), the third is the name of the mark (a string).
Fred Drakefc576191998-04-04 07:15:02 +000088\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000089
Fred Drakefc576191998-04-04 07:15:02 +000090\begin{methoddesc}[aifc]{getmark}{id}
91Return the tuple as described in \method{getmarkers()} for the mark
92with the given \var{id}.
93\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000094
Fred Drakefc576191998-04-04 07:15:02 +000095\begin{methoddesc}[aifc]{readframes}{nframes}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000096Read and return the next \var{nframes} frames from the audio file. The
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000097returned data is a string containing for each frame the uncompressed
98samples of all channels.
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]{rewind}{}
102Rewind the read pointer. The next \method{readframes()} will start from
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000103the beginning.
Fred Drakefc576191998-04-04 07:15:02 +0000104\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000105
Fred Drakefc576191998-04-04 07:15:02 +0000106\begin{methoddesc}[aifc]{setpos}{pos}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000107Seek to the specified frame number.
Fred Drakefc576191998-04-04 07:15:02 +0000108\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000109
Fred Drakefc576191998-04-04 07:15:02 +0000110\begin{methoddesc}[aifc]{tell}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000111Return the current frame number.
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 +0000114\begin{methoddesc}[aifc]{close}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000115Close the AIFF file. After calling this method, the object can no
116longer be used.
Fred Drakefc576191998-04-04 07:15:02 +0000117\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000118
Fred Drakefc576191998-04-04 07:15:02 +0000119Objects returned by \function{open()} when a file is opened for
120writing have all the above methods, except for \method{readframes()} and
121\method{setpos()}. In addition the following methods exist. The
122\method{get*()} methods can only be called after the corresponding
123\method{set*()} methods have been called. Before the first
124\method{writeframes()} or \method{writeframesraw()}, all parameters
125except for the number of frames must be filled in.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000126
Fred Drakefc576191998-04-04 07:15:02 +0000127\begin{methoddesc}[aifc]{aiff}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000128Create an AIFF file. The default is that an AIFF-C file is created,
Fred Drakeb666c151998-02-13 22:22:36 +0000129unless the name of the file ends in \code{'.aiff'} in which case the
130default is an AIFF file.
Fred Drakefc576191998-04-04 07:15:02 +0000131\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000132
Fred Drakefc576191998-04-04 07:15:02 +0000133\begin{methoddesc}[aifc]{aifc}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000134Create an AIFF-C file. The default is that an AIFF-C file is created,
Fred Drakeb666c151998-02-13 22:22:36 +0000135unless the name of the file ends in \code{'.aiff'} in which case the
136default is an AIFF file.
Fred Drakefc576191998-04-04 07:15:02 +0000137\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000138
Fred Drakefc576191998-04-04 07:15:02 +0000139\begin{methoddesc}[aifc]{setnchannels}{nchannels}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000140Specify the number of channels in the audio file.
Fred Drakefc576191998-04-04 07:15:02 +0000141\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000142
Fred Drakefc576191998-04-04 07:15:02 +0000143\begin{methoddesc}[aifc]{setsampwidth}{width}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000144Specify the size in bytes of audio samples.
Fred Drakefc576191998-04-04 07:15:02 +0000145\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000146
Fred Drakefc576191998-04-04 07:15:02 +0000147\begin{methoddesc}[aifc]{setframerate}{rate}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000148Specify the sampling frequency in frames per second.
Fred Drakefc576191998-04-04 07:15:02 +0000149\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000150
Fred Drakefc576191998-04-04 07:15:02 +0000151\begin{methoddesc}[aifc]{setnframes}{nframes}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000152Specify the number of frames that are to be written to the audio file.
153If this parameter is not set, or not set correctly, the file needs to
154support seeking.
Fred Drakefc576191998-04-04 07:15:02 +0000155\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000156
Fred Drakefc576191998-04-04 07:15:02 +0000157\begin{methoddesc}[aifc]{setcomptype}{type, name}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000158Specify the compression type. If not specified, the audio data will
159not be compressed. In AIFF files, compression is not possible. The
160name parameter should be a human-readable description of the
161compression type, the type parameter should be a four-character
162string. Currently the following compression types are supported:
163NONE, ULAW, ALAW, G722.
Fred Drakefc576191998-04-04 07:15:02 +0000164\index{u-LAW}
165\index{A-LAW}
166\index{G.722}
167\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000168
Fred Drakefc576191998-04-04 07:15:02 +0000169\begin{methoddesc}[aifc]{setparams}{nchannels, sampwidth, framerate, comptype, compname}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000170Set all the above parameters at once. The argument is a tuple
171consisting of the various parameters. This means that it is possible
Fred Drakefc576191998-04-04 07:15:02 +0000172to use the result of a \method{getparams()} call as argument to
173\method{setparams()}.
174\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000175
Fred Drakefc576191998-04-04 07:15:02 +0000176\begin{methoddesc}[aifc]{setmark}{id, pos, name}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000177Add a mark with the given id (larger than 0), and the given name at
178the given position. This method can be called at any time before
Fred Drakefc576191998-04-04 07:15:02 +0000179\method{close()}.
180\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000181
Fred Drakefc576191998-04-04 07:15:02 +0000182\begin{methoddesc}[aifc]{tell}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000183Return the current write position in the output file. Useful in
Fred Drakefc576191998-04-04 07:15:02 +0000184combination with \method{setmark()}.
185\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000186
Fred Drakefc576191998-04-04 07:15:02 +0000187\begin{methoddesc}[aifc]{writeframes}{data}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000188Write data to the output file. This method can only be called after
189the audio file parameters have been set.
Fred Drakefc576191998-04-04 07:15:02 +0000190\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000191
Fred Drakefc576191998-04-04 07:15:02 +0000192\begin{methoddesc}[aifc]{writeframesraw}{data}
193Like \method{writeframes()}, except that the header of the audio file
194is not updated.
195\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000196
Fred Drakefc576191998-04-04 07:15:02 +0000197\begin{methoddesc}[aifc]{close}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000198Close the AIFF file. The header of the file is updated to reflect the
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000199actual size of the audio data. After calling this method, the object
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000200can no longer be used.
Fred Drakefc576191998-04-04 07:15:02 +0000201\end{methoddesc}