blob: 76ff871d82b95542cd08f083b8ba966fb5396c75 [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
16Audio files have a number of parameters that describe the audio data.
17The sampling rate or frame rate is the number of times per second the
18sound is sampled. The number of channels indicate if the audio is
19mono, stereo, or quadro. Each frame consists of one sample per
20channel. The sample size is the size in bytes of each sample. Thus a
Guido van Rossumecde7811995-03-28 13:35:14 +000021frame consists of \var{nchannels}*\var{samplesize} bytes, and a
22second's worth of audio consists of
23\var{nchannels}*\var{samplesize}*\var{framerate} bytes.
24
25For example, CD quality audio has a sample size of two bytes (16
26bits), uses two channels (stereo) and has a frame rate of 44,100
27frames/second. This gives a frame size of 4 bytes (2*2), and a
28second's worth occupies 2*2*44100 bytes, i.e.\ 176,400 bytes.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000029
Fred Drakefc576191998-04-04 07:15:02 +000030Module \module{aifc} defines the following function:
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000031
Fred Drakecce10901998-03-17 06:33:25 +000032\begin{funcdesc}{open}{file, mode}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000033Open an AIFF or AIFF-C file and return an object instance with
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000034methods that are described below. The argument file is either a
35string naming a file or a file object. The mode is either the string
Guido van Rossum470be141995-03-17 16:07:09 +000036\code{'r'} when the file must be opened for reading, or \code{'w'}
37when the file must be opened for writing. When used for writing, the
38file object should be seekable, unless you know ahead of time how many
39samples you are going to write in total and use
Fred Drakefc576191998-04-04 07:15:02 +000040\method{writeframesraw()} and \method{setnframes()}.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000041\end{funcdesc}
42
Fred Drakefc576191998-04-04 07:15:02 +000043Objects returned by \function{open()} when a file is opened for
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000044reading have the following methods:
45
Fred Drakefc576191998-04-04 07:15:02 +000046\begin{methoddesc}[aifc]{getnchannels}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000047Return the number of audio channels (1 for mono, 2 for stereo).
Fred Drakefc576191998-04-04 07:15:02 +000048\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000049
Fred Drakefc576191998-04-04 07:15:02 +000050\begin{methoddesc}[aifc]{getsampwidth}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000051Return the size in bytes of individual samples.
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]{getframerate}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000055Return the sampling rate (number of audio frames per second).
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]{getnframes}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000059Return the number of audio frames in the file.
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]{getcomptype}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000063Return a four-character string describing the type of compression used
64in the audio file. For AIFF files, the returned value is
65\code{'NONE'}.
Fred Drakefc576191998-04-04 07:15:02 +000066\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000067
Fred Drakefc576191998-04-04 07:15:02 +000068\begin{methoddesc}[aifc]{getcompname}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000069Return a human-readable description of the type of compression used in
70the audio file. For AIFF files, the returned value is \code{'not
71compressed'}.
Fred Drakefc576191998-04-04 07:15:02 +000072\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000073
Fred Drakefc576191998-04-04 07:15:02 +000074\begin{methoddesc}[aifc]{getparams}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000075Return a tuple consisting of all of the above values in the above
76order.
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]{getmarkers}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000080Return a list of markers in the audio file. A marker consists of a
81tuple of three elements. The first is the mark ID (an integer), the
82second is the mark position in frames from the beginning of the data
83(an integer), the third is the name of the mark (a string).
Fred Drakefc576191998-04-04 07:15:02 +000084\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000085
Fred Drakefc576191998-04-04 07:15:02 +000086\begin{methoddesc}[aifc]{getmark}{id}
87Return the tuple as described in \method{getmarkers()} for the mark
88with the given \var{id}.
89\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000090
Fred Drakefc576191998-04-04 07:15:02 +000091\begin{methoddesc}[aifc]{readframes}{nframes}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000092Read and return the next \var{nframes} frames from the audio file. The
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000093returned data is a string containing for each frame the uncompressed
94samples of all channels.
Fred Drakefc576191998-04-04 07:15:02 +000095\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000096
Fred Drakefc576191998-04-04 07:15:02 +000097\begin{methoddesc}[aifc]{rewind}{}
98Rewind the read pointer. The next \method{readframes()} will start from
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +000099the beginning.
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]{setpos}{pos}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000103Seek to the specified frame number.
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]{tell}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000107Return the current 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]{close}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000111Close the AIFF file. After calling this method, the object can no
112longer be used.
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 +0000115Objects returned by \function{open()} when a file is opened for
116writing have all the above methods, except for \method{readframes()} and
117\method{setpos()}. In addition the following methods exist. The
118\method{get*()} methods can only be called after the corresponding
119\method{set*()} methods have been called. Before the first
120\method{writeframes()} or \method{writeframesraw()}, all parameters
121except for the number of frames must be filled in.
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000122
Fred Drakefc576191998-04-04 07:15:02 +0000123\begin{methoddesc}[aifc]{aiff}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000124Create an AIFF file. The default is that an AIFF-C file is created,
Fred Drakeb666c151998-02-13 22:22:36 +0000125unless the name of the file ends in \code{'.aiff'} in which case the
126default is an AIFF file.
Fred Drakefc576191998-04-04 07:15:02 +0000127\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000128
Fred Drakefc576191998-04-04 07:15:02 +0000129\begin{methoddesc}[aifc]{aifc}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000130Create an AIFF-C file. The default is that an AIFF-C file is created,
Fred Drakeb666c151998-02-13 22:22:36 +0000131unless the name of the file ends in \code{'.aiff'} in which case the
132default is an AIFF file.
Fred Drakefc576191998-04-04 07:15:02 +0000133\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000134
Fred Drakefc576191998-04-04 07:15:02 +0000135\begin{methoddesc}[aifc]{setnchannels}{nchannels}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000136Specify the number of channels in the audio 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]{setsampwidth}{width}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000140Specify the size in bytes of audio samples.
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]{setframerate}{rate}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000144Specify the sampling frequency in frames per second.
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]{setnframes}{nframes}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000148Specify the number of frames that are to be written to the audio file.
149If this parameter is not set, or not set correctly, the file needs to
150support seeking.
Fred Drakefc576191998-04-04 07:15:02 +0000151\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000152
Fred Drakefc576191998-04-04 07:15:02 +0000153\begin{methoddesc}[aifc]{setcomptype}{type, name}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000154Specify the compression type. If not specified, the audio data will
155not be compressed. In AIFF files, compression is not possible. The
156name parameter should be a human-readable description of the
157compression type, the type parameter should be a four-character
158string. Currently the following compression types are supported:
159NONE, ULAW, ALAW, G722.
Fred Drakefc576191998-04-04 07:15:02 +0000160\index{u-LAW}
161\index{A-LAW}
162\index{G.722}
163\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000164
Fred Drakefc576191998-04-04 07:15:02 +0000165\begin{methoddesc}[aifc]{setparams}{nchannels, sampwidth, framerate, comptype, compname}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000166Set all the above parameters at once. The argument is a tuple
167consisting of the various parameters. This means that it is possible
Fred Drakefc576191998-04-04 07:15:02 +0000168to use the result of a \method{getparams()} call as argument to
169\method{setparams()}.
170\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000171
Fred Drakefc576191998-04-04 07:15:02 +0000172\begin{methoddesc}[aifc]{setmark}{id, pos, name}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000173Add a mark with the given id (larger than 0), and the given name at
174the given position. This method can be called at any time before
Fred Drakefc576191998-04-04 07:15:02 +0000175\method{close()}.
176\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000177
Fred Drakefc576191998-04-04 07:15:02 +0000178\begin{methoddesc}[aifc]{tell}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000179Return the current write position in the output file. Useful in
Fred Drakefc576191998-04-04 07:15:02 +0000180combination with \method{setmark()}.
181\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000182
Fred Drakefc576191998-04-04 07:15:02 +0000183\begin{methoddesc}[aifc]{writeframes}{data}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000184Write data to the output file. This method can only be called after
185the audio file parameters have been set.
Fred Drakefc576191998-04-04 07:15:02 +0000186\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000187
Fred Drakefc576191998-04-04 07:15:02 +0000188\begin{methoddesc}[aifc]{writeframesraw}{data}
189Like \method{writeframes()}, except that the header of the audio file
190is not updated.
191\end{methoddesc}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000192
Fred Drakefc576191998-04-04 07:15:02 +0000193\begin{methoddesc}[aifc]{close}{}
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000194Close the AIFF file. The header of the file is updated to reflect the
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000195actual size of the audio data. After calling this method, the object
Sjoerd Mullendercd57dc31994-09-29 16:46:42 +0000196can no longer be used.
Fred Drakefc576191998-04-04 07:15:02 +0000197\end{methoddesc}