1*3678e563Sop.\" $OpenBSD: sio_open.3,v 1.56 2022/05/30 10:31:59 op Exp $ 2f153e440Sratchov.\" 3f153e440Sratchov.\" Copyright (c) 2007 Alexandre Ratchov <alex@caoua.org> 4f153e440Sratchov.\" 5f153e440Sratchov.\" Permission to use, copy, modify, and distribute this software for any 6f153e440Sratchov.\" purpose with or without fee is hereby granted, provided that the above 7f153e440Sratchov.\" copyright notice and this permission notice appear in all copies. 8f153e440Sratchov.\" 9f153e440Sratchov.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10f153e440Sratchov.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11f153e440Sratchov.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12f153e440Sratchov.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13f153e440Sratchov.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14f153e440Sratchov.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15f153e440Sratchov.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16f153e440Sratchov.\" 17*3678e563Sop.Dd $Mdocdate: May 30 2022 $ 18f153e440Sratchov.Dt SIO_OPEN 3 19f153e440Sratchov.Os 20f153e440Sratchov.Sh NAME 21f153e440Sratchov.Nm sio_open , 22f153e440Sratchov.Nm sio_close , 23f153e440Sratchov.Nm sio_setpar , 24f153e440Sratchov.Nm sio_getpar , 25f153e440Sratchov.Nm sio_getcap , 26f153e440Sratchov.Nm sio_start , 27f153e440Sratchov.Nm sio_stop , 28ec8a3410Sratchov.Nm sio_flush , 29f153e440Sratchov.Nm sio_read , 30f153e440Sratchov.Nm sio_write , 31f153e440Sratchov.Nm sio_onmove , 32f153e440Sratchov.Nm sio_nfds , 33f153e440Sratchov.Nm sio_pollfd , 34f153e440Sratchov.Nm sio_revents , 35f153e440Sratchov.Nm sio_eof , 36634fa8c1Sratchov.Nm sio_setvol , 37634fa8c1Sratchov.Nm sio_onvol , 38a852c866Sschwarze.Nm sio_initpar , 39a852c866Sschwarze.Nm SIO_BPS 400064b033Sratchov.Nd sndio interface to audio devices 41f153e440Sratchov.Sh SYNOPSIS 4286f9d4cdStedu.In sndio.h 43f153e440Sratchov.Ft "struct sio_hdl *" 445dfee228Sschwarze.Fn sio_open "const char *name" "unsigned int mode" "int nbio_flag" 45b9b724f1Snaddy.Ft "void" 465dfee228Sschwarze.Fn sio_close "struct sio_hdl *hdl" 47f153e440Sratchov.Ft "int" 485dfee228Sschwarze.Fn sio_setpar "struct sio_hdl *hdl" "struct sio_par *par" 49f153e440Sratchov.Ft "int" 505dfee228Sschwarze.Fn sio_getpar "struct sio_hdl *hdl" "struct sio_par *par" 51f153e440Sratchov.Ft "int" 525dfee228Sschwarze.Fn sio_getcap "struct sio_hdl *hdl" "struct sio_cap *cap" 53f153e440Sratchov.Ft "int" 545dfee228Sschwarze.Fn sio_start "struct sio_hdl *hdl" 55f153e440Sratchov.Ft "int" 565dfee228Sschwarze.Fn sio_stop "struct sio_hdl *hdl" 57ec8a3410Sratchov.Ft "int" 58ec8a3410Sratchov.Fn sio_flush "struct sio_hdl *hdl" 59f153e440Sratchov.Ft "size_t" 605dfee228Sschwarze.Fn sio_read "struct sio_hdl *hdl" "void *addr" "size_t nbytes" 61f153e440Sratchov.Ft "size_t" 625dfee228Sschwarze.Fn sio_write "struct sio_hdl *hdl" "const void *addr" "size_t nbytes" 63f153e440Sratchov.Ft "void" 64c4b445c6Sschwarze.Fo sio_onmove 65c4b445c6Sschwarze.Fa "struct sio_hdl *hdl" 66c4b445c6Sschwarze.Fa "void (*cb)(void *arg, int delta)" 67c4b445c6Sschwarze.Fa "void *arg" 68c4b445c6Sschwarze.Fc 69f153e440Sratchov.Ft "int" 705dfee228Sschwarze.Fn sio_nfds "struct sio_hdl *hdl" 71f153e440Sratchov.Ft "int" 725dfee228Sschwarze.Fn sio_pollfd "struct sio_hdl *hdl" "struct pollfd *pfd" "int events" 73f153e440Sratchov.Ft "int" 745dfee228Sschwarze.Fn sio_revents "struct sio_hdl *hdl" "struct pollfd *pfd" 75f153e440Sratchov.Ft "int" 765dfee228Sschwarze.Fn sio_eof "struct sio_hdl *hdl" 77634fa8c1Sratchov.Ft "int" 785dfee228Sschwarze.Fn sio_setvol "struct sio_hdl *hdl" "unsigned int vol" 794c68e881Sratchov.Ft "int" 80c4b445c6Sschwarze.Fo sio_onvol 81c4b445c6Sschwarze.Fa "struct sio_hdl *hdl" 82c4b445c6Sschwarze.Fa "void (*cb)(void *arg, unsigned int vol)" 83c4b445c6Sschwarze.Fa "void *arg" 84c4b445c6Sschwarze.Fc 85f153e440Sratchov.Ft "void" 865dfee228Sschwarze.Fn sio_initpar "struct sio_par *par" 87a852c866Sschwarze.Ft unsigned int 88a852c866Sschwarze.Fn SIO_BPS "unsigned int bits" 89f153e440Sratchov.Sh DESCRIPTION 90f153e440SratchovThe 91b9b724f1Snaddy.Nm sndio 92f153e440Sratchovlibrary allows user processes to access 93f153e440Sratchov.Xr audio 4 94f153e440Sratchovhardware and the 953f2e611aSratchov.Xr sndiod 8 96f153e440Sratchovaudio server in a uniform way. 97305a9af0Sratchov.Ss Opening and closing an audio device 98f153e440SratchovFirst the application must call the 99f153e440Sratchov.Fn sio_open 100305a9af0Sratchovfunction to obtain a handle to the device; 101f153e440Sratchovlater it will be passed as the 102eb79ad94Sschwarze.Fa hdl 103f153e440Sratchovargument of most other functions. 104f153e440SratchovThe 105eb79ad94Sschwarze.Fa name 1066efede29Sratchovparameter gives the device string discussed in 1076efede29Sratchov.Xr sndio 7 . 108eb79ad94SschwarzeIn most cases it should be set to 109eb79ad94Sschwarze.Dv SIO_DEVANY 110eb79ad94Sschwarzeto allow the user to select it using the 111f153e440Sratchov.Ev AUDIODEVICE 112f153e440Sratchovenvironment variable. 113f153e440Sratchov.Pp 114305a9af0SratchovThe following values of the 115eb79ad94Sschwarze.Fa mode 116305a9af0Sratchovparameter are supported: 117f153e440Sratchov.Bl -tag -width "SIO_PLAY | SIO_REC" 118eb79ad94Sschwarze.It Dv SIO_PLAY 119305a9af0SratchovPlay-only mode: data written will be played by the device. 120eb79ad94Sschwarze.It Dv SIO_REC 121305a9af0SratchovRecord-only mode: samples are recorded by the device and must be read. 122eb79ad94Sschwarze.It Dv SIO_PLAY | SIO_REC 123305a9af0SratchovThe device plays and records synchronously; this means that 124f153e440Sratchovthe n-th recorded sample was physically sampled exactly when 125f153e440Sratchovthe n-th played sample was actually played. 126f153e440Sratchov.El 127f153e440Sratchov.Pp 128f153e440SratchovIf the 129eb79ad94Sschwarze.Fa nbio_flag 130f153e440Sratchovargument is true (i.e. non-zero), then the 131f153e440Sratchov.Fn sio_read 132f153e440Sratchovand 133f153e440Sratchov.Fn sio_write 134961f4a66Sratchovfunctions (see below) will be non-blocking. 135f153e440Sratchov.Pp 136f153e440SratchovThe 137f153e440Sratchov.Fn sio_close 138033c109eSratchovfunction stops the device as if 139a0404652Sratchov.Fn sio_stop 140033c109eSratchovis called and frees the handle. 141033c109eSratchovThus, no samples submitted with 142033c109eSratchov.Fn sio_write 143033c109eSratchovare discarded. 144f153e440Sratchov.Ss Negotiating audio parameters 145305a9af0SratchovAudio samples are interleaved. 146305a9af0SratchovA frame consists of one sample for each channel. 147305a9af0SratchovFor example, a 16-bit stereo encoding has two samples per frame 148305a9af0Sratchovand, two bytes per sample (thus 4 bytes per frame). 14978bf1adeSratchov.Pp 150305a9af0SratchovThe set of parameters of the device that can be controlled 151f153e440Sratchovis given by the following structure: 152dd80057dSratchov.Bd -literal 153f153e440Sratchovstruct sio_par { 1547207b069Sratchov unsigned int bits; /* bits per sample */ 1557207b069Sratchov unsigned int bps; /* bytes per sample */ 1567207b069Sratchov unsigned int sig; /* 1 = signed, 0 = unsigned int */ 1577207b069Sratchov unsigned int le; /* 1 = LE, 0 = BE byte order */ 1587207b069Sratchov unsigned int msb; /* 1 = MSB, 0 = LSB aligned */ 1597207b069Sratchov unsigned int rchan; /* number channels for recording */ 1607207b069Sratchov unsigned int pchan; /* number channels for playback */ 1617207b069Sratchov unsigned int rate; /* frames per second */ 1627207b069Sratchov unsigned int appbufsz; /* minimum buffer size without xruns */ 1637207b069Sratchov unsigned int bufsz; /* end-to-end buffer size (read-only) */ 1647207b069Sratchov unsigned int round; /* optimal buffer size divisor */ 165f153e440Sratchov#define SIO_IGNORE 0 /* pause during xrun */ 166f153e440Sratchov#define SIO_SYNC 1 /* resync after xrun */ 167f153e440Sratchov#define SIO_ERROR 2 /* terminate on xrun */ 1687207b069Sratchov unsigned int xrun; /* what to do on overrun/underrun */ 169f153e440Sratchov}; 170f153e440Sratchov.Ed 171f153e440Sratchov.Pp 172f153e440SratchovThe parameters are as follows: 173dd80057dSratchov.Bl -tag -width "appbufsz" 174c4b445c6Sschwarze.It Fa bits 175f153e440SratchovNumber of bits per sample: must be between 1 and 32. 176c4b445c6Sschwarze.It Fa bps 177f153e440SratchovBytes per samples; if specified, it must be large enough to hold all bits. 178f153e440SratchovBy default it's set to the smallest power of two large enough to hold 179c4b445c6Sschwarze.Fa bits . 180c4b445c6Sschwarze.It Fa sig 181f153e440SratchovIf set (i.e. non-zero) then the samples are signed, else unsigned. 182c4b445c6Sschwarze.It Fa le 183f153e440SratchovIf set, then the byte order is little endian, else big endian; 184f153e440Sratchovit's meaningful only if 185c4b445c6Sschwarze.Fa bps No > 1 . 186c4b445c6Sschwarze.It Fa msb 187f153e440SratchovIf set, then the 188c4b445c6Sschwarze.Fa bits 18971c4d082Sjmcare aligned in the packet to the most significant bit 190f153e440Sratchov(i.e. lower bits are padded), 191f153e440Sratchovelse to the least significant bit 192f153e440Sratchov(i.e. higher bits are padded); 193f153e440Sratchovit's meaningful only if 194c4b445c6Sschwarze.Fa bits No < Fa bps No * 8 . 195c4b445c6Sschwarze.It Fa rchan 196f153e440SratchovThe number of recorded channels; meaningful only if 197eb79ad94Sschwarze.Dv SIO_REC 198f153e440Sratchovmode was selected. 199c4b445c6Sschwarze.It Fa pchan 200f153e440SratchovThe number of played channels; meaningful only if 201eb79ad94Sschwarze.Dv SIO_PLAY 202f153e440Sratchovmode was selected. 203c4b445c6Sschwarze.It Fa rate 204f153e440SratchovThe sampling frequency in Hz. 205c4b445c6Sschwarze.It Fa appbufsz 2061943030dSjmcSize of the buffer in frames the application must maintain non-empty 2071943030dSjmc(on the play end) or non-full (on the record end) by calling 208dd80057dSratchov.Fn sio_write 209dd80057dSratchovor 210dd80057dSratchov.Fn sio_read 211dd80057dSratchovfast enough to avoid overrun or underrun conditions. 21205585f70SjmcThe audio subsystem may use additional buffering, thus this 2131943030dSjmcparameter cannot be used for latency calculations. 214*3678e563Sop.It Fa bufsz 215*3678e563SopThe maximum number of frames that may be buffered. 216*3678e563SopThis parameter takes into account any buffers, and 217*3678e563Sopcan be used for latency calculations. 218*3678e563SopIt is read-only. 219c4b445c6Sschwarze.It Fa round 220f153e440SratchovOptimal number of frames that the application buffers 221d0e01a5cSratchovshould be a multiple of, to get best performance. 222d0e01a5cSratchovApplications can use this parameter to round their block size. 223c4b445c6Sschwarze.It Fa xrun 224f153e440SratchovThe action when the client doesn't accept 225f153e440Sratchovrecorded data or doesn't provide data to play fast enough; 226f153e440Sratchovit can be set to one of the 227eb79ad94Sschwarze.Dv SIO_IGNORE , 228eb79ad94Sschwarze.Dv SIO_SYNC , 229f153e440Sratchovor 230eb79ad94Sschwarze.Dv SIO_ERROR 231f153e440Sratchovconstants. 232f153e440Sratchov.El 233f153e440Sratchov.Pp 234305a9af0SratchovThe following approach is recommended to negotiate device parameters: 235f153e440Sratchov.Bl -bullet 236f153e440Sratchov.It 237aaee2692SratchovInitialize a 238eb79ad94Sschwarze.Vt sio_par 239aaee2692Sratchovstructure using 240f153e440Sratchov.Fn sio_initpar 241aaee2692Sratchovand fill it with 242aaee2692Sratchovthe desired parameters. 243aaee2692SratchovThen call 244f153e440Sratchov.Fn sio_setpar 245305a9af0Sratchovto request the device to use them. 246033c109eSratchovParameters left unset in the 247eb79ad94Sschwarze.Vt sio_par 248033c109eSratchovstructure will be set to device-specific defaults. 249aaee2692Sratchov.It 250aaee2692SratchovCall 251f153e440Sratchov.Fn sio_getpar 252305a9af0Sratchovto retrieve the actual parameters of the device 253aaee2692Sratchovand check that they are usable. 254aaee2692SratchovIf they are not, then fail or set up a conversion layer. 2551a0f7b70SratchovSometimes the rate set can be slightly different to what was requested. 2561a0f7b70SratchovA difference of about 0.5% is not audible and should be ignored. 257f153e440Sratchov.El 258f153e440Sratchov.Pp 259305a9af0SratchovParameters cannot be changed after 260bf675721Sjakemsr.Fn sio_start 261bf675721Sjakemsrhas been called, 262bf675721Sjakemsr.Fn sio_stop 263ec8a3410Sratchovor 264ec8a3410Sratchov.Fn sio_flush 265bf675721Sjakemsrmust be called before parameters can be changed. 266bf675721Sjakemsr.Pp 267033c109eSratchovIf the device is exposed by the 2683f2e611aSratchov.Xr sndiod 8 269033c109eSratchovserver, which is the default configuration, 270033c109eSratchova transparent emulation layer will 271033c109eSratchovautomatically be set up, and in this case any combination of 272033c109eSratchovrate, encoding and numbers of channels is supported. 273f153e440Sratchov.Pp 274f153e440SratchovTo ease filling the 275eb79ad94Sschwarze.Vt sio_par 276f153e440Sratchovstructure, the 277f153e440Sratchovfollowing macros can be used: 278f153e440Sratchov.Bl -tag -width "SIO_BPS(bits)" 279a852c866Sschwarze.It Fn SIO_BPS bits 280f153e440SratchovReturn the smallest value for 281c4b445c6Sschwarze.Fa bps 282f153e440Sratchovthat is a power of two and that is large enough to 283f153e440Sratchovhold 284eb79ad94Sschwarze.Fa bits . 285eb79ad94Sschwarze.It Dv SIO_LE_NATIVE 286f153e440SratchovCan be used to set the 287c4b445c6Sschwarze.Fa le 288f153e440Sratchovparameter when native byte order is required. 289a852c866SschwarzeIt is 1 if the native byte order is little endian or 0 otherwise. 290f153e440Sratchov.El 291305a9af0Sratchov.Ss Getting device capabilities 292aaee2692SratchovThere's no way to get an exhaustive list of all parameter 293305a9af0Sratchovcombinations the device supports. 294aaee2692SratchovApplications that need to have a set of working 295aaee2692Sratchovparameter combinations in advance can use the 296aaee2692Sratchov.Fn sio_getcap 297aaee2692Sratchovfunction. 298dbc87f2bSratchovHowever, for most new applications it's generally 299dbc87f2bSratchovnot recommended to use 300dbc87f2bSratchov.Fn sio_getcap . 301dbc87f2bSratchovInstead, follow the recommendations for negotiating 302dbc87f2bSratchovdevice parameters (see above). 303aaee2692Sratchov.Pp 304f153e440SratchovThe 305eb79ad94Sschwarze.Vt sio_cap 306aaee2692Sratchovstructure contains the list of parameter configurations. 307f153e440SratchovEach configuration contains multiple parameter sets. 308f153e440SratchovThe application must examine all configurations, and 309f153e440Sratchovchoose its parameter set from 310f153e440Sratchov.Em one 311f153e440Sratchovof the configurations. 312f153e440SratchovParameters of different configurations 313f153e440Sratchov.Em are not 314f153e440Sratchovusable together. 315f153e440Sratchov.Bd -literal 316f153e440Sratchovstruct sio_cap { 317f153e440Sratchov struct sio_enc { /* allowed encodings */ 3187207b069Sratchov unsigned int bits; 3197207b069Sratchov unsigned int bps; 3207207b069Sratchov unsigned int sig; 3217207b069Sratchov unsigned int le; 3227207b069Sratchov unsigned int msb; 323f153e440Sratchov } enc[SIO_NENC]; 3247207b069Sratchov unsigned int rchan[SIO_NCHAN]; /* allowed rchans */ 3257207b069Sratchov unsigned int pchan[SIO_NCHAN]; /* allowed pchans */ 3267207b069Sratchov unsigned int rate[SIO_NRATE]; /* allowed rates */ 3277207b069Sratchov unsigned int nconf; /* num. of confs[] */ 328f153e440Sratchov struct sio_conf { 3297207b069Sratchov unsigned int enc; /* bitmask of enc[] indexes */ 3307207b069Sratchov unsigned int rchan; /* bitmask of rchan[] indexes */ 3317207b069Sratchov unsigned int pchan; /* bitmask of pchan[] indexes */ 3327207b069Sratchov unsigned int rate; /* bitmask of rate[] indexes */ 333f153e440Sratchov } confs[SIO_NCONF]; 334f153e440Sratchov}; 335f153e440Sratchov.Ed 336f153e440Sratchov.Pp 337f153e440SratchovThe parameters are as follows: 338f153e440Sratchov.Bl -tag -width "rchan[SIO_NCHAN]" 339c4b445c6Sschwarze.It Fa enc Ns Bq Dv SIO_NENC 340f153e440SratchovArray of supported encodings. 341f153e440SratchovThe tuple of 342c4b445c6Sschwarze.Fa bits , 343c4b445c6Sschwarze.Fa bps , 344c4b445c6Sschwarze.Fa sig , 345c4b445c6Sschwarze.Fa le , 346f153e440Sratchovand 347c4b445c6Sschwarze.Fa msb 348f153e440Sratchovparameters are usable in the corresponding parameters 349f153e440Sratchovof the 350eb79ad94Sschwarze.Vt sio_par 351f153e440Sratchovstructure. 352c4b445c6Sschwarze.It Fa rchan Ns Bq Dv SIO_NCHAN 353f153e440SratchovArray of supported channel numbers for recording usable 354f153e440Sratchovin the 355eb79ad94Sschwarze.Vt sio_par 356f153e440Sratchovstructure. 357c4b445c6Sschwarze.It Fa pchan Ns Bq Dv SIO_NCHAN 358f153e440SratchovArray of supported channel numbers for playback usable 359f153e440Sratchovin the 360eb79ad94Sschwarze.Vt sio_par 361f153e440Sratchovstructure. 362c4b445c6Sschwarze.It Fa rate Ns Bq Dv SIO_NRATE 363f153e440SratchovArray of supported sample rates usable 364f153e440Sratchovin the 365eb79ad94Sschwarze.Vt sio_par 366f153e440Sratchovstructure. 367c4b445c6Sschwarze.It Fa nconf 368f153e440SratchovNumber of different configurations available, i.e. number 369f153e440Sratchovof filled elements of the 370c4b445c6Sschwarze.Fa confs Ns Bq 371f153e440Sratchovarray. 372c4b445c6Sschwarze.It Fa confs Ns Bq Dv SIO_NCONF 373f153e440SratchovArray of available configurations. 374f153e440SratchovEach configuration contains bitmasks indicating which 375f153e440Sratchovelements of the above parameter arrays are valid for the 376f153e440Sratchovgiven configuration. 377f153e440SratchovFor instance, if the second bit of 378c4b445c6Sschwarze.Fa rate 379f153e440Sratchovis set, in the 380eb79ad94Sschwarze.Vt sio_conf 381f153e440Sratchovstructure, then the second element of the 382c4b445c6Sschwarze.Fa rate Ns Bq Dv SIO_NRATE 383f153e440Sratchovarray of the 384eb79ad94Sschwarze.Vt sio_cap 385f153e440Sratchovstructure is valid for this configuration. 386dbc87f2bSratchovAs such, when reading the array elements in the 387dbc87f2bSratchov.Vt sio_cap 388dbc87f2bSratchovstructure, the corresponding 389dbc87f2bSratchov.Vt sio_conf 390dbc87f2bSratchovbitmasks should always be used. 391f153e440Sratchov.El 392305a9af0Sratchov.Ss Starting and stopping the device 393f153e440SratchovThe 394f153e440Sratchov.Fn sio_start 395b710ff09Sratchovfunction prepares the device to start. 396b710ff09SratchovOnce the play buffer is full, i.e.\& 397b710ff09Sratchov.Fa sio_par.bufsz 398b710ff09Sratchovsamples are queued with 399b710ff09Sratchov.Fn sio_write , 400b710ff09Sratchovplayback starts automatically. 401b710ff09SratchovIf record-only mode is selected, then 402b710ff09Sratchov.Fn sio_start 403b710ff09Sratchovstarts recording immediately. 404f153e440SratchovIn full-duplex mode, playback and recording will start 405b710ff09Sratchovsynchronously as soon as the play buffer is full. 406f153e440Sratchov.Pp 407f153e440SratchovThe 408f153e440Sratchov.Fn sio_stop 409033c109eSratchovfunction puts the audio subsystem 410033c109eSratchovin the same state as before 411033c109eSratchov.Fn sio_start 412edb0e8e8Sratchovwas called. 413033c109eSratchovIt stops recording, drains the play buffer and then stops playback. 414a0404652SratchovIf samples to play are queued but playback hasn't started yet 415033c109eSratchovthen playback is forced immediately; playback will actually stop 416a0404652Sratchovonce the buffer is drained. 417033c109eSratchovIn no case are samples in the play buffer discarded. 418ec8a3410Sratchov.Pp 419ec8a3410SratchovThe 420ec8a3410Sratchov.Fn sio_flush 421ec8a3410Sratchovfunction stops playback and recording immediately, 422ec8a3410Sratchovpossibly discarding play buffer contents, and puts the audio subsystem 423ec8a3410Sratchovin the same state as before 424ec8a3410Sratchov.Fn sio_start 425edb0e8e8Sratchovwas called. 426f153e440Sratchov.Ss Playing and recording 427f153e440SratchovWhen record mode is selected, the 428f153e440Sratchov.Fn sio_read 429961f4a66Sratchovfunction must be called to retrieve recorded data; it must be called 430f153e440Sratchovoften enough to ensure that internal buffers will not overrun. 431f153e440SratchovIt will store at most 432eb79ad94Sschwarze.Fa nbytes 433f153e440Sratchovbytes at the 434eb79ad94Sschwarze.Fa addr 435f153e440Sratchovlocation and return the number of bytes stored. 436f153e440SratchovUnless the 437eb79ad94Sschwarze.Fa nbio_flag 438f153e440Sratchovflag is set, it will block until data becomes available and 439f153e440Sratchovwill return zero only on error. 440f153e440Sratchov.Pp 441f153e440SratchovSimilarly, when play mode is selected, the 442f153e440Sratchov.Fn sio_write 443961f4a66Sratchovfunction must be called to provide data to play. 444f153e440SratchovUnless the 445eb79ad94Sschwarze.Fa nbio_flag 446f153e440Sratchovis set, 447f153e440Sratchov.Fn sio_write 448f153e440Sratchovwill block until the requested amount of data is written. 449f153e440Sratchov.Ss Non-blocking mode operation 450f153e440SratchovIf the 451eb79ad94Sschwarze.Fa nbio_flag 452f153e440Sratchovis set on 453f153e440Sratchov.Fn sio_open , 454f153e440Sratchovthen the 455f153e440Sratchov.Fn sio_read 456f153e440Sratchovand 457f153e440Sratchov.Fn sio_write 458961f4a66Sratchovfunctions will never block; if no data is available, they will 459f153e440Sratchovreturn zero immediately. 460f153e440Sratchov.Pp 461305a9af0SratchovThe 462f153e440Sratchov.Xr poll 2 463f153e440Sratchovsystem call can be used to check if data can be 464305a9af0Sratchovread from or written to the device. 465f153e440SratchovThe 466f153e440Sratchov.Fn sio_pollfd 467961f4a66Sratchovfunction fills the array 468eb79ad94Sschwarze.Fa pfd 469f153e440Sratchovof 470eb79ad94Sschwarze.Vt pollfd 471f153e440Sratchovstructures, used by 472f153e440Sratchov.Xr poll 2 , 473f153e440Sratchovwith 474eb79ad94Sschwarze.Fa events ; 475f153e440Sratchovthe latter is a bit-mask of 476eb79ad94Sschwarze.Dv POLLIN 477f153e440Sratchovand 478eb79ad94Sschwarze.Dv POLLOUT 479f153e440Sratchovconstants; refer to 480f153e440Sratchov.Xr poll 2 481f153e440Sratchovfor more details. 482f153e440SratchovThe 483f153e440Sratchov.Fn sio_revents 484961f4a66Sratchovfunction returns the bit-mask set by 485f153e440Sratchov.Xr poll 2 486f153e440Sratchovin the 487eb79ad94Sschwarze.Fa pfd 488f153e440Sratchovarray of 489eb79ad94Sschwarze.Vt pollfd 490f153e440Sratchovstructures. 491f153e440SratchovIf 492eb79ad94Sschwarze.Dv POLLIN 493305a9af0Sratchovis set, recorded samples are available in the device buffer 494305a9af0Sratchovand can be read with 495305a9af0Sratchov.Fn sio_read . 496f153e440SratchovIf 497eb79ad94Sschwarze.Dv POLLOUT 498305a9af0Sratchovis set, space is available in the device buffer and new samples 499305a9af0Sratchovto play can be submitted with 500305a9af0Sratchov.Fn sio_write . 501eb79ad94Sschwarze.Dv POLLHUP 502eb79ad94Sschwarzemay be set if an error occurs, even if 5032e34e999Sratchovit is not selected with 5042e34e999Sratchov.Fn sio_pollfd . 505f153e440Sratchov.Pp 50638b9dda8SnatanoThe size of the 507c4b445c6Sschwarze.Fa pfd 50838b9dda8Snatanoarray, which the caller must pre-allocate, is provided by the 509f153e440Sratchov.Fn sio_nfds 51038b9dda8Snatanofunction. 511305a9af0Sratchov.Ss Synchronizing non-audio events to the audio stream in real-time 512305a9af0SratchovIn order to perform actions at precise positions of the audio stream, 513f153e440Sratchovsuch as displaying video in sync with the audio stream, 514f153e440Sratchovthe application must be notified in real-time of the exact 515f153e440Sratchovposition in the stream the hardware is processing. 516f153e440Sratchov.Pp 517f153e440SratchovThe 518f153e440Sratchov.Fn sio_onmove 519961f4a66Sratchovfunction can be used to register the 520eb79ad94Sschwarze.Fn cb 521305a9af0Sratchovcallback function called at regular time intervals. 522f153e440SratchovThe 523eb79ad94Sschwarze.Fa delta 524305a9af0Sratchovargument contains the number of frames the hardware played and/or recorded 525f153e440Sratchovsince the last call of 526eb79ad94Sschwarze.Fn cb . 527305a9af0SratchovIt is called by 528eb79ad94Sschwarze.Fn sio_read , 529eb79ad94Sschwarze.Fn sio_write , 530305a9af0Sratchovand 531305a9af0Sratchov.Fn sio_revents . 532305a9af0SratchovWhen the first sample is played and/or recorded, right after the device starts, 533305a9af0Sratchovthe callback is invoked with a zero 534eb79ad94Sschwarze.Fa delta 535a0404652Sratchovargument. 536f153e440SratchovThe value of the 537eb79ad94Sschwarze.Fa arg 53822c6a57dSjmcpointer is passed to the callback and can contain anything. 539f153e440Sratchov.Pp 540f153e440SratchovIf desired, the application can maintain the current position by 541f153e440Sratchovstarting from zero (when 542f153e440Sratchov.Fn sio_start 543f153e440Sratchovis called) and adding to the current position 544eb79ad94Sschwarze.Fa delta 545f153e440Sratchovevery time 546f153e440Sratchov.Fn cb 547f153e440Sratchovis called. 548f153e440Sratchov.Ss Measuring the latency and buffers usage 549f153e440SratchovThe playback latency is the delay it will take for the 550f153e440Sratchovframe just written to become audible, expressed in number of frames. 551f153e440SratchovThe exact playback 552f153e440Sratchovlatency can be obtained by subtracting the current position 553f153e440Sratchovfrom the number of frames written. 55441ce3b17SnaddyOnce playback is actually started (first sample audible), 555f153e440Sratchovthe latency will never exceed the 556c4b445c6Sschwarze.Fa bufsz 557f153e440Sratchovparameter (see the sections above). 558f153e440SratchovThere's a phase during which 559f153e440Sratchov.Fn sio_write 560f153e440Sratchovonly queues data; 561f153e440Sratchovonce there's enough data, actual playback starts. 562a0404652SratchovDuring this phase talking about latency is meaningless. 563f153e440Sratchov.Pp 564f153e440SratchovIn any cases, at most 565c4b445c6Sschwarze.Fa bufsz 566f153e440Sratchovframes are buffered. 567305a9af0SratchovThis value takes into account all buffers. 568a0404652SratchovThe number of frames stored is equal to the number of frames 569f153e440Sratchovwritten minus the current position. 570f153e440Sratchov.Pp 571f153e440SratchovThe recording latency is obtained similarly, by subtracting 572f153e440Sratchovthe number of frames read from the current position. 573f153e440Sratchov.Pp 574305a9af0SratchovNote that 575f153e440Sratchov.Fn sio_write 576f153e440Sratchovmight block even if there is buffer space left; 577f153e440Sratchovusing the buffer usage to guess if 578f153e440Sratchov.Fn sio_write 579f153e440Sratchovwould block is false and leads to unreliable programs \(en consider using 580f153e440Sratchov.Xr poll 2 581f153e440Sratchovfor this. 582f153e440Sratchov.Ss Handling buffer overruns and underruns 583f153e440SratchovWhen the application cannot accept recorded data fast enough, 584f153e440Sratchovthe record buffer (of size 585c4b445c6Sschwarze.Fa appbufsz ) 586f153e440Sratchovmight overrun; in this case recorded data is lost. 587f153e440SratchovSimilarly if the application cannot provide data to play 588f153e440Sratchovfast enough, the play buffer underruns and silence is played 589f153e440Sratchovinstead. 590f153e440SratchovDepending on the 591c4b445c6Sschwarze.Fa xrun 592f153e440Sratchovparameter of the 593eb79ad94Sschwarze.Vt sio_par 594f153e440Sratchovstructure, the audio subsystem will behave as follows: 595f153e440Sratchov.Bl -tag -width "SIO_IGNORE" 596eb79ad94Sschwarze.It Dv SIO_IGNORE 597b710ff09SratchovThe device pauses during overruns and underruns, 598f153e440Sratchovthus the current position (obtained through 599eb79ad94Sschwarze.Fn sio_onmove ) 600f153e440Sratchovstops being incremented. 601305a9af0SratchovOnce the overrun and/or underrun condition is gone, the device resumes; 602f153e440Sratchovplay and record are always kept in sync. 603f153e440SratchovWith this mode, the application cannot notice 604f153e440Sratchovunderruns and/or overruns and shouldn't care about them. 605f153e440Sratchov.Pp 606f153e440SratchovThis mode is the default. 607f153e440SratchovIt's suitable for applications, 608f153e440Sratchovlike audio players and telephony, where time 609f153e440Sratchovis not important and overruns or underruns are not short. 610eb79ad94Sschwarze.It Dv SIO_SYNC 611f153e440SratchovIf the play buffer underruns, then silence is played, 612f153e440Sratchovbut in order to reach the right position in time, 613f153e440Sratchovthe same amount of written samples will be 614f153e440Sratchovdiscarded once the application is unblocked. 615f153e440SratchovSimilarly, if the record buffer overruns, then 616f153e440Sratchovsamples are discarded, but the same amount of silence will be 617f153e440Sratchovreturned later. 618f153e440SratchovThe current position (obtained through 619eb79ad94Sschwarze.Fn sio_onmove ) 620f153e440Sratchovis still incremented. 621f153e440SratchovWhen the play buffer underruns the play latency might become negative; 622f153e440Sratchovwhen the record buffer overruns, the record latency might become 623f153e440Sratchovlarger than 624c4b445c6Sschwarze.Fa bufsz . 625f153e440Sratchov.Pp 626f153e440SratchovThis mode is suitable for applications, like music production, 627f153e440Sratchovwhere time is important and where underruns or overruns are 628f153e440Sratchovshort and rare. 629eb79ad94Sschwarze.It Dv SIO_ERROR 630f153e440SratchovWith this mode, on the first play buffer underrun or 631305a9af0Sratchovrecord buffer overrun, playback and/or recording is terminated and 632f153e440Sratchovno other function than 633f153e440Sratchov.Fn sio_close 634f153e440Sratchovwill succeed. 635f153e440Sratchov.Pp 636305a9af0SratchovThis mode is mostly useful for testing. 637f153e440Sratchov.El 638634fa8c1Sratchov.Ss Controlling the volume 639634fa8c1SratchovThe 640634fa8c1Sratchov.Fn sio_setvol 64122c6a57dSjmcfunction can be used to set playback attenuation. 642634fa8c1SratchovThe 643eb79ad94Sschwarze.Fa vol 644634fa8c1Sratchovparameter takes a value between 0 (maximum attenuation) 645634fa8c1Sratchovand 64622c6a57dSjmc.Dv SIO_MAXVOL 647634fa8c1Sratchov(no attenuation). 64822c6a57dSjmcIt specifies the weight the audio subsystem will 649634fa8c1Sratchovgive to this stream. 65022c6a57dSjmcIt is not meant to control hardware parameters like 65122c6a57dSjmcspeaker gain; the 652596a878aSderaadt.Xr mixerctl 8 653634fa8c1Sratchovinterface should be used for that purpose instead. 654634fa8c1Sratchov.Pp 655634fa8c1SratchovAn application can use the 656634fa8c1Sratchov.Fn sio_onvol 65722c6a57dSjmcfunction to register a callback function that 65822c6a57dSjmcwill be called each time the volume is changed, 65922c6a57dSjmcincluding when 660634fa8c1Sratchov.Fn sio_setvol 661634fa8c1Sratchovis used. 66222c6a57dSjmcThe callback is always invoked when 663634fa8c1Sratchov.Fn sio_onvol 66422c6a57dSjmcis called in order to provide the initial volume. 66522c6a57dSjmcAn application can safely assume that once 666634fa8c1Sratchov.Fn sio_onvol 667812dc1b2Sjmchas returned a non-zero value, 668812dc1b2Sjmcthe callback has been invoked and thus 669634fa8c1Sratchovthe current volume is available. 6704c68e881SratchovIf there's no volume setting available, 6714c68e881Sratchov.Fn sio_onvol 6724c68e881Sratchovreturns 0 and the callback is never invoked and calls to 6734c68e881Sratchov.Fn sio_setvol 6744c68e881Sratchovare ignored. 6754c68e881Sratchov.Pp 6764c68e881SratchovThe 6774c68e881Sratchov.Fn sio_onvol 678c4b445c6Sschwarzefunction can be called with a 679c4b445c6Sschwarze.Dv NULL 680c4b445c6Sschwarzeargument to check whether a volume knob is available. 681f153e440Sratchov.Ss Error handling 682f153e440SratchovErrors related to the audio subsystem 683f153e440Sratchov(like hardware errors, dropped connections) and 684f153e440Sratchovprogramming errors (e.g. call to 685f153e440Sratchov.Fn sio_read 686f153e440Sratchovon a play-only stream) are considered fatal. 687f153e440SratchovOnce an error occurs, all functions taking a 688eb79ad94Sschwarze.Fa sio_hdl 689f153e440Sratchovargument, except 690f153e440Sratchov.Fn sio_close 691f153e440Sratchovand 692f153e440Sratchov.Fn sio_eof , 693f153e440Sratchovstop working (i.e. always return 0). 694f153e440SratchovThe 695f153e440Sratchov.Fn sio_eof 69638b9dda8Snatanofunction can be used at any stage. 697edb9bfb9Stim.Ss Use with Xr pledge 2 698edb9bfb9StimIf the 699edb9bfb9Stim.Nm sndio 700edb9bfb9Stimlibrary is used in combination with 701edb9bfb9Stim.Xr pledge 2 , 702edb9bfb9Stimthen the 703edb9bfb9Stim.Fn sio_open 704edb9bfb9Stimfunction needs the 705c4b445c6Sschwarze.Cm stdio , 706c4b445c6Sschwarze.Cm rpath , 707c4b445c6Sschwarze.Cm wpath , 708c4b445c6Sschwarze.Cm cpath , 709c4b445c6Sschwarze.Cm inet , 710c4b445c6Sschwarze.Cm unix , 711c4b445c6Sschwarze.Cm dns , 712edb9bfb9Stimand 713c4b445c6Sschwarze.Cm audio 714edb9bfb9Stim.Xr pledge 2 715edb9bfb9Stimpromises. 716edb9bfb9Stim.Bl -bullet 717edb9bfb9Stim.It 718c4b445c6Sschwarze.Cm rpath , 719c4b445c6Sschwarze.Cm wpath , 720edb9bfb9Stimand 721c4b445c6Sschwarze.Cm cpath 722edb9bfb9Stimare needed to read, write or create the authentication cookie 723096da69dSratchov.Pa ~/.sndio/cookie . 724edb9bfb9Stim.It 725c4b445c6Sschwarze.Cm rpath , 726c4b445c6Sschwarze.Cm wpath , 727edb9bfb9Stimand 728c4b445c6Sschwarze.Cm audio 729edb9bfb9Stimare needed when the device is a local raw device. 730edb9bfb9Stim.It 731c4b445c6Sschwarze.Cm unix 732edb9bfb9Stimis needed when the device is a local 733edb9bfb9Stim.Xr sndiod 8 734edb9bfb9Stimsub-device. 735edb9bfb9Stim.It 736c4b445c6Sschwarze.Cm inet 737edb9bfb9Stimand 738c4b445c6Sschwarze.Cm dns 739edb9bfb9Stimare needed when the device is a remote 740edb9bfb9Stim.Xr sndiod 8 741edb9bfb9Stimsub-device. 742edb9bfb9Stim.El 743edb9bfb9Stim.Pp 744edb9bfb9StimOnce no further calls to 745edb9bfb9Stim.Fn sio_open 746edb9bfb9Stimwill be made, all these 747edb9bfb9Stim.Xr pledge 2 748edb9bfb9Stimpromises may be dropped, except for the 749c4b445c6Sschwarze.Cm audio 750edb9bfb9Stimpromise. 751f153e440Sratchov.Sh RETURN VALUES 752f153e440SratchovThe 753f153e440Sratchov.Fn sio_open 754c4b445c6Sschwarzefunction returns the newly created handle on success or 755c4b445c6Sschwarze.Dv NULL 756f153e440Sratchovon failure. 75738b9dda8Snatano.Pp 758f153e440SratchovThe 759f153e440Sratchov.Fn sio_setpar , 760f153e440Sratchov.Fn sio_getpar , 761b7045568Sratchov.Fn sio_getcap , 762f153e440Sratchov.Fn sio_start , 763f153e440Sratchov.Fn sio_stop , 764ec8a3410Sratchov.Fn sio_flush , 765b7045568Sratchovand 766b7045568Sratchov.Fn sio_setvol 767f153e440Sratchovfunctions return 1 on success and 0 on failure. 76838b9dda8Snatano.Pp 76938b9dda8SnatanoThe 77038b9dda8Snatano.Fn sio_pollfd 77138b9dda8Snatanofunction returns the number of 772c4b445c6Sschwarze.Vt pollfd 77338b9dda8Snatanostructures filled. 77438b9dda8SnatanoThe 77538b9dda8Snatano.Fn sio_nfds 77638b9dda8Snatanofunction returns the number of 777c4b445c6Sschwarze.Vt pollfd 77838b9dda8Snatanostructures the caller must preallocate in order to be sure 77938b9dda8Snatanothat 78038b9dda8Snatano.Fn sio_pollfd 78138b9dda8Snatanowill never overrun. 78238b9dda8Snatano.Pp 783f153e440SratchovThe 784f153e440Sratchov.Fn sio_read 785f153e440Sratchovand 786f153e440Sratchov.Fn sio_write 787b9b724f1Snaddyfunctions return the number of bytes transferred. 78838b9dda8Snatano.Pp 78938b9dda8SnatanoThe 79038b9dda8Snatano.Fn sio_eof 79138b9dda8Snatanofunction returns 0 if there's no pending error, and a non-zero 79238b9dda8Snatanovalue if there's an error. 793f153e440Sratchov.Sh ENVIRONMENT 7948825335aSratchov.Bl -tag -width "SNDIO_DEBUGXXX" -compact 795f153e440Sratchov.It Ev AUDIODEVICE 7966efede29SratchovDevice to use if 7976efede29Sratchov.Fn sio_open 798eb79ad94Sschwarzeis called with 799eb79ad94Sschwarze.Dv SIO_DEVANY 800eb79ad94Sschwarzeas the 801eb79ad94Sschwarze.Fa name 8026efede29Sratchovargument. 80382bfc72bSratchov.It Ev SNDIO_DEBUG 80488fc5364StoddThe debug level: 80588fc5364Stoddmay be a value between 0 and 2. 806f153e440Sratchov.El 807f153e440Sratchov.Sh SEE ALSO 808edb9bfb9Stim.Xr pledge 2 , 809a852c866Sschwarze.Xr mio_open 3 , 810a852c866Sschwarze.Xr sioctl_open 3 , 811f153e440Sratchov.Xr audio 4 , 8126efede29Sratchov.Xr sndio 7 , 8133f2e611aSratchov.Xr sndiod 8 , 814f153e440Sratchov.Xr audio 9 815a852c866Sschwarze.Sh HISTORY 816a852c866SschwarzeThese functions first appeared in 817a852c866Sschwarze.Ox 4.5 . 818a852c866Sschwarze.Sh AUTHORS 819a852c866Sschwarze.An Alexandre Ratchov Aq Mt ratchov@openbsd.org 820f153e440Sratchov.Sh BUGS 821f153e440SratchovThe 822f153e440Sratchov.Xr audio 4 8233d57a6b8Sschwarzedriver doesn't drain playback buffers, thus if sndio 824f153e440Sratchovis used to directly access an 825f153e440Sratchov.Xr audio 4 826f153e440Sratchovdevice, 827f153e440Sratchovthe 828f153e440Sratchov.Fn sio_stop 829961f4a66Sratchovfunction will stop playback immediately. 830f153e440Sratchov.Pp 831f153e440SratchovIf the application doesn't consume recorded data fast enough then 832f153e440Sratchov.Dq "control messages" 833305a9af0Sratchovfrom the 8343f2e611aSratchov.Xr sndiod 8 835305a9af0Sratchovserver are delayed and consequently 836eb79ad94Sschwarze.Fn sio_onmove 837a0404652Sratchovcallback or volume changes may be delayed. 838f153e440Sratchov.Pp 839f153e440SratchovThe 840f153e440Sratchov.Fn sio_open , 841f153e440Sratchov.Fn sio_setpar , 842f153e440Sratchov.Fn sio_getpar , 843b7045568Sratchov.Fn sio_getcap , 844eb79ad94Sschwarze.Fn sio_start , 845ec8a3410Sratchov.Fn sio_stop , 846f153e440Sratchovand 847ec8a3410Sratchov.Fn sio_flush 848961f4a66Sratchovfunctions may block for a very short period of time, thus they should 84981680aa3Sratchovbe avoided in code sections where blocking is not desirable. 850