blob: f8a6ab7ef84bb3860d6296e0b8f182cb2de41478 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`aifc` --- Read and write AIFF and AIFC files
2==================================================
3
4.. module:: aifc
5 :synopsis: Read and write audio files in AIFF or AIFC format.
6
7
8.. index::
9 single: Audio Interchange File Format
10 single: AIFF
11 single: AIFF-C
12
Raymond Hettingeread49752011-01-24 16:28:06 +000013**Source code:** :source:`Lib/aifc.py`
14
15--------------
16
Georg Brandl116aa622007-08-15 14:28:22 +000017This module provides support for reading and writing AIFF and AIFF-C files.
18AIFF is Audio Interchange File Format, a format for storing digital audio
19samples in a file. AIFF-C is a newer version of the format that includes the
20ability to compress the audio data.
21
Georg Brandle720c0a2009-04-27 16:20:50 +000022.. note::
Georg Brandl48310cd2009-01-03 21:18:54 +000023
Guido van Rossumda27fd22007-08-17 00:24:54 +000024 Some operations may only work under IRIX; these will raise :exc:`ImportError`
Georg Brandle720c0a2009-04-27 16:20:50 +000025 when attempting to import the :mod:`cl` module, which is only available on
26 IRIX.
Georg Brandl116aa622007-08-15 14:28:22 +000027
28Audio files have a number of parameters that describe the audio data. The
29sampling rate or frame rate is the number of times per second the sound is
30sampled. The number of channels indicate if the audio is mono, stereo, or
31quadro. Each frame consists of one sample per channel. The sample size is the
32size in bytes of each sample. Thus a frame consists of
33*nchannels*\**samplesize* bytes, and a second's worth of audio consists of
34*nchannels*\**samplesize*\**framerate* bytes.
35
36For example, CD quality audio has a sample size of two bytes (16 bits), uses two
37channels (stereo) and has a frame rate of 44,100 frames/second. This gives a
38frame size of 4 bytes (2\*2), and a second's worth occupies 2\*2\*44100 bytes
39(176,400 bytes).
40
41Module :mod:`aifc` defines the following function:
42
43
Georg Brandlb868a662009-04-02 02:56:10 +000044.. function:: open(file, mode=None)
Georg Brandl116aa622007-08-15 14:28:22 +000045
46 Open an AIFF or AIFF-C file and return an object instance with methods that are
Antoine Pitrou11cb9612010-09-15 11:11:28 +000047 described below. The argument *file* is either a string naming a file or a
48 :term:`file object`. *mode* must be ``'r'`` or ``'rb'`` when the file must be
49 opened for reading, or ``'w'`` or ``'wb'`` when the file must be opened for writing.
50 If omitted, ``file.mode`` is used if it exists, otherwise ``'rb'`` is used. When
Georg Brandl116aa622007-08-15 14:28:22 +000051 used for writing, the file object should be seekable, unless you know ahead of
52 time how many samples you are going to write in total and use
53 :meth:`writeframesraw` and :meth:`setnframes`.
Serhiy Storchaka44c66c72012-12-29 22:54:49 +020054 Objects returned by :func:`.open` also supports the :keyword:`with` statement.
55
56.. versionchanged:: 3.4
57 Support for the :keyword:`with` statement was added.
Georg Brandl116aa622007-08-15 14:28:22 +000058
Georg Brandl502d9a52009-07-26 15:02:41 +000059Objects returned by :func:`.open` when a file is opened for reading have the
Georg Brandl116aa622007-08-15 14:28:22 +000060following methods:
61
62
63.. method:: aifc.getnchannels()
64
65 Return the number of audio channels (1 for mono, 2 for stereo).
66
67
68.. method:: aifc.getsampwidth()
69
70 Return the size in bytes of individual samples.
71
72
73.. method:: aifc.getframerate()
74
75 Return the sampling rate (number of audio frames per second).
76
77
78.. method:: aifc.getnframes()
79
80 Return the number of audio frames in the file.
81
82
83.. method:: aifc.getcomptype()
84
R. David Murraye08a66a2009-04-29 21:50:39 +000085 Return a bytes array of length 4 describing the type of compression
86 used in the audio file. For AIFF files, the returned value is
87 ``b'NONE'``.
Georg Brandl116aa622007-08-15 14:28:22 +000088
89
90.. method:: aifc.getcompname()
91
R. David Murraye08a66a2009-04-29 21:50:39 +000092 Return a bytes array convertible to a human-readable description
93 of the type of compression used in the audio file. For AIFF files,
94 the returned value is ``b'not compressed'``.
Georg Brandl116aa622007-08-15 14:28:22 +000095
96
97.. method:: aifc.getparams()
98
99 Return a tuple consisting of all of the above values in the above order.
100
101
102.. method:: aifc.getmarkers()
103
104 Return a list of markers in the audio file. A marker consists of a tuple of
105 three elements. The first is the mark ID (an integer), the second is the mark
106 position in frames from the beginning of the data (an integer), the third is the
107 name of the mark (a string).
108
109
110.. method:: aifc.getmark(id)
111
112 Return the tuple as described in :meth:`getmarkers` for the mark with the given
113 *id*.
114
115
116.. method:: aifc.readframes(nframes)
117
118 Read and return the next *nframes* frames from the audio file. The returned
119 data is a string containing for each frame the uncompressed samples of all
120 channels.
121
122
123.. method:: aifc.rewind()
124
125 Rewind the read pointer. The next :meth:`readframes` will start from the
126 beginning.
127
128
129.. method:: aifc.setpos(pos)
130
131 Seek to the specified frame number.
132
133
134.. method:: aifc.tell()
135
136 Return the current frame number.
137
138
139.. method:: aifc.close()
140
141 Close the AIFF file. After calling this method, the object can no longer be
142 used.
143
Georg Brandl502d9a52009-07-26 15:02:41 +0000144Objects returned by :func:`.open` when a file is opened for writing have all the
Georg Brandl116aa622007-08-15 14:28:22 +0000145above methods, except for :meth:`readframes` and :meth:`setpos`. In addition
146the following methods exist. The :meth:`get\*` methods can only be called after
147the corresponding :meth:`set\*` methods have been called. Before the first
148:meth:`writeframes` or :meth:`writeframesraw`, all parameters except for the
149number of frames must be filled in.
150
151
152.. method:: aifc.aiff()
153
154 Create an AIFF file. The default is that an AIFF-C file is created, unless the
155 name of the file ends in ``'.aiff'`` in which case the default is an AIFF file.
156
157
158.. method:: aifc.aifc()
159
160 Create an AIFF-C file. The default is that an AIFF-C file is created, unless
161 the name of the file ends in ``'.aiff'`` in which case the default is an AIFF
162 file.
163
164
165.. method:: aifc.setnchannels(nchannels)
166
167 Specify the number of channels in the audio file.
168
169
170.. method:: aifc.setsampwidth(width)
171
172 Specify the size in bytes of audio samples.
173
174
175.. method:: aifc.setframerate(rate)
176
177 Specify the sampling frequency in frames per second.
178
179
180.. method:: aifc.setnframes(nframes)
181
182 Specify the number of frames that are to be written to the audio file. If this
183 parameter is not set, or not set correctly, the file needs to support seeking.
184
185
186.. method:: aifc.setcomptype(type, name)
187
188 .. index::
189 single: u-LAW
190 single: A-LAW
191 single: G.722
192
R. David Murraye08a66a2009-04-29 21:50:39 +0000193 Specify the compression type. If not specified, the audio data will
194 not be compressed. In AIFF files, compression is not possible.
195 The name parameter should be a human-readable description of the
196 compression type as a bytes array, the type parameter should be a
197 bytes array of length 4. Currently the following compression types
198 are supported: ``b'NONE'``, ``b'ULAW'``, ``b'ALAW'``, ``b'G722'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000199
200
201.. method:: aifc.setparams(nchannels, sampwidth, framerate, comptype, compname)
202
203 Set all the above parameters at once. The argument is a tuple consisting of the
204 various parameters. This means that it is possible to use the result of a
205 :meth:`getparams` call as argument to :meth:`setparams`.
206
207
208.. method:: aifc.setmark(id, pos, name)
209
210 Add a mark with the given id (larger than 0), and the given name at the given
211 position. This method can be called at any time before :meth:`close`.
212
213
214.. method:: aifc.tell()
215
216 Return the current write position in the output file. Useful in combination
217 with :meth:`setmark`.
218
219
220.. method:: aifc.writeframes(data)
221
222 Write data to the output file. This method can only be called after the audio
223 file parameters have been set.
224
225
226.. method:: aifc.writeframesraw(data)
227
228 Like :meth:`writeframes`, except that the header of the audio file is not
229 updated.
230
231
232.. method:: aifc.close()
233
234 Close the AIFF file. The header of the file is updated to reflect the actual
235 size of the audio data. After calling this method, the object can no longer be
236 used.
237