xref: /illumos-gate/usr/src/man/man4i/audio.4i (revision 02b17e23cf5bf66a5ea787e066ae3d1aa49bd856)
1.\"  Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved.
2.\" Copyright 2018, Joyent, Inc.
3.\" The contents of this file are subject to the terms of the
4.\" Common Development and Distribution License (the "License").
5.\" You may not use this file except in compliance with the License.
6.\"
7.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
8.\" or http://www.opensolaris.org/os/licensing.
9.\" See the License for the specific language governing permissions
10.\" and limitations under the License.
11.\"
12.\" When distributing Covered Code, include this CDDL HEADER in each
13.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
14.\" If applicable, add the following below this CDDL HEADER, with the
15.\" fields enclosed by brackets "[]" replaced with your own identifying
16.\" information: Portions Copyright [yyyy] [name of copyright owner]
17.Dd July 8, 2018
18.Dt AUDIO 4I
19.Os
20.Sh NAME
21.Nm audio
22.Nd generic audio device interface
23.Sh SYNOPSIS
24.In sys/audio.h
25.Sh OVERVIEW
26An audio device is used to play and/or record a stream of audio data.
27Since a
28specific audio device may not support all functionality described below, refer
29to the device-specific manual pages for a complete description of each hardware
30device.
31An application can use the
32.Dv AUDIO_GETDEV
33.Xr ioctl 2
34to determine the current audio hardware associated with
35.Pa /dev/audio .
36.Pp
37The audio framework provides a software mixing engine (audio mixer) for all
38audio devices, allowing more than one process to play or record audio at the
39same time.
40.Ss "Backward Compatibility"
41It is no longer possible to disable the mixing function.
42Applications must not
43assume that they have exclusive access to the audio device.
44.Ss "Multi-Stream Codecs"
45The audio mixer supports multi-stream Codecs.
46These devices have DSP engines
47that provide sample rate conversion, hardware mixing, and other features.
48The use of such hardware features is opaque to applications.
49.Sh AUDIO FORMATS
50Digital audio data represents a quantized approximation of an analog audio
51signal waveform.
52In the simplest case, these quantized numbers represent the
53amplitude of the input waveform at particular sampling intervals.
54To achieve the best approximation of an input signal, the highest possible sampling
55frequency and precision should be used.
56However, increased accuracy comes at a cost of increased data storage requirements.
57For instance, one minute of
58monaural audio recorded in \(*m-Law format (pronounced
59.Em mew-law )
60at 8 KHz requires nearly 0.5 megabytes of storage, while the standard Compact Disc
61audio format (stereo 16-bit linear
62.Sy PCM
63data sampled at 44.1 KHz) requires approximately 10 megabytes per minute.
64.Pp
65Audio data may be represented in several different formats.
66An audio device's
67current audio data format can be determined by using the
68.Dv AUDIO_GETINFO
69.Xr ioctl 2
70described below.
71.Pp
72An audio data format is characterized in the audio driver by four parameters:
73Sample Rate, Encoding, Precision, and Channels.
74Refer to the device-specific
75manual pages for a list of the audio formats that each device supports.
76In addition to the formats that the audio device supports directly, other formats
77provide higher data compression.
78Applications may convert audio data to and
79from these formats when playing or recording.
80.Ss "Sample Rate"
81Sample rate is a number that represents the sampling frequency (in samples per
82second) of the audio data.
83.Pp
84The audio mixer always configures the hardware for the highest possible sample
85rate for both play and record.
86This ensures that none of the audio streams
87require compute-intensive low pass filtering.
88The result is that high sample rate audio streams are not degraded by filtering.
89.Pp
90Sample rate conversion can be a compute-intensive operation, depending on the
91number of channels and a device's sample rate.
92For example, an 8KHz signal can
93be easily converted to 48KHz, requiring a low cost up sampling by 6.
94However,
95converting from 44.1KHz to 48KHz is compute intensive because it must be up
96sampled by 160 and then down sampled by 147.
97This is only done using integer multipliers.
98.Pp
99Applications can greatly reduce the impact of sample rate conversion by
100carefully picking the sample rate.
101Applications should always use the highest
102sample rate the device supports.
103An application can also do its own sample rate
104conversion (to take advantage of floating point and accelerated instruction or
105use small integers for up and down sampling.
106.Pp
107All modern audio devices run at 48 kHz or a multiple thereof, hence just using
10848 kHz may be a reasonable compromise if the application is not prepared to
109select higher sample rates.
110.Ss "Encodings"
111An encoding parameter specifies the audio data representation.
112\(*m-Law
113encoding corresponds to
114.Em CCITT G.711 ,
115and is the standard for voice data
116used by telephone companies in the United States, Canada, and Japan.
117A-Law encoding is also part of
118.Em CCITT G.711
119and is the standard encoding for telephony elsewhere in the world.
120A-Law and \(*m-Law audio data are sampled at
121a rate of 8000 samples per second with 12-bit precision, with the data
122compressed to 8-bit samples.
123The resulting audio data quality is equivalent to
124that of standard analog telephone service.
125.Pp
126Linear Pulse Code Modulation (PCM) is an uncompressed, signed audio format in
127which sample values are directly proportional to audio signal voltages.
128Each sample is a 2's complement number that represents a positive or negative
129amplitude.
130.Ss "Precision"
131Precision indicates the number of bits used to store each audio sample.
132For instance, \(*m-Law and A-Law data are stored with 8-bit precision.
133.Sy PCM
134data may be stored at various precisions, though 16-bit is the most common.
135.Ss "Channels"
136Multiple channels of audio may be interleaved at sample boundaries.
137A sample frame consists of a single sample from each active channel.
138For example, a sample frame of stereo 16-bit
139.Sy PCM
140data consists of two 16-bit samples,
141corresponding to the left and right channel data.
142.Pp
143The audio mixer sets the hardware to the maximum number of channels supported.
144If a mono signal is played or recorded, it is mixed on the first two (usually
145the left and right) channels only.
146Silence is mixed on the remaining channels
147.Ss "Supported Formats"
148The audio mixer supports the following audio formats:
149.Bl -column "Signed Linear PCM" "Precision" "Mono or Stereo" -offset 2n
150.It Encoding Ta Precision Ta Channels
151.It "Signed Linear PCM" Ta "32-bit" Ta "Mono or Stereo"
152.It "Signed Linear PCM" Ta "16-bit" Ta "Mono or Stereo"
153.It "Signed Linear PCM" Ta "8-bit" Ta "Mono or Stereo"
154.It "\(*m-Law" Ta "8-bit" Ta "Mono or Stereo"
155.It "A-Law" Ta "8-bit" Ta "Mono or Stereo"
156.El
157.Pp
158The audio mixer converts all audio streams to 24-bit Linear PCM before mixing.
159After mixing, conversion is made to the best possible Codec format.
160The
161conversion process is not compute intensive and audio applications can choose
162the encoding format that best meets their needs.
163.Pp
164Note that the mixer discards the low order 8 bits of 32-bit Signed Linear PCM
165in order to perform mixing.
166(This is done to allow for possible overflows to
167fit into 32-bits when mixing multiple streams together.)
168Hence, the maximum effective precision is 24-bits.
169.Sh DESCRIPTION
170The device
171.Pa /dev/audio
172is a device driver that dispatches audio requests
173to the appropriate underlying audio hardware.
174The audio driver is implemented as a
175.Sy STREAMS
176driver.
177In order to record audio input, applications
178.Xr open 2
179the
180.Pa /dev/audio
181device and read data from it using the
182.Xr read 2
183system call.
184Similarly, sound data is queued to the audio output port by using the
185.Xr write 2
186system call.
187Device configuration is performed using the
188.Xr ioctl 2
189interface.
190.Pp
191Because some systems may contain more than one audio device, application
192writers are encouraged to query the
193.Ev AUDIODEV
194environment variable.
195If this variable is present in the environment, its value should identify the
196path name of the default audio device.
197.Ss "Opening the Audio Device"
198The audio device is not treated as an exclusive resource.
199Each process may open the audio device once.
200.Pp
201Each
202.Xr open 2
203completes as long as there are channels available to be allocated.
204If no channels are available to be allocated:
205.Bl -bullet -offset indent
206.It
207if either the
208.Dv O_NDELAY
209or
210.Dv O_NONBLOCK
211flags are set in the
212.Xr open 2
213.Fa oflag
214argument, then -1 is immediately returned, with
215.Va errno
216set to
217.Er EBUSY .
218.It
219if neither the
220.Dv O_NDELAY
221nor the
222.Dv O_NONBLOCK
223flag are set, then
224.Xr open 2
225hangs until the device is available or a signal is delivered to
226the process, in which case a -1 is returned with
227.Va errno
228set to
229.Er EINTR .
230.El
231.Pp
232Upon the initial
233.Xr open 2
234of the audio channel, the audio mixer sets the data
235format of the audio channel to the default state of 8-bit, 8Khz, mono \(*m-Law
236data.
237.Pp
238If the audio device does not support this configuration, it informs the
239audio mixer of the initial configuration.
240Audio applications should explicitly set the encoding characteristics to match the
241audio data requirements, and not depend on the default configuration.
242.Ss "Recording Audio Data"
243The
244.Xr read 2
245system call copies data from the system's buffers to the application.
246Ordinarily,
247.Xr read 2
248blocks until the user buffer is filled.
249The
250.Dv I_NREAD
251.Sy ioctl
252(see
253.Xr streamio 4I )
254may be used to determine the amount of data that may be read without blocking.
255The device may alternatively be set to a non-blocking mode, in which case
256.Xr read 2
257completes immediately, but may return fewer bytes than requested.
258Refer to the
259.Xr read 2
260manual page for a complete description of this behavior.
261.Pp
262When the audio device is opened with read access, the device driver immediately
263starts buffering audio input data.
264Since this consumes system resources,
265processes that do not record audio data should open the device write-only
266.Pq Dv O_WRONLY .
267.Pp
268The transfer of input data to
269.Sy STREAMS
270buffers may be paused (or resumed) by using the
271.Dv AUDIO_SETINFO
272.Sy ioctl
273to set (or clear) the
274.Fa record.pause
275flag in the audio information structure (see below).
276All unread input data in the
277.Sy STREAMS
278queue may be discarded by using the
279.Dv I_FLUSH
280.Sy STREAMS
281ioctl.
282See
283.Xr streamio 4I .
284When changing record parameters, the input stream should be paused and flushed
285before the change, and resumed afterward.
286Otherwise, subsequent reads may return samples
287in the old format followed by samples in the new format.
288This is particularly important when new parameters result in a changed sample size.
289.Pp
290Input data can accumulate in
291.Sy STREAMS
292buffers very quickly.
293At a minimum,
294it will accumulate at 8000 bytes per second for 8-bit, 8 KHz, mono, \(*m-Law data.
295If the device is configured for 16-bit linear or higher sample rates, it will
296accumulate even faster.
297If the application that consumes the data cannot keep
298up with this data rate, the
299.Sy STREAMS
300queue may become full.
301When this occurs, the
302.Fa record.error
303flag is set in the audio information structure and input sampling ceases until
304there is room in the input queue for additional data.
305In such cases, the input data stream contains a discontinuity.
306For this reason, audio recording applications should open the audio device when
307they are prepared to begin reading data, rather than at the start of extensive
308initialization.
309.Ss "Playing Audio Data"
310The
311.Xr write 2
312system call copies data from an application's buffer to the
313.Sy STREAMS
314output queue.
315Ordinarily,
316.Xr write 2
317blocks until the entire user buffer is transferred.
318The device may alternatively be set to a non-blocking mode, in which case
319.Xr write 2
320completes immediately, but may have transferred fewer bytes than requested.
321See
322.Xr write 2 .
323.Pp
324Although
325.Xr write 2
326returns when the data is successfully queued, the actual
327completion of audio output may take considerably longer.
328The
329.Dv AUDIO_DRAIN
330ioctl may be issued to allow an application to block until all of the
331queued output data has been played.
332Alternatively, a process may request
333asynchronous notification of output completion by writing a zero-length buffer
334(end-of-file record) to the output stream.
335When such a buffer has been
336processed, the
337.Fa play.eof
338flag in the audio information structure is incremented.
339.Pp
340The final
341.Xr close 2
342of the file descriptor hangs until all of the audio
343output has drained.
344If a signal interrupts the
345.Xr close 2 ,
346or if the process exits without closing the device, any remaining data queued
347for audio output is flushed and the device is closed immediately.
348.Pp
349The consumption of output data may be paused (or resumed) by using the
350.Dv AUDIO_SETINFO
351ioctl to set (or clear) the
352.Fa play.pause
353flag in the audio information structure.
354Queued output data may be discarded by using
355the
356.Dv I_FLUSH
357.Sy STREAMS
358ioctl.
359(See
360.Xr streamio 4I ) .
361.Pp
362Output data is played from the
363.Sy STREAMS
364buffers at a default rate of at
365least 8000 bytes per second for \(*m-Law, A-Law or 8-bit PCM data (faster for
36616-bit linear data or higher sampling rates).
367If the output queue becomes
368empty, the
369.Fa play.error
370flag is set in the audio information structure and
371output is stopped until additional data is written.
372If an application attempts
373to write a number of bytes that is not a multiple of the current sample frame
374size, an error is generated and the bad data is thrown away.
375Additional writes are allowed.
376.Ss "Asynchronous I/O"
377The
378.Dv I_SETSIG
379.Sy STREAMS
380ioctl
381enables asynchronous notification, through the
382.Dv SIGPOLL
383signal, of input and output ready condition changes.
384The
385.Dv O_NONBLOCK
386flag may be set using the
387.Dv F_SETFL
388.Xr fcntl 2
389to
390enable non-blocking
391.Xr read 2
392and
393.Xr write 2
394requests.
395This is normally
396sufficient for applications to maintain an audio stream in the background.
397.Ss "Audio Control Pseudo-Device"
398It is sometimes convenient to have an application, such as a volume control
399panel, modify certain characteristics of the audio device while it is being
400used by an unrelated process.
401.Pp
402The
403.Pa /dev/audioctl
404pseudo-device is provided for this purpose.
405Any number of processes may open
406.Pa /dev/audioctl
407simultaneously.
408However,
409.Xr read 2
410and
411.Xr write 2
412system calls are ignored by
413.Pa /dev/audioctl .
414The
415.Dv AUDIO_GETINFO
416and
417.Dv AUDIO_SETINFO
418ioctl commands may be issued to
419.Pa /dev/audioctl
420to determine the status or alter the behavior of
421.Pa /dev/audio .
422Note: In general, the audio control device name is
423constructed by appending the letters
424.Qq Sy ctl
425to the path name of the audio device.
426.Ss "Audio Status Change Notification"
427Applications that open the audio control pseudo-device may request asynchronous
428notification of changes in the state of the audio device by setting the
429.Dv S_MSG
430flag in an
431.Dv I_SETSIG
432.Sy STREAMS
433ioctl.
434Such processes receive a
435.Dv SIGPOLL
436signal when any of the following events occur:
437.Bl -bullet -offset indent
438.It
439An
440.Dv AUDIO_SETINFO
441ioctl has altered the device state.
442.It
443An input overflow or output underflow has occurred.
444.It
445An end-of-file record (zero-length buffer) has been processed on output.
446.It
447An
448.Xr open 2
449or
450.Xr close 2
451of
452.Pa /dev/audio
453has altered the device state.
454.It
455An external event (such as speakerbox's volume control) has altered the device
456state.
457.El
458.Sh IOCTLS
459.Ss "Audio Information Structure"
460The state of the audio device may be polled or modified using the
461.Dv AUDIO_GETINFO
462and
463.Dv AUDIO_SETINFO
464ioctl commands.
465These commands operate on the
466.Vt audio_info
467structure as defined, in
468.In sys/audio.h ,
469as follows:
470.Bd -literal -offset 2n
471/*
472 * This structure contains state information for audio device
473 * IO streams
474 */
475
476struct audio_prinfo {
477     /*
478      * The following values describe the
479      * audio data encoding
480      */
481      uint_t sample_rate; /* samples per second */
482      uint_t channels;    /* number of interleaved channels */
483      uint_t precision;   /* number of bits per sample */
484      uint_t encoding;    /* data encoding method */
485
486      /*
487       * The following values control audio device
488       * configuration
489       */
490      uint_t gain; /* volume level */
491      uint_t port; /* selected I/O port */
492      uint_t buffer_size; /* I/O buffer size */
493
494     /*
495      * The following values describe the current device
496      * state
497      */
498     uint_t samples; /* number of samples converted */
499     uint_t eof;     /* End Of File counter (play only) */
500     uchar_t pause;  /* non-zero if paused, zero to resume */
501     uchar_t error;  /* non-zero if overflow/underflow */
502     uchar_t waiting; /* non-zero if a process wants access */
503     uchar_t balance; /* stereo channel balance */
504
505     /*
506      * The following values are read-only device state
507      * information
508      */
509      uchar_t open;   /* non-zero if open access granted */
510      uchar_t active; /* non-zero if I/O active */
511      uint_t avail_ports; /* available I/O ports */
512      uint_t mod_ports; /* modifiable I/O ports */
513};
514typedef struct audio_prinfo audio_prinfo_t;
515
516/*
517 * This structure is used in AUDIO_GETINFO and AUDIO_SETINFO ioctl
518 * commands
519 */
520struct audio_info {
521    audio_prinfo_t record;/* input status info */
522    audio_prinfo_t play; /* output status info */
523    uint_t monitor_gain; /* input to output mix */
524    uchar_toutput_muted; /* non-zero if output muted */
525    uint_t hw_features;  /* supported H/W features */
526    uint_t sw_features;  /* supported S/W features */
527    uint_t sw_features_enabled;
528                /* supported S/W features enabled */
529};
530typedef struct audio_info audio_info_t;
531
532/* Audio encoding types */
533#define AUDIO_ENCODING_ULAW (1) /* u-Law encoding */
534#define AUDIO_ENCODING_ALAW (2) /* A-Law encoding */
535#define AUDIO_ENCODING_LINEAR (3) /* Signed Linear PCM encoding */
536
537/*
538 * These ranges apply to record, play, and
539 * monitor gain values
540 */
541#define AUDIO_MIN_GAIN (0)/* minimum gain value */
542#define AUDIO_MAX_GAIN (255) /* maximum gain value */
543
544/*
545 * These values apply to the balance field to adjust channel
546 * gain values
547 */
548#define AUDIO_LEFT_BALANCE   (0) /* left channel only */
549#define AUDIO_MID_BALANCE   (32) /* equal left/right balance */
550#define AUDIO_RIGHT_BALANCE (64) /* right channel only */
551
552/*
553 * Define some convenient audio port names
554 * (for port, avail_ports and mod_ports)
555 */
556
557/* output ports (several might be enabled at once) */
558#define AUDIO_SPEAKER   (0x01) /* built-in speaker */
559#define AUDIO_HEADPHONE (0x02) /* headphone jack */
560#define AUDIO_LINE_OUT  (0x04) /* line out */
561#define AUDIO_SPDIF_OUT (0x08) /* SPDIF port */
562#define AUDIO_AUX1_OUT  (0x10) /* aux1 out */
563#define AUDIO_AUX2_OUT  (0x20) /* aux2 out */
564
565/*
566 * input ports (usually only one may be enabled at a time)
567 */
568#define AUDIO_MICROPHONE (0x01) /* microphone */
569#define AUDIO_LINE_IN  (0x02) /* line in */
570#define AUDIO_CD       (0x04) /* on-board CD inputs */
571#define AUDIO_SPDIF_IN (0x08) /* SPDIF port */
572#define AUDIO_AUX1_IN  (0x10) /* aux1 in */
573#define AUDIO_AUX2_IN  (0x20) /* aux2 in */
574#define AUDIO_CODEC_LOOPB_IN (0x40) /* Codec inter. loopback */
575
576/* These defines are for hardware features */
577#define AUDIO_HWFEATURE_DUPLEX (0x00000001u)
578/* simult. play & cap. supported */
579
580#define AUDIO_HWFEATURE_MSCODEC (0x00000002u)
581/* multi-stream Codec */
582
583/* These defines are for software features *
584#define AUDIO_SWFEATURE_MIXER (0x00000001u)
585/* audio mixer audio pers. mod. */
586
587/*
588 * Parameter for the AUDIO_GETDEV ioctl
589 * to determine current audio devices
590 */
591#define MAX_AUDIO_DEV_LEN (16)
592struct audio_device {
593    char name[MAX_AUDIO_DEV_LEN];
594    char version[MAX_AUDIO_DEV_LEN];
595    char config[MAX_AUDIO_DEV_LEN];
596};
597typedef struct audio_device audio_device_t;
598.Ed
599.Pp
600The
601.Fa play.gain
602and
603.Fa record.gain
604fields specify the output and input volume levels.
605A value of
606.Dv AUDIO_MAX_GAIN
607indicates maximum volume.
608Audio output may also be temporarily muted by setting a non-zero value in the
609.Fa output_muted
610field.
611Clearing this field restores audio output to the normal state.
612.Pp
613The
614.Va monitor_gain
615field is present for compatibility, and is no longer supported.
616See
617.Xr dsp 4I
618for more detail.
619.Pp
620Likewise, the
621.Fa play.port ,
622.Fa play.ports ,
623.Fa play.mod_ports ,
624.Fa record.port ,
625.Fa record.ports ,
626and
627.Fa record.mod_ports
628are no longer supported.
629See
630.Xr dsp 4I
631for more detail.
632.Pp
633The
634.Fa play.balance
635and
636.Fa record.balance
637fields are fixed to
638.Dv AUDIO_MID_BALANCE .
639Changes to volume levels for different channels can be
640made using the interfaces in
641.Xr dsp 4I .
642.Pp
643The
644.Fa play.pause
645and
646.Fa record.pause
647flags may be used to pause and
648resume the transfer of data between the audio device and the
649.Sy STREAMS
650buffers.
651The
652.Fa play.error
653and
654.Fa record.error
655flags indicate that data underflow or overflow has occurred.
656The
657.Fa play.active
658and
659.Fa record.active
660flags indicate that data transfer is currently active in the corresponding
661direction.
662.Pp
663The
664.Fa play.open
665and
666.Fa record.open
667flags indicate that the device is
668currently open with the corresponding access permission.
669The
670.Fa play.waiting
671and
672.Fa record.waiting
673flags provide an indication that a process may be waiting to access the device.
674These flags are set automatically when a process blocks on
675.Xr open 2 ,
676though they may also be set using the
677.Dv AUDIO_SETINFO
678ioctl command.
679They are cleared only when a process relinquishes access by closing the device.
680.Pp
681The
682.Fa play.samples
683and
684.Fa record.samples
685fields are zeroed at
686.Xr open 2
687and are incremented each time a data sample is copied to or from
688the associated
689.Sy STREAMS
690queue.
691Some audio drivers may be limited to counting
692buffers of samples, instead of single samples for their samples accounting.
693For this reason, applications should not assume that the samples fields contain a
694perfectly accurate count.
695The
696.Fa play.eof
697field increments whenever a zero-length output buffer is synchronously processed.
698Applications may use this
699field to detect the completion of particular segments of audio output.
700.Pp
701The
702.Fa record.buffer_size
703field controls the amount of input data that is
704buffered in the device driver during record operations.
705Applications that have
706particular requirements for low latency should set the value appropriately.
707Note however that smaller input buffer sizes may result in higher system
708overhead.
709The value of this field is specified in bytes and drivers will
710constrain it to be a multiple of the current sample frame size.
711Some drivers may place other requirements on the value of this field.
712Refer to the audio device-specific manual page for more details.
713If an application changes the
714format of the audio device and does not modify the
715.Fa record.buffer_size
716field, the device driver may use a default value to compensate for the new data
717rate.
718Therefore, if an application is going to modify this field, it should
719modify it during or after the format change itself, not before.
720When changing
721the
722.Fa record.buffer_size
723parameters, the input stream should be paused and
724flushed before the change, and resumed afterward.
725Otherwise, subsequent reads
726may return samples in the old format followed by samples in the new format.
727This is particularly important when new parameters result in a changed sample
728size.
729If you change the
730.Fa record.buffer_size
731for the first packet, this protocol must be followed or the first buffer will
732be the default buffer size for the device, followed by packets of the requested
733change size.
734.Pp
735The
736.Fa record.buffer_size
737field may be modified only on the
738.Pa /dev/audio
739device by processes that have it opened for reading.
740.Pp
741The
742.Fa play.buffer_size
743field is currently not supported.
744.Pp
745The audio data format is indicated by the
746.Fa sample_rate ,
747.Fa channels ,
748.Fa precision ,
749and
750.Fa encoding
751fields.
752The values of these fields correspond to the
753descriptions in the
754.Sx "AUDIO FORMATS"
755section of this man page.
756Refer to the
757audio device-specific manual pages for a list of supported data format
758combinations.
759.Pp
760The data format fields can be modified only on the
761.Pa /dev/audio
762device.
763.Pp
764If the parameter changes requested by an
765.Dv AUDIO_SETINFO
766ioctl cannot all be accommodated,
767.Xr ioctl 2
768returns with
769.Va errno
770set to
771.Er EINVAL
772and no changes are made to the device state.
773.Ss "Streamio IOCTLS"
774All of the
775.Xr streamio 4I
776.Xr ioctl 2
777commands may be issued for the
778.Pa /dev/audio
779device.
780Because the
781.Pa /dev/audioctl
782device has its own
783.Sy STREAMS
784queues, most of these commands neither modify nor report the
785state of
786.Pa /dev/audio
787if issued for the
788.Pa /dev/audioctl
789device.
790The
791.Dv I_SETSIG
792ioctl may be issued for
793.Pa /dev/audioctl
794to enable the notification of audio status changes, as described above.
795.Ss "Audio IOCTLS"
796The audio device additionally supports the following
797.Xr ioctl 2
798commands:
799.Bl -tag -width "AUDIO_GETINFO"
800.It Dv AUDIO_DRAIN
801The argument is ignored.
802This command suspends the calling process until the
803output STREAMS queue is empty and all queued samples have been played, or until
804a signal is delivered to the calling process.
805It may not be issued for the
806.Pa /dev/audioctl
807device.
808An implicit
809.Dv AUDIO_DRAIN
810is performed on the final
811.Xr close 2
812of
813.Pa /dev/audio .
814.It Dv AUDIO_GETDEV
815The argument is a pointer to an
816.Vt audio_device_t
817structure.
818This command may be issued for either
819.Pa /dev/audio
820or
821.Pa /dev/audioctl .
822The returned
823value in the name field will be a string that will identify the current
824.Pa /dev/audio
825hardware device, the value in version will be a string
826indicating the current version of the hardware, and
827.Fa config
828will be a device-specific string identifying the properties of the audio stream
829associated with that file descriptor.
830Refer to the audio device-specific manual
831pages to determine the actual strings returned by the device driver.
832.It Dv AUDIO_GETINFO
833The argument is a pointer to an
834.Vt audio_info_t
835structure.
836This command may be issued for either
837.Pa /dev/audio
838or
839.Pa /dev/audioctl .
840The current state of the
841.Pa /dev/audio
842device is returned in the structure.
843Values return pertain to a logical view of the device as seen by and private to
844the process, and do not necessarily reflect the actual hardware device itself.
845.It Dv AUDIO_SETINFO
846The argument is a pointer to an
847.Vt audio_info_t
848structure.
849This command may be issued for either the
850.Pa /dev/audio
851or the
852.Pa /dev/audioctl
853device with some restrictions.
854This command configures the audio device according to
855the supplied structure and overwrites the existing structure with the new state
856of the device.
857Note: The
858.Fa play.samples ,
859.Fa record.samples ,
860.Fa play.error ,
861.Fa record.error ,
862and
863.Fa play.eof
864fields are modified to reflect the state of the device when the
865.Dv AUDIO_SETINFO
866is issued.
867This allows programs to automatically modify these fields while retrieving the
868previous value.
869.Pp
870As with
871.Dv AUDIO_SETINFO ,
872the settings managed by this ioctl deal with a
873logical view of the device which is private to the process, and don't
874necessarily have any impact on the hardware device itself.
875.El
876.Pp
877Certain fields in the audio information structure, such as the pause flags, are
878treated as read-only when
879.Pa /dev/audio
880is not open with the corresponding access permission.
881Other fields, such as the gain levels and encoding
882information, may have a restricted set of acceptable values.
883Applications that
884attempt to modify such fields should check the returned values to be sure that
885the corresponding change took effect.
886The
887.Fa sample_rate ,
888.Fa channels ,
889.Fa precision ,
890and
891.Fa encoding
892fields treated as read-only for
893.Pa /dev/audioctl ,
894so that applications can be guaranteed that the existing
895audio format will stay in place until they relinquish the audio device.
896.Dv AUDIO_SETINFO
897will return
898.Er EINVAL
899when the desired configuration is not possible, or
900.Er EBUSY
901when another process has control of the audio device.
902.Pp
903All of the logical device state is reset when the corresponding I/O stream of
904.Pa /dev/audio
905is closed.
906.Pp
907The
908.Vt audio_info_t
909structure may be initialized through the use of the
910.Dv AUDIO_INITINFO
911macro.
912This macro sets all fields in the structure to
913values that are ignored by the
914.Dv AUDIO_SETINFO
915command.
916For instance, the following code switches the output port from the built-in
917speaker to the headphone jack without modifying any other audio parameters:
918.Bd -literal -offset 2n
919audio_info_t info;
920AUDIO_INITINFO();
921info.play.port = AUDIO_HEADPHONE;
922err = ioctl(audio_fd, AUDIO_SETINFO, );
923.Ed
924.Pp
925This technique eliminates problems associated with using a sequence of
926.Dv AUDIO_GETINFO
927followed by
928.Dv AUDIO_SETINFO .
929.Sh FILES
930The physical audio device names are system dependent and are rarely used by
931programmers.
932Programmers should use the following generic device names:
933.Bl -tag -width "/usr/share/audio/samples"
934.It Pa /dev/audio
935Symbolic link to the system's primary audio device
936.It Pa /dev/audioctl
937Symbolic link to the control device for
938.Pa /dev/audio
939.It Pa /dev/sound/0
940First audio device in the system
941.It Pa /dev/sound/0ctl
942Audio control device for
943.Pa /dev/sound/0
944.It Pa /usr/share/audio/samples
945Audio files
946.El
947.Sh ERRORS
948An
949.Xr open 2
950call will fail if:
951.Bl -tag -width "EINTR"
952.It Er EBUSY
953The requested play or record access is busy and either the
954.Dv O_NDELAY
955or
956.Dv O_NONBLOCK
957flag was set in the
958.Xr open 2
959request.
960.It Er EINTR
961The requested play or record access is busy and a signal interrupted the
962.Xr open 2
963request.
964.El
965.Pp
966An
967.Xr ioctl 2
968call will fail if:
969.Bl -tag -width "EINVAL"
970.It Er EINVAL
971The parameter changes requested in the
972.Dv AUDIO_SETINFO
973ioctl are invalid or are not supported by the device.
974.El
975.Sh ARCHITECTURE
976SPARC
977X86
978.Sh INTERFACE STABILITY
979Obsolete Uncommitted
980.Sh SEE ALSO
981.Xr close 2 ,
982.Xr fcntl 2 ,
983.Xr ioctl 2 ,
984.Xr open 2 ,
985.Xr poll 2 ,
986.Xr read 2 ,
987.Xr write 2 ,
988.Xr dsp 4I ,
989.Xr streamio 4I ,
990.Xr attributes 7
991.Sh BUGS
992Due to a feature of the
993.Sy STREAMS
994implementation, programs that are terminated or
995exit without closing the audio device may hang for a short period while audio
996output drains.
997In general, programs that produce audio output should catch the
998.Dv SIGINT
999signal and flush the output stream before exiting.
1000