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