blob: d9b4fe5603b4fa7aac410baabffecbdc99e6b491 [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 Wardc50b0882003-05-03 19:45:47 +00005\platform{Linux, FreeBSD, possibly other \UNIX-like systems}
Greg Ward3e34f592003-03-10 02:09:51 +00006\modulesynopsis{Access to OSS-compatible audio devices.}
Greg Ward620e3432003-03-09 23:34:52 +00007
Greg Ward3e34f592003-03-10 02:09:51 +00008This module allows you to access the OSS (Open Sound System) audio
9interface. OSS is available for a wide range of open-source and
Greg Wardc50b0882003-05-03 19:45:47 +000010commercial Unices, and is the standard audio interface for Linux and
11recent versions of FreeBSD.
12
13% Things will get more complicated for future Linux versions, since
14% ALSA is in the standard kernel as of 2.5.x. Presumably if you
15% use ALSA, you'll have to make sure its OSS compatibility layer
16% is active to use ossaudiodev, but you're gonna need it for the vast
17% majority of Linux audio apps anyways.
18%
19% Sounds like things are also complicated for other BSDs. In response
20% to my python-dev query, Thomas Wouters said:
21%
22% > Likewise, googling shows OpenBSD also uses OSS/Free -- the commercial
23% > OSS installation manual tells you to remove references to OSS/Free from the
24% > kernel :)
25%
26% but Aleksander Piotrowsk actually has an OpenBSD box, and he quotes
27% from its <soundcard.h>:
28% > * WARNING! WARNING!
29% > * This is an OSS (Linux) audio emulator.
30% > * Use the Native NetBSD API for developing new code, and this
31% > * only for compiling Linux programs.
32%
33% There's also an ossaudio manpage on OpenBSD that explains things
34% further. Presumably NetBSD and OpenBSD have a different standard
35% audio interface. That's the great thing about standards, there are so
36% many to choose from ... ;-)
37%
38% This probably all warrants a footnote or two, but I don't understand
39% things well enough right now to write it! --GPW
Greg Ward620e3432003-03-09 23:34:52 +000040
Greg Ward3e34f592003-03-10 02:09:51 +000041\begin{seealso}
42\seetitle[http://www.opensound.com/pguide/oss.pdf]
Greg Wardc50b0882003-05-03 19:45:47 +000043 {Open Sound System Programmer's Guide} {the official
44 documentation for the OSS C API}
Greg Ward3e34f592003-03-10 02:09:51 +000045\seetext{The module defines a large number of constants supplied by
46 the OSS device driver; see \file{<sys/soundcard.h>} on either
47 Linux or FreeBSD for a listing .}
48\end{seealso}
Greg Ward620e3432003-03-09 23:34:52 +000049
Greg Ward3e34f592003-03-10 02:09:51 +000050\module{ossaudiodev} defines the following variables and functions:
Greg Ward620e3432003-03-09 23:34:52 +000051
52\begin{excdesc}{error}
Greg Wardf882c772003-03-10 03:05:21 +000053This exception is raised on certain errors. The argument is a string
54describing what went wrong.
55
56(If \module{ossaudiodev} receives an error from a system call such as
57\cfunction{open()}, \cfunction{write()}, or \cfunction{ioctl()}, it
58raises \exception{IOError}. Errors detected directly by
59\module{ossaudiodev} result in \exception{ossaudiodev.error}.)
Greg Ward620e3432003-03-09 23:34:52 +000060\end{excdesc}
61
62\begin{funcdesc}{open}{\optional{device, }mode}
Greg Wardf882c772003-03-10 03:05:21 +000063Open an audio device and return an OSS audio device object. This
64object supports many file-like methods, such as \method{read()},
65\method{write()}, and \method{fileno()} (although there are subtle
66differences between conventional Unix read/write semantics and those of
67OSS audio devices). It also supports a number of audio-specific
68methods; see below for the complete list of methods.
Greg Ward620e3432003-03-09 23:34:52 +000069
Greg Wardf882c772003-03-10 03:05:21 +000070Note the unusual calling syntax: the \emph{first} argument is optional,
71and the second is required. This is a historical artifact for
72compatibility with the older \module{linuxaudiodev} module which
73\module{ossaudiodev} supersedes. % XXX it might also be motivated
74% by my unfounded-but-still-possibly-true belief that the default
75% audio device varies unpredictably across operating systems. -GW
76
77\var{device} is the audio device filename to use. If it is not
78specified, this module first looks in the environment variable
79\envvar{AUDIODEV} for a device to use. If not found, it falls back to
80\file{/dev/dsp}.
81
82\var{mode} is one of \code{'r'} for read-only (record) access,
83\code{'w'} for write-only (playback) access and \code{'rw'} for both.
84Since many soundcards only allow one process to have the recorder or
85player open at a time it is a good idea to open the device only for the
86activity needed. Further, some soundcards are half-duplex: they can be
87opened for reading or writing, but not both at once.
Greg Ward620e3432003-03-09 23:34:52 +000088\end{funcdesc}
89
Greg Wardcd930f52003-03-10 03:18:19 +000090\begin{funcdesc}{openmixer}{\optional{device}}
Greg Wardf882c772003-03-10 03:05:21 +000091Open a mixer device and return an OSS mixer device object.
92\var{device} is the mixer device filename to use. If it is
Greg Ward33bcd982003-03-09 23:57:34 +000093not specified, this module first looks in the environment variable
Greg Ward3e34f592003-03-10 02:09:51 +000094\envvar{MIXERDEV} for a device to use. If not found, it falls back to
Greg Wardcd930f52003-03-10 03:18:19 +000095\file{/dev/mixer}.
Greg Wardf882c772003-03-10 03:05:21 +000096
Greg Ward620e3432003-03-09 23:34:52 +000097\end{funcdesc}
98
99\subsection{Audio Device Objects \label{ossaudio-device-objects}}
100
101Setting up the device
102
Greg Ward33bcd982003-03-09 23:57:34 +0000103To set up the device, three functions must be called in the correct
104sequence:
Greg Ward074472b2003-03-10 00:24:42 +0000105\begin{enumerate}
Greg Ward3e34f592003-03-10 02:09:51 +0000106\item \method{setfmt()} to set the output format,
107\item \method{channels()} to set the number of channels, and
108\item \method{speed()} to set the sample rate.
Greg Ward074472b2003-03-10 00:24:42 +0000109\end{enumerate}
Greg Ward620e3432003-03-09 23:34:52 +0000110
111The audio device objects are returned by \function{open()} define the
112following methods:
113
114\begin{methoddesc}[audio device]{close}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000115This method explicitly closes the device. It is useful in situations
116where deleting the object does not immediately close it since there are
117other references to it. A closed device should not be used again.
Greg Ward620e3432003-03-09 23:34:52 +0000118\end{methoddesc}
119
120\begin{methoddesc}[audio device]{fileno}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000121Returns the file descriptor associated with the device.
Greg Ward620e3432003-03-09 23:34:52 +0000122\end{methoddesc}
123
124\begin{methoddesc}[audio device]{read}{size}
Greg Ward33bcd982003-03-09 23:57:34 +0000125Reads \var{size} samples from the audio input and returns them as a
126Python string. The function blocks until enough data is available.
Greg Ward620e3432003-03-09 23:34:52 +0000127\end{methoddesc}
128
129\begin{methoddesc}[audio device]{write}{data}
Greg Ward33bcd982003-03-09 23:57:34 +0000130Writes Python string \var{data} to the audio device and returns the
131number of bytes written. If the audio device is opened in blocking
132mode, the entire string is always written. If the device is opened in
Greg Ward3e34f592003-03-10 02:09:51 +0000133nonblocking mode, some data may not be written---see
134\method{writeall()}.
Greg Ward620e3432003-03-09 23:34:52 +0000135\end{methoddesc}
136
137\begin{methoddesc}[audio device]{writeall}{data}
Greg Ward33bcd982003-03-09 23:57:34 +0000138Writes the entire Python string \var{data} to the audio device. If the
Greg Ward3e34f592003-03-10 02:09:51 +0000139device is opened in blocking mode, behaves identially to
140\method{write()}; in nonblocking mode, waits until the device becomes
141available before feeding it more data. Returns \code{None}, since the
142amount of data written is always equal to the amount of data supplied.
Greg Ward620e3432003-03-09 23:34:52 +0000143\end{methoddesc}
144
145Simple IOCTLs:
146
147\begin{methoddesc}[audio device]{nonblock}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000148Attempts to put the device into nonblocking mode. Once in nonblocking
149mode there is no way to return to blocking mode.
Greg Ward620e3432003-03-09 23:34:52 +0000150
151Raises \exception{IOError} if the IOCTL failed.
152\end{methoddesc}
153
154\begin{methoddesc}[audio device]{getfmts}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000155Returns a bitmask of the audio output formats supported by the
156soundcard. On a typical Linux system, these formats are:
Greg Ward620e3432003-03-09 23:34:52 +0000157
Greg Ward074472b2003-03-10 00:24:42 +0000158\begin{tableii}{l|l}{constant}{Format}{Description}
159\lineii{AFMT_MU_LAW}
160 {a logarithmic encoding. This is the default format on
Greg Ward3e34f592003-03-10 02:09:51 +0000161 \file{/dev/audio} and is the format used by Sun .au files.}
Greg Ward074472b2003-03-10 00:24:42 +0000162\lineii{AFMT_A_LAW}
163 {a logarithmic encoding}
164\lineii{AFMT_IMA_ADPCM}
165 {a 4:1 compressed format defined by the Interactive Multimedia
166 Association.}
167\lineii{AFMT_U8}
168 {Unsigned, 8-bit audio.}
169\lineii{AFMT_S16_LE}
170 {Unsigned, 16-bit audio, little-endian byte order (as used by
171 Intel processors)}
172\lineii{AFMT_S16_BE}
173 {Unsigned, 16-bit audio, big-endian byte order (as used by 68k,
174 PowerPC, Sparc)}
175\lineii{AFMT_S8}
176 {Signed, 8 bit audio.}
177\lineii{AFMT_U16_LE}
178 {Signed, 16-bit little-endian audio}
179\lineii{AFMT_U16_BE}
180 {Signed, 16-bit big-endian audio}
181\end{tableii}
Greg Ward33bcd982003-03-09 23:57:34 +0000182Most systems support only a subset of these formats. Many devices only
Greg Ward3e34f592003-03-10 02:09:51 +0000183support \constant{AFMT_U8}; the most common format used today is
184\constant{AFMT_S16_LE}.
Greg Ward620e3432003-03-09 23:34:52 +0000185\end{methoddesc}
186
187\begin{methoddesc}[audio device]{setfmt}{format}
Greg Ward33bcd982003-03-09 23:57:34 +0000188Used to set the current audio format to \var{format}---see
Greg Ward3e34f592003-03-10 02:09:51 +0000189\method{getfmts()} for a list. May also be used to return the current
190audio format---do this by passing an ``audio format'' of
191\constant{AFMT_QUERY}. Returns the audio format that the device was set
192to, which may not be the requested format.
Greg Ward620e3432003-03-09 23:34:52 +0000193\end{methoddesc}
194
195\begin{methoddesc}[audio device]{channels}{num_channels}
Greg Ward33bcd982003-03-09 23:57:34 +0000196Sets the number of output channels to \var{num_channels}. A value of 1
197indicates monophonic sound, 2 stereophonic. Some devices may have more
198than 2 channels, and some high-end devices may not support mono.
199Returns the number of channels the device was set to.
Greg Ward620e3432003-03-09 23:34:52 +0000200\end{methoddesc}
201
202\begin{methoddesc}[audio device]{speed}{samplerate}
Greg Ward33bcd982003-03-09 23:57:34 +0000203Sets the samplerate to \var{samplerate} samples per second and returns
204the rate actually set. Most sound devices don't support arbitrary
205sample rates. Common rates are:
Greg Ward620e3432003-03-09 23:34:52 +0000206
Greg Ward33bcd982003-03-09 23:57:34 +00002078000---default rate
20811025---speech recording
Greg Ward620e3432003-03-09 23:34:52 +000020922050
Greg Ward33bcd982003-03-09 23:57:34 +000021044100---Audio CD-quality (at 16 bits/sample and 2 channels)
21196000---DVD-quality
Greg Ward620e3432003-03-09 23:34:52 +0000212\end{methoddesc}
213
214\begin{methoddesc}[audio device]{sync}
Greg Ward33bcd982003-03-09 23:57:34 +0000215Waits until the sound device has played every byte in its buffer and
216returns. This also occurs when the sound device is closed. The OSS
217documentation recommends simply closing and re-opening the device rather
Greg Ward3e34f592003-03-10 02:09:51 +0000218than using \method{sync()}.
Greg Ward620e3432003-03-09 23:34:52 +0000219\end{methoddesc}
220
221\begin{methoddesc}[audio device]{reset}
Greg Ward33bcd982003-03-09 23:57:34 +0000222Immediately stops and playing or recording and returns the device to a
223state where it can accept commands. The OSS documentation recommends
Greg Ward3e34f592003-03-10 02:09:51 +0000224closing and re-opening the device after calling \method{reset()}.
Greg Ward620e3432003-03-09 23:34:52 +0000225\end{methoddesc}
226
227\begin{methoddesc}[audio device]{post}
Greg Ward3e34f592003-03-10 02:09:51 +0000228To be used like a lightweight \method{sync()}, the \method{post()}
229IOCTL informs the audio device that there is a likely to be a pause in
230the audio output---i.e., after playing a spot sound effect, before
231waiting for user input, or before doing disk I/O.
Greg Ward620e3432003-03-09 23:34:52 +0000232\end{methoddesc}
233
234Convenience methods
235
236\begin{methoddesc}[audio device]{setparameters}{samplerate,num_channels,format,emulate}
Greg Ward33bcd982003-03-09 23:57:34 +0000237Initialise the sound device in one method. \var{samplerate},
238\var{channels} and \var{format} should be as specified in the
Greg Ward3e34f592003-03-10 02:09:51 +0000239\method{speed()}, \method{channels()} and \method{setfmt()}
240methods. If \var{emulate} is true, attempt to find the closest matching
241format instead, otherwise raise ValueError if the device does not
242support the format. The default is to raise ValueError on unsupported
243formats.
Greg Ward620e3432003-03-09 23:34:52 +0000244\end{methoddesc}
245
246\begin{methoddesc}[audio device]{bufsize}{}
247Returns the size of the hardware buffer, in samples.
248\end{methoddesc}
249
250\begin{methoddesc}[audio device]{obufcount}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000251Returns the number of samples that are in the hardware buffer yet to be
252played.
Greg Ward620e3432003-03-09 23:34:52 +0000253\end{methoddesc}
254
255\begin{methoddesc}[audio device]{obuffree}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000256Returns the number of samples that could be queued into the hardware
257buffer to be played without blocking.
Greg Ward620e3432003-03-09 23:34:52 +0000258\end{methoddesc}
259
260\subsection{Mixer Device Objects \label{mixer-device-objects}}
261
262File-like interface
263
264\begin{methoddesc}[mixer device]{close}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000265This method closes the open mixer device file. Any further attempts to
266use the mixer after this file is closed will raise an IOError.
Greg Ward620e3432003-03-09 23:34:52 +0000267\end{methoddesc}
268
269\begin{methoddesc}[mixer device]{fileno}{}
270Returns the file handle number of the open mixer device file.
271\end{methoddesc}
272
273Mixer interface
274
275\begin{methoddesc}[mixer device]{controls}{}
276This method returns a bitmask specifying the available mixer controls
277(``Control'' being a specific mixable ``channel'', such as
Greg Ward3e34f592003-03-10 02:09:51 +0000278\constant{SOUND_MIXER_PCM} or \constant{SOUND_MIXER_SYNTH}). This
Greg Ward33bcd982003-03-09 23:57:34 +0000279bitmask indicates a subset of all available mixer channels---the
Greg Ward3e34f592003-03-10 02:09:51 +0000280\constant{SOUND_MIXER_*} constants defined at module level. To determine if,
Greg Ward620e3432003-03-09 23:34:52 +0000281for example, the current mixer object supports a PCM mixer, use the
282following Python code:
283
284\begin{verbatim}
285mixer=ossaudiodev.openmixer()
286if mixer.channels() & (1 << ossaudiodev.SOUND_MIXER_PCM):
287 # PCM is supported
288 <code>
289\end{verbatim}
290
Greg Ward3e34f592003-03-10 02:09:51 +0000291For most purposes, the \constant{SOUND_MIXER_VOLUME} (Master volume) and
292\constant{SOUND_MIXER_PCM} channels should suffice---but code that uses the
Greg Ward33bcd982003-03-09 23:57:34 +0000293mixer should be flexible when it comes to choosing sound channels. On
Greg Ward3e34f592003-03-10 02:09:51 +0000294the Gravis Ultrasound, for example, \constant{SOUND_MIXER_VOLUME} does not
Greg Ward33bcd982003-03-09 23:57:34 +0000295exist.
Greg Ward620e3432003-03-09 23:34:52 +0000296\end{methoddesc}
297
298\begin{methoddesc}[mixer device]{stereocontrols}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000299Returns a bitmask indicating stereo mixer channels. If a bit is set,
300the corresponding channel is stereo; if it is unset, the channel is
301either monophonic or not supported by the mixer (use in combination with
Greg Ward3e34f592003-03-10 02:09:51 +0000302\method{channels()} to determine which).
Greg Ward620e3432003-03-09 23:34:52 +0000303
Greg Ward3e34f592003-03-10 02:09:51 +0000304See the code example for the \method{channels()} function for an example
Greg Ward33bcd982003-03-09 23:57:34 +0000305of getting data from a bitmask.
Greg Ward620e3432003-03-09 23:34:52 +0000306\end{methoddesc}
307
308\begin{methoddesc}[mixer device]{reccontrols}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000309Returns a bitmask specifying the mixer controls that may be used to
Greg Ward3e34f592003-03-10 02:09:51 +0000310record. See the code example for \method{controls()} for an example of
Greg Ward33bcd982003-03-09 23:57:34 +0000311reading from a bitmask.
Greg Ward620e3432003-03-09 23:34:52 +0000312\end{methoddesc}
313
314\begin{methoddesc}[mixer device]{get}{channel}
Greg Ward33bcd982003-03-09 23:57:34 +0000315Returns the volume of a given mixer channel. The returned volume is a
Greg Ward3e34f592003-03-10 02:09:51 +00003162-tuple \code{(left_volume,right_volume)}. Volumes are specified as
Greg Ward33bcd982003-03-09 23:57:34 +0000317numbers from 0 (silent) to 100 (full volume). If the channel is
318monophonic, a 2-tuple is still returned, but both channel volumes are
319the same.
Greg Ward620e3432003-03-09 23:34:52 +0000320
321If an unknown channel is specified, \exception{error} is raised.
322\end{methoddesc}
323
324\begin{methoddesc}[mixer device]{set}{channel, (left, right)}
Greg Ward3e34f592003-03-10 02:09:51 +0000325Sets the volume for a given mixer channel to \code{(left,right)}.
Greg Ward620e3432003-03-09 23:34:52 +0000326\code{left} and \code{right} must be ints and between 0 (silent) and 100
Greg Ward33bcd982003-03-09 23:57:34 +0000327(full volume). On success, the new volume is returned as a 2-tuple.
328Note that this may not be exactly the same as the volume specified,
329because of the limited resolution of some soundcard's mixers.
Greg Ward620e3432003-03-09 23:34:52 +0000330
331Raises \exception{IOError} if an invalid mixer channel was specified;
Greg Ward33bcd982003-03-09 23:57:34 +0000332\exception{TypeError} if the argument format was incorrect, and
Greg Ward620e3432003-03-09 23:34:52 +0000333\exception{error} if the specified volumes were out-of-range.
334\end{methoddesc}
335
336\begin{methoddesc}[mixer device]{get_recsrc}{}
337This method returns a bitmask indicating which channel or channels are
338currently being used as a recording source.
339\end{methoddesc}
340
341\begin{methoddesc}[mixer device]{set_recsrc}{bitmask}
Greg Ward33bcd982003-03-09 23:57:34 +0000342Call this function to specify a recording source. Returns a bitmask
Greg Ward620e3432003-03-09 23:34:52 +0000343indicating the new recording source (or sources) if successful; raises
Greg Ward33bcd982003-03-09 23:57:34 +0000344\exception{IOError} if an invalid source was specified. To set the current
Greg Ward620e3432003-03-09 23:34:52 +0000345recording source to the microphone input:
346
347\begin{verbatim}
348mixer.setrecsrc (1 << ossaudiodev.SOUND_MIXER_MIC)
349\end{verbatim}
350\end{methoddesc}
351
352
353