blob: 414ec670d5b761ccdab3d0949af6f61b0b786523 [file] [log] [blame]
Greg Ward620e3432003-03-09 23:34:52 +00001\section{\module{ossaudiodev} ---
Greg Ward3e34f592003-03-10 02:09:51 +00002 Access to OSS-compatible audio devices}
Greg Ward620e3432003-03-09 23:34:52 +00003
4\declaremodule{builtin}{ossaudiodev}
Greg Ward3e34f592003-03-10 02:09:51 +00005\platform{Linux, FreeBSD}
6\modulesynopsis{Access to OSS-compatible audio devices.}
Greg Ward620e3432003-03-09 23:34:52 +00007
Greg Ward3e34f592003-03-10 02:09:51 +00008% XXX OSS is standard for Linux and FreeBSD -- what about NetBSD?
9% OpenBSD? others?
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 (up to
13kernel 2.4) and FreeBSD.
Greg Ward620e3432003-03-09 23:34:52 +000014
Greg Ward3e34f592003-03-10 02:09:51 +000015\begin{seealso}
16\seetitle[http://www.opensound.com/pguide/oss.pdf]
17 {Open Sound System Programmer's Guide}
18 {the official documentation for the OSS C API}
19\seetext{The module defines a large number of constants supplied by
20 the OSS device driver; see \file{<sys/soundcard.h>} on either
21 Linux or FreeBSD for a listing .}
22\end{seealso}
Greg Ward620e3432003-03-09 23:34:52 +000023
Greg Ward3e34f592003-03-10 02:09:51 +000024\module{ossaudiodev} defines the following variables and functions:
Greg Ward620e3432003-03-09 23:34:52 +000025
26\begin{excdesc}{error}
Greg Wardf882c772003-03-10 03:05:21 +000027This exception is raised on certain errors. The argument is a string
28describing what went wrong.
29
30(If \module{ossaudiodev} receives an error from a system call such as
31\cfunction{open()}, \cfunction{write()}, or \cfunction{ioctl()}, it
32raises \exception{IOError}. Errors detected directly by
33\module{ossaudiodev} result in \exception{ossaudiodev.error}.)
Greg Ward620e3432003-03-09 23:34:52 +000034\end{excdesc}
35
36\begin{funcdesc}{open}{\optional{device, }mode}
Greg Wardf882c772003-03-10 03:05:21 +000037Open an audio device and return an OSS audio device object. This
38object supports many file-like methods, such as \method{read()},
39\method{write()}, and \method{fileno()} (although there are subtle
40differences between conventional Unix read/write semantics and those of
41OSS audio devices). It also supports a number of audio-specific
42methods; see below for the complete list of methods.
Greg Ward620e3432003-03-09 23:34:52 +000043
Greg Wardf882c772003-03-10 03:05:21 +000044Note the unusual calling syntax: the \emph{first} argument is optional,
45and the second is required. This is a historical artifact for
46compatibility with the older \module{linuxaudiodev} module which
47\module{ossaudiodev} supersedes. % XXX it might also be motivated
48% by my unfounded-but-still-possibly-true belief that the default
49% audio device varies unpredictably across operating systems. -GW
50
51\var{device} is the audio device filename to use. If it is not
52specified, this module first looks in the environment variable
53\envvar{AUDIODEV} for a device to use. If not found, it falls back to
54\file{/dev/dsp}.
55
56\var{mode} is one of \code{'r'} for read-only (record) access,
57\code{'w'} for write-only (playback) access and \code{'rw'} for both.
58Since many soundcards only allow one process to have the recorder or
59player open at a time it is a good idea to open the device only for the
60activity needed. Further, some soundcards are half-duplex: they can be
61opened for reading or writing, but not both at once.
Greg Ward620e3432003-03-09 23:34:52 +000062\end{funcdesc}
63
Greg Wardcd930f52003-03-10 03:18:19 +000064\begin{funcdesc}{openmixer}{\optional{device}}
Greg Wardf882c772003-03-10 03:05:21 +000065Open a mixer device and return an OSS mixer device object.
66\var{device} is the mixer device filename to use. If it is
Greg Ward33bcd982003-03-09 23:57:34 +000067not specified, this module first looks in the environment variable
Greg Ward3e34f592003-03-10 02:09:51 +000068\envvar{MIXERDEV} for a device to use. If not found, it falls back to
Greg Wardcd930f52003-03-10 03:18:19 +000069\file{/dev/mixer}.
Greg Wardf882c772003-03-10 03:05:21 +000070
Greg Ward620e3432003-03-09 23:34:52 +000071\end{funcdesc}
72
73\subsection{Audio Device Objects \label{ossaudio-device-objects}}
74
75Setting up the device
76
Greg Ward33bcd982003-03-09 23:57:34 +000077To set up the device, three functions must be called in the correct
78sequence:
Greg Ward074472b2003-03-10 00:24:42 +000079\begin{enumerate}
Greg Ward3e34f592003-03-10 02:09:51 +000080\item \method{setfmt()} to set the output format,
81\item \method{channels()} to set the number of channels, and
82\item \method{speed()} to set the sample rate.
Greg Ward074472b2003-03-10 00:24:42 +000083\end{enumerate}
Greg Ward620e3432003-03-09 23:34:52 +000084
85The audio device objects are returned by \function{open()} define the
86following methods:
87
88\begin{methoddesc}[audio device]{close}{}
Greg Ward33bcd982003-03-09 23:57:34 +000089This method explicitly closes the device. It is useful in situations
90where deleting the object does not immediately close it since there are
91other references to it. A closed device should not be used again.
Greg Ward620e3432003-03-09 23:34:52 +000092\end{methoddesc}
93
94\begin{methoddesc}[audio device]{fileno}{}
Greg Ward33bcd982003-03-09 23:57:34 +000095Returns the file descriptor associated with the device.
Greg Ward620e3432003-03-09 23:34:52 +000096\end{methoddesc}
97
98\begin{methoddesc}[audio device]{read}{size}
Greg Ward33bcd982003-03-09 23:57:34 +000099Reads \var{size} samples from the audio input and returns them as a
100Python string. The function blocks until enough data is available.
Greg Ward620e3432003-03-09 23:34:52 +0000101\end{methoddesc}
102
103\begin{methoddesc}[audio device]{write}{data}
Greg Ward33bcd982003-03-09 23:57:34 +0000104Writes Python string \var{data} to the audio device and returns the
105number of bytes written. If the audio device is opened in blocking
106mode, the entire string is always written. If the device is opened in
Greg Ward3e34f592003-03-10 02:09:51 +0000107nonblocking mode, some data may not be written---see
108\method{writeall()}.
Greg Ward620e3432003-03-09 23:34:52 +0000109\end{methoddesc}
110
111\begin{methoddesc}[audio device]{writeall}{data}
Greg Ward33bcd982003-03-09 23:57:34 +0000112Writes the entire Python string \var{data} to the audio device. If the
Greg Ward3e34f592003-03-10 02:09:51 +0000113device is opened in blocking mode, behaves identially to
114\method{write()}; in nonblocking mode, waits until the device becomes
115available before feeding it more data. Returns \code{None}, since the
116amount of data written is always equal to the amount of data supplied.
Greg Ward620e3432003-03-09 23:34:52 +0000117\end{methoddesc}
118
119Simple IOCTLs:
120
121\begin{methoddesc}[audio device]{nonblock}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000122Attempts to put the device into nonblocking mode. Once in nonblocking
123mode there is no way to return to blocking mode.
Greg Ward620e3432003-03-09 23:34:52 +0000124
125Raises \exception{IOError} if the IOCTL failed.
126\end{methoddesc}
127
128\begin{methoddesc}[audio device]{getfmts}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000129Returns a bitmask of the audio output formats supported by the
130soundcard. On a typical Linux system, these formats are:
Greg Ward620e3432003-03-09 23:34:52 +0000131
Greg Ward074472b2003-03-10 00:24:42 +0000132\begin{tableii}{l|l}{constant}{Format}{Description}
133\lineii{AFMT_MU_LAW}
134 {a logarithmic encoding. This is the default format on
Greg Ward3e34f592003-03-10 02:09:51 +0000135 \file{/dev/audio} and is the format used by Sun .au files.}
Greg Ward074472b2003-03-10 00:24:42 +0000136\lineii{AFMT_A_LAW}
137 {a logarithmic encoding}
138\lineii{AFMT_IMA_ADPCM}
139 {a 4:1 compressed format defined by the Interactive Multimedia
140 Association.}
141\lineii{AFMT_U8}
142 {Unsigned, 8-bit audio.}
143\lineii{AFMT_S16_LE}
144 {Unsigned, 16-bit audio, little-endian byte order (as used by
145 Intel processors)}
146\lineii{AFMT_S16_BE}
147 {Unsigned, 16-bit audio, big-endian byte order (as used by 68k,
148 PowerPC, Sparc)}
149\lineii{AFMT_S8}
150 {Signed, 8 bit audio.}
151\lineii{AFMT_U16_LE}
152 {Signed, 16-bit little-endian audio}
153\lineii{AFMT_U16_BE}
154 {Signed, 16-bit big-endian audio}
155\end{tableii}
Greg Ward33bcd982003-03-09 23:57:34 +0000156Most systems support only a subset of these formats. Many devices only
Greg Ward3e34f592003-03-10 02:09:51 +0000157support \constant{AFMT_U8}; the most common format used today is
158\constant{AFMT_S16_LE}.
Greg Ward620e3432003-03-09 23:34:52 +0000159\end{methoddesc}
160
161\begin{methoddesc}[audio device]{setfmt}{format}
Greg Ward33bcd982003-03-09 23:57:34 +0000162Used to set the current audio format to \var{format}---see
Greg Ward3e34f592003-03-10 02:09:51 +0000163\method{getfmts()} for a list. May also be used to return the current
164audio format---do this by passing an ``audio format'' of
165\constant{AFMT_QUERY}. Returns the audio format that the device was set
166to, which may not be the requested format.
Greg Ward620e3432003-03-09 23:34:52 +0000167\end{methoddesc}
168
169\begin{methoddesc}[audio device]{channels}{num_channels}
Greg Ward33bcd982003-03-09 23:57:34 +0000170Sets the number of output channels to \var{num_channels}. A value of 1
171indicates monophonic sound, 2 stereophonic. Some devices may have more
172than 2 channels, and some high-end devices may not support mono.
173Returns the number of channels the device was set to.
Greg Ward620e3432003-03-09 23:34:52 +0000174\end{methoddesc}
175
176\begin{methoddesc}[audio device]{speed}{samplerate}
Greg Ward33bcd982003-03-09 23:57:34 +0000177Sets the samplerate to \var{samplerate} samples per second and returns
178the rate actually set. Most sound devices don't support arbitrary
179sample rates. Common rates are:
Greg Ward620e3432003-03-09 23:34:52 +0000180
Greg Ward33bcd982003-03-09 23:57:34 +00001818000---default rate
18211025---speech recording
Greg Ward620e3432003-03-09 23:34:52 +000018322050
Greg Ward33bcd982003-03-09 23:57:34 +000018444100---Audio CD-quality (at 16 bits/sample and 2 channels)
18596000---DVD-quality
Greg Ward620e3432003-03-09 23:34:52 +0000186\end{methoddesc}
187
188\begin{methoddesc}[audio device]{sync}
Greg Ward33bcd982003-03-09 23:57:34 +0000189Waits until the sound device has played every byte in its buffer and
190returns. This also occurs when the sound device is closed. The OSS
191documentation recommends simply closing and re-opening the device rather
Greg Ward3e34f592003-03-10 02:09:51 +0000192than using \method{sync()}.
Greg Ward620e3432003-03-09 23:34:52 +0000193\end{methoddesc}
194
195\begin{methoddesc}[audio device]{reset}
Greg Ward33bcd982003-03-09 23:57:34 +0000196Immediately stops and playing or recording and returns the device to a
197state where it can accept commands. The OSS documentation recommends
Greg Ward3e34f592003-03-10 02:09:51 +0000198closing and re-opening the device after calling \method{reset()}.
Greg Ward620e3432003-03-09 23:34:52 +0000199\end{methoddesc}
200
201\begin{methoddesc}[audio device]{post}
Greg Ward3e34f592003-03-10 02:09:51 +0000202To be used like a lightweight \method{sync()}, the \method{post()}
203IOCTL informs the audio device that there is a likely to be a pause in
204the audio output---i.e., after playing a spot sound effect, before
205waiting for user input, or before doing disk I/O.
Greg Ward620e3432003-03-09 23:34:52 +0000206\end{methoddesc}
207
208Convenience methods
209
210\begin{methoddesc}[audio device]{setparameters}{samplerate,num_channels,format,emulate}
Greg Ward33bcd982003-03-09 23:57:34 +0000211Initialise the sound device in one method. \var{samplerate},
212\var{channels} and \var{format} should be as specified in the
Greg Ward3e34f592003-03-10 02:09:51 +0000213\method{speed()}, \method{channels()} and \method{setfmt()}
214methods. If \var{emulate} is true, attempt to find the closest matching
215format instead, otherwise raise ValueError if the device does not
216support the format. The default is to raise ValueError on unsupported
217formats.
Greg Ward620e3432003-03-09 23:34:52 +0000218\end{methoddesc}
219
220\begin{methoddesc}[audio device]{bufsize}{}
221Returns the size of the hardware buffer, in samples.
222\end{methoddesc}
223
224\begin{methoddesc}[audio device]{obufcount}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000225Returns the number of samples that are in the hardware buffer yet to be
226played.
Greg Ward620e3432003-03-09 23:34:52 +0000227\end{methoddesc}
228
229\begin{methoddesc}[audio device]{obuffree}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000230Returns the number of samples that could be queued into the hardware
231buffer to be played without blocking.
Greg Ward620e3432003-03-09 23:34:52 +0000232\end{methoddesc}
233
234\subsection{Mixer Device Objects \label{mixer-device-objects}}
235
236File-like interface
237
238\begin{methoddesc}[mixer device]{close}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000239This method closes the open mixer device file. Any further attempts to
240use the mixer after this file is closed will raise an IOError.
Greg Ward620e3432003-03-09 23:34:52 +0000241\end{methoddesc}
242
243\begin{methoddesc}[mixer device]{fileno}{}
244Returns the file handle number of the open mixer device file.
245\end{methoddesc}
246
247Mixer interface
248
249\begin{methoddesc}[mixer device]{controls}{}
250This method returns a bitmask specifying the available mixer controls
251(``Control'' being a specific mixable ``channel'', such as
Greg Ward3e34f592003-03-10 02:09:51 +0000252\constant{SOUND_MIXER_PCM} or \constant{SOUND_MIXER_SYNTH}). This
Greg Ward33bcd982003-03-09 23:57:34 +0000253bitmask indicates a subset of all available mixer channels---the
Greg Ward3e34f592003-03-10 02:09:51 +0000254\constant{SOUND_MIXER_*} constants defined at module level. To determine if,
Greg Ward620e3432003-03-09 23:34:52 +0000255for example, the current mixer object supports a PCM mixer, use the
256following Python code:
257
258\begin{verbatim}
259mixer=ossaudiodev.openmixer()
260if mixer.channels() & (1 << ossaudiodev.SOUND_MIXER_PCM):
261 # PCM is supported
262 <code>
263\end{verbatim}
264
Greg Ward3e34f592003-03-10 02:09:51 +0000265For most purposes, the \constant{SOUND_MIXER_VOLUME} (Master volume) and
266\constant{SOUND_MIXER_PCM} channels should suffice---but code that uses the
Greg Ward33bcd982003-03-09 23:57:34 +0000267mixer should be flexible when it comes to choosing sound channels. On
Greg Ward3e34f592003-03-10 02:09:51 +0000268the Gravis Ultrasound, for example, \constant{SOUND_MIXER_VOLUME} does not
Greg Ward33bcd982003-03-09 23:57:34 +0000269exist.
Greg Ward620e3432003-03-09 23:34:52 +0000270\end{methoddesc}
271
272\begin{methoddesc}[mixer device]{stereocontrols}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000273Returns a bitmask indicating stereo mixer channels. If a bit is set,
274the corresponding channel is stereo; if it is unset, the channel is
275either monophonic or not supported by the mixer (use in combination with
Greg Ward3e34f592003-03-10 02:09:51 +0000276\method{channels()} to determine which).
Greg Ward620e3432003-03-09 23:34:52 +0000277
Greg Ward3e34f592003-03-10 02:09:51 +0000278See the code example for the \method{channels()} function for an example
Greg Ward33bcd982003-03-09 23:57:34 +0000279of getting data from a bitmask.
Greg Ward620e3432003-03-09 23:34:52 +0000280\end{methoddesc}
281
282\begin{methoddesc}[mixer device]{reccontrols}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000283Returns a bitmask specifying the mixer controls that may be used to
Greg Ward3e34f592003-03-10 02:09:51 +0000284record. See the code example for \method{controls()} for an example of
Greg Ward33bcd982003-03-09 23:57:34 +0000285reading from a bitmask.
Greg Ward620e3432003-03-09 23:34:52 +0000286\end{methoddesc}
287
288\begin{methoddesc}[mixer device]{get}{channel}
Greg Ward33bcd982003-03-09 23:57:34 +0000289Returns the volume of a given mixer channel. The returned volume is a
Greg Ward3e34f592003-03-10 02:09:51 +00002902-tuple \code{(left_volume,right_volume)}. Volumes are specified as
Greg Ward33bcd982003-03-09 23:57:34 +0000291numbers from 0 (silent) to 100 (full volume). If the channel is
292monophonic, a 2-tuple is still returned, but both channel volumes are
293the same.
Greg Ward620e3432003-03-09 23:34:52 +0000294
295If an unknown channel is specified, \exception{error} is raised.
296\end{methoddesc}
297
298\begin{methoddesc}[mixer device]{set}{channel, (left, right)}
Greg Ward3e34f592003-03-10 02:09:51 +0000299Sets the volume for a given mixer channel to \code{(left,right)}.
Greg Ward620e3432003-03-09 23:34:52 +0000300\code{left} and \code{right} must be ints and between 0 (silent) and 100
Greg Ward33bcd982003-03-09 23:57:34 +0000301(full volume). On success, the new volume is returned as a 2-tuple.
302Note that this may not be exactly the same as the volume specified,
303because of the limited resolution of some soundcard's mixers.
Greg Ward620e3432003-03-09 23:34:52 +0000304
305Raises \exception{IOError} if an invalid mixer channel was specified;
Greg Ward33bcd982003-03-09 23:57:34 +0000306\exception{TypeError} if the argument format was incorrect, and
Greg Ward620e3432003-03-09 23:34:52 +0000307\exception{error} if the specified volumes were out-of-range.
308\end{methoddesc}
309
310\begin{methoddesc}[mixer device]{get_recsrc}{}
311This method returns a bitmask indicating which channel or channels are
312currently being used as a recording source.
313\end{methoddesc}
314
315\begin{methoddesc}[mixer device]{set_recsrc}{bitmask}
Greg Ward33bcd982003-03-09 23:57:34 +0000316Call this function to specify a recording source. Returns a bitmask
Greg Ward620e3432003-03-09 23:34:52 +0000317indicating the new recording source (or sources) if successful; raises
Greg Ward33bcd982003-03-09 23:57:34 +0000318\exception{IOError} if an invalid source was specified. To set the current
Greg Ward620e3432003-03-09 23:34:52 +0000319recording source to the microphone input:
320
321\begin{verbatim}
322mixer.setrecsrc (1 << ossaudiodev.SOUND_MIXER_MIC)
323\end{verbatim}
324\end{methoddesc}
325
326
327