source: vendor/python/2.5/Doc/lib/libossaudiodev.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 15.9 KB
Line 
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
10This module allows you to access the OSS (Open Sound System) audio
11interface. OSS is available for a wide range of open-source and
12commercial Unices, and is the standard audio interface for Linux and
13recent 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}
55This exception is raised on certain errors. The argument is a string
56describing 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
60raises \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}
68Open an audio device and return an OSS audio device object. This
69object supports many file-like methods, such as \method{read()},
70\method{write()}, and \method{fileno()} (although there are subtle
71differences between conventional \UNIX{} read/write semantics and those of
72OSS audio devices). It also supports a number of audio-specific
73methods; see below for the complete list of methods.
74
75\var{device} is the audio device filename to use. If it is not
76specified, 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.
82Since many sound cards only allow one process to have the recorder or
83player open at a time, it is a good idea to open the device only for the
84activity needed. Further, some sound cards are half-duplex: they can be
85opened for reading or writing, but not both at once.
86
87Note the unusual calling syntax: the \emph{first} argument is optional,
88and the second is required. This is a historical artifact for
89compatibility 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}}
96Open a mixer device and return an OSS mixer device object.
97\var{device} is the mixer device filename to use. If it is
98not 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
106Before you can write to or read from an audio device, you must call
107three 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}
113Alternately, you can use the \method{setparameters()} method to set all
114three audio parameters at once. This is more convenient, but may not be
115as flexible in all cases.
116
117The audio device objects returned by \function{open()} define the
118following methods and (read-only) attributes:
119
120\begin{methoddesc}[audio device]{close}{}
121Explicitly close the audio device. When you are done writing to or
122reading from an audio device, you should explicitly close it. A closed
123device cannot be used again.
124\end{methoddesc}
125
126\begin{methoddesc}[audio device]{fileno}{}
127Return the file descriptor associated with the device.
128\end{methoddesc}
129
130\begin{methoddesc}[audio device]{read}{size}
131Read \var{size} bytes from the audio input and return them as a Python
132string. Unlike most \UNIX{} device drivers, OSS audio devices in
133blocking mode (the default) will block \function{read()} until the
134entire requested amount of data is available.
135\end{methoddesc}
136
137\begin{methoddesc}[audio device]{write}{data}
138Write the Python string \var{data} to the audio device and return the
139number of bytes written. If the audio device is in blocking mode (the
140default), the entire string is always written (again, this is different
141from usual \UNIX{} device semantics). If the device is in non-blocking
142mode, some data may not be written---see \method{writeall()}.
143\end{methoddesc}
144
145\begin{methoddesc}[audio device]{writeall}{data}
146Write the entire Python string \var{data} to the audio device: waits
147until the audio device is able to accept data, writes as much data as it
148will accept, and repeats until \var{data} has been completely written.
149If the device is in blocking mode (the default), this has the same
150effect as \method{write()}; \method{writeall()} is only useful in
151non-blocking mode. Has no return value, since the amount of data
152written is always equal to the amount of data supplied.
153\end{methoddesc}
154
155The following methods each map to exactly one
156\function{ioctl()} system call. The correspondence is obvious: for
157example, \method{setfmt()} corresponds to the \code{SNDCTL_DSP_SETFMT}
158ioctl, and \method{sync()} to \code{SNDCTL_DSP_SYNC} (this can be useful
159when consulting the OSS documentation). If the underlying
160\function{ioctl()} fails, they all raise \exception{IOError}.
161
162\begin{methoddesc}[audio device]{nonblock}{}
163Put the device into non-blocking mode. Once in non-blocking mode, there
164is no way to return it to blocking mode.
165\end{methoddesc}
166
167\begin{methoddesc}[audio device]{getfmts}{}
168Return a bitmask of the audio output formats supported by the
169soundcard. 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}
195Consult the OSS documentation for a full list of audio formats, and note
196that most devices support only a subset of these formats. Some older
197devices only support \constant{AFMT_U8}; the most common format used
198today is \constant{AFMT_S16_LE}.
199\end{methoddesc}
200
201\begin{methoddesc}[audio device]{setfmt}{format}
202Try to set the current audio format to \var{format}---see
203\method{getfmts()} for a list. Returns the audio format that the device
204was set to, which may not be the requested format. May also be used to
205return the current audio format---do this by passing an ``audio format''
206of
207\constant{AFMT_QUERY}.
208\end{methoddesc}
209
210\begin{methoddesc}[audio device]{channels}{nchannels}
211Set the number of output channels to \var{nchannels}. A value of 1
212indicates monophonic sound, 2 stereophonic. Some devices may have more
213than 2 channels, and some high-end devices may not support mono.
214Returns the number of channels the device was set to.
215\end{methoddesc}
216
217\begin{methoddesc}[audio device]{speed}{samplerate}
218Try to set the audio sampling rate to \var{samplerate} samples per
219second. Returns the rate actually set. Most sound devices don't
220support 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}{}
231Wait until the sound device has played every byte in its buffer. (This
232happens implicitly when the device is closed.) The OSS documentation
233recommends closing and re-opening the device rather than using
234\method{sync()}.
235\end{methoddesc}
236
237\begin{methoddesc}[audio device]{reset}{}
238Immediately stop playing or recording and return the device to a
239state where it can accept commands. The OSS documentation recommends
240closing and re-opening the device after calling \method{reset()}.
241\end{methoddesc}
242
243\begin{methoddesc}[audio device]{post}{}
244Tell the driver that there is likely to be a pause in the output, making
245it possible for the device to handle the pause more intelligently. You
246might use this after playing a spot sound effect, before waiting for
247user input, or before doing disk I/O.
248\end{methoddesc}
249
250The following convenience methods combine several ioctls, or one ioctl
251and some simple calculations.
252
253\begin{methoddesc}[audio device]{setparameters}
254 {format, nchannels, samplerate \optional{, strict=False}}
255
256Set the key audio sampling parameters---sample format, number of
257channels, 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()}
260methods. If \var{strict} is true, \method{setparameters()} checks to
261see if each parameter was actually set to the requested value, and
262raises \exception{OSSAudioError} if not. Returns a tuple (\var{format},
263\var{nchannels}, \var{samplerate}) indicating the parameter values that
264were actually set by the device driver (i.e., the same as the return
265values of \method{setfmt()}, \method{channels()}, and \method{speed()}).
266
267For example,
268\begin{verbatim}
269 (fmt, channels, rate) = dsp.setparameters(fmt, channels, rate)
270\end{verbatim}
271is 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}{}
280Returns the size of the hardware buffer, in samples.
281\end{methoddesc}
282
283\begin{methoddesc}[audio device]{obufcount}{}
284Returns the number of samples that are in the hardware buffer yet to be
285played.
286\end{methoddesc}
287
288\begin{methoddesc}[audio device]{obuffree}{}
289Returns the number of samples that could be queued into the hardware
290buffer to be played without blocking.
291\end{methoddesc}
292
293Audio device objects also support several read-only attributes:
294
295\begin{memberdesc}[audio device]{closed}{}
296Boolean indicating whether the device has been closed.
297\end{memberdesc}
298
299\begin{memberdesc}[audio device]{name}{}
300String containing the name of the device file.
301\end{memberdesc}
302
303\begin{memberdesc}[audio device]{mode}{}
304The 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
310The mixer object provides two file-like methods:
311
312\begin{methoddesc}[mixer device]{close}{}
313This method closes the open mixer device file. Any further attempts to
314use the mixer after this file is closed will raise an \exception{IOError}.
315\end{methoddesc}
316
317\begin{methoddesc}[mixer device]{fileno}{}
318Returns the file handle number of the open mixer device file.
319\end{methoddesc}
320
321The remaining methods are specific to audio mixing:
322
323\begin{methoddesc}[mixer device]{controls}{}
324This 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
327bitmask indicates a subset of all available mixer controls---the
328\constant{SOUND_MIXER_*} constants defined at module level. To determine if,
329for example, the current mixer object supports a PCM mixer, use the
330following Python code:
331
332\begin{verbatim}
333mixer=ossaudiodev.openmixer()
334if mixer.controls() & (1 << ossaudiodev.SOUND_MIXER_PCM):
335 # PCM is supported
336 ... code ...
337\end{verbatim}
338
339For most purposes, the \constant{SOUND_MIXER_VOLUME} (master volume) and
340\constant{SOUND_MIXER_PCM} controls should suffice---but code that uses the
341mixer should be flexible when it comes to choosing mixer controls. On
342the Gravis Ultrasound, for example, \constant{SOUND_MIXER_VOLUME} does not
343exist.
344\end{methoddesc}
345
346\begin{methoddesc}[mixer device]{stereocontrols}{}
347Returns a bitmask indicating stereo mixer controls. If a bit is set,
348the corresponding control is stereo; if it is unset, the control is
349either monophonic or not supported by the mixer (use in combination with
350\method{controls()} to determine which).
351
352See the code example for the \method{controls()} function for an example
353of getting data from a bitmask.
354\end{methoddesc}
355
356\begin{methoddesc}[mixer device]{reccontrols}{}
357Returns a bitmask specifying the mixer controls that may be used to
358record. See the code example for \method{controls()} for an example of
359reading from a bitmask.
360\end{methoddesc}
361
362\begin{methoddesc}[mixer device]{get}{control}
363Returns the volume of a given mixer control. The returned volume is a
3642-tuple \code{(left_volume,right_volume)}. Volumes are specified as
365numbers from 0 (silent) to 100 (full volume). If the control is
366monophonic, a 2-tuple is still returned, but both volumes are
367the same.
368
369Raises \exception{OSSAudioError} if an invalid control was is specified,
370or \exception{IOError} if an unsupported control is specified.
371\end{methoddesc}
372
373\begin{methoddesc}[mixer device]{set}{control, (left, right)}
374Sets 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.
377Note that this may not be exactly the same as the volume specified,
378because of the limited resolution of some soundcard's mixers.
379
380Raises \exception{OSSAudioError} if an invalid mixer control was
381specified, or if the specified volumes were out-of-range.
382\end{methoddesc}
383
384\begin{methoddesc}[mixer device]{get_recsrc}{}
385This method returns a bitmask indicating which control(s) are
386currently being used as a recording source.
387\end{methoddesc}
388
389\begin{methoddesc}[mixer device]{set_recsrc}{bitmask}
390Call this function to specify a recording source. Returns a bitmask
391indicating the new recording source (or sources) if successful; raises
392\exception{IOError} if an invalid source was specified. To set the current
393recording source to the microphone input:
394
395\begin{verbatim}
396mixer.setrecsrc (1 << ossaudiodev.SOUND_MIXER_MIC)
397\end{verbatim}
398\end{methoddesc}
399
400
401
Note: See TracBrowser for help on using the repository browser.