sndio/libsndio/sio_open.3

765 lines
20 KiB
Groff
Raw Normal View History

.\" $OpenBSD$
2010-08-19 15:38:45 -05:00
.\"
.\" Copyright (c) 2007 Alexandre Ratchov <alex@caoua.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate$
2010-08-19 15:38:45 -05:00
.Dt SIO_OPEN 3
.Os
.Sh NAME
.Nm sio_open ,
.Nm sio_close ,
.Nm sio_setpar ,
.Nm sio_getpar ,
.Nm sio_getcap ,
.Nm sio_start ,
.Nm sio_stop ,
.Nm sio_read ,
.Nm sio_write ,
.Nm sio_onmove ,
.Nm sio_nfds ,
.Nm sio_pollfd ,
.Nm sio_revents ,
.Nm sio_eof ,
.Nm sio_setvol ,
.Nm sio_onvol ,
.Nm sio_initpar
2014-03-05 14:17:42 -06:00
.Nd sndio interface to audio devices
2010-08-19 15:38:45 -05:00
.Sh SYNOPSIS
.In sndio.h
2010-08-19 15:38:45 -05:00
.Ft "struct sio_hdl *"
.Fn sio_open "const char *name" "unsigned int mode" "int nbio_flag"
2010-08-19 15:38:45 -05:00
.Ft "void"
.Fn sio_close "struct sio_hdl *hdl"
2010-08-19 15:38:45 -05:00
.Ft "int"
.Fn sio_setpar "struct sio_hdl *hdl" "struct sio_par *par"
2010-08-19 15:38:45 -05:00
.Ft "int"
.Fn sio_getpar "struct sio_hdl *hdl" "struct sio_par *par"
2010-08-19 15:38:45 -05:00
.Ft "int"
.Fn sio_getcap "struct sio_hdl *hdl" "struct sio_cap *cap"
2010-08-19 15:38:45 -05:00
.Ft "int"
.Fn sio_start "struct sio_hdl *hdl"
2010-08-19 15:38:45 -05:00
.Ft "int"
.Fn sio_stop "struct sio_hdl *hdl"
2010-08-19 15:38:45 -05:00
.Ft "size_t"
.Fn sio_read "struct sio_hdl *hdl" "void *addr" "size_t nbytes"
2010-08-19 15:38:45 -05:00
.Ft "size_t"
.Fn sio_write "struct sio_hdl *hdl" "const void *addr" "size_t nbytes"
2010-08-19 15:38:45 -05:00
.Ft "void"
.Fn sio_onmove "struct sio_hdl *hdl" "void (*cb)(void *arg, int delta)" "void *arg"
2010-08-19 15:38:45 -05:00
.Ft "int"
.Fn sio_nfds "struct sio_hdl *hdl"
2010-08-19 15:38:45 -05:00
.Ft "int"
.Fn sio_pollfd "struct sio_hdl *hdl" "struct pollfd *pfd" "int events"
2010-08-19 15:38:45 -05:00
.Ft "int"
.Fn sio_revents "struct sio_hdl *hdl" "struct pollfd *pfd"
2010-08-19 15:38:45 -05:00
.Ft "int"
.Fn sio_eof "struct sio_hdl *hdl"
2010-08-19 15:38:45 -05:00
.Ft "int"
.Fn sio_setvol "struct sio_hdl *hdl" "unsigned int vol"
.Ft "int"
.Fn sio_onvol "struct sio_hdl *hdl" "void (*cb)(void *arg, unsigned int vol)" "void *arg"
2010-08-19 15:38:45 -05:00
.Ft "void"
.Fn sio_initpar "struct sio_par *par"
2010-08-19 15:38:45 -05:00
.\"Fd #define SIO_BPS(bits)
.\"Fd #define SIO_LE_NATIVE
.Sh DESCRIPTION
The
.Nm sndio
library allows user processes to access
.Xr audio 4
hardware and the
2016-01-07 10:48:47 -06:00
.Xr sndiod 8
2010-08-19 15:38:45 -05:00
audio server in a uniform way.
.Ss Opening and closing an audio device
2010-08-19 15:38:45 -05:00
First the application must call the
.Fn sio_open
function to obtain a handle to the device;
2010-08-19 15:38:45 -05:00
later it will be passed as the
2014-01-27 06:16:23 -06:00
.Fa hdl
2010-08-19 15:38:45 -05:00
argument of most other functions.
The
2014-01-27 06:16:23 -06:00
.Fa name
2010-08-19 15:38:45 -05:00
parameter gives the device string discussed in
.Xr sndio 7 .
2014-01-27 06:16:23 -06:00
In most cases it should be set to
.Dv SIO_DEVANY
to allow the user to select it using the
2010-08-19 15:38:45 -05:00
.Ev AUDIODEVICE
environment variable.
.Pp
The following values of the
2014-01-27 06:16:23 -06:00
.Fa mode
parameter are supported:
2010-08-19 15:38:45 -05:00
.Bl -tag -width "SIO_PLAY | SIO_REC"
2014-01-27 06:16:23 -06:00
.It Dv SIO_PLAY
Play-only mode: data written will be played by the device.
2014-01-27 06:16:23 -06:00
.It Dv SIO_REC
Record-only mode: samples are recorded by the device and must be read.
2014-01-27 06:16:23 -06:00
.It Dv SIO_PLAY | SIO_REC
The device plays and records synchronously; this means that
2010-08-19 15:38:45 -05:00
the n-th recorded sample was physically sampled exactly when
the n-th played sample was actually played.
.El
.Pp
If the
2014-01-27 06:16:23 -06:00
.Fa nbio_flag
2010-08-19 15:38:45 -05:00
argument is true (i.e. non-zero), then the
.Fn sio_read
and
.Fn sio_write
functions (see below) will be non-blocking.
.Pp
The
.Fn sio_close
2013-12-27 06:20:04 -06:00
function stops the device as if
2010-08-19 15:38:45 -05:00
.Fn sio_stop
2013-12-27 06:20:04 -06:00
is called and frees the handle.
Thus, no samples submitted with
.Fn sio_write
are discarded.
2010-08-19 15:38:45 -05:00
.Ss Negotiating audio parameters
Audio samples are interleaved.
A frame consists of one sample for each channel.
For example, a 16-bit stereo encoding has two samples per frame
and, two bytes per sample (thus 4 bytes per frame).
2010-08-19 15:38:45 -05:00
.Pp
The set of parameters of the device that can be controlled
2010-08-19 15:38:45 -05:00
is given by the following structure:
.Bd -literal
struct sio_par {
2013-08-09 06:08:06 -05:00
unsigned int bits; /* bits per sample */
unsigned int bps; /* bytes per sample */
unsigned int sig; /* 1 = signed, 0 = unsigned int */
unsigned int le; /* 1 = LE, 0 = BE byte order */
unsigned int msb; /* 1 = MSB, 0 = LSB aligned */
unsigned int rchan; /* number channels for recording */
unsigned int pchan; /* number channels for playback */
unsigned int rate; /* frames per second */
unsigned int appbufsz; /* minimum buffer size without xruns */
2013-08-09 06:08:06 -05:00
unsigned int bufsz; /* end-to-end buffer size (read-only) */
unsigned int round; /* optimal buffer size divisor */
2010-08-19 15:38:45 -05:00
#define SIO_IGNORE 0 /* pause during xrun */
#define SIO_SYNC 1 /* resync after xrun */
#define SIO_ERROR 2 /* terminate on xrun */
2013-08-09 06:08:06 -05:00
unsigned int xrun; /* what to do on overrun/underrun */
2010-08-19 15:38:45 -05:00
};
.Ed
.Pp
The parameters are as follows:
.Bl -tag -width "appbufsz"
.It Va bits
Number of bits per sample: must be between 1 and 32.
.It Va bps
Bytes per samples; 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, else unsigned.
.It Va le
If set, then the byte order is little endian, else big endian;
it's meaningful only if
.Va bps
\*(Gt 1.
.It Va msb
If set, then the
.Va bits
2012-03-22 03:39:06 -05:00
are aligned in the packet to the most significant bit
2010-08-19 15:38:45 -05:00
(i.e. lower bits are padded),
else to the least significant bit
(i.e. higher bits are padded);
it's meaningful only if
.Va bits
\*(Lt
.Va bps
* 8.
.It Va rchan
The number of recorded channels; meaningful only if
2014-01-27 06:16:23 -06:00
.Dv SIO_REC
2010-08-19 15:38:45 -05:00
mode was selected.
.It Va pchan
The number of played channels; meaningful only if
2014-01-27 06:16:23 -06:00
.Dv SIO_PLAY
2010-08-19 15:38:45 -05:00
mode was selected.
.It Va rate
The sampling frequency in Hz.
.It Va bufsz
The maximum number of frames that may be buffered.
This parameter takes into account any buffers, and
can be used for latency calculations.
It is read-only.
.It Va appbufsz
2011-10-05 07:06:44 -05:00
Size of the buffer in frames the application must maintain non-empty
(on the play end) or non-full (on the record end) by calling
2010-08-19 15:38:45 -05:00
.Fn sio_write
or
.Fn sio_read
fast enough to avoid overrun or underrun conditions.
The audio subsystem may use additional buffering, thus this
2011-10-05 07:06:44 -05:00
parameter cannot be used for latency calculations.
2010-08-19 15:38:45 -05:00
.It Va round
Optimal number of frames that the application buffers
should be a multiple of, to get best performance.
Applications can use this parameter to round their block size.
.It Va xrun
The action when the client doesn't accept
recorded data or doesn't provide data to play fast enough;
it can be set to one of the
2014-01-27 06:16:23 -06:00
.Dv SIO_IGNORE ,
.Dv SIO_SYNC ,
2010-08-19 15:38:45 -05:00
or
2014-01-27 06:16:23 -06:00
.Dv SIO_ERROR
2010-08-19 15:38:45 -05:00
constants.
.El
.Pp
The following approach is recommended to negotiate device parameters:
2010-08-19 15:38:45 -05:00
.Bl -bullet
.It
Initialize a
2014-01-27 06:16:23 -06:00
.Vt sio_par
2010-08-19 15:38:45 -05:00
structure using
.Fn sio_initpar
and fill it with
the desired parameters.
Then call
.Fn sio_setpar
to request the device to use them.
2013-12-27 06:20:04 -06:00
Parameters left unset in the
2014-01-27 06:16:23 -06:00
.Vt sio_par
2013-12-27 06:20:04 -06:00
structure will be set to device-specific defaults.
2010-08-19 15:38:45 -05:00
.It
Call
.Fn sio_getpar
to retrieve the actual parameters of the device
2010-08-19 15:38:45 -05:00
and check that they are usable.
If they are not, then fail or set up a conversion layer.
Sometimes the rate set can be slightly different to what was requested.
A difference of about 0.5% is not audible and should be ignored.
.El
.Pp
Parameters cannot be changed after
2010-08-19 15:38:45 -05:00
.Fn sio_start
has been called,
.Fn sio_stop
must be called before parameters can be changed.
.Pp
2013-12-27 06:20:04 -06:00
If the device is exposed by the
2016-01-07 10:48:47 -06:00
.Xr sndiod 8
2013-12-27 06:20:04 -06:00
server, which is the default configuration,
a transparent emulation layer will
automatically be set up, and in this case any combination of
rate, encoding and numbers of channels is supported.
2010-08-19 15:38:45 -05:00
.Pp
To ease filling the
2014-01-27 06:16:23 -06:00
.Vt sio_par
2010-08-19 15:38:45 -05:00
structure, the
following macros can be used:
.Bl -tag -width "SIO_BPS(bits)"
2014-01-27 06:16:23 -06:00
.It Dv SIO_BPS Ns Pq Fa bits
2010-08-19 15:38:45 -05:00
Return the smallest value for
.Va bps
that is a power of two and that is large enough to
hold
2014-01-27 06:16:23 -06:00
.Fa bits .
.It Dv SIO_LE_NATIVE
2010-08-19 15:38:45 -05:00
Can be used to set the
.Va le
parameter when native byte order is required.
.El
.Ss Getting device capabilities
2010-08-19 15:38:45 -05:00
There's no way to get an exhaustive list of all parameter
combinations the device supports.
2010-08-19 15:38:45 -05:00
Applications that need to have a set of working
parameter combinations in advance can use the
.Fn sio_getcap
function.
However, for most new applications it's generally
not recommended to use
.Fn sio_getcap .
Instead, follow the recommendations for negotiating
device parameters (see above).
2010-08-19 15:38:45 -05:00
.Pp
The
2014-01-27 06:16:23 -06:00
.Vt sio_cap
2010-08-19 15:38:45 -05:00
structure contains the list of parameter configurations.
Each configuration contains multiple parameter sets.
The application must examine all configurations, and
choose its parameter set from
.Em one
of the configurations.
Parameters of different configurations
.Em are not
usable together.
.Bd -literal
struct sio_cap {
2013-08-09 06:08:06 -05:00
struct sio_enc { /* allowed encodings */
unsigned int bits;
unsigned int bps;
unsigned int sig;
unsigned int le;
unsigned int msb;
2010-08-19 15:38:45 -05:00
} enc[SIO_NENC];
unsigned int rchan[SIO_NCHAN]; /* allowed rchans */
unsigned int pchan[SIO_NCHAN]; /* allowed pchans */
unsigned int rate[SIO_NRATE]; /* allowed rates */
unsigned int nconf; /* num. of confs[] */
2010-08-19 15:38:45 -05:00
struct sio_conf {
2013-08-09 06:08:06 -05:00
unsigned int enc; /* bitmask of enc[] indexes */
unsigned int rchan; /* bitmask of rchan[] indexes */
unsigned int pchan; /* bitmask of pchan[] indexes */
unsigned int rate; /* bitmask of rate[] indexes */
2010-08-19 15:38:45 -05:00
} confs[SIO_NCONF];
};
.Ed
.Pp
The parameters are as follows:
.Bl -tag -width "rchan[SIO_NCHAN]"
2014-01-27 06:16:23 -06:00
.It Va enc Ns Bq Dv SIO_NENC
2010-08-19 15:38:45 -05:00
Array of supported encodings.
The tuple of
.Va bits ,
.Va bps ,
.Va sig ,
2014-01-27 06:16:23 -06:00
.Va le ,
2010-08-19 15:38:45 -05:00
and
.Va msb
parameters are usable in the corresponding parameters
of the
2014-01-27 06:16:23 -06:00
.Vt sio_par
2010-08-19 15:38:45 -05:00
structure.
2014-01-27 06:16:23 -06:00
.It Va rchan Ns Bq Dv SIO_NCHAN
2010-08-19 15:38:45 -05:00
Array of supported channel numbers for recording usable
in the
2014-01-27 06:16:23 -06:00
.Vt sio_par
2010-08-19 15:38:45 -05:00
structure.
2014-01-27 06:16:23 -06:00
.It Va pchan Ns Bq Dv SIO_NCHAN
2010-08-19 15:38:45 -05:00
Array of supported channel numbers for playback usable
in the
2014-01-27 06:16:23 -06:00
.Vt sio_par
2010-08-19 15:38:45 -05:00
structure.
2014-01-27 06:16:23 -06:00
.It Va rate Ns Bq Dv SIO_NRATE
2010-08-19 15:38:45 -05:00
Array of supported sample rates usable
in the
2014-01-27 06:16:23 -06:00
.Vt sio_par
2010-08-19 15:38:45 -05:00
structure.
.It Va nconf
Number of different configurations available, i.e. number
of filled elements of the
.Va confs[]
array.
2014-01-27 06:16:23 -06:00
.It Va confs Ns Bq Dv SIO_NCONF
2010-08-19 15:38:45 -05:00
Array of available configurations.
Each configuration contains bitmasks indicating which
elements of the above parameter arrays are valid for the
given configuration.
For instance, if the second bit of
.Va rate
is set, in the
2014-01-27 06:16:23 -06:00
.Vt sio_conf
2010-08-19 15:38:45 -05:00
structure, then the second element of the
2014-01-27 06:16:23 -06:00
.Va rate Ns Bq Dv SIO_NRATE
2010-08-19 15:38:45 -05:00
array of the
2014-01-27 06:16:23 -06:00
.Vt sio_cap
2010-08-19 15:38:45 -05:00
structure is valid for this configuration.
As such, when reading the array elements in the
.Vt sio_cap
structure, the corresponding
.Vt sio_conf
bitmasks should always be used.
2010-08-19 15:38:45 -05:00
.El
.Ss Starting and stopping the device
2010-08-19 15:38:45 -05:00
The
.Fn sio_start
function puts the device in a waiting state:
the device will wait for playback data to be provided
2010-08-19 15:38:45 -05:00
(using the
.Fn sio_write
function).
Once enough data is queued to ensure that play buffers
will not underrun, actual playback is started automatically.
If record mode only is selected, then recording starts
immediately.
In full-duplex mode, playback and recording will start
synchronously as soon as enough data to play is available.
.Pp
The
.Fn sio_stop
2013-12-27 06:20:04 -06:00
function puts the audio subsystem
in the same state as before
.Fn sio_start
2010-08-19 15:38:45 -05:00
is called.
2013-12-27 06:20:04 -06:00
It stops recording, drains the play buffer and then stops playback.
2010-08-19 15:38:45 -05:00
If samples to play are queued but playback hasn't started yet
2013-12-27 06:20:04 -06:00
then playback is forced immediately; playback will actually stop
2010-08-19 15:38:45 -05:00
once the buffer is drained.
2013-12-27 06:20:04 -06:00
In no case are samples in the play buffer discarded.
2010-08-19 15:38:45 -05:00
.Ss Playing and recording
When record mode is selected, the
.Fn sio_read
function must be called to retrieve recorded data; it must be called
often enough to ensure that internal buffers will not overrun.
It will store at most
2014-01-27 06:16:23 -06:00
.Fa nbytes
2010-08-19 15:38:45 -05:00
bytes at the
2014-01-27 06:16:23 -06:00
.Fa addr
2010-08-19 15:38:45 -05:00
location and return the number of bytes stored.
Unless the
2014-01-27 06:16:23 -06:00
.Fa nbio_flag
2010-08-19 15:38:45 -05:00
flag is set, it will block until data becomes available and
will return zero only on error.
.Pp
Similarly, when play mode is selected, the
.Fn sio_write
function must be called to provide data to play.
Unless the
2014-01-27 06:16:23 -06:00
.Fa nbio_flag
2010-08-19 15:38:45 -05:00
is set,
.Fn sio_write
will block until the requested amount of data is written.
.Ss Non-blocking mode operation
If the
2014-01-27 06:16:23 -06:00
.Fa nbio_flag
2010-08-19 15:38:45 -05:00
is set on
.Fn sio_open ,
then the
.Fn sio_read
and
.Fn sio_write
functions will never block; if no data is available, they will
return zero immediately.
.Pp
The
2010-08-19 15:38:45 -05:00
.Xr poll 2
system call can be used to check if data can be
read from or written to the device.
2010-08-19 15:38:45 -05:00
The
.Fn sio_pollfd
function fills the array
2014-01-27 06:16:23 -06:00
.Fa pfd
2010-08-19 15:38:45 -05:00
of
2014-01-27 06:16:23 -06:00
.Vt pollfd
2010-08-19 15:38:45 -05:00
structures, used by
.Xr poll 2 ,
with
2014-01-27 06:16:23 -06:00
.Fa events ;
2010-08-19 15:38:45 -05:00
the latter is a bit-mask of
2014-01-27 06:16:23 -06:00
.Dv POLLIN
2010-08-19 15:38:45 -05:00
and
2014-01-27 06:16:23 -06:00
.Dv POLLOUT
2010-08-19 15:38:45 -05:00
constants; refer to
.Xr poll 2
for more details.
The
.Fn sio_revents
function returns the bit-mask set by
.Xr poll 2
in the
2014-01-27 06:16:23 -06:00
.Fa pfd
2010-08-19 15:38:45 -05:00
array of
2014-01-27 06:16:23 -06:00
.Vt pollfd
2010-08-19 15:38:45 -05:00
structures.
If
2014-01-27 06:16:23 -06:00
.Dv POLLIN
is set, recorded samples are available in the device buffer
and can be read with
.Fn sio_read .
2010-08-19 15:38:45 -05:00
If
2014-01-27 06:16:23 -06:00
.Dv POLLOUT
is set, space is available in the device buffer and new samples
to play can be submitted with
.Fn sio_write .
2014-01-27 06:16:23 -06:00
.Dv POLLHUP
may be set if an error occurs, even if
2010-08-19 15:38:45 -05:00
it is not selected with
.Fn sio_pollfd .
.Pp
The size of the
.Ar pfd
array, which the caller must pre-allocate, is provided by the
2010-08-19 15:38:45 -05:00
.Fn sio_nfds
function.
.Ss Synchronizing non-audio events to the audio stream in real-time
In order to perform actions at precise positions of the audio stream,
2010-08-19 15:38:45 -05:00
such as displaying video in sync with the audio stream,
the application must be notified in real-time of the exact
position in the stream the hardware is processing.
.Pp
The
.Fn sio_onmove
function can be used to register the
2014-01-27 06:16:23 -06:00
.Fn cb
callback function called at regular time intervals.
2010-08-19 15:38:45 -05:00
The
2014-01-27 06:16:23 -06:00
.Fa delta
argument contains the number of frames the hardware played and/or recorded
2010-08-19 15:38:45 -05:00
since the last call of
2014-01-27 06:16:23 -06:00
.Fn cb .
It is called by
2014-01-27 06:16:23 -06:00
.Fn sio_read ,
.Fn sio_write ,
and
.Fn sio_revents .
When the first sample is played and/or recorded, right after the device starts,
the callback is invoked with a zero
2014-01-27 06:16:23 -06:00
.Fa delta
2010-08-19 15:38:45 -05:00
argument.
The value of the
2014-01-27 06:16:23 -06:00
.Fa arg
2010-08-19 15:38:45 -05:00
pointer is passed to the callback and can contain anything.
.Pp
If desired, the application can maintain the current position by
starting from zero (when
.Fn sio_start
is called) and adding to the current position
2014-01-27 06:16:23 -06:00
.Fa delta
2010-08-19 15:38:45 -05:00
every time
.Fn cb
is called.
.Ss Measuring the latency and buffers usage
The playback latency is the delay it will take for the
frame just written to become audible, expressed in number of frames.
The exact playback
latency can be obtained by subtracting the current position
from the number of frames written.
Once playback is actually started (first sample audible)
the latency will never exceed the
.Va bufsz
parameter (see the sections above).
There's a phase during which
.Fn sio_write
only queues data;
once there's enough data, actual playback starts.
During this phase talking about latency is meaningless.
.Pp
In any cases, at most
.Va bufsz
frames are buffered.
This value takes into account all buffers.
2010-08-19 15:38:45 -05:00
The number of frames stored is equal to the number of frames
written minus the current position.
.Pp
The recording latency is obtained similarly, by subtracting
the number of frames read from the current position.
.Pp
Note that
2010-08-19 15:38:45 -05:00
.Fn sio_write
might block even if there is buffer space left;
using the buffer usage to guess if
.Fn sio_write
would block is false and leads to unreliable programs \(en consider using
.Xr poll 2
for this.
.Ss Handling buffer overruns and underruns
When the application cannot accept recorded data fast enough,
the record buffer (of size
.Va appbufsz )
might overrun; in this case recorded data is lost.
Similarly if the application cannot provide data to play
fast enough, the play buffer underruns and silence is played
instead.
Depending on the
.Va xrun
parameter of the
2014-01-27 06:16:23 -06:00
.Vt sio_par
2010-08-19 15:38:45 -05:00
structure, the audio subsystem will behave as follows:
.Bl -tag -width "SIO_IGNORE"
2014-01-27 06:16:23 -06:00
.It Dv SIO_IGNORE
The devices pauses during overruns and underruns,
2010-08-19 15:38:45 -05:00
thus the current position (obtained through
2014-01-27 06:16:23 -06:00
.Fn sio_onmove )
2010-08-19 15:38:45 -05:00
stops being incremented.
Once the overrun and/or underrun condition is gone, the device resumes;
2010-08-19 15:38:45 -05:00
play and record are always kept in sync.
With this mode, the application cannot notice
underruns and/or overruns and shouldn't care about them.
.Pp
This mode is the default.
It's suitable for applications,
like audio players and telephony, where time
is not important and overruns or underruns are not short.
2014-01-27 06:16:23 -06:00
.It Dv SIO_SYNC
2010-08-19 15:38:45 -05:00
If the play buffer underruns, then silence is played,
but in order to reach the right position in time,
the same amount of written samples will be
discarded once the application is unblocked.
Similarly, if the record buffer overruns, then
samples are discarded, but the same amount of silence will be
returned later.
The current position (obtained through
2014-01-27 06:16:23 -06:00
.Fn sio_onmove )
2010-08-19 15:38:45 -05:00
is still incremented.
When the play buffer underruns the play latency might become negative;
when the record buffer overruns, the record latency might become
larger than
.Va bufsz .
.Pp
This mode is suitable for applications, like music production,
where time is important and where underruns or overruns are
short and rare.
2014-01-27 06:16:23 -06:00
.It Dv SIO_ERROR
2010-08-19 15:38:45 -05:00
With this mode, on the first play buffer underrun or
record buffer overrun, playback and/or recording is terminated and
2010-08-19 15:38:45 -05:00
no other function than
.Fn sio_close
will succeed.
.Pp
This mode is mostly useful for testing.
2010-08-19 15:38:45 -05:00
.El
.Ss Controlling the volume
The
.Fn sio_setvol
function can be used to set playback attenuation.
The
2014-01-27 06:16:23 -06:00
.Fa vol
2010-08-19 15:38:45 -05:00
parameter takes a value between 0 (maximum attenuation)
and
.Dv SIO_MAXVOL
(no attenuation).
It specifies the weight the audio subsystem will
give to this stream.
It is not meant to control hardware parameters like
speaker gain; the
.Xr mixerctl 8
2010-08-19 15:38:45 -05:00
interface should be used for that purpose instead.
.Pp
An application can use the
.Fn sio_onvol
function to register a callback function that
will be called each time the volume is changed,
including when
.Fn sio_setvol
is used.
The callback is always invoked when
.Fn sio_onvol
is called in order to provide the initial volume.
An application can safely assume that once
.Fn sio_onvol
2011-03-17 02:51:01 -05:00
has returned a non-zero value,
the callback has been invoked and thus
2010-08-19 15:38:45 -05:00
the current volume is available.
If there's no volume setting available,
.Fn sio_onvol
returns 0 and the callback is never invoked and calls to
.Fn sio_setvol
are ignored.
.Pp
The
.Fn sio_onvol
function can be called with a NULL argument to check whether
a volume knob is available.
2010-08-19 15:38:45 -05:00
.Ss Error handling
Errors related to the audio subsystem
(like hardware errors, dropped connections) and
programming errors (e.g. call to
.Fn sio_read
on a play-only stream) are considered fatal.
Once an error occurs, all functions taking a
2014-01-27 06:16:23 -06:00
.Fa sio_hdl
2010-08-19 15:38:45 -05:00
argument, except
.Fn sio_close
and
.Fn sio_eof ,
stop working (i.e. always return 0).
The
.Fn sio_eof
function can be used at any stage.
2010-08-19 15:38:45 -05:00
.Sh RETURN VALUES
The
.Fn sio_open
function returns the newly created handle on success or NULL
on failure.
.Pp
2010-08-19 15:38:45 -05:00
The
.Fn sio_setpar ,
.Fn sio_getpar ,
.Fn sio_getcap ,
.Fn sio_start ,
.Fn sio_stop ,
and
.Fn sio_setvol
functions return 1 on success and 0 on failure.
.Pp
The
.Fn sio_pollfd
function returns the number of
.Va pollfd
structures filled.
The
.Fn sio_nfds
function returns the number of
.Va pollfd
structures the caller must preallocate in order to be sure
that
.Fn sio_pollfd
will never overrun.
.Pp
2010-08-19 15:38:45 -05:00
The
.Fn sio_read
and
.Fn sio_write
functions return the number of bytes transferred.
.Pp
The
.Fn sio_eof
function returns 0 if there's no pending error, and a non-zero
value if there's an error.
2010-08-19 15:38:45 -05:00
.Sh ENVIRONMENT
2011-10-17 17:29:58 -05:00
.Bl -tag -width "SNDIO_DEBUGXXX" -compact
2010-08-19 15:38:45 -05:00
.It Ev AUDIODEVICE
Device to use if
.Fn sio_open
2014-01-27 06:16:23 -06:00
is called with
.Dv SIO_DEVANY
as the
.Fa name
2010-08-19 15:38:45 -05:00
argument.
.It Ev SNDIO_DEBUG
2010-08-19 15:38:45 -05:00
The debug level:
may be a value between 0 and 2.
.El
.Sh SEE ALSO
.Xr audio 4 ,
.Xr sndio 7 ,
2016-01-07 10:48:47 -06:00
.Xr sndiod 8 ,
2010-08-19 15:38:45 -05:00
.Xr audio 9
.Sh BUGS
The
.Xr audio 4
2015-01-17 17:09:04 -06:00
driver doesn't drain playback buffers, thus if sndio
2010-08-19 15:38:45 -05:00
is used to directly access an
.Xr audio 4
device,
the
.Fn sio_stop
function will stop playback immediately.
.Pp
If the application doesn't consume recorded data fast enough then
.Dq "control messages"
from the
2016-01-07 10:48:47 -06:00
.Xr sndiod 8
server are delayed and consequently
2014-01-27 06:16:23 -06:00
.Fn sio_onmove
2010-08-19 15:38:45 -05:00
callback or volume changes may be delayed.
.Pp
The
.Fn sio_open ,
.Fn sio_setpar ,
.Fn sio_getpar ,
.Fn sio_getcap ,
2014-01-27 06:16:23 -06:00
.Fn sio_start ,
2010-08-19 15:38:45 -05:00
and
.Fn sio_stop
functions may block for a very short period of time, thus they should
be avoided in code sections where blocking is not desirable.