2 .\" Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
4 .\" See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the
5 .\" fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH DSP 7I "May 11, 2009"
8 dsp \- generic audio device interface
12 \fB#include\fR \fB<sys/soundcard.h>\fR
18 To record audio input, applications \fBopen()\fR the appropriate device and
19 read data from it using the \fBread()\fR system call. Similarly, sound data is
20 queued to the audio output port by using the \fBwrite\fR(2) system call. Device
21 configuration is performed using the \fBioctl\fR(2) interface.
24 Because some systems can contain more than one audio device, application
25 writers are encouraged to open the \fB/dev/mixer\fR device and determine the
26 physical devices present on the system using the \fBSNDCTL_SYSINFO\fR and
27 \fBSNDCTL_AUDIOINFO\fR ioctls. See \fBmixer\fR(7I). The user should be provided
28 a the ability to select a different audio device, or alternatively, an
29 environment variable such as \fBAUDIODSP\fR can be used. In the absence of any
30 specific configuration from the user, the generic device file, \fB/dev/dsp\fR,
31 can be used. This normally points to a reasonably appropriate default audio
32 device for the system.
33 .SS "Opening the Audio Device"
36 The audio device is not treated as an exclusive resource.
39 Each \fBopen()\fR completes as long as there are channels available to be
40 allocated. If no channels are available to be allocated, the call returns
41 \fB-1\fR with the \fBerrno\fR set to \fBEBUSY\fR.
44 Audio applications should explicitly set the encoding characteristics to match
45 the audio data requirements after opening the device, and not depend on any
46 default configuration.
47 .SS "Recording Audio Data"
50 The \fBread()\fR system call copies data from the system's buffers to the
51 application. Ordinarily, \fBread()\fR blocks until the user buffer is filled.
52 The \fBpoll\fR(2) system call can be used to determine the presence of data
53 that can be read without blocking. The device can alternatively be set to a
54 non-blocking mode, in which case \fBread()\fR completes immediately, but can
55 return fewer bytes than requested. Refer to the \fBread\fR(2) manual page for a
56 complete description of this behavior.
59 When the audio device is opened with read access, the device driver allocates
60 resources for recording. Since this consumes system resources, processes that
61 do not record audio data should open the device write-only (\fBO_WRONLY\fR).
64 The recording process can be stopped by using the \fBSNDCTL_DSP_HALT_INPUT\fR
65 ioctl, which also discards all pending record data in underlying device FIFOs.
68 Before changing record parameters, the input should be stopped using the
69 \fBSNDCTL_DSP_HALT_INPUT\fR ioctl, which also flushes the any underlying device
70 input FIFOs. (This is not necessary if the process never started recording by
71 calling \fBread\fR(2). Otherwise, subsequent reads can return samples in the
72 old format followed by samples in the new format. This is particularly
73 important when new parameters result in a changed sample size.
76 Input data can accumulate in device buffers very quickly. At a minimum, it
77 accumulates at 8000 bytes per second for 8-bit, 8 KHz, mono, \fBu-Law\fR data.
78 If the device is configured for more channels, higher sample resolution, or
79 higher sample rates, it accumulates even faster. If the application that
80 consumes the data cannot keep up with this data rate, the underlying FIFOs can
81 become full. When this occurs, any new incoming data is lost until the
82 application makes room available by consuming data. Additionally, a record
83 overrun is noted, which can be retrieved using the \fBSNDCTL_DSP_GETERROR\fR
87 Record volume for a stream can be adjusted by issuing the
88 \fBSNDCTL_DSP_SETRECVOL\fR ioctl. The volume can also be retrieved using the
89 \fBSNDCTL_DSP_GETRECVOL\fR.
90 .SS "Playing Audio Data"
93 The \fBwrite()\fR system call copies data from an application's buffer to the
94 device output FIFO. Ordinarily, \fBwrite()\fR blocks until the entire user
95 buffer is transferred. The device can alternatively be set to a non-blocking
96 mode, in which case \fBwrite()\fRcompletes immediately, but might have
97 transferred fewer bytes than requested. See \fBwrite\fR(2).
100 Although \fBwrite()\fR returns when the data is successfully queued, the actual
101 completion of audio output might take considerably longer. The
102 \fBSNDCTL_DSP_SYNC\fR ioctl can be issued to allow an application to block
103 until all of the queued output data has been played.
106 The final \fBclose\fR(2) of the file descriptor waits until all of the audio
107 output has drained. If a signal interrupts the \fBclose()\fR, or if the process
108 exits without closing the device, any remaining data queued for audio output is
109 flushed and the device is closed immediately.
112 The output of playback data can be halted entirely, by calling the
113 \fBSNDCTL_DSP_HALT_OUTPUT\fR ioctl. This also discards any data that is queued
114 for playback in device FIFOs.
117 Before changing playback parameters, the output should be drained using the
118 \fBSNDCTL_DSP_SYNC\fR ioctl, and then stopped using the
119 \fBSNDCTL_DSP_HALT_OUTPUT\fR ioctl, which also flushes the any underlying
120 device output FIFOs. This is not necessary if the process never started
121 playback, such as by calling \fBwrite\fR(2). This is particularly important
122 when new parameters result in a changed sample size.
125 Output data is played from the playback buffers at a default rate of at least
126 8000 bytes per second for u-Law, A-Law or 8-bit PCM data (faster for 16-bit
127 linear data or higher sampling rates). If the output FIFO becomes empty, the
128 framework plays silence, resulting in audible stall or click in the output,
129 until more data is supplied by the application. The condition is also noted as
130 a play underrun, which can be determined using the \fBSNDCTL_DSP_GETERROR\fR
134 Playback volume for a stream can be adjusted by issuing the
135 \fBSNDCTL_DSP_SETPLAYVOL\fR ioctl. The volume can also be retrieved using the
136 \fBSNDCTL_DSP_GETPLAYVOL\fR.
137 .SS "Asynchronous I/O"
140 The \fBO_NONBLOCK\fR flag can be set using the \fBF_SETFL\fR \fBfcntl\fR(2) to
141 enable non-blocking \fBread()\fR and \fBwrite()\fR requests. This is normally
142 sufficient for applications to maintain an audio stream in the background.
145 It is also possible to determine the amount of data that can be transferred for
146 playback or recording without blocking using the \fBSNDCTL_DSP_GETOSPACE\fR or
147 \fBSNDCTL_DSP_GETISPACE\fR ioctls, respectively.
148 .SS "Mixer Pseudo-Device"
151 The \fB/dev/mixer\fR provides access to global hardware settings such as master
152 volume settings, etc. It is also the interface used for determining the
153 hardware configuration on the system.
156 Applications should \fBopen\fR(2) \fB/dev/mixer\fR, and use the
157 \fBSNDCTL_SYSINFO\fR and \fBSNDCTL_AUDIOINFO\fR ioctls to determine the device
158 node names of audio devices on the system. See \fBmixer\fR(7I) for additional
161 .SS "Information IOCTLs"
164 The following ioctls are supported on the audio device, as well as the mixer
165 device. See \fBmixer\fR(7I) for details.
180 The \fBdsp\fR device supports the following ioctl commands:
184 \fB\fBSNDCTL_DSP_SYNC\fR\fR
187 The argument is ignored. This command suspends the calling process until the
188 output FIFOs are empty and all queued samples have been played, or until a
189 signal is delivered to the calling process. An implicit \fBSNDCTL_DSP_SYNC\fR
190 is performed on the final \fBclose()\fR of the \fBdsp\fR device.
192 This ioctl should not be used unnecessarily, as if it is used in the middle of
193 playback it causes a small click or pause, as the FIFOs are drained. The
194 correct use of this ioctl is just before changing sample formats.
200 \fB\fBSNDCTL_DSP_HALT\fR\fR
204 \fB\fBSNDCTL_DSP_HALT_INPUT\fR\fR
208 \fB\fBSNDCTL_DSP_HALT_OUTPUT\fR\fR
211 The argument is ignored. All input or output (or both) associated with the file
212 is halted, and any pending data is discarded.
218 \fB\fBSNDCTL_DSP_SPEED\fR\fR
221 The argument is a pointer to an integer, indicating the sample rate (in Hz) to
222 be used. The rate applies to both input and output for the file descriptor. On
223 return the actual rate, which can differ from that requested, is stored in the
224 integer pointed to by the argument. To query the configured speed without
225 changing it the value 0 can be used by the application
231 \fB\fBSNDCTL_DSP_GETFMTS\fR\fR
234 The argument is a pointer to an integer, which receives a bit mask of encodings
235 supported by the device. Possible values are
239 AFMT_MU_LAW 8-bit unsigned u-Law
240 AFMT_A_LAW 8-bit unsigned a-Law
241 AFMT_U8 8-bit unsigned linear PCM
242 AFMT_S16_LE 16-bit signed
243 little-endian linear PCM
244 AFMT_S16_BE 16-bit signed
245 big-endian linear PCM
246 AFMT_S16_NE 16-bit signed native-endian
248 AFMT_U16_LE 16-bit unsigned
249 little-endian linear PCM
250 AFMT_U16_BE 16-bit unsigned big-endian
252 AFMT_U16_NE 16-bit unsigned big-endian
254 AFMT_S24_LE 24-bit signed little-endian
255 linear PCM, 32-bit aligned
256 AFMT_S24_BE 24-bit signed big-endian
257 linear PCM, 32-bit aligned
258 AFMT_S24_NE 24-bit signed native-endian
259 linear PCM, 32-bit aligned
260 AFMT_S32_LE 32-bit signed little-endian
262 AFMT_S32_BE 32-bit signed big-endian
264 AFMT_S32_NE 32-bit signed native-endian
266 AFMT_S24_PACKED 24-bit signed little-endian
272 Not all devices support all of these encodings. This implementation uses
273 \fBAFMT_S24_LE\fR or \fBAFMT_S24_BE\fR, whichever is native, internally.
279 \fB\fBSNDCTL_DSP_SETFMT\fR\fR
282 The argument is a pointer to an integer, which indicates the encoding to be
283 used. The same values as for \fBSNDCTL_DSP_GETFMT\fR can be used, but the
284 caller can only specify a single option. The encoding is used for both input
285 and output performed on the file descriptor.
291 \fB\fBSNDCTL_DSP_CHANNELS\fR\fR
294 The argument is a pointer to an integer, indicating the number of channels to
295 be used (1 for mono, 2 for stereo, etc.) The value applies to both input and
296 output for the file descriptor. On return the actual channel configuration
297 (which can differ from that requested) is stored in the integer pointed to by
298 the argument. To query the configured channels without changing it the value 0
299 can be used by the application.
305 \fB\fBSNDDCTL_DSP_GETCAPS\fR\fR
308 The argument is a pointer to an integer bit mask, which indicates the
309 capabilities of the device. The bits returned can include
313 PCM_CAP_OUTPUT Device supports playback
314 PCM_CAP_INPUT Device supports recording
315 PCM_CAP_DUPLEX Device supports simultaneous
316 playback and recording
326 \fB\fBSNDCTL_DSP_GETPLAYVOL\fR\fR
330 \fB\fBSNDCTL_DSP_GETRECVOL\fR\fR
333 The argument is a pointer to an integer to receive the volume level for either
334 playback or record. The value is encoded as a stereo value with the values for
335 two channels in the least significant two bytes. The value for each channel
336 thus has a range of 0-100. In this implementation, only the low order byte is
337 used, as the value is treated as a monophonic value, but a stereo value (with
338 both channel levels being identical) is returned for compatibility.
344 \fB\fBSNDCTL_DSP_SETPLAYVOL\fR\fR
348 \fB\fBSNDCTL_DSP_SETRECVOL\fR\fR
351 The argument is a pointer to an integer indicating volume level for either
352 playback or record. The value is encoded as a stereo value with the values for
353 two channels in the least significant two bytes. The value for each channel has
354 a range of 0-100. Note that in this implementation, only the low order byte is
355 used, as the value is treated as a monophonic value. Portable applications
356 should assign the same value to both bytes
362 \fB\fBSNDCTL_DSP_GETISPACE\fR\fR
366 \fB\fBSNDCTL_DSP_GETOSPACE\fR\fR
369 The argument is a pointer to a \fBstruct audio_buf_info\fR, which has the
374 typedef struct audio_buf_info {
375 int fragments;* /# of available fragments */
377 /* Total # of fragments allocated */
379 /* Size of a fragment in bytes */
381 /* Available space in bytes */
382 /* Note! 'bytes' could be more than
383 fragments*fragsize */
389 The fields fragments, \fBfragstotal\fR, and \fBfragsize\fR are intended for use
390 with compatible applications (and in the future with \fBmmap\fR(2)) only, and
391 need not be used by typical applications. On successful return the bytes member
392 contains the number of bytes that can be transferred without blocking.
398 \fB\fBSNDCTL_DSP_CURRENT_IPTR\fR\fR
402 \fB\fBSNDCTL_DSP_CURRENT_OPTR\fR\fR
405 The argument is a pointer to an \fBoss_count_t\fR, which has the following
412 /* Total # of samples */
414 /* Samples in device FIFO */
415 int filler[32];/* For future use */
421 The \fBsamples\fR field contains the total number of samples transferred by the
422 device so far. The \fBfifo_samples\fR is the depth of any hardware FIFO. This
423 structure can be useful for accurate stream positioning and latency
430 \fB\fBSNDCTL_DSP_GETIPTR\fR\fR
434 \fB\fBSNDCTL_DSP_GETOPTR\fR\fR
437 The argument is a pointer to a \fBstruct count_info\fR, which has the following
442 typedef struct count_info {
444 /* Total # of bytes processed */
446 /* # of fragment transitions since
448 int ptr;/* Current DMA pointer value */
454 These ioctls are primarily supplied for compatibility, and should not be used
455 by most applications.
461 \fB\fBSNDCTL_DSP_GETODELAY\fR\fR
464 The argument is a pointer to an integer. On return, the integer contains the
465 number of bytes still to be played before the next byte written are played.
466 This can be used for accurate determination of device latency. The result can
467 differ from actual value by up the depth of the internal device FIFO, which is
474 \fB\fBSNDCTL_DSP_GETERROR\fR\fR
477 The argument is a pointer to a \fBstruct audio_errinfo\fR, defined as follows:
481 typedef struct audio_errinfo {
484 unsigned int play_ptradjust;
485 unsigned int rec_ptradjust;
498 For this implementation, only the \fBplay_underruns\fR and \fBrec_overruns\fR
499 values are significant. No other fields are used in this implementation.
501 These fields are reset to zero each time their value is retrieved using this
505 .SS "Compatibility IOCTLS"
508 These ioctls are supplied exclusively for compatibility with existing
509 applications. Their use is not recommended, and they are not documented here.
510 Many of these are implemented as simple no-ops.
519 SNDCTL_DSP_GETBLKSIZE
521 SNDCTL_DSP_SETFRAGMENT
522 SNDCTL_DSP_COOKEDMODE
528 SNDCTL_DSP_GET_RECSRC
529 SNDCTL_DSP_SET_RECSRC
530 SNDCTL_DSP_SET_RECSRC_NAMES
531 SNDCTL_DSP_GET_PLAYTGT
532 SNDCTL_DSP_SET_PLAYTGT
533 SNDCTL_DSP_SET_PLAYTGT_NAMES
534 SNDCTL_DSP_GETTRIGGER
535 SNDCTL_DSP_SETTRIGGER
545 An \fBopen()\fR fails if:
552 The requested play or record access isbusy and either the \fBO_NDELAY\fR or
553 \fBO_NONBLOCK\fR flag was set in the \fBopen()\fR request.
562 The requested play or record access is busy and a signal interrupted the
563 \fBopen()\fR request.
572 The device cannot support the requested play or record access.
577 An \fBioctl()\fR fails if:
584 The parameter changes requested in the ioctl are invalid or are not supported
591 The physical audio device names are system dependent and are rarely used by
592 programmers. Programmers should use the generic device names listed below.
599 Symbolic link to the system's primary audio device
605 \fB\fB/dev/mixer\fR\fR
608 Symbolic link to the pseudo mixer device for the system
614 \fB\fB/dev/sndstat\fR\fR
617 Symbolic link to the pseudo mixer device for the system
623 \fB\fB/usr/share/audio/samples\fR\fR
632 See \fBattributes\fR(5) for a description of the following attributes:
640 ATTRIBUTE TYPE ATTRIBUTE VALUE
642 Architecture SPARC, x86
644 Stability Level Uncommitted
650 \fBclose\fR(2), \fBfcntl\fR(2), \fBioctl\fR(2), \fBmmap\fR(2), \fBopen\fR(2),
651 \fBpoll\fR(2), \fBread\fR(2), \fBwrite\fR(2), \fBattributes\fR(5),
652 \fBaudio\fR(7D), \fBmixer\fR(7I)