1.\" $NetBSD: audio.4,v 1.69 2009/01/03 17:44:20 christos Exp $ 2.\" 3.\" Copyright (c) 1996 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by John T. Kohl. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd January 3, 2009 31.Dt AUDIO 4 32.Os 33.Sh NAME 34.Nm audio 35.Nd device-independent audio driver layer 36.Sh SYNOPSIS 37.In sys/audioio.h 38.Sh DESCRIPTION 39The 40.Nm 41driver provides support for various audio peripherals. 42It provides a uniform programming interface layer above different 43underlying audio hardware drivers. 44The audio layer provides full-duplex operation if the 45underlying hardware configuration supports it. 46.Pp 47There are four device files available for audio operation: 48.Pa /dev/audio , 49.Pa /dev/sound , 50.Pa /dev/audioctl , 51and 52.Pa /dev/mixer . 53.Pp 54.Pa /dev/audio 55and 56.Pa /dev/sound 57are used for recording or playback of digital samples. 58.Pp 59.Pa /dev/mixer 60is used to manipulate volume, recording source, or other audio mixer 61functions. 62.Pp 63.Pa /dev/audioctl 64accepts the same 65.Xr ioctl 2 66operations as 67.Pa /dev/sound , 68but no other operations. 69.Pp 70In contrast to 71.Pa /dev/sound 72which has the exclusive open property 73.Pa /dev/audioctl 74can be opened at any time and can be used to manipulate the audio device 75while it is in use. 76.Sh SAMPLING DEVICES 77When 78.Pa /dev/audio 79is opened, it automatically directs the underlying driver to manipulate 80monaural 8-bit mu-law samples. 81In addition, if it is opened read-only 82(write-only) the device is set to half-duplex record (play) mode with 83recording (playing) unpaused and playing (recording) paused. 84When 85.Pa /dev/sound 86is opened, it maintains the previous audio sample mode and 87record/playback mode. 88In all other respects 89.Pa /dev/audio 90and 91.Pa /dev/sound 92are identical. 93.Pp 94Only one process may hold open a sampling device at a given time 95(although file descriptors may be shared between processes once the 96first open completes). 97.Pp 98On a half-duplex device, writes while recording is in progress will be 99immediately discarded. 100Similarly, reads while playback is in progress 101will be filled with silence but delayed to return at the current 102sampling rate. 103If both playback and recording are requested on a half-duplex 104device, playback mode takes precedence and recordings will get silence. 105.Pp 106On a full-duplex device, reads and writes may operate 107concurrently without interference. 108If a full-duplex capable audio device is opened for both reading and writing 109it will start in half-duplex play mode; full-duplex mode has to be set 110explicitly. 111.Pp 112On either type of device, if the playback mode is paused then silence is 113played instead of the provided samples, and if recording is paused then 114the process blocks in 115.Xr read 2 116until recording is unpaused. 117.Pp 118If a writing process does not call 119.Xr write 2 120frequently enough to provide samples at the pace the hardware 121consumes them silence is inserted. 122If the 123.Dv AUMODE_PLAY_ALL 124mode is not set the writing process must 125provide enough data via 126subsequent write calls to 127.Dq catch up 128in time to the current audio 129block before any more process-provided samples will be played. 130If a reading process does not call 131.Xr read 2 132frequently enough, it will simply miss samples. 133.Pp 134The audio device is normally accessed with 135.Xr read 2 136or 137.Xr write 2 138calls, but it can also be mapped into user memory with 139.Xr mmap 2 140(when supported by the device). 141Once the device has been mapped it can no longer be accessed 142by read or write; all access is by reading and writing to 143the mapped memory. 144The device appears as a block of memory 145of size 146.Va buffersize 147(as available via 148.Dv AUDIO_GETINFO 149or 150.Dv AUDIO_GETBUFINFO ) . 151The device driver will continuously move data from this buffer 152from/to the audio hardware, wrapping around at the end of the buffer. 153To find out where the hardware is currently accessing data in the buffer the 154.Dv AUDIO_GETIOFFS 155and 156.Dv AUDIO_GETOOFFS 157calls can be used. 158The playing and recording buffers are distinct and must be 159mapped separately if both are to be used. 160Only encodings that are not emulated (i.e. where 161.Dv AUDIO_ENCODINGFLAG_EMULATED 162is not set) work properly for a mapped device. 163.Pp 164The audio device, like most devices, can be used in 165.Va select , 166can be set in non-blocking mode and can be set (with a 167.Dv FIOASYNC 168ioctl) to send a 169.Dv SIGIO 170when I/O is possible. 171The mixer device can be set to generate a 172.Dv SIGIO 173whenever a mixer value is changed. 174.Pp 175The following 176.Xr ioctl 2 177commands are supported on the sample devices: 178.Pp 179.Bl -tag -width indent 180.It Dv AUDIO_FLUSH 181This command stops all playback and recording, clears all queued 182buffers, resets error counters, and restarts recording and playback as 183appropriate for the current sampling mode. 184.It Dv AUDIO_RERROR (int) 185This command fetches the count of dropped input samples into its integer 186argument. 187There is no information regarding when in the sample stream 188they were dropped. 189.It Dv AUDIO_WSEEK (int) 190This command fetches the count of samples that are queued ahead of the 191first sample in the most recent sample block written into its integer argument. 192.It Dv AUDIO_DRAIN 193This command suspends the calling process until all queued playback 194samples have been played by the hardware. 195.It Dv AUDIO_GETDEV (audio_device_t) 196This command fetches the current hardware device information into the 197audio_device_t argument. 198.Bd -literal 199typedef struct audio_device { 200 char name[MAX_AUDIO_DEV_LEN]; 201 char version[MAX_AUDIO_DEV_LEN]; 202 char config[MAX_AUDIO_DEV_LEN]; 203} audio_device_t; 204.Ed 205.It Dv AUDIO_GETFD (int) 206The command returns the current setting of the full duplex mode. 207.It Dv AUDIO_GETENC (audio_encoding_t) 208This command is used iteratively to fetch sample encoding names and 209format_ids into the input/output audio_encoding_t argument. 210.Bd -literal 211typedef struct audio_encoding { 212 int index; /* input: nth encoding */ 213 char name[MAX_AUDIO_DEV_LEN]; /* name of encoding */ 214 int encoding; /* value for encoding parameter */ 215 int precision; /* value for precision parameter */ 216 int flags; 217#define AUDIO_ENCODINGFLAG_EMULATED 1 /* software emulation mode */ 218} audio_encoding_t; 219.Ed 220.Pp 221To query 222all the supported encodings, start with an index field of 0 and 223continue with successive encodings (1, 2, ...) until the command returns 224an error. 225.It Dv AUDIO_SETFD (int) 226This command sets the device into full-duplex operation if its integer 227argument has a non-zero value, or into half-duplex operation if it 228contains a zero value. 229If the device does not support full-duplex 230operation, attempting to set full-duplex mode returns an error. 231.It Dv AUDIO_GETPROPS (int) 232This command gets a bit set of hardware properties. 233If the hardware 234has a certain property the corresponding bit is set, otherwise it is not. 235The properties can have the following values: 236.Pp 237.Bl -tag -width AUDIO_PROP_INDEPENDENT -compact 238.It Dv AUDIO_PROP_FULLDUPLEX 239the device admits full duplex operation. 240.It Dv AUDIO_PROP_MMAP 241the device can be used with 242.Xr mmap 2 . 243.It Dv AUDIO_PROP_INDEPENDENT 244the device can set the playing and recording encoding parameters 245independently. 246.El 247.It Dv AUDIO_GETIOFFS (audio_offset_t) 248.It Dv AUDIO_GETOOFFS (audio_offset_t) 249This command fetches the current offset in the input(output) buffer where 250the audio hardware's DMA engine will be putting(getting) data. 251It mostly useful when the device 252buffer is available in user space via the 253.Xr mmap 2 254call. 255The information is returned in the audio_offset structure. 256.Bd -literal 257typedef struct audio_offset { 258 u_int samples; /* Total number of bytes transferred */ 259 u_int deltablks; /* Blocks transferred since last checked */ 260 u_int offset; /* Physical transfer offset in buffer */ 261} audio_offset_t; 262.Ed 263.It Dv AUDIO_GETINFO (audio_info_t) 264.It Dv AUDIO_GETBUFINFO (audio_info_t) 265.It Dv AUDIO_SETINFO (audio_info_t) 266Get or set audio information as encoded in the audio_info structure. 267.Bd -literal 268typedef struct audio_info { 269 struct audio_prinfo play; /* info for play (output) side */ 270 struct audio_prinfo record; /* info for record (input) side */ 271 u_int monitor_gain; /* input to output mix */ 272 /* BSD extensions */ 273 u_int blocksize; /* H/W read/write block size */ 274 u_int hiwat; /* output high water mark */ 275 u_int lowat; /* output low water mark */ 276 u_int _ispare1; 277 u_int mode; /* current device mode */ 278#define AUMODE_PLAY 0x01 279#define AUMODE_RECORD 0x02 280#define AUMODE_PLAY_ALL 0x04 /* do not do real-time correction */ 281} audio_info_t; 282.Ed 283.Pp 284When setting the current state with 285.Dv AUDIO_SETINFO , 286the audio_info structure should first be initialized with 287.Dv AUDIO_INITINFO Po \*[Am]info Pc 288and then the particular values to be changed should be set. 289This allows the audio driver to only set those things that you wish 290to change and eliminates the need to query the device with 291.Dv AUDIO_GETINFO 292or 293.Dv AUDIO_GETBUFINFO 294first. 295.Pp 296The 297.Va mode 298field should be set to 299.Dv AUMODE_PLAY , 300.Dv AUMODE_RECORD , 301.Dv AUMODE_PLAY_ALL , 302or a bitwise OR combination of the three. 303Only full-duplex audio devices support 304simultaneous record and playback. 305.Pp 306.Va hiwat 307and 308.Va lowat 309are used to control write behavior. 310Writes to the audio devices will queue up blocks until the high-water 311mark is reached, at which point any more write calls will block 312until the queue is drained to the low-water mark. 313.Va hiwat 314and 315.Va lowat 316set those high- and low-water marks (in audio blocks). 317The default for 318.Va hiwat 319is the maximum value and for 320.Va lowat 32175 % of 322.Va hiwat . 323.Pp 324.Va blocksize 325sets the current audio blocksize. 326The generic audio driver layer and the hardware driver have the 327opportunity to adjust this block size to get it within 328implementation-required limits. 329Upon return from an 330.Dv AUDIO_SETINFO 331call, the actual blocksize set is returned in this field. 332Normally the 333.Va blocksize 334is calculated to correspond to 50ms of sound and it is recalculated 335when the encoding parameter changes, but if the 336.Va blocksize 337is set explicitly this value becomes sticky, i.e., it remains 338even when the encoding is changed. 339The stickiness can be cleared by reopening the device or setting the 340.Va blocksize 341to 0. 342.Bd -literal 343struct audio_prinfo { 344 u_int sample_rate; /* sample rate in samples/s */ 345 u_int channels; /* number of channels, usually 1 or 2 */ 346 u_int precision; /* number of bits/sample */ 347 u_int encoding; /* data encoding (AUDIO_ENCODING_* below) */ 348 u_int gain; /* volume level */ 349 u_int port; /* selected I/O port */ 350 u_long seek; /* BSD extension */ 351 u_int avail_ports; /* available I/O ports */ 352 u_int buffer_size; /* total size audio buffer */ 353 u_int _ispare[1]; 354 /* Current state of device: */ 355 u_int samples; /* number of samples */ 356 u_int eof; /* End Of File (zero-size writes) counter */ 357 u_char pause; /* non-zero if paused, zero to resume */ 358 u_char error; /* non-zero if underflow/overflow occurred */ 359 u_char waiting; /* non-zero if another process hangs in open */ 360 u_char balance; /* stereo channel balance */ 361 u_char cspare[2]; 362 u_char open; /* non-zero if currently open */ 363 u_char active; /* non-zero if I/O is currently active */ 364}; 365.Ed 366.Pp 367Note: many hardware audio drivers require identical playback and 368recording sample rates, sample encodings, and channel counts. 369The playing information is always set last and will prevail on such hardware. 370If the hardware can handle different settings the 371.Dv AUDIO_PROP_INDEPENDENT 372property is set. 373.Pp 374The encoding parameter can have the following values: 375.Pp 376.Bl -tag -width AUDIO_ENCODING_SLINEAR_BE -compact 377.It Dv AUDIO_ENCODING_ULAW 378mu-law encoding, 8 bits/sample 379.It Dv AUDIO_ENCODING_ALAW 380A-law encoding, 8 bits/sample 381.It Dv AUDIO_ENCODING_SLINEAR 382two's complement signed linear encoding with the platform byte order 383.It Dv AUDIO_ENCODING_ULINEAR 384unsigned linear encoding with the platform byte order 385.It Dv AUDIO_ENCODING_ADPCM 386ADPCM encoding, 8 bits/sample 387.It Dv AUDIO_ENCODING_SLINEAR_LE 388two's complement signed linear encoding with little endian byte order 389.It Dv AUDIO_ENCODING_SLINEAR_BE 390two's complement signed linear encoding with big endian byte order 391.It Dv AUDIO_ENCODING_ULINEAR_LE 392unsigned linear encoding with little endian byte order 393.It Dv AUDIO_ENCODING_ULINEAR_BE 394unsigned linear encoding with big endian byte order 395.El 396.Pp 397The 398.Va gain , 399.Va port 400and 401.Va balance 402settings provide simple shortcuts to the richer mixer 403interface described below and are not obtained by 404.Dv AUDIO_GETBUFINFO . 405The gain should be in the range 406.Bq Dv AUDIO_MIN_GAIN , Dv AUDIO_MAX_GAIN 407and the balance in the range 408.Bq Dv AUDIO_LEFT_BALANCE , Dv AUDIO_RIGHT_BALANCE 409with the normal setting at 410.Dv AUDIO_MID_BALANCE . 411.Pp 412The input port should be a combination of: 413.Pp 414.Bl -tag -width AUDIO_MICROPHONE -compact 415.It Dv AUDIO_MICROPHONE 416to select microphone input. 417.It Dv AUDIO_LINE_IN 418to select line input. 419.It Dv AUDIO_CD 420to select CD input. 421.El 422.Pp 423The output port should be a combination of: 424.Pp 425.Bl -tag -width AUDIO_HEADPHONE -compact 426.It Dv AUDIO_SPEAKER 427to select speaker output. 428.It Dv AUDIO_HEADPHONE 429to select headphone output. 430.It Dv AUDIO_LINE_OUT 431to select line output. 432.El 433.Pp 434The available ports can be found in 435.Va avail_ports 436.Dv ( AUDIO_GETBUFINFO 437only). 438.Pp 439.Va buffer_size 440is the total size of the audio buffer. 441The buffer size divided by the 442.Va blocksize 443gives the maximum value for 444.Va hiwat . 445Currently the 446.Va buffer_size 447can only be read and not set. 448.Pp 449The 450.Va seek 451and 452.Va samples 453fields are only used by 454.Dv AUDIO_GETINFO 455and 456.Dv AUDIO_GETBUFINFO . 457.Va seek 458represents the count of 459samples pending; 460.Va samples 461represents the total number of bytes recorded or played, less those 462that were dropped due to inadequate consumption/production rates. 463.Pp 464.Va pause 465returns the current pause/unpause state for recording or playback. 466For 467.Dv AUDIO_SETINFO , 468if the pause value is specified it will either pause 469or unpause the particular direction. 470.El 471.Sh MIXER DEVICE 472The mixer device, 473.Pa /dev/mixer , 474may be manipulated with 475.Xr ioctl 2 476but does not support 477.Xr read 2 478or 479.Xr write 2 . 480It supports the following 481.Xr ioctl 2 482commands: 483.Bl -tag -width indent 484.It Dv AUDIO_GETDEV (audio_device_t) 485This command is the same as described above for the sampling devices. 486.It Dv AUDIO_MIXER_READ (mixer_ctrl_t) 487.It Dv AUDIO_MIXER_WRITE (mixer_ctrl_t) 488These commands read the current mixer state or set new mixer state for 489the specified device 490.Va dev . 491.Va type 492identifies which type of value is supplied in the 493.Va mixer_ctrl_t 494argument. 495.Bd -literal 496#define AUDIO_MIXER_CLASS 0 497#define AUDIO_MIXER_ENUM 1 498#define AUDIO_MIXER_SET 2 499#define AUDIO_MIXER_VALUE 3 500typedef struct mixer_ctrl { 501 int dev; /* input: nth device */ 502 int type; 503 union { 504 int ord; /* enum */ 505 int mask; /* set */ 506 mixer_level_t value; /* value */ 507 } un; 508} mixer_ctrl_t; 509 510#define AUDIO_MIN_GAIN 0 511#define AUDIO_MAX_GAIN 255 512typedef struct mixer_level { 513 int num_channels; 514 u_char level[8]; /* [num_channels] */ 515} mixer_level_t; 516#define AUDIO_MIXER_LEVEL_MONO 0 517#define AUDIO_MIXER_LEVEL_LEFT 0 518#define AUDIO_MIXER_LEVEL_RIGHT 1 519.Ed 520.Pp 521For a mixer value, the 522.Va value 523field specifies both the number of channels and the values for each 524channel. 525If the channel count does not match the current channel count, the 526attempt to change the setting may fail (depending on the hardware 527device driver implementation). 528For an enumeration value, the 529.Va ord 530field should be set to one of the possible values as returned by a prior 531.Dv AUDIO_MIXER_DEVINFO 532command. 533The type 534.Dv AUDIO_MIXER_CLASS 535is only used for classifying particular mixer device 536types and is not used for 537.Dv AUDIO_MIXER_READ 538or 539.Dv AUDIO_MIXER_WRITE . 540.It Dv AUDIO_MIXER_DEVINFO (mixer_devinfo_t) 541This command is used iteratively to fetch audio mixer device information 542into the input/output mixer_devinfo_t argument. 543To query all the supported devices, start with an index field of 5440 and continue with successive devices (1, 2, ...) until the 545command returns an error. 546.Bd -literal 547typedef struct mixer_devinfo { 548 int index; /* input: nth mixer device */ 549 audio_mixer_name_t label; 550 int type; 551 int mixer_class; 552 int next, prev; 553#define AUDIO_MIXER_LAST -1 554 union { 555 struct audio_mixer_enum { 556 int num_mem; 557 struct { 558 audio_mixer_name_t label; 559 int ord; 560 } member[32]; 561 } e; 562 struct audio_mixer_set { 563 int num_mem; 564 struct { 565 audio_mixer_name_t label; 566 int mask; 567 } member[32]; 568 } s; 569 struct audio_mixer_value { 570 audio_mixer_name_t units; 571 int num_channels; 572 int delta; 573 } v; 574 } un; 575} mixer_devinfo_t; 576.Ed 577.Pp 578The 579.Va label 580field identifies the name of this particular mixer control. 581The 582.Va index 583field may be used as the 584.Va dev 585field in 586.Dv AUDIO_MIXER_READ 587and 588.Dv AUDIO_MIXER_WRITE 589commands. 590The 591.Va type 592field identifies the type of this mixer control. 593Enumeration types are typically used for on/off style controls (e.g. a 594mute control) or for input/output device selection (e.g. select 595recording input source from CD, line in, or microphone). 596Set types are similar to enumeration types but any combination 597of the mask bits can be used. 598.Pp 599The 600.Va mixer_class 601field identifies what class of control this is. 602The 603.Pq arbitrary 604value set by the hardware driver may be determined by examining the 605.Va mixer_class 606field of the class itself, 607a mixer of type 608.Dv AUDIO_MIXER_CLASS . 609For example, a mixer controlling the input gain on the line in circuit 610would have a 611.Va mixer_class 612that matches an input class device with the name 613.Dq inputs 614.Dv ( AudioCinputs ) , 615and would have a 616.Va label 617of 618.Dq line 619.Dv ( AudioNline ) . 620Mixer controls which control audio circuitry for a particular audio 621source (e.g. line-in, CD in, DAC output) are collected under the input class, 622while those which control all audio sources (e.g. master volume, 623equalization controls) are under the output class. 624Hardware devices capable of recording typically also have a record class, 625for controls that only affect recording, 626and also a monitor class. 627.Pp 628The 629.Va next 630and 631.Va prev 632may be used by the hardware device driver to provide hints for the next 633and previous devices in a related set (for example, the line in level 634control would have the line in mute as its 635.Dq next 636value). 637If there is no relevant next or previous value, 638.Dv AUDIO_MIXER_LAST 639is specified. 640.Pp 641For 642.Dv AUDIO_MIXER_ENUM 643mixer control types, 644the enumeration values and their corresponding names are filled in. 645For example, a mute control would return appropriate values paired with 646.Dv AudioNon 647and 648.Dv AudioNoff . 649For 650.Dv AUDIO_MIXER_VALUE 651and 652.Dv AUDIO_MIXER_SET 653mixer control types, the channel count is 654returned; the units name specifies what the level controls (typical 655values are 656.Dv AudioNvolume , 657.Dv AudioNtreble , 658.Dv AudioNbass ) . 659.\" For AUDIO_MIXER_SET mixer control types, what is what? 660.El 661.Pp 662By convention, all the mixer devices can be distinguished from other 663mixer controls because they use a name from one of the 664.Dv AudioC* 665string values. 666.Sh FILES 667.Bl -tag -width /dev/audioctl -compact 668.It Pa /dev/audio 669.It Pa /dev/audioctl 670.It Pa /dev/sound 671.It Pa /dev/mixer 672.El 673.Sh SEE ALSO 674.Xr audioctl 1 , 675.Xr mixerctl 1 , 676.Xr ioctl 2 , 677.Xr ossaudio 3 , 678.Xr midi 4 , 679.Xr radio 4 680.Ss ISA bus 681.Xr aria 4 , 682.Xr ess 4 , 683.Xr gus 4 , 684.Xr guspnp 4 , 685.Xr pas 4 , 686.Xr sb 4 , 687.Xr wss 4 , 688.Xr ym 4 689.Ss PCI bus 690.Xr auacer 4 , 691.Xr auich 4 , 692.Xr auixp 4 , 693.Xr autri 4 , 694.Xr auvia 4 , 695.Xr azalia 4 , 696.Xr clcs 4 , 697.Xr clct 4 , 698.Xr cmpci 4 , 699.Xr eap 4 , 700.Xr emuxki 4 , 701.Xr esa 4 , 702.Xr esm 4 , 703.Xr eso 4 , 704.Xr fms 4 , 705.Xr neo 4 , 706.Xr sv 4 , 707.Xr yds 4 708.Ss TURBOchannel 709.Xr bba 4 710.Ss USB 711.Xr uaudio 4 712.Sh BUGS 713If the device is used in 714.Xr mmap 2 715it is currently always mapped for writing (playing) due to 716VM system weirdness. 717