AUDIO(4)
π NAME π
okay so this is AUDIO(4) from the Kernel Interfaces Manual β literally the device-independent audio driver layer, and bestie, it's lowkey iconic. In manpage speak: audio β device-independent audio driver layer. That's the name, periodt. π
β¨ SYNOPSIS β¨
#include <sys/audioio.h>
π§ DESCRIPTION π§
The audio driver provides support for various audio peripherals. It gives you 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. That's the whole elevator pitch, no cap. β¨
There are four device files available for audio operation: /dev/audio, /dev/sound, /dev/audioctl, and /dev/mixer.
/dev/audioand/dev/soundβ used for recording or playback of digital samples./dev/mixerβ used to manipulate volume, recording source, or other audio mixer functions./dev/audioctlβ accepts the sameioctl(2)operations as/dev/sound, but no other operations. It 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 is opened, it automatically sets the track to manipulate monaural 8-bit mu-law 8000Hz. When /dev/sound is opened, it maintains the audio format and pause/unpause state of the most recently opened track. In all other respects /dev/audio and /dev/sound are identical. same device energy, different little quirks. π¦
On a full-duplex device, reads and writes may operate concurrently without interference.
On a half-duplex device, if there are any recording descriptors already, opening with write mode will fail. Similarly, if there are any playback descriptors already, opening with read mode will fail. If both playback and recording are requested on a half-duplex device, it will be treated as playback mode. the way that half-duplex has rules, honestly. π
On either type of device, opening with write mode will start in playback mode, opening with read mode will start in recording mode.
If the playback mode is paused then silence is played instead of the provided samples, and if recording is paused then the process blocks in read(2) until recording is unpaused.
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. missed samples are just gone, bestie β gone. π
The audio driver supports track multiplexing. All sampling devices can be opened at any time without interference. For playback, all tracks opened simultaneously are mixed, even if their specified format is different. For recording, recorded data is distributed to all opened tracks, even if their specified format is different. To achieve this, the audio driver has a small efficient encoding converter, a channel mixer, and a frequency converter. The frequency conversion adapts the simplest way (interpolation method for upward, and simple thinning method for downward) due to restrictions in kernel resources and processing time. It will work well in most cases but don't expect excessive quality.
The audio device is normally accessed with read(2) or write(2) calls, but it can also be mapped into user memory with mmap(2). Once the device has been mapped it can no longer be accessed by read or write; all access is by reading and writing to the mapped memory. The mmap'ped buffer appears as a block of memory of size buffersize (as available via AUDIO_GETINFO or AUDIO_GETBUFINFO). The audio driver will continuously move data from this buffer from/to the mixing buffer, wrapping around at the end of the buffer. To find out where the hardware is currently accessing data in the buffer the AUDIO_GETIOFFS and AUDIO_GETOOFFS calls can be used.
π«Ά heads up:mmap(2)no longer maps hardware buffers directly. Now it is achieved by emulation, so don't expect significant improvements over normalwrite(2). For historical reasons, only encodings that are not setAUDIO_ENCODINGFLAG_EMULATEDare able tommap(2). iykyk.
The audio device, like most devices, can be used in select(2), can be set in non-blocking mode and can be set (with a FIOASYNC ioctl) to send a SIGIO when I/O is possible. The mixer device can be set to generate a SIGIO whenever a mixer value is changed.
The following ioctl(2) commands are supported on the sample devices:
AUDIO_FLUSHβ this command stops all playback and recording, clears all queued buffers, resets error counters on this track, and restarts recording and playback as appropriate for the current sampling mode.AUDIO_PERROR(int),AUDIO_RERROR(int) β this command fetches the count of dropped output (input) bytes into its integer argument. There is no information regarding when in the sample stream they were dropped.AUDIO_WSEEK(u_long) β this command fetches the count of bytes that are queued ahead of the first sample in the most recent sample block written into its integer argument.AUDIO_DRAINβ this command suspends the calling process until all queued playback samples have been played.AUDIO_GETDEV(audio_device_t) β this command fetches the current hardware device information into theaudio_device_targument.
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_GETENC(audio_encoding_t) β this command is used iteratively to fetch sample encoding names and format IDs into the input/outputaudio_encoding_targument. The encoding returned by the command is the user-accessible encoding, not the hardware-supported encoding.
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 flags;
#define AUDIO_ENCODINGFLAG_EMULATED 1 /* software emulation mode */
} 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_GETFD(int) β this command is obsolete.AUDIO_SETFD(int) β this command is obsolete.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_MMAPβ the device can be used withmmap(2).AUDIO_PROP_INDEPENDENTβ the device can set the playing and recording encoding parameters independently.AUDIO_PROP_PLAYBACKβ the device is capable of audio playback.AUDIO_PROP_CAPTUREβ the device is capable of audio capture.
AUDIO_GETIOFFS(audio_offset_t),AUDIO_GETOOFFS(audio_offset_t) β this command fetches the current offset in the input(output) buffer where the track mixer will be putting(getting) data. It mostly useful when the device buffer is available in user space via themmap(2)call. The information is returned in theaudio_offset_tstructure.
typedef struct audio_offset {
u_int samples; /* Total number of bytes transferred */
u_int deltablks; /* Blocks transferred since last checked */
u_int offset; /* Physical transfer offset in buffer */
} audio_offset_t;
AUDIO_GETINFO(audio_info_t),AUDIO_GETBUFINFO(audio_info_t),AUDIO_SETINFO(audio_info_t) β get or set audio information as encoded in theaudio_infostructure. For historical reasons, theaudio_infostructure has three different layer's parameters: track, track mixer, and hardware rich mixer.
typedef struct audio_info {
struct audio_prinfo play; /* info for play (output) side */
struct audio_prinfo record; /* info for record (input) side */
u_int monitor_gain; /* input to output mix [HWmixer] */
/* BSD extensions */
u_int blocksize; /* read/write block size [track] */
u_int hiwat; /* output high water mark [track] */
u_int lowat; /* output low water mark [track] */
u_int _ispare1;
u_int mode; /* current operation mode [track] */
#define AUMODE_PLAY 0x01
#define AUMODE_RECORD 0x02
#define AUMODE_PLAY_ALL 0x04 /* Not used anymore */
} audio_info_t;
When setting the current state with AUDIO_SETINFO, the audio_info structure should first be initialized with AUDIO_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 or AUDIO_GETBUFINFO first. that's the low-effort, high-efficiency vibe. β¨
The mode field indicates current operation mode, either one of AUMODE_PLAY or AUMODE_RECORD. These two flags can not be changed once this descriptor is opened. For playback mode, the obsolete AUMODE_PLAY_ALL flag can be set but has no effect.
hiwat and lowat are used to control write behavior. Writes to the audio devices will queue up blocks until the high-water mark is reached, at which point any more write calls will block until the queue is drained to the low-water mark. hiwat and lowat set those high- and low-water marks (in audio blocks). The default for hiwat is the maximum value and for lowat 75% of hiwat.
blocksize sets the current audio blocksize. 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 blocksize is calculated to correspond to the value of the hw.audioX.blk_ms sysctl and is recalculated when the encoding parameters change. If the descriptor is opened for read only, blocksize indicates the blocksize for the recording track. Otherwise, blocksize indicates the blocksize for the playback track.
okay so hereβs the rest of the audio_prinfo struct β every field, every comment, no skips, because we do not do missing details in this house π
β¨
struct audio_prinfo {
u_int sample_rate; /* sample rate in samples/s [track] */
u_int channels; /* number of channels, usually 1 or 2 [track] */
u_int precision; /* number of bits/sample [track] */
u_int encoding; /* data encoding (AUDIO_ENCODING_* below) [track] */
u_int gain; /* volume level [HWmixer] */
u_int port; /* selected I/O port [HWmixer] */
u_long seek; /* BSD extension [track] */
u_int avail_ports; /* available I/O ports [HWmixer] */
u_int buffer_size; /* total size audio buffer [track] */
u_int _ispare[1];
u_int samples; /* number of samples [track] */
u_int eof; /* End Of File (zero-size writes) counter [track] */
u_char pause; /* non-zero if paused, zero to resume [track] */
u_char error; /* non-zero if underflow/overflow occurred [track] */
u_char waiting; /* non-zero if another process hangs in open [track] */
u_char balance; /* stereo channel balance [HWmixer] */
u_char cspare[2];
u_char open; /* non-zero if currently open [trackmixer] */
u_char active; /* non-zero if I/O is currently active [trackmixer] */
};
Quick heads-up, bestie: many hardware audio drivers require identical playback and recording sample rates, sample encodings, and channel counts. On that kind of hardware, the playing information is always set last and will prevail. If the hardware can actually handle different settings, the AUDIO_PROP_INDEPENDENT property is set. π
The encoding parameter can have the following values:
AUDIO_ENCODING_ULAWβ mu-law encoding, 8 bits/sampleAUDIO_ENCODING_ALAWβ A-law encoding, 8 bits/sampleAUDIO_ENCODING_SLINEARβ two's complement signed linear encoding with the platform byte orderAUDIO_ENCODING_ULINEARβ unsigned linear encoding with the platform byte orderAUDIO_ENCODING_ADPCMβ ADPCM encoding, 8 bits/sampleAUDIO_ENCODING_SLINEAR_LEβ two's complement signed linear encoding with little endian byte orderAUDIO_ENCODING_SLINEAR_BEβ two's complement signed linear encoding with big endian byte orderAUDIO_ENCODING_ULINEAR_LEβ unsigned linear encoding with little endian byte orderAUDIO_ENCODING_ULINEAR_BEβ unsigned linear encoding with big endian byte orderAUDIO_ENCODING_AC3β Dolby Digital AC3
The audio driver accepts the following formats. encoding and precision are one of the values obtained by AUDIO_GETENC, regardless of formats supported by underlying driver. frequency ranges from 1000Hz to 192000Hz, regardless of frequency (ranges) supported by underlying driver. channels depends on your underlying driver. If the underlying driver only supports monaural (1 channel) or stereo (2 channels), you can specify 1 or 2 regardless of number of channels supported by underlying driver. If the underlying driver supports three or more channels, you can specify the number of channels supported by the underlying driver or fewer. π
The gain, port and balance settings provide simple shortcuts to the richer mixer interface described below and are not obtained by AUDIO_GETBUFINFO. The gain should be in the range [AUDIO_MIN_GAIN, AUDIO_MAX_GAIN] and the balance in the range [AUDIO_LEFT_BALANCE, AUDIO_RIGHT_BALANCE] with the normal setting at AUDIO_MID_BALANCE. ποΈ
The input port should be a combination of:
AUDIO_MICROPHONEβ to select microphone input.AUDIO_LINE_INβ to select line input.AUDIO_CDβ to select CD input.
The output port should be a combination of:
AUDIO_SPEAKERβ to select speaker output.AUDIO_HEADPHONEβ to select headphone output.AUDIO_LINE_OUTβ to select line output.
The available ports can be found in avail_ports (AUDIO_GETBUFINFO only). π
buffer_size is the total size of the audio buffer. The buffer size divided by the blocksize gives the maximum value for hiwat. Currently the buffer_size can only be read and not set. π«Ά
The seek and samples fields are only used by AUDIO_GETINFO and AUDIO_GETBUFINFO. seek represents the count of bytes pending; samples represents the total number of bytes recorded or played, less those that were dropped due to inadequate consumption/production rates. π
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. βΈοΈβΆοΈ
π§ AUDIO_QUERYFORMAT (audio_format_query_t)
This command enumerates formats supported by the hardware. Similarly to AUDIO_GETENC, to query all the supported formats, start with an index field of 0 and continue with successive formats (1, 2, ...) until the command returns an error. π₯
typedef struct audio_format_query {
u_int index;
struct audio_format fmt;
} audio_format_query_t;
π AUDIO_GETFORMAT (audio_info_t)
This command fetches the current hardware format. Only the following members in audio_info_t are used. Members which are not listed here or belong in invalid direction are filled by -1. β¨
modeplay.encodingplay.precisionplay.channelsplay.sample_raterecord.encodingrecord.precisionrecord.channelsrecord.sample_rate
mode indicates which direction is valid. π
π¦ AUDIO_SETFORMAT (audio_info_t)
This command sets the hardware format. It will fail if there are any opened descriptors. So obviously, it must be issued on /dev/audioctl. Similarly to AUDIO_GETFORMAT, only above members in audio_info_t are used. Members which is not listed or belong in invalid direction are ignored. The parameters can be chosen from the choices obtained by AUDIO_QUERYFORMAT. πΈ
π AUDIO_GETCHAN (int)
This command is obsolete. literally. thatβs the whole note. π
π AUDIO_SETCHAN (int)
This command is obsolete. same energy. no cap. π
ποΈ MIXER DEVICE ποΈ
okay so the mixer device lives at /dev/mixer, and here's the thing β you can poke it with ioctl(2) but it does not support read(2) or write(2). nope, not happening, don't even try. π
it's an ioctl-only kinda girl. here's the commands it supports:
AUDIO_GETDEV(audio_device_t) β literally the same command as described above for the sampling devices. same vibe, no notes.AUDIO_MIXER_READ(mixer_ctrl_t)AUDIO_MIXER_WRITE(mixer_ctrl_t) β these two read the current mixer state or set new mixer state for the specified devicedev. thetypefield tells you which kind of value is being supplied in themixer_ctrl_targument. slay.
#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. and β οΈ heads up: if the channel count doesn't match the current channel count, the attempt to change the setting may fail β depends on the hardware device driver implementation. so like, not guaranteed, bestie. audio levels may be adjusted in increments of the delta value returned by AUDIO_MIXER_DEVINFO. this field is optional for hardware drivers to specify β devices with a delta of 0 may allow arbitrary adjustment of levels. fancy β¨
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.
and the type AUDIO_MIXER_CLASS is only used for classifying particular mixer device types β it is not used for AUDIO_MIXER_READ or AUDIO_MIXER_WRITE. don't come for me, that's just how it is π«Ά
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 keep going with successive devices (1, 2, ...) until the command returns an error. that error is your little finish line π
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. iconic behavior honestly π₯
the mixer_class field identifies what class of control this is. 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 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. it's giving organizational queen π
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. no cap.
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 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, AudioNbass). π
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. iykyk β¨
π FILES π
/dev/audio/dev/audioctl/dev/sound/dev/mixer
π SEE ALSO π
audiocfg(1), audioctl(1), audioplay(1), audiorecord(1), mixerctl(1), ioctl(2), ossaudio(3), acorn32/vidcaudio(4), arcofi(4), aria(4), auacer(4), audiocs(4), auich(4), auixp(4), autri(4), auvia(4), bba(4), btsco(4), clcs(4), clct(4), cmpci(4), dreamcast/aica(4), eap(4), emuxki(4), esa(4), esm(4), eso(4), ess(4), fms(4), gcscaudio(4), gus(4), guspnp(4), hdafg(4), hdaudio(4), hppa/harmony(4), macppc/awacs(4), macppc/snapper(4), midi(4), neo(4), pad(4), pas(4), radio(4), sb(4), sgimips/haltwo(4), sgimips/mavb(4), sparc/audioamd(4), sparc/dbri(4), sv(4), uaudio(4), wss(4), x68k/vs(4), yds(4), ym(4)
like, that's a lot of friends. the whole squad π«Ά
π¦ HISTORY π¦
support for virtual channels and mixing first appeared in NetBSD 8.0. vintage era, honestly π
π BUGS π
if the device is used in mmap(2) it is currently always mapped for writing (playing) due to VM system weirdness. yeah. weirdness. that's the technical term. don't ask π
and that's the whole page, bestie β you ate that π β¨ this little beauty comes to you from Debian, dated May 27, 2024. go forth and mix responsibly ππ¦
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 1996 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms