NAME
audio
, mixer
— device-independent audio
driver layer
SYNOPSIS
audio* at ...
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/audioio.h>
#include <string.h>
DESCRIPTION
The audio
driver provides support for
various audio peripherals. It provides a uniform programming interface layer
above different underlying audio hardware drivers. The audio layer provides
full-duplex operation if the underlying hardware configuration supports
it.
There are four device files available for audio operation:
/dev/audio, /dev/sound,
/dev/audioctl, and
/dev/mixer. /dev/audio and
/dev/sound are used for recording or playback of
digital samples. /dev/mixer is used to manipulate
volume, recording source, or other audio mixer functions.
/dev/audioctl accepts the same
ioctl(2) operations as /dev/sound, but no
other operations. In contrast to /dev/sound, which
has the exclusive open property, /dev/audioctl can
be opened at any time and can be used to manipulate the
audio
device while it is in use.
SAMPLING DEVICES
When /dev/audio or /dev/sound is opened, it attempts to maintain the previous audio sample format and record/playback mode. In addition, if it is opened read-only (write-only) the device is set to half-duplex record (play) mode with recording (playing) unpaused. In all respects /dev/audio and /dev/sound are identical.
Only one process may hold open a sampling device at a given time (although file descriptors may be shared between processes once the first open completes).
On a half-duplex device, either reads or writes are allowed, but not both. On a full-duplex device, reads and writes may operate concurrently without interference.
If a writing process does not call write(2) frequently enough to provide samples at the pace the hardware consumes them silence is inserted. If a reading process does not call read(2) frequently enough, it will simply miss samples.
The audio
device is accessed with
read(2) or
write(2).
The audio
device, like most devices, can
be used in poll(2),
The following ioctl(2) commands are supported on the sample devices:
AUDIO_RERROR
int *AUDIO_PERROR
int *- Obsolete. These commands fetch the count of dropped input or output samples into the int * argument, respectively. There is no information regarding when in the sample stream they were dropped.
AUDIO_GETDEV
audio_device_t *- This command fetches the current hardware device information into the
audio_device_t * argument.
typedef struct audio_device { char name[MAX_AUDIO_DEV_LEN]; char version[MAX_AUDIO_DEV_LEN]; char config[MAX_AUDIO_DEV_LEN]; } audio_device_t;
AUDIO_GETFD
int *- This command returns 1 if in full-duplex mode, else 0.
AUDIO_GETENC
audio_encoding_t *- This command is used iteratively to fetch sample encoding
names and format_ids into the
input/output audio_encoding_t * argument.
typedef struct audio_encoding { int index; /* input: nth encoding */ char name[MAX_AUDIO_DEV_LEN]; /* name of encoding */ int encoding; /* value for encoding parameter */ int precision; /* value for precision parameter */ int bps; /* value for bps parameter */ int msb; /* value for msb parameter */ } audio_encoding_t;
To query all the supported encodings, start with an index field of 0 and continue with successive encodings (1, 2, ...) until the command returns an error.
AUDIO_SETFD
int *- Does nothing, left for compatibility; argument must point to a non-zero integer if the device is opened in read-write mode.
AUDIO_GETPROPS
int *- This command gets a bit set of hardware properties. If the hardware has a
certain property, the corresponding bit is set, otherwise it is not. The
properties can have the following values:
AUDIO_PROP_FULLDUPLEX
- The device admits full-duplex operation.
AUDIO_PROP_INDEPENDENT
- The device can set playing and recording channel counts independently.
AUDIO_GETIOFFS
audio_offset_t *AUDIO_GETOOFFS
audio_offset_t *- Obsolete. These commands fetch the number of bytes played or recorded. The
information is returned in the audio_offset
structure.
typedef struct audio_offset { u_int samples; /* Total number of bytes transferred */ } audio_offset_t;
AUDIO_GETINFO
audio_info_t *AUDIO_SETINFO
audio_info_t *- Get or set audio information as encoded in the
audio_info structure.
typedef struct audio_info { struct audio_prinfo play; /* info for play (output) side */ struct audio_prinfo record; /* info for record (input) side */ u_int hiwat; /* blocks count in play buffer */ u_int mode; /* current device mode */ #define AUMODE_PLAY 0x01 #define AUMODE_RECORD 0x02 } audio_info_t;
When setting the current state with
AUDIO_SETINFO
, the audio_info structure should first be initialized withAUDIO_INITINFO(&info);
and then the particular values to be changed should be set. This allows the audio driver to only set those things that you wish to change and eliminates the need to query the device with
AUDIO_GETINFO
first.The mode field is read-only and set to
AUMODE_PLAY
,AUMODE_RECORD
, or a bitwise OR combination of the three. Only full-duplex audio devices support simultaneous record and playback.hiwat contains the number of blocks in the kernel play buffer. Writes to the audio devices will queue blocks until the play buffer is full, at which point any more write calls will block until space for at least one byte is available.
struct audio_prinfo { u_int sample_rate; /* sample rate in bit/s */ u_int channels; /* number of channels, usually 1 or 2 */ u_int precision; /* number of bits/sample */ u_int bps; /* number of bytes/sample */ u_int msb; /* data alignment */ u_int encoding; /* data encoding (AUDIO_ENCODING_* below) */ u_int block_size; /* size a block */ /* Current state of device: */ u_char pause; /* non-zero if paused, zero to resume */ u_char active; /* non-zero if I/O is currently active */ };
The
audio
driver requires identical playback and recording sample rates, sample encodings, and block durations.The encoding parameter can have the following values:
AUDIO_ENCODING_ULAW
- mu-law encoding, 8 bits/sample
AUDIO_ENCODING_ALAW
- A-law encoding, 8 bits/sample
AUDIO_ENCODING_SLINEAR_LE
- two's complement signed linear encoding with little endian byte order
AUDIO_ENCODING_SLINEAR_BE
- two's complement signed linear encoding with big endian byte order
AUDIO_ENCODING_ULINEAR_LE
- unsigned linear encoding with little endian byte order
AUDIO_ENCODING_ULINEAR_BE
- unsigned linear encoding with big endian byte order
The precision parameter describes the number of bits of audio data per sample. The bps parameter describes the number of bytes of audio data per sample. The msb parameter describes the alignment of the data in the sample. It is only meaningful when precision / NBBY < bps. A value of 1 means the data is aligned to the most significant bit.
block_size is the block size in bytes, which determines the frequency at which blocking read(2), write(2), or poll(2), wake up. The generic
audio
driver layer and the hardware driver have the opportunity to adjust this block size to get it within implementation-required limits. Normally the block_size is recalculated when other parameters changes.It is recommended to set block_size at the same time as, or after, all other parameters have been set.
pause returns the current pause/unpause state for recording or playback. For
AUDIO_SETINFO
, if the pause value is specified it will either pause or unpause the particular direction. In full-duplex the pause values for both directions must be equal. AUDIO_SETPAR
struct audio_swpar *AUDIO_GETPAR
struct audio_swpar *- Set or get audio parameters as encoded in the
audio_swpar structure.
struct audio_swpar { unsigned int sig; /* if 1, encoding is signed */ unsigned int le; /* if 1, encoding is little-endian */ unsigned int bits; /* bits per sample */ unsigned int bps; /* bytes per sample */ unsigned int msb; /* if 1, bits are msb-aligned */ unsigned int rate; /* common play & rec sample rate */ unsigned int pchan; /* play channels */ unsigned int rchan; /* rec channels */ unsigned int nblks; /* number of blocks in play buffer */ unsigned int round; /* common frames per block */ };
When setting the device parameters with
AUDIO_SETPAR
, the audio_swpar structure should first be initialized withstruct audio_swpar ap; AUDIO_INITPAR(&ap);
and then only the values to be changed should be set. This ensures that the software will work with future versions of the driver. The driver will attempt to set the given parameters; if the device doesn't support them, it will choose other parameters. Then the software must call
AUDIO_GETINFO
to obtain the parameters in use.The parameters are as follows:
- bits
- Number of bits per sample: must be between 1 and 32.
- bps
- Bytes per sample; if specified, it must be large enough to hold all bits. By default it's set to the smallest power of two large enough to hold bits.
- sig
- If set (i.e. non-zero) then the samples are signed, otherwise they are unsigned.
- le
- If set, then the byte order is little endian; if not it is big endian; it's meaningful only if bps > 1.
- msb
- If set, then the bits are aligned in the packet to the most significant bit (i.e. lower bits are padded), otherwise to the least significant bit (i.e. higher bits are padded). It's meaningful only if bits < bps * 8.
- rchan
- The number of recorded channels; meaningful only if the device is opened for reading.
- pchan
- The number of channels playing; meaningful only if the device is opened for writing.
- rate
- The sampling frequency in Hz.
- nblks
- The number of blocks in the play buffer.
- round
- The audio block size.
AUDIO_START
- Start playback and/or recording immediately. If the device is open for
writing (playback), then the play buffer must be filled with the
write(2) syscall. The buffer size is obtained by multiplying the
nblks, round, and
bps parameters obtained with
AUDIO_GETPAR
. AUDIO_STOP
- Stop playback and recording immediately.
AUDIO_GETPOS
struct audio_pos *- Fetch an atomic snapshot of device timing information in the
audio_pos structure.
struct audio_pos { unsigned int play_pos; /* total bytes played */ unsigned int play_xrun; /* bytes of silence inserted */ unsigned int rec_pos; /* total bytes recorded */ unsigned int rec_xrun; /* bytes dropped */ };
The properties have the following meaning:
- play_pos
- Total number of bytes played by the device since playback started (a.k.a the device wall clock).
- play_xrun
- The number of bytes corresponding to silence played because write(2) wasn't called fast enough.
- rec_pos
- Total number of bytes recorded by the device since recording started (a.k.a the device wall clock).
- rec_xrun
- The number of bytes dropped because read(2) wasn't called fast enough.
AUDIO_GETSTATUS
struct audio_status *- Fetch the current device status from the audio driver in the
audio_status structure. This
ioctl(2) is intended for use with diagnostic tools and is of no use
to audio programs.
struct audio_status { #define AUMODE_PLAY 0x01 #define AUMODE_RECORD 0x02 int mode; /* current mode */ int pause; /* not started yet */ int active; /* playing/recording in progress */ };
The properties have the following meaning:
- mode
- The current mode determined by open(2) flags.
- pause
- If set, indicates that
AUDIO_STOP
was called, and the device is not attempting to start. - active
- If set, indicates that the device is playing and/or recording.
MIXER DEVICE
The mixer
device,
/dev/mixer, may be manipulated with
ioctl(2) but does not support
read(2) or
write(2). It supports the following
ioctl(2) commands:
AUDIO_GETDEV
audio_device_t *- This command is the same as described above for the sampling devices.
AUDIO_MIXER_READ
mixer_ctrl_t *AUDIO_MIXER_WRITE
mixer_ctrl_t *- These commands read the current mixer state or set new mixer state for the
specified device dev. type
identifies which type of value is supplied in the
mixer_ctrl_t * argument.
#define AUDIO_MIXER_CLASS 0 #define AUDIO_MIXER_ENUM 1 #define AUDIO_MIXER_SET 2 #define AUDIO_MIXER_VALUE 3 typedef struct mixer_ctrl { int dev; /* input: nth device */ int type; union { int ord; /* enum */ int mask; /* set */ mixer_level_t value; /* value */ } un; } mixer_ctrl_t; #define AUDIO_MIN_GAIN 0 #define AUDIO_MAX_GAIN 255 typedef struct mixer_level { int num_channels; u_char level[8]; /* [num_channels] */ } mixer_level_t; #define AUDIO_MIXER_LEVEL_MONO 0 #define AUDIO_MIXER_LEVEL_LEFT 0 #define AUDIO_MIXER_LEVEL_RIGHT 1
For a mixer value, the value field specifies both the number of channels and the values for each channel. If the channel count does not match the current channel count, the attempt to change the setting may fail (depending on the hardware device driver implementation). For an enumeration value, the ord field should be set to one of the possible values as returned by a prior
AUDIO_MIXER_DEVINFO
command. The typeAUDIO_MIXER_CLASS
is only used for classifying particularmixer
device types and is not used forAUDIO_MIXER_READ
orAUDIO_MIXER_WRITE
. AUDIO_MIXER_DEVINFO
mixer_devinfo_t *- This command is used iteratively to fetch audio
mixer
device information into the input/output mixer_devinfo_t * argument. To query all the supported devices, start with an index field of 0 and continue with successive devices (1, 2, ...) until the command returns an error.typedef struct mixer_devinfo { int index; /* input: nth mixer device */ audio_mixer_name_t label; int type; int mixer_class; int next, prev; #define AUDIO_MIXER_LAST -1 union { struct audio_mixer_enum { int num_mem; struct { audio_mixer_name_t label; int ord; } member[32]; } e; struct audio_mixer_set { int num_mem; struct { audio_mixer_name_t label; int mask; } member[32]; } s; struct audio_mixer_value { audio_mixer_name_t units; int num_channels; int delta; } v; } un; } mixer_devinfo_t;
The label field identifies the name of this particular mixer control. The index field may be used as the dev field in
AUDIO_MIXER_READ
andAUDIO_MIXER_WRITE
commands. The type field identifies the type of this mixer control. Enumeration types are typically used for on/off style controls (e.g., a mute control) or for input/output device selection (e.g., select recording input source from CD, line in, or microphone). Set types are similar to enumeration types but any combination of the mask bits can be used.The mixer_class field identifies what class of control this is. This value is set to the index value used to query the class itself. The (arbitrary) value set by the hardware driver may be determined by examining the mixer_class field of the class itself, a mixer of type
AUDIO_MIXER_CLASS
. For example, a mixer level controlling the input gain on the “line in” circuit would have a mixer_class that matches an input class device with the name “inputs” (AudioCinputs
) and would have a label of “line” (AudioNline
). Mixer controls which control audio circuitry for a particular audio source (e.g., line-in, CD in, DAC output) are collected under the input class, while those which control all audio sources (e.g., master volume, equalization controls) are under the output class. Hardware devices capable of recording typically also have a record class, for controls that only affect recording, and also a monitor class.The next and prev may be used by the hardware device driver to provide hints for the next and previous devices in a related set (for example, the line in level control would have the line in mute as its “next” value). If there is no relevant next or previous value,
AUDIO_MIXER_LAST
is specified.For
AUDIO_MIXER_ENUM
mixer control types, the enumeration values and their corresponding names are filled in. For example, a mute control would return appropriate values paired withAudioNon
andAudioNoff
. For theAUDIO_MIXER_VALUE
andAUDIO_MIXER_SET
mixer control types, the channel count is returned; the units name specifies what the level controls (typical values areAudioNvolume
,AudioNtreble
, andAudioNbass
).
By convention, all the mixer devices can be distinguished from
other mixer controls because they use a name from one of the
AudioC*
string values.
FILES
- /dev/audio
- /dev/audioctl
- /dev/sound
- /dev/mixer
SEE ALSO
aucat(1), audioctl(1), cdio(1), mixerctl(1), ioctl(2), sio_open(3), ac97(4), uaudio(4), audio(9)