14.11.1 Audio Device Objects

Python 2.4

14.11.1 Audio Device Objects

Before you can write to or read from an audio device, you must call three methods in the correct order:

  1. setfmt() to set the output format
  2. channels() to set the number of channels
  3. speed() to set the sample rate
Alternately, you can use the setparameters() method to set all three audio parameters at once. This is more convenient, but may not be as flexible in all cases.

The audio device objects returned by open() define the following methods:

Explicitly close the audio device. When you are done writing to or reading from an audio device, you should explicitly close it. A closed device cannot be used again.

Return the file descriptor associated with the device.

Read size bytes from the audio input and return them as a Python string. Unlike most Unix device drivers, OSS audio devices in blocking mode (the default) will block read() until the entire requested amount of data is available.

Write the Python string data to the audio device and return the number of bytes written. If the audio device is in blocking mode (the default), the entire string is always written (again, this is different from usual Unix device semantics). If the device is in non-blocking mode, some data may not be written--see writeall().

Write the entire Python string data to the audio device: waits until the audio device is able to accept data, writes as much data as it will accept, and repeats until data has been completely written. If the device is in blocking mode (the default), this has the same effect as write(); writeall() is only useful in non-blocking mode. Has no return value, since the amount of data written is always equal to the amount of data supplied.

The following methods each map to exactly one ioctl() system call. The correspondence is obvious: for example, setfmt() corresponds to the SNDCTL_DSP_SETFMT ioctl, and sync() to SNDCTL_DSP_SYNC (this can be useful when consulting the OSS documentation). If the underlying ioctl() fails, they all raise IOError.

Put the device into non-blocking mode. Once in non-blocking mode, there is no way to return it to blocking mode.

Return a bitmask of the audio output formats supported by the soundcard. Some of the formats supported by OSS are:

Format Description
AFMT_MU_LAW a logarithmic encoding (used by Sun .au files and /dev/audio)
AFMT_A_LAW a logarithmic encoding
AFMT_IMA_ADPCM a 4:1 compressed format defined by the Interactive Multimedia Association
AFMT_U8 Unsigned, 8-bit audio
AFMT_S16_LE Signed, 16-bit audio, little-endian byte order (as used by Intel processors)
AFMT_S16_BE Signed, 16-bit audio, big-endian byte order (as used by 68k, PowerPC, Sparc)
AFMT_S8 Signed, 8 bit audio
AFMT_U16_LE Unsigned, 16-bit little-endian audio
AFMT_U16_BE Unsigned, 16-bit big-endian audio
Consult the OSS documentation for a full list of audio formats, and note that most devices support only a subset of these formats. Some older devices only support AFMT_U8; the most common format used today is AFMT_S16_LE.

Try to set the current audio format to format--see getfmts() for a list. Returns the audio format that the device was set to, which may not be the requested format. May also be used to return the current audio format--do this by passing an ``audio format'' of AFMT_QUERY.

Set the number of output channels to nchannels. A value of 1 indicates monophonic sound, 2 stereophonic. Some devices may have more than 2 channels, and some high-end devices may not support mono. Returns the number of channels the device was set to.

Try to set the audio sampling rate to samplerate samples per second. Returns the rate actually set. Most sound devices don't support arbitrary sampling rates. Common rates are:
Rate Description
8000 default rate for /dev/audio
11025 speech recording
22050
44100 CD quality audio (at 16 bits/sample and 2 channels)
96000 DVD quality audio (at 24 bits/sample)

Wait until the sound device has played every byte in its buffer. (This happens implicitly when the device is closed.) The OSS documentation recommends closing and re-opening the device rather than using sync().

Immediately stop playing or recording and return the device to a state where it can accept commands. The OSS documentation recommends closing and re-opening the device after calling reset().

Tell the driver that there is likely to be a pause in the output, making it possible for the device to handle the pause more intelligently. You might use this after playing a spot sound effect, before waiting for user input, or before doing disk I/O.

The following convenience methods combine several ioctls, or one ioctl and some simple calculations.

Set the key audio sampling parameters--sample format, number of channels, and sampling rate--in one method call. format, nchannels, and samplerate should be as specified in the setfmt(), channels(), and speed() methods. If strict is true, setparameters() checks to see if each parameter was actually set to the requested value, and raises OSSAudioError if not. Returns a tuple (format, nchannels, samplerate) indicating the parameter values that were actually set by the device driver (i.e., the same as the return values of setfmt(), channels(), and speed()).

For example,

  (fmt, channels, rate) = dsp.setparameters(fmt, channels, rate)
is equivalent to
  fmt = dsp.setfmt(fmt)
  channels = dsp.channels(channels)
  rate = dsp.rate(channels)

Returns the size of the hardware buffer, in samples.

Returns the number of samples that are in the hardware buffer yet to be played.

Returns the number of samples that could be queued into the hardware buffer to be played without blocking.

See About this document... for information on suggesting changes.