1.\" $OpenBSD: audio.4,v 1.86 2020/11/01 21:32:03 jmc Exp $ 2.\" $NetBSD: audio.4,v 1.20 1998/05/28 17:27:15 augustss Exp $ 3.\" 4.\" Copyright (c) 1996 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" This code is derived from software contributed to The NetBSD Foundation 8.\" by John T. Kohl. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29.\" POSSIBILITY OF SUCH DAMAGE. 30.\" 31.Dd $Mdocdate: November 1 2020 $ 32.Dt AUDIO 4 33.Os 34.Sh NAME 35.Nm audio , 36.Nm audioctl 37.Nd device-independent audio driver layer 38.Sh SYNOPSIS 39.Cd "audio* at ..." 40.Pp 41.In sys/types.h 42.In sys/ioctl.h 43.In sys/audioio.h 44.In string.h 45.Sh DESCRIPTION 46The 47.Nm audio 48driver provides support for various audio peripherals. 49It provides a uniform programming interface layer above different 50underlying audio hardware drivers. 51.Pp 52In addition to hardware mixer controls like those 53documented in 54.Xr azalia 4 , 55the 56.Nm 57driver exposes the 58.Va record.enable 59control. 60The superuser can change it with 61.Xr mixerctl 8 . 62It accepts the following values: 63.Pp 64.Bl -tag -offset indent -width "sysctl" -compact 65.It Cm on 66Recording is enabled. 67.It Cm off 68Silence is returned instead of the recorded samples. 69.It Cm sysctl 70Behavior is controlled by the 71.Va kern.audio.record 72.Xr sysctl 2 73variable. 74This is the default. 75.El 76.Pp 77There are two types of device files available for audio operation: 78.Bl -dash -offset indent 79.It 80Audio devices are used for recording or playback of digital samples. 81.It 82Control devices are used to manipulate audio device 83parameters like volume or recording source. 84They can also read certain 85.Nm 86driver variables while it is in use. 87.El 88.Sh AUDIO DEVICES 89When audio devices are opened, 90they attempt to maintain the previous audio sample format and record/playback mode. 91In addition, if one is opened read-only 92(write-only) the device is set to record-only (play-only) mode with 93recording (playing) unpaused. 94.Pp 95If a writing process does not call 96.Xr write 2 97frequently enough to provide samples at the pace the hardware 98consumes them silence is inserted. 99If a reading process does not call 100.Xr read 2 101frequently enough, it will simply miss samples. 102.Pp 103The following 104.Xr ioctl 2 105commands are supported on the sample devices: 106.Pp 107.Bl -tag -width Ds -compact 108.It Dv AUDIO_GETDEV Fa "audio_device_t *" 109This command fetches the current hardware device information into the 110.Vt audio_device_t * 111argument. 112.Bd -literal 113typedef struct audio_device { 114 char name[MAX_AUDIO_DEV_LEN]; 115 char version[MAX_AUDIO_DEV_LEN]; 116 char config[MAX_AUDIO_DEV_LEN]; 117} audio_device_t; 118.Ed 119.Pp 120.It Dv AUDIO_SETPAR Fa "struct audio_swpar *" 121.It Dv AUDIO_GETPAR Fa "struct audio_swpar *" 122Set or get audio parameters as encoded in the 123.Vt audio_swpar 124structure. 125.Bd -literal 126struct audio_swpar { 127 unsigned int sig; /* if 1, encoding is signed */ 128 unsigned int le; /* if 1, encoding is little-endian */ 129 unsigned int bits; /* bits per sample */ 130 unsigned int bps; /* bytes per sample */ 131 unsigned int msb; /* if 1, bits are msb-aligned */ 132 unsigned int rate; /* common play & rec sample rate */ 133 unsigned int pchan; /* play channels */ 134 unsigned int rchan; /* rec channels */ 135 unsigned int nblks; /* number of blocks in play buffer */ 136 unsigned int round; /* common frames per block */ 137}; 138.Ed 139.Pp 140When setting the device parameters with 141.Dv AUDIO_SETPAR , 142the 143.Vt audio_swpar 144structure should first be initialized with 145.Bd -literal 146struct audio_swpar ap; 147 148AUDIO_INITPAR(&ap); 149.Ed 150.Pp 151and then only the values to be changed should be set. 152This ensures that the software will work with future versions 153of the driver. 154The driver will attempt to set the given parameters; if the 155device doesn't support them, it will choose other parameters. 156Then the software must call 157.Dv AUDIO_GETPAR 158to obtain the parameters in use. 159.Pp 160The parameters are as follows: 161.Bl -tag -width "round" 162.It Va bits 163Number of bits per sample: must be between 1 and 32. 164.It Va bps 165Bytes per sample; if specified, it must be large enough to hold all bits. 166By default it's set to the smallest power of two large enough to hold 167.Va bits . 168.It Va sig 169If set (i.e. non-zero) then the samples are signed; 170otherwise they are unsigned. 171.It Va le 172If set then the byte order is little endian; 173if not it is big endian. 174It is meaningful only if 175.Va bps 176> 1. 177.It Va msb 178If set, then the 179.Va bits 180are aligned in the packet to the most significant bit 181(i.e. lower bits are padded), 182otherwise to the least significant bit 183(i.e. higher bits are padded). 184It's meaningful only if 185.Va bits 186< 187.Va bps 188* 8. 189.It Va rchan 190The number of recorded channels; meaningful only if the 191device is opened for reading. 192.It Va pchan 193The number of channels playing; meaningful only if 194the device is opened for writing. 195.It Va rate 196The sampling frequency in Hz. 197.It Va nblks 198The number of blocks in the play buffer. 199.It Va round 200The audio block size. 201.El 202.Pp 203.It Dv AUDIO_START 204Start playback and/or recording immediately. 205If the device is open for writing (playback), then 206the play buffer must be filled with the 207.Xr write 2 208syscall. 209The buffer size is obtained by multiplying 210the 211.Va nblks , 212.Va round , 213and 214.Va bps 215parameters obtained with 216.Dv AUDIO_GETPAR . 217.Pp 218.It Dv AUDIO_STOP 219Stop playback and recording immediately. 220.Pp 221.It Dv AUDIO_GETPOS Fa "struct audio_pos *" 222Fetch an atomic snapshot of device timing information in the 223.Vt audio_pos 224structure. 225.Bd -literal 226struct audio_pos { 227 unsigned int play_pos; /* total bytes played */ 228 unsigned int play_xrun; /* bytes of silence inserted */ 229 unsigned int rec_pos; /* total bytes recorded */ 230 unsigned int rec_xrun; /* bytes dropped */ 231}; 232.Ed 233.Pp 234The properties have the following meaning: 235.Bl -tag -width "play_xrun" 236.It Va play_pos 237Total number of bytes played by the device since playback started 238(a.k.a the device wall clock). 239.It Va play_xrun 240The number of bytes corresponding to silence played because 241.Xr write 2 242wasn't called fast enough. 243.It Va rec_pos 244Total number of bytes recorded by the device since recording started 245(a.k.a the device wall clock). 246.It Va rec_xrun 247The number of bytes dropped because 248.Xr read 2 249wasn't called fast enough. 250.El 251.Pp 252.It Dv AUDIO_GETSTATUS Fa "struct audio_status *" 253Fetch the current device status from the audio driver in the 254.Vt audio_status 255structure. 256This 257.Xr ioctl 2 258is intended for use with diagnostic tools 259and is of no use to audio programs. 260.Bd -literal 261struct audio_status { 262#define AUMODE_PLAY 0x01 263#define AUMODE_RECORD 0x02 264 int mode; /* current mode */ 265 int pause; /* not started yet */ 266 int active; /* playing/recording in progress */ 267}; 268.Ed 269.Pp 270The properties have the following meaning: 271.Bl -tag -width "active" 272.It Va mode 273The current mode determined by 274.Xr open 2 275flags. 276.It Va pause 277If set, indicates that 278.Dv AUDIO_STOP 279was called, and the device is not attempting to start. 280.It Va active 281If set, indicates that the device is playing and/or recording. 282.El 283.El 284.Sh CONTROL DEVICES 285Control devices support the following 286.Xr ioctl 2 287commands: 288.Pp 289.Bl -tag -width Ds -compact 290.It Dv AUDIO_GETDEV Fa "audio_device_t *" 291.It Dv AUDIO_GETPOS Fa "struct audio_pos *" 292.It Dv AUDIO_GETSTATUS Fa "struct audio_status *" 293.It Dv AUDIO_GETPAR Fa "struct audio_swpar *" 294.It Dv AUDIO_SETPAR Fa "struct audio_swpar *" 295These commands are the same as described above for the audio devices. 296While the audio device is open, 297.Dv AUDIO_SETPAR 298may not be used. 299.Pp 300.It Dv AUDIO_MIXER_READ Fa "mixer_ctrl_t *" 301.It Dv AUDIO_MIXER_WRITE Fa "mixer_ctrl_t *" 302These commands read the current mixer state or set new mixer state for 303the specified device 304.Va dev . 305.Va type 306identifies which type of value is supplied in the 307.Vt mixer_ctrl_t * 308argument. 309.Bd -literal 310#define AUDIO_MIXER_CLASS 0 311#define AUDIO_MIXER_ENUM 1 312#define AUDIO_MIXER_SET 2 313#define AUDIO_MIXER_VALUE 3 314typedef struct mixer_ctrl { 315 int dev; /* input: nth device */ 316 int type; 317 union { 318 int ord; /* enum */ 319 int mask; /* set */ 320 mixer_level_t value; /* value */ 321 } un; 322} mixer_ctrl_t; 323 324#define AUDIO_MIN_GAIN 0 325#define AUDIO_MAX_GAIN 255 326typedef struct mixer_level { 327 int num_channels; 328 u_char level[8]; /* [num_channels] */ 329} mixer_level_t; 330#define AUDIO_MIXER_LEVEL_MONO 0 331#define AUDIO_MIXER_LEVEL_LEFT 0 332#define AUDIO_MIXER_LEVEL_RIGHT 1 333.Ed 334.Pp 335For a mixer value, the 336.Va value 337field specifies both the number of channels and the values for each 338channel. 339If the channel count does not match the current channel count, the 340attempt to change the setting may fail (depending on the hardware 341device driver implementation). 342For an enumeration value, the 343.Va ord 344field should be set to one of the possible values as returned by a prior 345.Dv AUDIO_MIXER_DEVINFO 346command. 347The type 348.Dv AUDIO_MIXER_CLASS 349is only used for classifying particular 350.Nm mixer 351device types and is not used for 352.Dv AUDIO_MIXER_READ 353or 354.Dv AUDIO_MIXER_WRITE . 355.Pp 356.It Dv AUDIO_MIXER_DEVINFO Fa "mixer_devinfo_t *" 357This command is used iteratively to fetch audio 358.Nm mixer 359device information into the input/output 360.Vt mixer_devinfo_t * 361argument. 362To query all the supported devices, start with an index field of 3630 and continue with successive devices (1, 2, ...) until the 364command returns an error. 365.Bd -literal 366typedef struct mixer_devinfo { 367 int index; /* input: nth mixer device */ 368 audio_mixer_name_t label; 369 int type; 370 int mixer_class; 371 int next, prev; 372#define AUDIO_MIXER_LAST -1 373 union { 374 struct audio_mixer_enum { 375 int num_mem; 376 struct { 377 audio_mixer_name_t label; 378 int ord; 379 } member[32]; 380 } e; 381 struct audio_mixer_set { 382 int num_mem; 383 struct { 384 audio_mixer_name_t label; 385 int mask; 386 } member[32]; 387 } s; 388 struct audio_mixer_value { 389 audio_mixer_name_t units; 390 int num_channels; 391 int delta; 392 } v; 393 } un; 394} mixer_devinfo_t; 395.Ed 396.Pp 397The 398.Va label 399field identifies the name of this particular mixer control. 400The 401.Va index 402field may be used as the 403.Va dev 404field in 405.Dv AUDIO_MIXER_READ 406and 407.Dv AUDIO_MIXER_WRITE 408commands. 409The 410.Va type 411field identifies the type of this mixer control. 412Enumeration types are typically used for on/off style controls (e.g., a 413mute control) or for input/output device selection (e.g., select 414recording input source from CD, line in, or microphone). 415Set types are similar to enumeration types but any combination 416of the mask bits can be used. 417.Pp 418The 419.Va mixer_class 420field identifies what class of control this is. 421This value is set to the index value used to query the class itself. 422The 423.Pq arbitrary 424value set by the hardware driver may be determined by examining the 425.Va mixer_class 426field of the class itself, 427a mixer of type 428.Dv AUDIO_MIXER_CLASS . 429For example, a mixer level controlling the input gain on the 430.Dq line in 431circuit would have a 432.Va mixer_class 433that matches an input class device with the name 434.Dq inputs 435.Dv ( AudioCinputs ) 436and would have a 437.Va label 438of 439.Dq line 440.Dv ( AudioNline ) . 441Mixer controls which control audio circuitry for a particular audio 442source (e.g., line-in, CD in, DAC output) are collected under the input class, 443while those which control all audio sources (e.g., master volume, 444equalization controls) are under the output class. 445Hardware devices capable of recording typically also have a record class, 446for controls that only affect recording, 447and also a monitor class. 448.Pp 449The 450.Va next 451and 452.Va prev 453may be used by the hardware device driver to provide hints for the next 454and previous devices in a related set (for example, the line in level 455control would have the line in mute as its 456.Dq next 457value). 458If there is no relevant next or previous value, 459.Dv AUDIO_MIXER_LAST 460is specified. 461.Pp 462For 463.Dv AUDIO_MIXER_ENUM 464mixer control types, 465the enumeration values and their corresponding names are filled in. 466For example, a mute control would return appropriate values paired with 467.Dv AudioNon 468and 469.Dv AudioNoff . 470For the 471.Dv AUDIO_MIXER_VALUE 472and 473.Dv AUDIO_MIXER_SET 474mixer control types, the channel count is 475returned; the units name specifies what the level controls (typical 476values are 477.Dv AudioNvolume , 478.Dv AudioNtreble , 479and 480.Dv AudioNbass ) . 481.\" For AUDIO_MIXER_SET mixer control types, what is what? 482.El 483.Pp 484A process may read the control device to get notifications about 485mixer changes. 486Whenever a control changes, the 487.Xr read 2 488function fetches an integer identifying the control. 489It may be used in the 490.Va dev 491field of the 492.Va mixer_ctrl 493structure to call 494.Dv AUDIO_MIXER_READ . 495.Pp 496In contrast to audio devices, which have the exclusive open property, 497control devices can be opened at any time in write-only mode. 498Only one reader is allowed at a time. 499.Sh FILES 500.Bl -tag -width /dev/audioctlN -compact 501.It Pa /dev/audio Ns Ar N 502Audio device. 503.It Pa /dev/audioctl Ns Ar N 504Control device. 505.El 506.Sh SEE ALSO 507.Xr aucat 1 , 508.Xr cdio 1 , 509.Xr sndioctl 1 , 510.Xr ioctl 2 , 511.Xr sio_open 3 , 512.Xr sioctl_open 3 , 513.Xr ac97 4 , 514.Xr uaudio 4 , 515.Xr sndio 7 , 516.Xr audioctl 8 , 517.Xr mixerctl 8 , 518.Xr sndiod 8 , 519.Xr audio 9 520.\" .Sh BUGS 521