1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
|
.\" $OpenBSD: audio.4,v 1.70 2016/03/16 07:38:12 jmc Exp $
.\" $NetBSD: audio.4,v 1.20 1998/05/28 17:27:15 augustss Exp $
.\"
.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by John T. Kohl.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: March 16 2016 $
.Dt AUDIO 4
.Os
.Sh NAME
.Nm audio ,
.Nm mixer
.Nd device-independent audio driver layer
.Sh SYNOPSIS
.Cd "audio* at ..."
.Pp
.In sys/types.h
.In sys/ioctl.h
.In sys/audioio.h
.In string.h
.Sh DESCRIPTION
The
.Nm audio
driver provides support for various audio peripherals.
It provides 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.
.Pp
There are four device files available for audio operation:
.Pa /dev/audio ,
.Pa /dev/sound ,
.Pa /dev/audioctl ,
and
.Pa /dev/mixer .
.Pa /dev/audio
and
.Pa /dev/sound
are used for recording or playback of digital samples.
.Pa /dev/mixer
is used to manipulate volume, recording source, or other audio mixer
functions.
.Pa /dev/audioctl
accepts the same
.Xr ioctl 2
operations as
.Pa /dev/sound ,
but no other operations.
In contrast to
.Pa /dev/sound ,
which has the exclusive open property,
.Pa /dev/audioctl
can be opened at any time and can be used to manipulate the
.Nm audio
device while it is in use.
.Sh SAMPLING DEVICES
When
.Pa /dev/audio
or
.Pa /dev/sound
is opened, it attempts to maintain the previous audio sample format and
record/playback mode.
In addition, if it is opened read-only
(write-only) the device is set to half-duplex record (play) mode with
recording (playing) unpaused.
In all respects
.Pa /dev/audio
and
.Pa /dev/sound
are identical.
.Pp
Only one process may hold open a sampling device at a given time
(although file descriptors may be shared between processes once the
first open completes).
.Pp
On a half-duplex device, either reads or writes are allowed,
but not both.
On a full-duplex device, reads and writes may operate
concurrently without interference.
.Pp
If a writing process does not call
.Xr write 2
frequently enough to provide samples at the pace the hardware
consumes them silence is inserted.
If a reading process does not call
.Xr read 2
frequently enough, it will simply miss samples.
.Pp
The
.Nm audio
device is accessed with
.Xr read 2
or
.Xr write 2 .
.Pp
The
.Nm audio
device, like most devices, can be used in
.Xr poll 2 ,
.Pp
The following
.Xr ioctl 2
commands are supported on the sample devices:
.Pp
.Bl -tag -width Ds -compact
.It Dv AUDIO_RERROR Fa "int *"
.It Dv AUDIO_PERROR Fa "int *"
Obsolete.
These commands fetch the count of dropped input or output samples into
the
.Vt int *
argument, respectively.
There is no information regarding when in the sample stream
they were dropped.
.Pp
.It Dv AUDIO_GETDEV Fa "audio_device_t *"
This command fetches the current hardware device information into the
.Vt audio_device_t *
argument.
.Bd -literal
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;
.Ed
.Pp
.It Dv AUDIO_GETFD Fa "int *"
This command returns 1 if in full-duplex mode, else 0.
.Pp
.It Dv AUDIO_GETENC Fa "audio_encoding_t *"
This command is used iteratively to fetch sample encoding
.Va name Ns s
and
.Va format_id Ns s
into the input/output
.Vt audio_encoding_t *
argument.
.Bd -literal
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 bps; /* value for bps parameter */
int msb; /* value for msb parameter */
} audio_encoding_t;
.Ed
.Pp
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.
.Pp
.It Dv AUDIO_SETFD Fa "int *"
Does nothing, left for compatibility; argument must point to a non-zero
integer if the device is opened in read-write mode.
.Pp
.It Dv AUDIO_GETPROPS Fa "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:
.Pp
.Bl -tag -width AUDIO_PROP_INDEPENDENT -compact
.It Dv AUDIO_PROP_FULLDUPLEX
The device admits full-duplex operation.
.It Dv AUDIO_PROP_INDEPENDENT
The device can set playing and recording channel counts independently.
.El
.Pp
.It Dv AUDIO_GETIOFFS Fa "audio_offset_t *"
.It Dv AUDIO_GETOOFFS Fa "audio_offset_t *"
Obsolete.
These commands fetch the number of bytes played or recorded.
The information is returned in the
.Vt audio_offset
structure.
.Bd -literal
typedef struct audio_offset {
u_int samples; /* Total number of bytes transferred */
} audio_offset_t;
.Ed
.Pp
.It Dv AUDIO_GETINFO Fa "audio_info_t *"
.It Dv AUDIO_SETINFO Fa "audio_info_t *"
Get or set audio information as encoded in the
.Vt audio_info
structure.
.Bd -literal
typedef struct audio_info {
struct audio_prinfo play; /* info for play (output) side */
struct audio_prinfo record; /* info for record (input) side */
u_int hiwat; /* blocks count in play buffer */
u_int mode; /* current device mode */
#define AUMODE_PLAY 0x01
#define AUMODE_RECORD 0x02
} audio_info_t;
.Ed
.Pp
When setting the current state with
.Dv AUDIO_SETINFO ,
the
.Vt audio_info
structure should first be initialized with
.Pp
.Dl "AUDIO_INITINFO(&info);"
.Pp
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
.Dv AUDIO_GETINFO
first.
.Pp
The
.Va mode
field is read-only and set to
.Dv AUMODE_PLAY ,
.Dv AUMODE_RECORD ,
or a bitwise OR combination of the three.
Only full-duplex audio devices support
simultaneous record and playback.
.Pp
.Va hiwat
contains the number of blocks in the kernel play buffer.
Writes to the audio devices will queue blocks until the play buffer
is full, at which point any more write calls will block
until space for at least one byte is available.
.Bd -literal
struct audio_prinfo {
u_int sample_rate; /* sample rate in bit/s */
u_int channels; /* number of channels, usually 1 or 2 */
u_int precision; /* number of bits/sample */
u_int bps; /* number of bytes/sample */
u_int msb; /* data alignment */
u_int encoding; /* data encoding (AUDIO_ENCODING_* below) */
u_int block_size; /* size a block */
/* Current state of device: */
u_char pause; /* non-zero if paused, zero to resume */
u_char active; /* non-zero if I/O is currently active */
};
.Ed
.Pp
The
.Nm
driver requires identical playback and
recording sample rates, sample encodings, and block durations.
.Pp
The
.Va encoding
parameter can have the following values:
.Pp
.Bl -tag -width AUDIO_ENCODING_SLINEAR_BE -compact
.It Dv AUDIO_ENCODING_ULAW
mu-law encoding, 8 bits/sample
.It Dv AUDIO_ENCODING_ALAW
A-law encoding, 8 bits/sample
.It Dv AUDIO_ENCODING_SLINEAR_LE
two's complement signed linear encoding with little endian byte order
.It Dv AUDIO_ENCODING_SLINEAR_BE
two's complement signed linear encoding with big endian byte order
.It Dv AUDIO_ENCODING_ULINEAR_LE
unsigned linear encoding with little endian byte order
.It Dv AUDIO_ENCODING_ULINEAR_BE
unsigned linear encoding with big endian byte order
.El
.Pp
The
.Va precision
parameter describes the number of bits of audio data per sample.
The
.Va bps
parameter describes the number of bytes of audio data per sample.
The
.Va msb
parameter describes the alignment of the data in the sample.
It is only meaningful when
.Va precision
/ NBBY <
.Va bps .
A value of 1 means the data is aligned to the most significant bit.
.Pp
.Va block_size
is the block size in bytes, which determines the frequency at which
blocking
.Xr read 2 ,
.Xr write 2 ,
or
.Xr poll 2 ,
wake up.
The generic
.Nm audio
driver layer and the hardware driver have the
opportunity to adjust this block size to get it within
implementation-required limits.
Normally the
.Va block_size
is recalculated
when other parameters changes.
.Pp
It is recommended to set
.Va block_size
at the same time as, or after, all other parameters have been set.
.Pp
.Va pause
returns the current pause/unpause state for recording or playback.
For
.Dv AUDIO_SETINFO ,
if the pause value is specified it will either pause
or unpause the particular direction.
In full-duplex the pause values for both directions must
be equal.
.Pp
.It Dv AUDIO_SETPAR Fa "struct audio_swpar *"
.It Dv AUDIO_GETPAR Fa "struct audio_swpar *"
Set or get audio parameters as encoded in the
.Vt audio_swpar
structure.
.Bd -literal
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 */
};
.Ed
.Pp
When setting the device parameters with
.Dv AUDIO_SETPAR ,
the
.Vt audio_swpar
structure should first be initialized with
.Bd -literal
struct audio_swpar ap;
AUDIO_INITPAR(&ap);
.Ed
.Pp
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
.Dv AUDIO_GETINFO
to obtain the parameters in use.
.Pp
The parameters are as follows:
.Bl -tag -width "round"
.It Va bits
Number of bits per sample: must be between 1 and 32.
.It Va 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
.Va bits .
.It Va sig
If set (i.e. non-zero) then the samples are signed,
otherwise they are unsigned.
.It Va le
If set, then the byte order is little endian;
if not it is big endian;
it's meaningful only if
.Va bps
> 1.
.It Va msb
If set, then the
.Va 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
.Va bits
<
.Va bps
* 8.
.It Va rchan
The number of recorded channels; meaningful only if the
device is opened for reading.
.It Va pchan
The number of channels playing; meaningful only if
the device is opened for writing.
.It Va rate
The sampling frequency in Hz.
.It Va nblks
The number of blocks in the play buffer.
.It Va round
The audio block size.
.El
.Pp
.It Dv 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
.Xr write 2
syscall.
The buffer size is obtained by multiplying
the
.Va nblks ,
.Va round ,
and
.Va bps
parameters obtained with
.Dv AUDIO_GETPAR .
.Pp
.It Dv AUDIO_STOP
Stop playback and recording immediately.
.Pp
.It Dv AUDIO_GETPOS Fa "struct audio_pos *"
Fetch an atomic snapshot of device timing information in the
.Vt audio_pos
structure.
.Bd -literal
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 */
};
.Ed
.Pp
The properties have the following meaning:
.Bl -tag -width "play_xrun"
.It Va play_pos
Total number of bytes played by the device since playback started
(a.k.a the device wall clock).
.It Va play_xrun
The number of bytes corresponding to silence played because
.Xr write 2
wasn't called fast enough.
.It Va rec_pos
Total number of bytes recorded by the device since recording started
(a.k.a the device wall clock).
.It Va rec_xrun
The number of bytes dropped because
.Xr read 2
wasn't called fast enough.
.El
.El
.Sh MIXER DEVICE
The
.Nm mixer
device,
.Pa /dev/mixer ,
may be manipulated with
.Xr ioctl 2
but does not support
.Xr read 2
or
.Xr write 2 .
It supports the following
.Xr ioctl 2
commands:
.Pp
.Bl -tag -width Ds -compact
.It Dv AUDIO_GETDEV Fa "audio_device_t *"
This command is the same as described above for the sampling devices.
.Pp
.It Dv AUDIO_MIXER_READ Fa "mixer_ctrl_t *"
.It Dv AUDIO_MIXER_WRITE Fa "mixer_ctrl_t *"
These commands read the current mixer state or set new mixer state for
the specified device
.Va dev .
.Va type
identifies which type of value is supplied in the
.Vt mixer_ctrl_t *
argument.
.Bd -literal
#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
.Ed
.Pp
For a mixer value, the
.Va 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
.Va ord
field should be set to one of the possible values as returned by a prior
.Dv AUDIO_MIXER_DEVINFO
command.
The type
.Dv AUDIO_MIXER_CLASS
is only used for classifying particular
.Nm mixer
device types and is not used for
.Dv AUDIO_MIXER_READ
or
.Dv AUDIO_MIXER_WRITE .
.Pp
.It Dv AUDIO_MIXER_DEVINFO Fa "mixer_devinfo_t *"
This command is used iteratively to fetch audio
.Nm mixer
device information into the input/output
.Vt 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.
.Bd -literal
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;
.Ed
.Pp
The
.Va label
field identifies the name of this particular mixer control.
The
.Va index
field may be used as the
.Va dev
field in
.Dv AUDIO_MIXER_READ
and
.Dv AUDIO_MIXER_WRITE
commands.
The
.Va 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.
.Pp
The
.Va 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
.Pq arbitrary
value set by the hardware driver may be determined by examining the
.Va mixer_class
field of the class itself,
a mixer of type
.Dv AUDIO_MIXER_CLASS .
For example, a mixer level controlling the input gain on the
.Dq line in
circuit would have a
.Va mixer_class
that matches an input class device with the name
.Dq inputs
.Dv ( AudioCinputs )
and would have a
.Va label
of
.Dq line
.Dv ( 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.
.Pp
The
.Va next
and
.Va 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
.Dq next
value).
If there is no relevant next or previous value,
.Dv AUDIO_MIXER_LAST
is specified.
.Pp
For
.Dv 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
.Dv AudioNon
and
.Dv AudioNoff .
For the
.Dv AUDIO_MIXER_VALUE
and
.Dv AUDIO_MIXER_SET
mixer control types, the channel count is
returned; the units name specifies what the level controls (typical
values are
.Dv AudioNvolume ,
.Dv AudioNtreble ,
and
.Dv AudioNbass ) .
.\" For AUDIO_MIXER_SET mixer control types, what is what?
.El
.Pp
By convention, all the mixer devices can be distinguished from other
mixer controls because they use a name from one of the
.Dv AudioC*
string values.
.Sh FILES
.Bl -tag -width /dev/audioctl -compact
.It Pa /dev/audio
.It Pa /dev/audioctl
.It Pa /dev/sound
.It Pa /dev/mixer
.El
.Sh SEE ALSO
.Xr aucat 1 ,
.Xr audioctl 1 ,
.Xr cdio 1 ,
.Xr mixerctl 1 ,
.Xr ioctl 2 ,
.Xr sio_open 3 ,
.Xr ac97 4 ,
.Xr uaudio 4 ,
.Xr audio 9
.\" .Sh BUGS
|