blob: fc141e9eeb810339c627e7a72eb2f446a53e445f [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`sunau` --- Read and write Sun AU files
2============================================
3
4.. module:: sunau
5 :synopsis: Provide an interface to the Sun AU sound format.
6.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
7
8
9The :mod:`sunau` module provides a convenient interface to the Sun AU sound
10format. Note that this module is interface-compatible with the modules
11:mod:`aifc` and :mod:`wave`.
12
13An audio file consists of a header followed by the data. The fields of the
14header are:
15
16+---------------+-----------------------------------------------+
17| Field | Contents |
18+===============+===============================================+
19| magic word | The four bytes ``.snd``. |
20+---------------+-----------------------------------------------+
21| header size | Size of the header, including info, in bytes. |
22+---------------+-----------------------------------------------+
23| data size | Physical size of the data, in bytes. |
24+---------------+-----------------------------------------------+
25| encoding | Indicates how the audio samples are encoded. |
26+---------------+-----------------------------------------------+
27| sample rate | The sampling rate. |
28+---------------+-----------------------------------------------+
29| # of channels | The number of channels in the samples. |
30+---------------+-----------------------------------------------+
31| info | ASCII string giving a description of the |
32| | audio file (padded with null bytes). |
33+---------------+-----------------------------------------------+
34
35Apart from the info field, all header fields are 4 bytes in size. They are all
3632-bit unsigned integers encoded in big-endian byte order.
37
38The :mod:`sunau` module defines the following functions:
39
40
41.. function:: open(file, mode)
42
43 If *file* is a string, open the file by that name, otherwise treat it as a
44 seekable file-like object. *mode* can be any of
45
46 ``'r'``
47 Read only mode.
48
49 ``'w'``
50 Write only mode.
51
52 Note that it does not allow read/write files.
53
54 A *mode* of ``'r'`` returns a :class:`AU_read` object, while a *mode* of ``'w'``
55 or ``'wb'`` returns a :class:`AU_write` object.
56
57
58.. function:: openfp(file, mode)
59
Georg Brandl502d9a52009-07-26 15:02:41 +000060 A synonym for :func:`.open`, maintained for backwards compatibility.
61
Georg Brandl116aa622007-08-15 14:28:22 +000062
63The :mod:`sunau` module defines the following exception:
64
Georg Brandl116aa622007-08-15 14:28:22 +000065.. exception:: Error
66
67 An error raised when something is impossible because of Sun AU specs or
68 implementation deficiency.
69
Georg Brandl116aa622007-08-15 14:28:22 +000070
Georg Brandl502d9a52009-07-26 15:02:41 +000071The :mod:`sunau` module defines the following data items:
Georg Brandl116aa622007-08-15 14:28:22 +000072
73.. data:: AUDIO_FILE_MAGIC
74
75 An integer every valid Sun AU file begins with, stored in big-endian form. This
76 is the string ``.snd`` interpreted as an integer.
77
78
79.. data:: AUDIO_FILE_ENCODING_MULAW_8
80 AUDIO_FILE_ENCODING_LINEAR_8
81 AUDIO_FILE_ENCODING_LINEAR_16
82 AUDIO_FILE_ENCODING_LINEAR_24
83 AUDIO_FILE_ENCODING_LINEAR_32
84 AUDIO_FILE_ENCODING_ALAW_8
85
86 Values of the encoding field from the AU header which are supported by this
87 module.
88
89
90.. data:: AUDIO_FILE_ENCODING_FLOAT
91 AUDIO_FILE_ENCODING_DOUBLE
92 AUDIO_FILE_ENCODING_ADPCM_G721
93 AUDIO_FILE_ENCODING_ADPCM_G722
94 AUDIO_FILE_ENCODING_ADPCM_G723_3
95 AUDIO_FILE_ENCODING_ADPCM_G723_5
96
97 Additional known values of the encoding field from the AU header, but which are
98 not supported by this module.
99
100
101.. _au-read-objects:
102
103AU_read Objects
104---------------
105
Georg Brandl502d9a52009-07-26 15:02:41 +0000106AU_read objects, as returned by :func:`.open` above, have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108
109.. method:: AU_read.close()
110
111 Close the stream, and make the instance unusable. (This is called automatically
112 on deletion.)
113
114
115.. method:: AU_read.getnchannels()
116
117 Returns number of audio channels (1 for mone, 2 for stereo).
118
119
120.. method:: AU_read.getsampwidth()
121
122 Returns sample width in bytes.
123
124
125.. method:: AU_read.getframerate()
126
127 Returns sampling frequency.
128
129
130.. method:: AU_read.getnframes()
131
132 Returns number of audio frames.
133
134
135.. method:: AU_read.getcomptype()
136
137 Returns compression type. Supported compression types are ``'ULAW'``, ``'ALAW'``
138 and ``'NONE'``.
139
140
141.. method:: AU_read.getcompname()
142
143 Human-readable version of :meth:`getcomptype`. The supported types have the
144 respective names ``'CCITT G.711 u-law'``, ``'CCITT G.711 A-law'`` and ``'not
145 compressed'``.
146
147
148.. method:: AU_read.getparams()
149
150 Returns a tuple ``(nchannels, sampwidth, framerate, nframes, comptype,
151 compname)``, equivalent to output of the :meth:`get\*` methods.
152
153
154.. method:: AU_read.readframes(n)
155
156 Reads and returns at most *n* frames of audio, as a string of bytes. The data
157 will be returned in linear format. If the original data is in u-LAW format, it
158 will be converted.
159
160
161.. method:: AU_read.rewind()
162
163 Rewind the file pointer to the beginning of the audio stream.
164
165The following two methods define a term "position" which is compatible between
166them, and is otherwise implementation dependent.
167
168
169.. method:: AU_read.setpos(pos)
170
171 Set the file pointer to the specified position. Only values returned from
172 :meth:`tell` should be used for *pos*.
173
174
175.. method:: AU_read.tell()
176
177 Return current file pointer position. Note that the returned value has nothing
178 to do with the actual position in the file.
179
180The following two functions are defined for compatibility with the :mod:`aifc`,
181and don't do anything interesting.
182
183
184.. method:: AU_read.getmarkers()
185
186 Returns ``None``.
187
188
189.. method:: AU_read.getmark(id)
190
191 Raise an error.
192
193
194.. _au-write-objects:
195
196AU_write Objects
197----------------
198
Georg Brandl502d9a52009-07-26 15:02:41 +0000199AU_write objects, as returned by :func:`.open` above, have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201
202.. method:: AU_write.setnchannels(n)
203
204 Set the number of channels.
205
206
207.. method:: AU_write.setsampwidth(n)
208
209 Set the sample width (in bytes.)
210
211
212.. method:: AU_write.setframerate(n)
213
214 Set the frame rate.
215
216
217.. method:: AU_write.setnframes(n)
218
219 Set the number of frames. This can be later changed, when and if more frames
220 are written.
221
222
223.. method:: AU_write.setcomptype(type, name)
224
225 Set the compression type and description. Only ``'NONE'`` and ``'ULAW'`` are
226 supported on output.
227
228
229.. method:: AU_write.setparams(tuple)
230
231 The *tuple* should be ``(nchannels, sampwidth, framerate, nframes, comptype,
232 compname)``, with values valid for the :meth:`set\*` methods. Set all
233 parameters.
234
235
236.. method:: AU_write.tell()
237
238 Return current position in the file, with the same disclaimer for the
239 :meth:`AU_read.tell` and :meth:`AU_read.setpos` methods.
240
241
242.. method:: AU_write.writeframesraw(data)
243
244 Write audio frames, without correcting *nframes*.
245
246
247.. method:: AU_write.writeframes(data)
248
249 Write audio frames and make sure *nframes* is correct.
250
251
252.. method:: AU_write.close()
253
254 Make sure *nframes* is correct, and close the file.
255
256 This method is called upon deletion.
257
258Note that it is invalid to set any parameters after calling :meth:`writeframes`
259or :meth:`writeframesraw`.
260