blob: ae3d16f80fa1fdad6b247f37888c97fa910aff82 [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}
Fred Drake1dc3bb42003-09-30 20:00:43 +00005\platform{Linux, FreeBSD, maybe 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
Greg Wardc316d0d2003-05-23 02:44:46 +000046 the OSS device driver; see \code{<sys/soundcard.h>} on either
Greg Ward3e34f592003-03-10 02:09:51 +000047 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
Greg Wardfb1b5a12003-05-29 01:39:32 +000052\begin{excdesc}{OSSAudioError}
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
Greg Wardfb1b5a12003-05-29 01:39:32 +000059\module{ossaudiodev} result in \exception{OSSAudioError}.)
60
61(For backwards compatibility, the exception class is also available as
62\code{ossaudiodev.error}.)
Greg Ward620e3432003-03-09 23:34:52 +000063\end{excdesc}
64
65\begin{funcdesc}{open}{\optional{device, }mode}
Greg Wardf882c772003-03-10 03:05:21 +000066Open an audio device and return an OSS audio device object. This
67object supports many file-like methods, such as \method{read()},
68\method{write()}, and \method{fileno()} (although there are subtle
69differences between conventional Unix read/write semantics and those of
70OSS audio devices). It also supports a number of audio-specific
71methods; see below for the complete list of methods.
Greg Ward620e3432003-03-09 23:34:52 +000072
Greg Wardf882c772003-03-10 03:05:21 +000073\var{device} is the audio device filename to use. If it is not
74specified, this module first looks in the environment variable
75\envvar{AUDIODEV} for a device to use. If not found, it falls back to
76\file{/dev/dsp}.
77
78\var{mode} is one of \code{'r'} for read-only (record) access,
79\code{'w'} for write-only (playback) access and \code{'rw'} for both.
Greg Ward451a7662003-05-26 01:51:33 +000080Since many sound cards only allow one process to have the recorder or
81player open at a time, it is a good idea to open the device only for the
82activity needed. Further, some sound cards are half-duplex: they can be
Greg Wardf882c772003-03-10 03:05:21 +000083opened for reading or writing, but not both at once.
Greg Wardc316d0d2003-05-23 02:44:46 +000084
85Note the unusual calling syntax: the \emph{first} argument is optional,
86and the second is required. This is a historical artifact for
87compatibility with the older \module{linuxaudiodev} module which
88\module{ossaudiodev} supersedes. % XXX it might also be motivated
89% by my unfounded-but-still-possibly-true belief that the default
90% audio device varies unpredictably across operating systems. -GW
Greg Ward620e3432003-03-09 23:34:52 +000091\end{funcdesc}
92
Greg Wardcd930f52003-03-10 03:18:19 +000093\begin{funcdesc}{openmixer}{\optional{device}}
Greg Wardf882c772003-03-10 03:05:21 +000094Open a mixer device and return an OSS mixer device object.
95\var{device} is the mixer device filename to use. If it is
Greg Ward33bcd982003-03-09 23:57:34 +000096not specified, this module first looks in the environment variable
Greg Ward3e34f592003-03-10 02:09:51 +000097\envvar{MIXERDEV} for a device to use. If not found, it falls back to
Greg Wardcd930f52003-03-10 03:18:19 +000098\file{/dev/mixer}.
Greg Wardf882c772003-03-10 03:05:21 +000099
Greg Ward620e3432003-03-09 23:34:52 +0000100\end{funcdesc}
101
102\subsection{Audio Device Objects \label{ossaudio-device-objects}}
103
Greg Wardc316d0d2003-05-23 02:44:46 +0000104Before you can write to or read from an audio device, you must call
105three methods in the correct order:
Greg Ward074472b2003-03-10 00:24:42 +0000106\begin{enumerate}
Greg Wardc316d0d2003-05-23 02:44:46 +0000107\item \method{setfmt()} to set the output format
108\item \method{channels()} to set the number of channels
109\item \method{speed()} to set the sample rate
Greg Ward074472b2003-03-10 00:24:42 +0000110\end{enumerate}
Greg Wardc316d0d2003-05-23 02:44:46 +0000111Alternately, you can use the \method{setparameters()} method to set all
112three audio parameters at once. This is more convenient, but may not be
113as flexible in all cases.
Greg Ward620e3432003-03-09 23:34:52 +0000114
Greg Ward451a7662003-05-26 01:51:33 +0000115The audio device objects returned by \function{open()} define the
Greg Ward620e3432003-03-09 23:34:52 +0000116following methods:
117
118\begin{methoddesc}[audio device]{close}{}
Greg Wardc316d0d2003-05-23 02:44:46 +0000119Explicitly close the audio device. When you are done writing to or
120reading from an audio device, you should explicitly close it. A closed
121device cannot be used again.
Greg Ward620e3432003-03-09 23:34:52 +0000122\end{methoddesc}
123
124\begin{methoddesc}[audio device]{fileno}{}
Greg Wardc316d0d2003-05-23 02:44:46 +0000125Return the file descriptor associated with the device.
Greg Ward620e3432003-03-09 23:34:52 +0000126\end{methoddesc}
127
128\begin{methoddesc}[audio device]{read}{size}
Greg Wardc316d0d2003-05-23 02:44:46 +0000129Read \var{size} bytes from the audio input and return them as a Python
130string. Unlike most \UNIX{} device drivers, OSS audio devices in
131blocking mode (the default) will block \function{read()} until the
132entire requested amount of data is available.
Greg Ward620e3432003-03-09 23:34:52 +0000133\end{methoddesc}
134
135\begin{methoddesc}[audio device]{write}{data}
Greg Wardc316d0d2003-05-23 02:44:46 +0000136Write the Python string \var{data} to the audio device and return the
137number of bytes written. If the audio device is in blocking mode (the
138default), the entire string is always written (again, this is different
139from usual \UNIX{} device semantics). If the device is in non-blocking
140mode, some data may not be written---see \method{writeall()}.
Greg Ward620e3432003-03-09 23:34:52 +0000141\end{methoddesc}
142
143\begin{methoddesc}[audio device]{writeall}{data}
Greg Ward451a7662003-05-26 01:51:33 +0000144Write the entire Python string \var{data} to the audio device: waits
145until the audio device is able to accept data, writes as much data as it
146will accept, and repeats until \var{data} has been completely written.
147If the device is in blocking mode (the default), this has the same
148effect as \method{write()}; \method{writeall()} is only useful in
149non-blocking mode. Has no return value, since the amount of data
150written is always equal to the amount of data supplied.
Greg Ward620e3432003-03-09 23:34:52 +0000151\end{methoddesc}
152
Greg Wardc316d0d2003-05-23 02:44:46 +0000153The following methods each map to exactly one
Greg Ward451a7662003-05-26 01:51:33 +0000154\function{ioctl()} system call. The correspondence is obvious: for
155example, \method{setfmt()} corresponds to the \code{SNDCTL_DSP_SETFMT}
156ioctl, and \method{sync()} to \code{SNDCTL_DSP_SYNC} (this can be useful
157when consulting the OSS documentation). If the underlying
158\function{ioctl()} fails, they all raise \exception{IOError}.
Greg Ward620e3432003-03-09 23:34:52 +0000159
160\begin{methoddesc}[audio device]{nonblock}{}
Greg Wardc316d0d2003-05-23 02:44:46 +0000161Put the device into non-blocking mode. Once in non-blocking mode, there
162is no way to return it to blocking mode.
Greg Ward620e3432003-03-09 23:34:52 +0000163\end{methoddesc}
164
165\begin{methoddesc}[audio device]{getfmts}{}
Greg Wardc316d0d2003-05-23 02:44:46 +0000166Return a bitmask of the audio output formats supported by the
Greg Ward33bcd982003-03-09 23:57:34 +0000167soundcard. On a typical Linux system, these formats are:
Greg Ward620e3432003-03-09 23:34:52 +0000168
Greg Ward074472b2003-03-10 00:24:42 +0000169\begin{tableii}{l|l}{constant}{Format}{Description}
170\lineii{AFMT_MU_LAW}
Greg Wardc316d0d2003-05-23 02:44:46 +0000171 {a logarithmic encoding (used by Sun \code{.au} files and
172 \filenq{/dev/audio})}
Greg Ward074472b2003-03-10 00:24:42 +0000173\lineii{AFMT_A_LAW}
174 {a logarithmic encoding}
175\lineii{AFMT_IMA_ADPCM}
176 {a 4:1 compressed format defined by the Interactive Multimedia
Greg Wardc316d0d2003-05-23 02:44:46 +0000177 Association}
Greg Ward074472b2003-03-10 00:24:42 +0000178\lineii{AFMT_U8}
Greg Wardc316d0d2003-05-23 02:44:46 +0000179 {Unsigned, 8-bit audio}
Greg Ward074472b2003-03-10 00:24:42 +0000180\lineii{AFMT_S16_LE}
181 {Unsigned, 16-bit audio, little-endian byte order (as used by
182 Intel processors)}
183\lineii{AFMT_S16_BE}
184 {Unsigned, 16-bit audio, big-endian byte order (as used by 68k,
185 PowerPC, Sparc)}
186\lineii{AFMT_S8}
Greg Wardc316d0d2003-05-23 02:44:46 +0000187 {Signed, 8 bit audio}
Greg Ward074472b2003-03-10 00:24:42 +0000188\lineii{AFMT_U16_LE}
189 {Signed, 16-bit little-endian audio}
190\lineii{AFMT_U16_BE}
191 {Signed, 16-bit big-endian audio}
192\end{tableii}
Greg Ward33bcd982003-03-09 23:57:34 +0000193Most systems support only a subset of these formats. Many devices only
Greg Ward3e34f592003-03-10 02:09:51 +0000194support \constant{AFMT_U8}; the most common format used today is
195\constant{AFMT_S16_LE}.
Greg Ward620e3432003-03-09 23:34:52 +0000196\end{methoddesc}
197
198\begin{methoddesc}[audio device]{setfmt}{format}
Greg Wardc316d0d2003-05-23 02:44:46 +0000199Try to set the current audio format to \var{format}---see
Greg Ward451a7662003-05-26 01:51:33 +0000200\method{getfmts()} for a list. Returns the audio format that the device
Greg Wardc316d0d2003-05-23 02:44:46 +0000201was set to, which may not be the requested format. May also be used to
202return the current audio format---do this by passing an ``audio format''
203of
204\constant{AFMT_QUERY}.
Greg Ward620e3432003-03-09 23:34:52 +0000205\end{methoddesc}
206
Greg Wardfb1b5a12003-05-29 01:39:32 +0000207\begin{methoddesc}[audio device]{channels}{nchannels}
208Set the number of output channels to \var{nchannels}. A value of 1
Greg Ward33bcd982003-03-09 23:57:34 +0000209indicates monophonic sound, 2 stereophonic. Some devices may have more
210than 2 channels, and some high-end devices may not support mono.
211Returns the number of channels the device was set to.
Greg Ward620e3432003-03-09 23:34:52 +0000212\end{methoddesc}
213
214\begin{methoddesc}[audio device]{speed}{samplerate}
Greg Wardc316d0d2003-05-23 02:44:46 +0000215Try to set the audio sampling rate to \var{samplerate} samples per
216second. Returns the rate actually set. Most sound devices don't
217support arbitrary sampling rates. Common rates are:
218\begin{tableii}{l|l}{textrm}{Rate}{Description}
219\lineii{8000}{default rate for \filenq{/dev/audio}}
220\lineii{11025}{speech recording}
221\lineii{22050}{}
222\lineii{44100}{CD quality audio (at 16 bits/sample and 2 channels)}
223\lineii{96000}{DVD quality audio (at 24 bits/sample)}
224\end{tableii}
Greg Ward620e3432003-03-09 23:34:52 +0000225\end{methoddesc}
226
Greg Wardc316d0d2003-05-23 02:44:46 +0000227\begin{methoddesc}[audio device]{sync}{}
Greg Ward451a7662003-05-26 01:51:33 +0000228Wait until the sound device has played every byte in its buffer. (This
229happens implicitly when the device is closed.) The OSS documentation
230recommends closing and re-opening the device rather than using
231\method{sync()}.
Greg Ward620e3432003-03-09 23:34:52 +0000232\end{methoddesc}
233
Greg Wardc316d0d2003-05-23 02:44:46 +0000234\begin{methoddesc}[audio device]{reset}{}
Greg Ward451a7662003-05-26 01:51:33 +0000235Immediately stop playing or recording and return the device to a
Greg Ward33bcd982003-03-09 23:57:34 +0000236state where it can accept commands. The OSS documentation recommends
Greg Ward3e34f592003-03-10 02:09:51 +0000237closing and re-opening the device after calling \method{reset()}.
Greg Ward620e3432003-03-09 23:34:52 +0000238\end{methoddesc}
239
Greg Wardc316d0d2003-05-23 02:44:46 +0000240\begin{methoddesc}[audio device]{post}{}
Greg Ward451a7662003-05-26 01:51:33 +0000241Tell the driver that there is likely to be a pause in the output, making
242it possible for the device to handle the pause more intelligently. You
243might use this after playing a spot sound effect, before waiting for
244user input, or before doing disk I/O.
Greg Ward620e3432003-03-09 23:34:52 +0000245\end{methoddesc}
246
Greg Wardfb1b5a12003-05-29 01:39:32 +0000247The following convenience methods combine several ioctls, or one ioctl
248and some simple calculations.
Greg Ward620e3432003-03-09 23:34:52 +0000249
Greg Wardfb1b5a12003-05-29 01:39:32 +0000250\begin{methoddesc}[audio device]{setparameters}
251 {format, nchannels, samplerate \optional{, strict=False}}
252
253Set the key audio sampling parameters---sample format, number of
254channels, and sampling rate---in one method call. \var{format},
255\var{nchannels}, and \var{samplerate} should be as specified in the
256\method{setfmt()}, \method{channels()}, and \method{speed()}
257methods. If \var{strict} is true, \method{setparameters()} checks to
258see if each parameter was actually set to the requested value, and
259raises \exception{OSSAudioError} if not. Returns a tuple (\var{format},
260\var{nchannels}, \var{samplerate}) indicating the parameter values that
261were actually set by the device driver (i.e., the same as the return
262valus of \method{setfmt()}, \method{channels()}, and \method{speed()}).
263
264For example,
265\begin{verbatim}
266 (fmt, channels, rate) = dsp.setparameters(fmt, channels, rate)
267\end{verbatim}
268is equivalent to
269\begin{verbatim}
270 fmt = dsp.setfmt(fmt)
271 channels = dsp.channels(channels)
272 rate = dsp.rate(channels)
273\end{verbatim}
Greg Ward620e3432003-03-09 23:34:52 +0000274\end{methoddesc}
275
276\begin{methoddesc}[audio device]{bufsize}{}
277Returns the size of the hardware buffer, in samples.
278\end{methoddesc}
279
280\begin{methoddesc}[audio device]{obufcount}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000281Returns the number of samples that are in the hardware buffer yet to be
282played.
Greg Ward620e3432003-03-09 23:34:52 +0000283\end{methoddesc}
284
285\begin{methoddesc}[audio device]{obuffree}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000286Returns the number of samples that could be queued into the hardware
287buffer to be played without blocking.
Greg Ward620e3432003-03-09 23:34:52 +0000288\end{methoddesc}
289
290\subsection{Mixer Device Objects \label{mixer-device-objects}}
291
292File-like interface
293
294\begin{methoddesc}[mixer device]{close}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000295This method closes the open mixer device file. Any further attempts to
296use the mixer after this file is closed will raise an IOError.
Greg Ward620e3432003-03-09 23:34:52 +0000297\end{methoddesc}
298
299\begin{methoddesc}[mixer device]{fileno}{}
300Returns the file handle number of the open mixer device file.
301\end{methoddesc}
302
303Mixer interface
304
305\begin{methoddesc}[mixer device]{controls}{}
306This method returns a bitmask specifying the available mixer controls
307(``Control'' being a specific mixable ``channel'', such as
Greg Ward3e34f592003-03-10 02:09:51 +0000308\constant{SOUND_MIXER_PCM} or \constant{SOUND_MIXER_SYNTH}). This
Greg Ward33bcd982003-03-09 23:57:34 +0000309bitmask indicates a subset of all available mixer channels---the
Greg Ward3e34f592003-03-10 02:09:51 +0000310\constant{SOUND_MIXER_*} constants defined at module level. To determine if,
Greg Ward620e3432003-03-09 23:34:52 +0000311for example, the current mixer object supports a PCM mixer, use the
312following Python code:
313
314\begin{verbatim}
315mixer=ossaudiodev.openmixer()
316if mixer.channels() & (1 << ossaudiodev.SOUND_MIXER_PCM):
317 # PCM is supported
318 <code>
319\end{verbatim}
320
Greg Ward3e34f592003-03-10 02:09:51 +0000321For most purposes, the \constant{SOUND_MIXER_VOLUME} (Master volume) and
322\constant{SOUND_MIXER_PCM} channels should suffice---but code that uses the
Greg Ward33bcd982003-03-09 23:57:34 +0000323mixer should be flexible when it comes to choosing sound channels. On
Greg Ward3e34f592003-03-10 02:09:51 +0000324the Gravis Ultrasound, for example, \constant{SOUND_MIXER_VOLUME} does not
Greg Ward33bcd982003-03-09 23:57:34 +0000325exist.
Greg Ward620e3432003-03-09 23:34:52 +0000326\end{methoddesc}
327
328\begin{methoddesc}[mixer device]{stereocontrols}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000329Returns a bitmask indicating stereo mixer channels. If a bit is set,
330the corresponding channel is stereo; if it is unset, the channel is
331either monophonic or not supported by the mixer (use in combination with
Greg Ward3e34f592003-03-10 02:09:51 +0000332\method{channels()} to determine which).
Greg Ward620e3432003-03-09 23:34:52 +0000333
Greg Ward3e34f592003-03-10 02:09:51 +0000334See the code example for the \method{channels()} function for an example
Greg Ward33bcd982003-03-09 23:57:34 +0000335of getting data from a bitmask.
Greg Ward620e3432003-03-09 23:34:52 +0000336\end{methoddesc}
337
338\begin{methoddesc}[mixer device]{reccontrols}{}
Greg Ward33bcd982003-03-09 23:57:34 +0000339Returns a bitmask specifying the mixer controls that may be used to
Greg Ward3e34f592003-03-10 02:09:51 +0000340record. See the code example for \method{controls()} for an example of
Greg Ward33bcd982003-03-09 23:57:34 +0000341reading from a bitmask.
Greg Ward620e3432003-03-09 23:34:52 +0000342\end{methoddesc}
343
344\begin{methoddesc}[mixer device]{get}{channel}
Greg Ward33bcd982003-03-09 23:57:34 +0000345Returns the volume of a given mixer channel. The returned volume is a
Greg Ward3e34f592003-03-10 02:09:51 +00003462-tuple \code{(left_volume,right_volume)}. Volumes are specified as
Greg Ward33bcd982003-03-09 23:57:34 +0000347numbers from 0 (silent) to 100 (full volume). If the channel is
348monophonic, a 2-tuple is still returned, but both channel volumes are
349the same.
Greg Ward620e3432003-03-09 23:34:52 +0000350
351If an unknown channel is specified, \exception{error} is raised.
352\end{methoddesc}
353
354\begin{methoddesc}[mixer device]{set}{channel, (left, right)}
Greg Ward3e34f592003-03-10 02:09:51 +0000355Sets the volume for a given mixer channel to \code{(left,right)}.
Greg Ward620e3432003-03-09 23:34:52 +0000356\code{left} and \code{right} must be ints and between 0 (silent) and 100
Greg Ward33bcd982003-03-09 23:57:34 +0000357(full volume). On success, the new volume is returned as a 2-tuple.
358Note that this may not be exactly the same as the volume specified,
359because of the limited resolution of some soundcard's mixers.
Greg Ward620e3432003-03-09 23:34:52 +0000360
361Raises \exception{IOError} if an invalid mixer channel was specified;
Greg Ward33bcd982003-03-09 23:57:34 +0000362\exception{TypeError} if the argument format was incorrect, and
Greg Ward620e3432003-03-09 23:34:52 +0000363\exception{error} if the specified volumes were out-of-range.
364\end{methoddesc}
365
366\begin{methoddesc}[mixer device]{get_recsrc}{}
367This method returns a bitmask indicating which channel or channels are
368currently being used as a recording source.
369\end{methoddesc}
370
371\begin{methoddesc}[mixer device]{set_recsrc}{bitmask}
Greg Ward33bcd982003-03-09 23:57:34 +0000372Call this function to specify a recording source. Returns a bitmask
Greg Ward620e3432003-03-09 23:34:52 +0000373indicating the new recording source (or sources) if successful; raises
Greg Ward33bcd982003-03-09 23:57:34 +0000374\exception{IOError} if an invalid source was specified. To set the current
Greg Ward620e3432003-03-09 23:34:52 +0000375recording source to the microphone input:
376
377\begin{verbatim}
378mixer.setrecsrc (1 << ossaudiodev.SOUND_MIXER_MIC)
379\end{verbatim}
380\end{methoddesc}
381
382
383