1 | \section{\module{ossaudiodev} ---
|
---|
2 | Access to OSS-compatible audio devices}
|
---|
3 |
|
---|
4 | \declaremodule{builtin}{ossaudiodev}
|
---|
5 | \platform{Linux, FreeBSD}
|
---|
6 | \modulesynopsis{Access to OSS-compatible audio devices.}
|
---|
7 |
|
---|
8 | \versionadded{2.3}
|
---|
9 |
|
---|
10 | This module allows you to access the OSS (Open Sound System) audio
|
---|
11 | interface. OSS is available for a wide range of open-source and
|
---|
12 | commercial Unices, and is the standard audio interface for Linux and
|
---|
13 | recent versions of FreeBSD.
|
---|
14 |
|
---|
15 | % Things will get more complicated for future Linux versions, since
|
---|
16 | % ALSA is in the standard kernel as of 2.5.x. Presumably if you
|
---|
17 | % use ALSA, you'll have to make sure its OSS compatibility layer
|
---|
18 | % is active to use ossaudiodev, but you're gonna need it for the vast
|
---|
19 | % majority of Linux audio apps anyways.
|
---|
20 | %
|
---|
21 | % Sounds like things are also complicated for other BSDs. In response
|
---|
22 | % to my python-dev query, Thomas Wouters said:
|
---|
23 | %
|
---|
24 | % > Likewise, googling shows OpenBSD also uses OSS/Free -- the commercial
|
---|
25 | % > OSS installation manual tells you to remove references to OSS/Free from the
|
---|
26 | % > kernel :)
|
---|
27 | %
|
---|
28 | % but Aleksander Piotrowsk actually has an OpenBSD box, and he quotes
|
---|
29 | % from its <soundcard.h>:
|
---|
30 | % > * WARNING! WARNING!
|
---|
31 | % > * This is an OSS (Linux) audio emulator.
|
---|
32 | % > * Use the Native NetBSD API for developing new code, and this
|
---|
33 | % > * only for compiling Linux programs.
|
---|
34 | %
|
---|
35 | % There's also an ossaudio manpage on OpenBSD that explains things
|
---|
36 | % further. Presumably NetBSD and OpenBSD have a different standard
|
---|
37 | % audio interface. That's the great thing about standards, there are so
|
---|
38 | % many to choose from ... ;-)
|
---|
39 | %
|
---|
40 | % This probably all warrants a footnote or two, but I don't understand
|
---|
41 | % things well enough right now to write it! --GPW
|
---|
42 |
|
---|
43 | \begin{seealso}
|
---|
44 | \seetitle[http://www.opensound.com/pguide/oss.pdf]
|
---|
45 | {Open Sound System Programmer's Guide} {the official
|
---|
46 | documentation for the OSS C API}
|
---|
47 | \seetext{The module defines a large number of constants supplied by
|
---|
48 | the OSS device driver; see \code{<sys/soundcard.h>} on either
|
---|
49 | Linux or FreeBSD for a listing .}
|
---|
50 | \end{seealso}
|
---|
51 |
|
---|
52 | \module{ossaudiodev} defines the following variables and functions:
|
---|
53 |
|
---|
54 | \begin{excdesc}{OSSAudioError}
|
---|
55 | This exception is raised on certain errors. The argument is a string
|
---|
56 | describing what went wrong.
|
---|
57 |
|
---|
58 | (If \module{ossaudiodev} receives an error from a system call such as
|
---|
59 | \cfunction{open()}, \cfunction{write()}, or \cfunction{ioctl()}, it
|
---|
60 | raises \exception{IOError}. Errors detected directly by
|
---|
61 | \module{ossaudiodev} result in \exception{OSSAudioError}.)
|
---|
62 |
|
---|
63 | (For backwards compatibility, the exception class is also available as
|
---|
64 | \code{ossaudiodev.error}.)
|
---|
65 | \end{excdesc}
|
---|
66 |
|
---|
67 | \begin{funcdesc}{open}{\optional{device, }mode}
|
---|
68 | Open an audio device and return an OSS audio device object. This
|
---|
69 | object supports many file-like methods, such as \method{read()},
|
---|
70 | \method{write()}, and \method{fileno()} (although there are subtle
|
---|
71 | differences between conventional \UNIX{} read/write semantics and those of
|
---|
72 | OSS audio devices). It also supports a number of audio-specific
|
---|
73 | methods; see below for the complete list of methods.
|
---|
74 |
|
---|
75 | \var{device} is the audio device filename to use. If it is not
|
---|
76 | specified, this module first looks in the environment variable
|
---|
77 | \envvar{AUDIODEV} for a device to use. If not found, it falls back to
|
---|
78 | \file{/dev/dsp}.
|
---|
79 |
|
---|
80 | \var{mode} is one of \code{'r'} for read-only (record) access,
|
---|
81 | \code{'w'} for write-only (playback) access and \code{'rw'} for both.
|
---|
82 | Since many sound cards only allow one process to have the recorder or
|
---|
83 | player open at a time, it is a good idea to open the device only for the
|
---|
84 | activity needed. Further, some sound cards are half-duplex: they can be
|
---|
85 | opened for reading or writing, but not both at once.
|
---|
86 |
|
---|
87 | Note the unusual calling syntax: the \emph{first} argument is optional,
|
---|
88 | and the second is required. This is a historical artifact for
|
---|
89 | compatibility with the older \module{linuxaudiodev} module which
|
---|
90 | \module{ossaudiodev} supersedes. % XXX it might also be motivated
|
---|
91 | % by my unfounded-but-still-possibly-true belief that the default
|
---|
92 | % audio device varies unpredictably across operating systems. -GW
|
---|
93 | \end{funcdesc}
|
---|
94 |
|
---|
95 | \begin{funcdesc}{openmixer}{\optional{device}}
|
---|
96 | Open a mixer device and return an OSS mixer device object.
|
---|
97 | \var{device} is the mixer device filename to use. If it is
|
---|
98 | not specified, this module first looks in the environment variable
|
---|
99 | \envvar{MIXERDEV} for a device to use. If not found, it falls back to
|
---|
100 | \file{/dev/mixer}.
|
---|
101 |
|
---|
102 | \end{funcdesc}
|
---|
103 |
|
---|
104 | \subsection{Audio Device Objects \label{ossaudio-device-objects}}
|
---|
105 |
|
---|
106 | Before you can write to or read from an audio device, you must call
|
---|
107 | three methods in the correct order:
|
---|
108 | \begin{enumerate}
|
---|
109 | \item \method{setfmt()} to set the output format
|
---|
110 | \item \method{channels()} to set the number of channels
|
---|
111 | \item \method{speed()} to set the sample rate
|
---|
112 | \end{enumerate}
|
---|
113 | Alternately, you can use the \method{setparameters()} method to set all
|
---|
114 | three audio parameters at once. This is more convenient, but may not be
|
---|
115 | as flexible in all cases.
|
---|
116 |
|
---|
117 | The audio device objects returned by \function{open()} define the
|
---|
118 | following methods and (read-only) attributes:
|
---|
119 |
|
---|
120 | \begin{methoddesc}[audio device]{close}{}
|
---|
121 | Explicitly close the audio device. When you are done writing to or
|
---|
122 | reading from an audio device, you should explicitly close it. A closed
|
---|
123 | device cannot be used again.
|
---|
124 | \end{methoddesc}
|
---|
125 |
|
---|
126 | \begin{methoddesc}[audio device]{fileno}{}
|
---|
127 | Return the file descriptor associated with the device.
|
---|
128 | \end{methoddesc}
|
---|
129 |
|
---|
130 | \begin{methoddesc}[audio device]{read}{size}
|
---|
131 | Read \var{size} bytes from the audio input and return them as a Python
|
---|
132 | string. Unlike most \UNIX{} device drivers, OSS audio devices in
|
---|
133 | blocking mode (the default) will block \function{read()} until the
|
---|
134 | entire requested amount of data is available.
|
---|
135 | \end{methoddesc}
|
---|
136 |
|
---|
137 | \begin{methoddesc}[audio device]{write}{data}
|
---|
138 | Write the Python string \var{data} to the audio device and return the
|
---|
139 | number of bytes written. If the audio device is in blocking mode (the
|
---|
140 | default), the entire string is always written (again, this is different
|
---|
141 | from usual \UNIX{} device semantics). If the device is in non-blocking
|
---|
142 | mode, some data may not be written---see \method{writeall()}.
|
---|
143 | \end{methoddesc}
|
---|
144 |
|
---|
145 | \begin{methoddesc}[audio device]{writeall}{data}
|
---|
146 | Write the entire Python string \var{data} to the audio device: waits
|
---|
147 | until the audio device is able to accept data, writes as much data as it
|
---|
148 | will accept, and repeats until \var{data} has been completely written.
|
---|
149 | If the device is in blocking mode (the default), this has the same
|
---|
150 | effect as \method{write()}; \method{writeall()} is only useful in
|
---|
151 | non-blocking mode. Has no return value, since the amount of data
|
---|
152 | written is always equal to the amount of data supplied.
|
---|
153 | \end{methoddesc}
|
---|
154 |
|
---|
155 | The following methods each map to exactly one
|
---|
156 | \function{ioctl()} system call. The correspondence is obvious: for
|
---|
157 | example, \method{setfmt()} corresponds to the \code{SNDCTL_DSP_SETFMT}
|
---|
158 | ioctl, and \method{sync()} to \code{SNDCTL_DSP_SYNC} (this can be useful
|
---|
159 | when consulting the OSS documentation). If the underlying
|
---|
160 | \function{ioctl()} fails, they all raise \exception{IOError}.
|
---|
161 |
|
---|
162 | \begin{methoddesc}[audio device]{nonblock}{}
|
---|
163 | Put the device into non-blocking mode. Once in non-blocking mode, there
|
---|
164 | is no way to return it to blocking mode.
|
---|
165 | \end{methoddesc}
|
---|
166 |
|
---|
167 | \begin{methoddesc}[audio device]{getfmts}{}
|
---|
168 | Return a bitmask of the audio output formats supported by the
|
---|
169 | soundcard. Some of the formats supported by OSS are:
|
---|
170 |
|
---|
171 | \begin{tableii}{l|l}{constant}{Format}{Description}
|
---|
172 | \lineii{AFMT_MU_LAW}
|
---|
173 | {a logarithmic encoding (used by Sun \code{.au} files and
|
---|
174 | \filenq{/dev/audio})}
|
---|
175 | \lineii{AFMT_A_LAW}
|
---|
176 | {a logarithmic encoding}
|
---|
177 | \lineii{AFMT_IMA_ADPCM}
|
---|
178 | {a 4:1 compressed format defined by the Interactive Multimedia
|
---|
179 | Association}
|
---|
180 | \lineii{AFMT_U8}
|
---|
181 | {Unsigned, 8-bit audio}
|
---|
182 | \lineii{AFMT_S16_LE}
|
---|
183 | {Signed, 16-bit audio, little-endian byte order (as used by
|
---|
184 | Intel processors)}
|
---|
185 | \lineii{AFMT_S16_BE}
|
---|
186 | {Signed, 16-bit audio, big-endian byte order (as used by 68k,
|
---|
187 | PowerPC, Sparc)}
|
---|
188 | \lineii{AFMT_S8}
|
---|
189 | {Signed, 8 bit audio}
|
---|
190 | \lineii{AFMT_U16_LE}
|
---|
191 | {Unsigned, 16-bit little-endian audio}
|
---|
192 | \lineii{AFMT_U16_BE}
|
---|
193 | {Unsigned, 16-bit big-endian audio}
|
---|
194 | \end{tableii}
|
---|
195 | Consult the OSS documentation for a full list of audio formats, and note
|
---|
196 | that most devices support only a subset of these formats. Some older
|
---|
197 | devices only support \constant{AFMT_U8}; the most common format used
|
---|
198 | today is \constant{AFMT_S16_LE}.
|
---|
199 | \end{methoddesc}
|
---|
200 |
|
---|
201 | \begin{methoddesc}[audio device]{setfmt}{format}
|
---|
202 | Try to set the current audio format to \var{format}---see
|
---|
203 | \method{getfmts()} for a list. Returns the audio format that the device
|
---|
204 | was set to, which may not be the requested format. May also be used to
|
---|
205 | return the current audio format---do this by passing an ``audio format''
|
---|
206 | of
|
---|
207 | \constant{AFMT_QUERY}.
|
---|
208 | \end{methoddesc}
|
---|
209 |
|
---|
210 | \begin{methoddesc}[audio device]{channels}{nchannels}
|
---|
211 | Set the number of output channels to \var{nchannels}. A value of 1
|
---|
212 | indicates monophonic sound, 2 stereophonic. Some devices may have more
|
---|
213 | than 2 channels, and some high-end devices may not support mono.
|
---|
214 | Returns the number of channels the device was set to.
|
---|
215 | \end{methoddesc}
|
---|
216 |
|
---|
217 | \begin{methoddesc}[audio device]{speed}{samplerate}
|
---|
218 | Try to set the audio sampling rate to \var{samplerate} samples per
|
---|
219 | second. Returns the rate actually set. Most sound devices don't
|
---|
220 | support arbitrary sampling rates. Common rates are:
|
---|
221 | \begin{tableii}{l|l}{textrm}{Rate}{Description}
|
---|
222 | \lineii{8000}{default rate for \filenq{/dev/audio}}
|
---|
223 | \lineii{11025}{speech recording}
|
---|
224 | \lineii{22050}{}
|
---|
225 | \lineii{44100}{CD quality audio (at 16 bits/sample and 2 channels)}
|
---|
226 | \lineii{96000}{DVD quality audio (at 24 bits/sample)}
|
---|
227 | \end{tableii}
|
---|
228 | \end{methoddesc}
|
---|
229 |
|
---|
230 | \begin{methoddesc}[audio device]{sync}{}
|
---|
231 | Wait until the sound device has played every byte in its buffer. (This
|
---|
232 | happens implicitly when the device is closed.) The OSS documentation
|
---|
233 | recommends closing and re-opening the device rather than using
|
---|
234 | \method{sync()}.
|
---|
235 | \end{methoddesc}
|
---|
236 |
|
---|
237 | \begin{methoddesc}[audio device]{reset}{}
|
---|
238 | Immediately stop playing or recording and return the device to a
|
---|
239 | state where it can accept commands. The OSS documentation recommends
|
---|
240 | closing and re-opening the device after calling \method{reset()}.
|
---|
241 | \end{methoddesc}
|
---|
242 |
|
---|
243 | \begin{methoddesc}[audio device]{post}{}
|
---|
244 | Tell the driver that there is likely to be a pause in the output, making
|
---|
245 | it possible for the device to handle the pause more intelligently. You
|
---|
246 | might use this after playing a spot sound effect, before waiting for
|
---|
247 | user input, or before doing disk I/O.
|
---|
248 | \end{methoddesc}
|
---|
249 |
|
---|
250 | The following convenience methods combine several ioctls, or one ioctl
|
---|
251 | and some simple calculations.
|
---|
252 |
|
---|
253 | \begin{methoddesc}[audio device]{setparameters}
|
---|
254 | {format, nchannels, samplerate \optional{, strict=False}}
|
---|
255 |
|
---|
256 | Set the key audio sampling parameters---sample format, number of
|
---|
257 | channels, and sampling rate---in one method call. \var{format},
|
---|
258 | \var{nchannels}, and \var{samplerate} should be as specified in the
|
---|
259 | \method{setfmt()}, \method{channels()}, and \method{speed()}
|
---|
260 | methods. If \var{strict} is true, \method{setparameters()} checks to
|
---|
261 | see if each parameter was actually set to the requested value, and
|
---|
262 | raises \exception{OSSAudioError} if not. Returns a tuple (\var{format},
|
---|
263 | \var{nchannels}, \var{samplerate}) indicating the parameter values that
|
---|
264 | were actually set by the device driver (i.e., the same as the return
|
---|
265 | values of \method{setfmt()}, \method{channels()}, and \method{speed()}).
|
---|
266 |
|
---|
267 | For example,
|
---|
268 | \begin{verbatim}
|
---|
269 | (fmt, channels, rate) = dsp.setparameters(fmt, channels, rate)
|
---|
270 | \end{verbatim}
|
---|
271 | is equivalent to
|
---|
272 | \begin{verbatim}
|
---|
273 | fmt = dsp.setfmt(fmt)
|
---|
274 | channels = dsp.channels(channels)
|
---|
275 | rate = dsp.rate(channels)
|
---|
276 | \end{verbatim}
|
---|
277 | \end{methoddesc}
|
---|
278 |
|
---|
279 | \begin{methoddesc}[audio device]{bufsize}{}
|
---|
280 | Returns the size of the hardware buffer, in samples.
|
---|
281 | \end{methoddesc}
|
---|
282 |
|
---|
283 | \begin{methoddesc}[audio device]{obufcount}{}
|
---|
284 | Returns the number of samples that are in the hardware buffer yet to be
|
---|
285 | played.
|
---|
286 | \end{methoddesc}
|
---|
287 |
|
---|
288 | \begin{methoddesc}[audio device]{obuffree}{}
|
---|
289 | Returns the number of samples that could be queued into the hardware
|
---|
290 | buffer to be played without blocking.
|
---|
291 | \end{methoddesc}
|
---|
292 |
|
---|
293 | Audio device objects also support several read-only attributes:
|
---|
294 |
|
---|
295 | \begin{memberdesc}[audio device]{closed}{}
|
---|
296 | Boolean indicating whether the device has been closed.
|
---|
297 | \end{memberdesc}
|
---|
298 |
|
---|
299 | \begin{memberdesc}[audio device]{name}{}
|
---|
300 | String containing the name of the device file.
|
---|
301 | \end{memberdesc}
|
---|
302 |
|
---|
303 | \begin{memberdesc}[audio device]{mode}{}
|
---|
304 | The I/O mode for the file, either \code{"r"}, \code{"rw"}, or \code{"w"}.
|
---|
305 | \end{memberdesc}
|
---|
306 |
|
---|
307 |
|
---|
308 | \subsection{Mixer Device Objects \label{mixer-device-objects}}
|
---|
309 |
|
---|
310 | The mixer object provides two file-like methods:
|
---|
311 |
|
---|
312 | \begin{methoddesc}[mixer device]{close}{}
|
---|
313 | This method closes the open mixer device file. Any further attempts to
|
---|
314 | use the mixer after this file is closed will raise an \exception{IOError}.
|
---|
315 | \end{methoddesc}
|
---|
316 |
|
---|
317 | \begin{methoddesc}[mixer device]{fileno}{}
|
---|
318 | Returns the file handle number of the open mixer device file.
|
---|
319 | \end{methoddesc}
|
---|
320 |
|
---|
321 | The remaining methods are specific to audio mixing:
|
---|
322 |
|
---|
323 | \begin{methoddesc}[mixer device]{controls}{}
|
---|
324 | This method returns a bitmask specifying the available mixer controls
|
---|
325 | (``Control'' being a specific mixable ``channel'', such as
|
---|
326 | \constant{SOUND_MIXER_PCM} or \constant{SOUND_MIXER_SYNTH}). This
|
---|
327 | bitmask indicates a subset of all available mixer controls---the
|
---|
328 | \constant{SOUND_MIXER_*} constants defined at module level. To determine if,
|
---|
329 | for example, the current mixer object supports a PCM mixer, use the
|
---|
330 | following Python code:
|
---|
331 |
|
---|
332 | \begin{verbatim}
|
---|
333 | mixer=ossaudiodev.openmixer()
|
---|
334 | if mixer.controls() & (1 << ossaudiodev.SOUND_MIXER_PCM):
|
---|
335 | # PCM is supported
|
---|
336 | ... code ...
|
---|
337 | \end{verbatim}
|
---|
338 |
|
---|
339 | For most purposes, the \constant{SOUND_MIXER_VOLUME} (master volume) and
|
---|
340 | \constant{SOUND_MIXER_PCM} controls should suffice---but code that uses the
|
---|
341 | mixer should be flexible when it comes to choosing mixer controls. On
|
---|
342 | the Gravis Ultrasound, for example, \constant{SOUND_MIXER_VOLUME} does not
|
---|
343 | exist.
|
---|
344 | \end{methoddesc}
|
---|
345 |
|
---|
346 | \begin{methoddesc}[mixer device]{stereocontrols}{}
|
---|
347 | Returns a bitmask indicating stereo mixer controls. If a bit is set,
|
---|
348 | the corresponding control is stereo; if it is unset, the control is
|
---|
349 | either monophonic or not supported by the mixer (use in combination with
|
---|
350 | \method{controls()} to determine which).
|
---|
351 |
|
---|
352 | See the code example for the \method{controls()} function for an example
|
---|
353 | of getting data from a bitmask.
|
---|
354 | \end{methoddesc}
|
---|
355 |
|
---|
356 | \begin{methoddesc}[mixer device]{reccontrols}{}
|
---|
357 | Returns a bitmask specifying the mixer controls that may be used to
|
---|
358 | record. See the code example for \method{controls()} for an example of
|
---|
359 | reading from a bitmask.
|
---|
360 | \end{methoddesc}
|
---|
361 |
|
---|
362 | \begin{methoddesc}[mixer device]{get}{control}
|
---|
363 | Returns the volume of a given mixer control. The returned volume is a
|
---|
364 | 2-tuple \code{(left_volume,right_volume)}. Volumes are specified as
|
---|
365 | numbers from 0 (silent) to 100 (full volume). If the control is
|
---|
366 | monophonic, a 2-tuple is still returned, but both volumes are
|
---|
367 | the same.
|
---|
368 |
|
---|
369 | Raises \exception{OSSAudioError} if an invalid control was is specified,
|
---|
370 | or \exception{IOError} if an unsupported control is specified.
|
---|
371 | \end{methoddesc}
|
---|
372 |
|
---|
373 | \begin{methoddesc}[mixer device]{set}{control, (left, right)}
|
---|
374 | Sets the volume for a given mixer control to \code{(left,right)}.
|
---|
375 | \code{left} and \code{right} must be ints and between 0 (silent) and 100
|
---|
376 | (full volume). On success, the new volume is returned as a 2-tuple.
|
---|
377 | Note that this may not be exactly the same as the volume specified,
|
---|
378 | because of the limited resolution of some soundcard's mixers.
|
---|
379 |
|
---|
380 | Raises \exception{OSSAudioError} if an invalid mixer control was
|
---|
381 | specified, or if the specified volumes were out-of-range.
|
---|
382 | \end{methoddesc}
|
---|
383 |
|
---|
384 | \begin{methoddesc}[mixer device]{get_recsrc}{}
|
---|
385 | This method returns a bitmask indicating which control(s) are
|
---|
386 | currently being used as a recording source.
|
---|
387 | \end{methoddesc}
|
---|
388 |
|
---|
389 | \begin{methoddesc}[mixer device]{set_recsrc}{bitmask}
|
---|
390 | Call this function to specify a recording source. Returns a bitmask
|
---|
391 | indicating the new recording source (or sources) if successful; raises
|
---|
392 | \exception{IOError} if an invalid source was specified. To set the current
|
---|
393 | recording source to the microphone input:
|
---|
394 |
|
---|
395 | \begin{verbatim}
|
---|
396 | mixer.setrecsrc (1 << ossaudiodev.SOUND_MIXER_MIC)
|
---|
397 | \end{verbatim}
|
---|
398 | \end{methoddesc}
|
---|
399 |
|
---|
400 |
|
---|
401 |
|
---|