---
name: "audio"
section: "4"
platform: "openbsd"
package: "man"
version: "8.0-current-20261101"
release: "OpenBSD 8.0-current (2026-11-01)"
platforms: ["openbsd"]
url: "https://new.carta.tech/audio"
---

# audio(4)

device-independent audio driver layer

## NAME

**audio**, **audioctl** – 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.

In addition to hardware mixer controls like those documented in [azalia(4)](/azalia/4.md), the **audio** driver exposes the *record.enable* control. The superuser can change it with [mixerctl(8)](/mixerctl/8.md). It accepts the following values:

- **on**

  Recording is enabled.

- **off**

  Silence is returned instead of the recorded samples.

- **sysctl**

  Behavior is controlled by the *kern.audio.record* [sysctl(2)](/sysctl/2.md) variable. This is the default.

There are two types of device files available for audio operation:

- Audio devices are used for recording or playback of digital samples.

- Control devices are used to manipulate audio device parameters like volume or recording source. They can also read certain **audio** driver variables while it is in use.

## AUDIO DEVICES

When audio devices are opened, they attempt to maintain the previous audio sample format and record/playback mode. In addition, if one is opened read-only (write-only) the device is set to record-only (play-only) mode with recording (playing) unpaused.

If a writing process does not call [write(2)](/write/2.md) frequently enough to provide samples at the pace the hardware consumes them, silence is inserted. If a reading process does not call [read(2)](/read/2.md) frequently enough, it will simply miss samples.

The following [ioctl(2)](/ioctl/2.md) commands are supported on the sample devices:

- 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\_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 with

  ```
  struct 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\_GETPAR 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 is 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)](/write/2.md) 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)](/write/2.md) 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)](/read/2.md) 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)](/ioctl/2.md) 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)](/open/2.md) 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.

## CONTROL DEVICES

Control devices support the following [ioctl(2)](/ioctl/2.md) commands:

- AUDIO\_GETDEV *audio\_device\_t \**

  - AUDIO\_GETPOS *struct audio\_pos \**

  - AUDIO\_GETSTATUS *struct audio\_status \**

  - AUDIO\_GETPAR *struct audio\_swpar \**

  - AUDIO\_SETPAR *struct audio\_swpar \**

  These commands are the same as described above for the audio devices. While the audio device is open, AUDIO\_SETPAR may not be used.

- 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 type AUDIO\_MIXER\_CLASS is only used for classifying particular **mixer** device types and is not used for AUDIO\_MIXER\_READ or AUDIO\_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 and AUDIO\_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 with AudioNon and AudioNoff. For the AUDIO\_MIXER\_VALUE and AUDIO\_MIXER\_SET mixer control types, the channel count is returned; the units name specifies what the level controls (typical values are AudioNvolume, AudioNtreble, and AudioNbass).

A process may read the control device to get notifications about mixer changes. Whenever a control changes, the [read(2)](/read/2.md) function fetches an integer identifying the control. It may be used in the *dev* field of the *mixer\_ctrl* structure to call AUDIO\_MIXER\_READ.

In contrast to audio devices, which have the exclusive open property, control devices can be opened at any time in write-only mode. Only one reader is allowed at a time.

## FILES

- */dev/audio**N*

  Audio device.

- */dev/audioctl**N*

  Control device.

## SEE ALSO

[aucat(1)](/aucat/1.md), [cdio(1)](/cdio/1.md), [sndioctl(1)](/sndioctl/1.md), [ioctl(2)](/ioctl/2.md), [sio\_open(3)](/sio_open/3.md), [sioctl\_open(3)](/sioctl_open/3.md), [ac97(4)](/ac97/4.md), [uaudio(4)](/uaudio/4.md), [sndio(7)](/sndio/7.md), [audioctl(8)](/audioctl/8.md), [mixerctl(8)](/mixerctl/8.md), [sndiod(8)](/sndiod/8.md), audio(9)
