1 .\" $NetBSD: audio.4,v 1.69 2009/01/03 17:44:20 christos Exp $
3 .\" Copyright (c) 1996 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
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.
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.
30 .Dd September 29, 2009
35 .Nd device-independent audio driver layer
41 driver provides support for various audio peripherals.
42 It provides a uniform programming interface layer above different
43 underlying audio hardware drivers.
44 The audio layer provides full-duplex operation if the
45 underlying hardware configuration supports it.
47 There are four device files available for audio operation:
57 are used for recording or playback of digital samples.
60 is used to manipulate volume, recording source, or other audio mixer
68 but no other operations.
72 which has the exclusive open property
74 can be opened at any time and can be used to manipulate the audio device
79 is opened, it automatically directs the underlying driver to manipulate
80 monaural 8-bit mu-law samples.
81 In addition, if it is opened read-only
82 (write-only) the device is set to half-duplex record (play) mode with
83 recording (playing) unpaused and playing (recording) paused.
86 is opened, it maintains the previous audio sample mode and
94 Only one process may hold open a sampling device at a given time
95 (although file descriptors may be shared between processes once the
96 first open completes).
98 On a half-duplex device, writes while recording is in progress will be
99 immediately discarded.
100 Similarly, reads while playback is in progress
101 will be filled with silence but delayed to return at the current
103 If both playback and recording are requested on a half-duplex
104 device, playback mode takes precedence and recordings will get silence.
106 On a full-duplex device, reads and writes may operate
107 concurrently without interference.
108 If a full-duplex capable audio device is opened for both reading and writing
109 it will start in half-duplex play mode; full-duplex mode has to be set
112 On either type of device, if the playback mode is paused then silence is
113 played instead of the provided samples, and if recording is paused then
114 the process blocks in
116 until recording is unpaused.
118 If a writing process does not call
120 frequently enough to provide samples at the pace the hardware
121 consumes them silence is inserted.
124 mode is not set the writing process must
125 provide enough data via
126 subsequent write calls to
128 in time to the current audio
129 block before any more process-provided samples will be played.
130 If a reading process does not call
132 frequently enough, it will simply miss samples.
134 The audio device is normally accessed with
138 calls, but it can also be mapped into user memory with
140 (when supported by the device).
141 Once the device has been mapped it can no longer be accessed
142 by read or write; all access is by reading and writing to
144 The device appears as a block of memory
150 .Dv AUDIO_GETBUFINFO ) .
151 The device driver will continuously move data from this buffer
152 from/to the audio hardware, wrapping around at the end of the buffer.
153 To find out where the hardware is currently accessing data in the buffer the
158 The playing and recording buffers are distinct and must be
159 mapped separately if both are to be used.
160 Only encodings that are not emulated (i.e. where
161 .Dv AUDIO_ENCODINGFLAG_EMULATED
162 is not set) work properly for a mapped device.
164 The audio device, like most devices, can be used in
166 can be set in non-blocking mode and can be set (with a
170 when I/O is possible.
171 The mixer device can be set to generate a
173 whenever a mixer value is changed.
177 commands are supported on the sample devices:
179 .Bl -tag -width indent
181 This command stops all playback and recording, clears all queued
182 buffers, resets error counters, and restarts recording and playback as
183 appropriate for the current sampling mode.
184 .It Dv AUDIO_RERROR (int)
185 This command fetches the count of dropped input samples into its integer
187 There is no information regarding when in the sample stream
189 .It Dv AUDIO_WSEEK (int)
190 This command fetches the count of samples that are queued ahead of the
191 first sample in the most recent sample block written into its integer argument.
193 This command suspends the calling process until all queued playback
194 samples have been played by the hardware.
195 .It Dv AUDIO_GETDEV (audio_device_t)
196 This command fetches the current hardware device information into the
197 audio_device_t argument.
199 typedef struct audio_device {
200 char name[MAX_AUDIO_DEV_LEN];
201 char version[MAX_AUDIO_DEV_LEN];
202 char config[MAX_AUDIO_DEV_LEN];
205 .It Dv AUDIO_GETFD (int)
206 The command returns the current setting of the full duplex mode.
207 .It Dv AUDIO_GETENC (audio_encoding_t)
208 This command is used iteratively to fetch sample encoding names and
209 format_ids into the input/output audio_encoding_t argument.
211 typedef 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 */
217 #define AUDIO_ENCODINGFLAG_EMULATED 1 /* software emulation mode */
222 all the supported encodings, start with an index field of 0 and
223 continue with successive encodings (1, 2, ...) until the command returns
225 .It Dv AUDIO_SETFD (int)
226 This command sets the device into full-duplex operation if its integer
227 argument has a non-zero value, or into half-duplex operation if it
228 contains a zero value.
229 If the device does not support full-duplex
230 operation, attempting to set full-duplex mode returns an error.
231 .It Dv AUDIO_GETPROPS (int)
232 This command gets a bit set of hardware properties.
234 has a certain property the corresponding bit is set, otherwise it is not.
235 The properties can have the following values:
237 .Bl -tag -width AUDIO_PROP_INDEPENDENT -compact
238 .It Dv AUDIO_PROP_FULLDUPLEX
239 the device admits full duplex operation.
240 .It Dv AUDIO_PROP_MMAP
241 the device can be used with
243 .It Dv AUDIO_PROP_INDEPENDENT
244 the device can set the playing and recording encoding parameters
246 .It Dv AUDIO_PROP_PLAYBACK
247 the device is capable of audio playback.
248 .It Dv AUDIO_PROP_CAPTURE
249 the device is capable of audio capture.
251 .It Dv AUDIO_GETIOFFS (audio_offset_t)
252 .It Dv AUDIO_GETOOFFS (audio_offset_t)
253 This command fetches the current offset in the input(output) buffer where
254 the audio hardware's DMA engine will be putting(getting) data.
255 It mostly useful when the device
256 buffer is available in user space via the
259 The information is returned in the audio_offset structure.
261 typedef struct audio_offset {
262 u_int samples; /* Total number of bytes transferred */
263 u_int deltablks; /* Blocks transferred since last checked */
264 u_int offset; /* Physical transfer offset in buffer */
267 .It Dv AUDIO_GETINFO (audio_info_t)
268 .It Dv AUDIO_GETBUFINFO (audio_info_t)
269 .It Dv AUDIO_SETINFO (audio_info_t)
270 Get or set audio information as encoded in the audio_info structure.
272 typedef struct audio_info {
273 struct audio_prinfo play; /* info for play (output) side */
274 struct audio_prinfo record; /* info for record (input) side */
275 u_int monitor_gain; /* input to output mix */
277 u_int blocksize; /* H/W read/write block size */
278 u_int hiwat; /* output high water mark */
279 u_int lowat; /* output low water mark */
281 u_int mode; /* current device mode */
282 #define AUMODE_PLAY 0x01
283 #define AUMODE_RECORD 0x02
284 #define AUMODE_PLAY_ALL 0x04 /* do not do real-time correction */
288 When setting the current state with
290 the audio_info structure should first be initialized with
291 .Dv AUDIO_INITINFO Po \*[Am]info Pc
292 and then the particular values to be changed should be set.
293 This allows the audio driver to only set those things that you wish
294 to change and eliminates the need to query the device with
302 field should be set to
305 .Dv AUMODE_PLAY_ALL ,
306 or a bitwise OR combination of the three.
307 Only full-duplex audio devices support
308 simultaneous record and playback.
313 are used to control write behavior.
314 Writes to the audio devices will queue up blocks until the high-water
315 mark is reached, at which point any more write calls will block
316 until the queue is drained to the low-water mark.
320 set those high- and low-water marks (in audio blocks).
323 is the maximum value and for
329 sets the current audio blocksize.
330 The generic audio driver layer and the hardware driver have the
331 opportunity to adjust this block size to get it within
332 implementation-required limits.
335 call, the actual blocksize set is returned in this field.
338 is calculated to correspond to 50ms of sound and it is recalculated
339 when the encoding parameter changes, but if the
341 is set explicitly this value becomes sticky, i.e., it remains
342 even when the encoding is changed.
343 The stickiness can be cleared by reopening the device or setting the
347 struct audio_prinfo {
348 u_int sample_rate; /* sample rate in samples/s */
349 u_int channels; /* number of channels, usually 1 or 2 */
350 u_int precision; /* number of bits/sample */
351 u_int encoding; /* data encoding (AUDIO_ENCODING_* below) */
352 u_int gain; /* volume level */
353 u_int port; /* selected I/O port */
354 u_long seek; /* BSD extension */
355 u_int avail_ports; /* available I/O ports */
356 u_int buffer_size; /* total size audio buffer */
358 /* Current state of device: */
359 u_int samples; /* number of samples */
360 u_int eof; /* End Of File (zero-size writes) counter */
361 u_char pause; /* non-zero if paused, zero to resume */
362 u_char error; /* non-zero if underflow/overflow occurred */
363 u_char waiting; /* non-zero if another process hangs in open */
364 u_char balance; /* stereo channel balance */
366 u_char open; /* non-zero if currently open */
367 u_char active; /* non-zero if I/O is currently active */
371 Note: many hardware audio drivers require identical playback and
372 recording sample rates, sample encodings, and channel counts.
373 The playing information is always set last and will prevail on such hardware.
374 If the hardware can handle different settings the
375 .Dv AUDIO_PROP_INDEPENDENT
378 The encoding parameter can have the following values:
380 .Bl -tag -width AUDIO_ENCODING_SLINEAR_BE -compact
381 .It Dv AUDIO_ENCODING_ULAW
382 mu-law encoding, 8 bits/sample
383 .It Dv AUDIO_ENCODING_ALAW
384 A-law encoding, 8 bits/sample
385 .It Dv AUDIO_ENCODING_SLINEAR
386 two's complement signed linear encoding with the platform byte order
387 .It Dv AUDIO_ENCODING_ULINEAR
388 unsigned linear encoding with the platform byte order
389 .It Dv AUDIO_ENCODING_ADPCM
390 ADPCM encoding, 8 bits/sample
391 .It Dv AUDIO_ENCODING_SLINEAR_LE
392 two's complement signed linear encoding with little endian byte order
393 .It Dv AUDIO_ENCODING_SLINEAR_BE
394 two's complement signed linear encoding with big endian byte order
395 .It Dv AUDIO_ENCODING_ULINEAR_LE
396 unsigned linear encoding with little endian byte order
397 .It Dv AUDIO_ENCODING_ULINEAR_BE
398 unsigned linear encoding with big endian byte order
406 settings provide simple shortcuts to the richer mixer
407 interface described below and are not obtained by
408 .Dv AUDIO_GETBUFINFO .
409 The gain should be in the range
410 .Bq Dv AUDIO_MIN_GAIN , Dv AUDIO_MAX_GAIN
411 and the balance in the range
412 .Bq Dv AUDIO_LEFT_BALANCE , Dv AUDIO_RIGHT_BALANCE
413 with the normal setting at
414 .Dv AUDIO_MID_BALANCE .
416 The input port should be a combination of:
418 .Bl -tag -width AUDIO_MICROPHONE -compact
419 .It Dv AUDIO_MICROPHONE
420 to select microphone input.
422 to select line input.
427 The output port should be a combination of:
429 .Bl -tag -width AUDIO_HEADPHONE -compact
431 to select speaker output.
432 .It Dv AUDIO_HEADPHONE
433 to select headphone output.
434 .It Dv AUDIO_LINE_OUT
435 to select line output.
438 The available ports can be found in
440 .Dv ( AUDIO_GETBUFINFO
444 is the total size of the audio buffer.
445 The buffer size divided by the
447 gives the maximum value for
451 can only be read and not set.
457 fields are only used by
460 .Dv AUDIO_GETBUFINFO .
462 represents the count of
465 represents the total number of bytes recorded or played, less those
466 that were dropped due to inadequate consumption/production rates.
469 returns the current pause/unpause state for recording or playback.
472 if the pause value is specified it will either pause
473 or unpause the particular direction.
478 may be manipulated with
484 It supports the following
487 .Bl -tag -width indent
488 .It Dv AUDIO_GETDEV (audio_device_t)
489 This command is the same as described above for the sampling devices.
490 .It Dv AUDIO_MIXER_READ (mixer_ctrl_t)
491 .It Dv AUDIO_MIXER_WRITE (mixer_ctrl_t)
492 These commands read the current mixer state or set new mixer state for
496 identifies which type of value is supplied in the
500 #define AUDIO_MIXER_CLASS 0
501 #define AUDIO_MIXER_ENUM 1
502 #define AUDIO_MIXER_SET 2
503 #define AUDIO_MIXER_VALUE 3
504 typedef struct mixer_ctrl {
505 int dev; /* input: nth device */
510 mixer_level_t value; /* value */
514 #define AUDIO_MIN_GAIN 0
515 #define AUDIO_MAX_GAIN 255
516 typedef struct mixer_level {
518 u_char level[8]; /* [num_channels] */
520 #define AUDIO_MIXER_LEVEL_MONO 0
521 #define AUDIO_MIXER_LEVEL_LEFT 0
522 #define AUDIO_MIXER_LEVEL_RIGHT 1
525 For a mixer value, the
527 field specifies both the number of channels and the values for each
529 If the channel count does not match the current channel count, the
530 attempt to change the setting may fail (depending on the hardware
531 device driver implementation).
532 For an enumeration value, the
534 field should be set to one of the possible values as returned by a prior
535 .Dv AUDIO_MIXER_DEVINFO
538 .Dv AUDIO_MIXER_CLASS
539 is only used for classifying particular mixer device
540 types and is not used for
543 .Dv AUDIO_MIXER_WRITE .
544 .It Dv AUDIO_MIXER_DEVINFO (mixer_devinfo_t)
545 This command is used iteratively to fetch audio mixer device information
546 into the input/output mixer_devinfo_t argument.
547 To query all the supported devices, start with an index field of
548 0 and continue with successive devices (1, 2, ...) until the
549 command returns an error.
551 typedef struct mixer_devinfo {
552 int index; /* input: nth mixer device */
553 audio_mixer_name_t label;
557 #define AUDIO_MIXER_LAST -1
559 struct audio_mixer_enum {
562 audio_mixer_name_t label;
566 struct audio_mixer_set {
569 audio_mixer_name_t label;
573 struct audio_mixer_value {
574 audio_mixer_name_t units;
584 field identifies the name of this particular mixer control.
587 field may be used as the
592 .Dv AUDIO_MIXER_WRITE
596 field identifies the type of this mixer control.
597 Enumeration types are typically used for on/off style controls (e.g. a
598 mute control) or for input/output device selection (e.g. select
599 recording input source from CD, line in, or microphone).
600 Set types are similar to enumeration types but any combination
601 of the mask bits can be used.
605 field identifies what class of control this is.
608 value set by the hardware driver may be determined by examining the
610 field of the class itself,
612 .Dv AUDIO_MIXER_CLASS .
613 For example, a mixer controlling the input gain on the line in circuit
616 that matches an input class device with the name
618 .Dv ( AudioCinputs ) ,
624 Mixer controls which control audio circuitry for a particular audio
625 source (e.g. line-in, CD in, DAC output) are collected under the input class,
626 while those which control all audio sources (e.g. master volume,
627 equalization controls) are under the output class.
628 Hardware devices capable of recording typically also have a record class,
629 for controls that only affect recording,
630 and also a monitor class.
636 may be used by the hardware device driver to provide hints for the next
637 and previous devices in a related set (for example, the line in level
638 control would have the line in mute as its
641 If there is no relevant next or previous value,
648 the enumeration values and their corresponding names are filled in.
649 For example, a mute control would return appropriate values paired with
654 .Dv AUDIO_MIXER_VALUE
657 mixer control types, the channel count is
658 returned; the units name specifies what the level controls (typical
663 .\" For AUDIO_MIXER_SET mixer control types, what is what?
666 By convention, all the mixer devices can be distinguished from other
667 mixer controls because they use a name from one of the
671 .Bl -tag -width /dev/audioctl -compact
717 If the device is used in
719 it is currently always mapped for writing (playing) due to