1.\" $OpenBSD: sio_open.3,v 1.33 2012/05/23 19:25:11 ratchov Exp $ 2.\" 3.\" Copyright (c) 2007 Alexandre Ratchov <alex@caoua.org> 4.\" 5.\" Permission to use, copy, modify, and distribute this software for any 6.\" purpose with or without fee is hereby granted, provided that the above 7.\" copyright notice and this permission notice appear in all copies. 8.\" 9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16.\" 17.Dd $Mdocdate: May 23 2012 $ 18.Dt SIO_OPEN 3 19.Os 20.Sh NAME 21.Nm sio_open , 22.Nm sio_close , 23.Nm sio_setpar , 24.Nm sio_getpar , 25.Nm sio_getcap , 26.Nm sio_start , 27.Nm sio_stop , 28.Nm sio_read , 29.Nm sio_write , 30.Nm sio_onmove , 31.Nm sio_nfds , 32.Nm sio_pollfd , 33.Nm sio_revents , 34.Nm sio_eof , 35.Nm sio_setvol , 36.Nm sio_onvol , 37.Nm sio_initpar 38.Nd interface to bidirectional audio streams 39.Sh SYNOPSIS 40.Fd #include <sndio.h> 41.Ft "struct sio_hdl *" 42.Fn "sio_open" "const char *name" "unsigned int mode" "int nbio_flag" 43.Ft "void" 44.Fn "sio_close" "struct sio_hdl *hdl" 45.Ft "int" 46.Fn "sio_setpar" "struct sio_hdl *hdl" "struct sio_par *par" 47.Ft "int" 48.Fn "sio_getpar" "struct sio_hdl *hdl" "struct sio_par *par" 49.Ft "int" 50.Fn "sio_getcap" "struct sio_hdl *hdl" "struct sio_cap *cap" 51.Ft "int" 52.Fn "sio_start" "struct sio_hdl *hdl" 53.Ft "int" 54.Fn "sio_stop" "struct sio_hdl *hdl" 55.Ft "size_t" 56.Fn "sio_read" "struct sio_hdl *hdl" "void *addr" "size_t nbytes" 57.Ft "size_t" 58.Fn "sio_write" "struct sio_hdl *hdl" "const void *addr" "size_t nbytes" 59.Ft "void" 60.Fn "sio_onmove" "struct sio_hdl *hdl" "void (*cb)(void *arg, int delta)" "void *arg" 61.Ft "int" 62.Fn "sio_nfds" "struct sio_hdl *hdl" 63.Ft "int" 64.Fn "sio_pollfd" "struct sio_hdl *hdl" "struct pollfd *pfd" "int events" 65.Ft "int" 66.Fn "sio_revents" "struct sio_hdl *hdl" "struct pollfd *pfd" 67.Ft "int" 68.Fn "sio_eof" "struct sio_hdl *hdl" 69.Ft "int" 70.Fn "sio_setvol" "struct sio_hdl *hdl" "unsigned int vol" 71.Ft "int" 72.Fn "sio_onvol" "struct sio_hdl *hdl" "void (*cb)(void *arg, unsigned int vol)" "void *arg" 73.Ft "void" 74.Fn "sio_initpar" "struct sio_par *par" 75.\"Fd #define SIO_BPS(bits) 76.\"Fd #define SIO_LE_NATIVE 77.Sh DESCRIPTION 78The 79.Nm sndio 80library allows user processes to access 81.Xr audio 4 82hardware and the 83.Xr sndiod 1 84audio server in a uniform way. 85It supports full-duplex operation, and when 86used with the 87.Xr sndiod 1 88server it supports resampling and format 89conversions on the fly. 90.Ss Opening and closing an audio stream 91First the application must call the 92.Fn sio_open 93function to obtain a handle representing the newly created stream; 94later it will be passed as the 95.Ar hdl 96argument of most other functions. 97The 98.Fn sio_open 99function first tries to connect to the 100.Xr sndiod 1 101audio server. 102If that fails, it then tries to use the 103.Xr audio 4 104hardware device. 105The 106.Ar name 107parameter gives the device string discussed in 108.Xr sndio 7 . 109In most cases it should be set to SIO_DEVANY to allow 110the user to select it using the 111.Ev AUDIODEVICE 112environment variable. 113.Pp 114The 115.Ar mode 116parameter gives the direction of the stream. 117The following are supported: 118.Bl -tag -width "SIO_PLAY | SIO_REC" 119.It SIO_PLAY 120The stream is play-only; data written to the stream will be played 121by the hardware. 122.It SIO_REC 123The stream is record-only; recorded samples by the hardware 124must be read from the stream. 125.It SIO_PLAY | SIO_REC 126The stream plays and records synchronously; this means that 127the n-th recorded sample was physically sampled exactly when 128the n-th played sample was actually played. 129.El 130.Pp 131If the 132.Ar nbio_flag 133argument is true (i.e. non-zero), then the 134.Fn sio_read 135and 136.Fn sio_write 137functions (see below) will be non-blocking. 138.Pp 139The 140.Fn sio_close 141function closes the stream and frees all allocated resources 142associated with the 143.Nm libsndio 144handle. 145If the stream is not stopped it will be stopped first as if 146.Fn sio_stop 147is called. 148.Ss Negotiating audio parameters 149Audio streams always use linear interleaved encoding. 150A frame consists of one sample for each channel in the stream. 151For example, a 16-bit stereo stream has two samples per frame 152and, typically, two bytes per sample (thus 4 bytes per frame). 153.Pp 154The set of parameters of the stream that can be controlled 155is given by the following structure: 156.Bd -literal 157struct sio_par { 158 unsigned int bits; /* bits per sample */ 159 unsigned int bps; /* bytes per sample */ 160 unsigned int sig; /* 1 = signed, 0 = unsigned int */ 161 unsigned int le; /* 1 = LE, 0 = BE byte order */ 162 unsigned int msb; /* 1 = MSB, 0 = LSB aligned */ 163 unsigned int rchan; /* number channels for recording */ 164 unsigned int pchan; /* number channels for playback */ 165 unsigned int rate; /* frames per second */ 166 unsigned int appbufsz; /* minimum buffer size without xruns */ 167 unsigned int bufsz; /* end-to-end buffer size (read-only) */ 168 unsigned int round; /* optimal buffer size divisor */ 169#define SIO_IGNORE 0 /* pause during xrun */ 170#define SIO_SYNC 1 /* resync after xrun */ 171#define SIO_ERROR 2 /* terminate on xrun */ 172 unsigned int xrun; /* what to do on overrun/underrun */ 173}; 174.Ed 175.Pp 176The parameters are as follows: 177.Bl -tag -width "appbufsz" 178.It Va bits 179Number of bits per sample: must be between 1 and 32. 180.It Va bps 181Bytes per samples; if specified, it must be large enough to hold all bits. 182By default it's set to the smallest power of two large enough to hold 183.Va bits . 184.It Va sig 185If set (i.e. non-zero) then the samples are signed, else unsigned. 186.It Va le 187If set, then the byte order is little endian, else big endian; 188it's meaningful only if 189.Va bps 190\*(Gt 1. 191.It Va msb 192If set, then the 193.Va bits 194are aligned in the packet to the most significant bit 195(i.e. lower bits are padded), 196else to the least significant bit 197(i.e. higher bits are padded); 198it's meaningful only if 199.Va bits 200\*(Lt 201.Va bps 202* 8. 203.It Va rchan 204The number of recorded channels; meaningful only if 205.Va SIO_REC 206mode was selected. 207.It Va pchan 208The number of played channels; meaningful only if 209.Va SIO_PLAY 210mode was selected. 211.It Va rate 212The sampling frequency in Hz. 213.It Va bufsz 214The maximum number of frames that may be buffered. 215This parameter takes into account any buffers, and 216can be used for latency calculations. 217It is read-only. 218.It Va appbufsz 219Size of the buffer in frames the application must maintain non-empty 220(on the play end) or non-full (on the record end) by calling 221.Fn sio_write 222or 223.Fn sio_read 224fast enough to avoid overrun or underrun conditions. 225The audio subsystem may use additional buffering, thus this 226parameter cannot be used for latency calculations. 227.It Va round 228Optimal number of frames that the application buffers 229should be a multiple of, to get best performance. 230Applications can use this parameter to round their block size. 231.It Va xrun 232The action when the client doesn't accept 233recorded data or doesn't provide data to play fast enough; 234it can be set to one of the 235.Va SIO_IGNORE , 236.Va SIO_SYNC 237or 238.Va SIO_ERROR 239constants. 240.El 241.Pp 242The following approach is recommended to negotiate parameters of the stream: 243.Bl -bullet 244.It 245Initialize a 246.Va sio_par 247structure using 248.Fn sio_initpar 249and fill it with 250the desired parameters. 251If the application supports any value for a given parameter, 252then the corresponding parameter should be left unset. 253Then call 254.Fn sio_setpar 255to request the stream to use them. 256.It 257Call 258.Fn sio_getpar 259to retrieve the actual parameters of the stream 260and check that they are usable. 261If they are not, then fail or set up a conversion layer. 262Sometimes the rate set can be slightly different to what was requested. 263A difference of about 0.5% is not audible and should be ignored. 264.El 265.Pp 266Parameters cannot be changed while the stream is in a waiting state; 267if 268.Fn sio_start 269has been called, 270.Fn sio_stop 271must be called before parameters can be changed. 272.Pp 273If 274.Nm libsndio 275is used to connect to the 276.Xr sndiod 1 277server, a transparent emulation layer will 278automatically be set up, and in this case any 279parameters are supported. 280.Pp 281To ease filling the 282.Va sio_par 283structure, the 284following macros can be used: 285.Bl -tag -width "SIO_BPS(bits)" 286.It "SIO_BPS(bits)" 287Return the smallest value for 288.Va bps 289that is a power of two and that is large enough to 290hold 291.Va bits . 292.It "SIO_LE_NATIVE" 293Can be used to set the 294.Va le 295parameter when native byte order is required. 296.El 297.Ss Getting stream capabilities 298There's no way to get an exhaustive list of all parameter 299combinations the stream supports. 300Applications that need to have a set of working 301parameter combinations in advance can use the 302.Fn sio_getcap 303function. 304.Pp 305The 306.Va sio_cap 307structure contains the list of parameter configurations. 308Each configuration contains multiple parameter sets. 309The application must examine all configurations, and 310choose its parameter set from 311.Em one 312of the configurations. 313Parameters of different configurations 314.Em are not 315usable together. 316.Bd -literal 317struct sio_cap { 318 struct sio_enc { /* allowed encodings */ 319 unsigned int bits; 320 unsigned int bps; 321 unsigned int sig; 322 unsigned int le; 323 unsigned int msb; 324 } enc[SIO_NENC]; 325 unsigned int rchan[SIO_NCHAN]; /* allowed rchans */ 326 unsigned int pchan[SIO_NCHAN]; /* allowed pchans */ 327 unsigned int rate[SIO_NRATE]; /* allowed rates */ 328 unsigned int nconf; /* num. of confs[] */ 329 struct sio_conf { 330 unsigned int enc; /* bitmask of enc[] indexes */ 331 unsigned int rchan; /* bitmask of rchan[] indexes */ 332 unsigned int pchan; /* bitmask of pchan[] indexes */ 333 unsigned int rate; /* bitmask of rate[] indexes */ 334 } confs[SIO_NCONF]; 335}; 336.Ed 337.Pp 338The parameters are as follows: 339.Bl -tag -width "rchan[SIO_NCHAN]" 340.It Va enc[SIO_NENC] 341Array of supported encodings. 342The tuple of 343.Va bits , 344.Va bps , 345.Va sig , 346.Va le 347and 348.Va msb 349parameters are usable in the corresponding parameters 350of the 351.Va sio_par 352structure. 353.It Va rchan[SIO_NCHAN] 354Array of supported channel numbers for recording usable 355in the 356.Va sio_par 357structure. 358.It Va pchan[SIO_NCHAN] 359Array of supported channel numbers for playback usable 360in the 361.Va sio_par 362structure. 363.It Va rate[SIO_NRATE] 364Array of supported sample rates usable 365in the 366.Va sio_par 367structure. 368.It Va nconf 369Number of different configurations available, i.e. number 370of filled elements of the 371.Va confs[] 372array. 373.It Va confs[SIO_NCONF] 374Array of available configurations. 375Each configuration contains bitmasks indicating which 376elements of the above parameter arrays are valid for the 377given configuration. 378For instance, if the second bit of 379.Va rate 380is set, in the 381.Va sio_conf 382structure, then the second element of the 383.Va rate[SIO_NRATE] 384array of the 385.Va sio_cap 386structure is valid for this configuration. 387.El 388.Ss Starting and stopping the stream 389The 390.Fn sio_start 391function puts the stream in a waiting state: 392the stream will wait for playback data to be provided 393(using the 394.Fn sio_write 395function). 396Once enough data is queued to ensure that play buffers 397will not underrun, actual playback is started automatically. 398If record mode only is selected, then recording starts 399immediately. 400In full-duplex mode, playback and recording will start 401synchronously as soon as enough data to play is available. 402.Pp 403The 404.Fn sio_stop 405function stops playback and recording and puts the audio subsystem 406in the same state as after 407.Fn sio_open 408is called. 409Samples in the play buffers are not discarded, and will continue to 410be played after 411.Fn sio_stop 412returns. 413If samples to play are queued but playback hasn't started yet 414then playback is forced immediately; the stream will actually stop 415once the buffer is drained. 416.Ss Playing and recording 417When record mode is selected, the 418.Fn sio_read 419function must be called to retrieve recorded data; it must be called 420often enough to ensure that internal buffers will not overrun. 421It will store at most 422.Ar nbytes 423bytes at the 424.Ar addr 425location and return the number of bytes stored. 426Unless the 427.Ar nbio_flag 428flag is set, it will block until data becomes available and 429will return zero only on error. 430.Pp 431Similarly, when play mode is selected, the 432.Fn sio_write 433function must be called to provide data to play. 434Unless the 435.Ar nbio_flag 436is set, 437.Fn sio_write 438will block until the requested amount of data is written. 439.Ss Non-blocking mode operation 440If the 441.Ar nbio_flag 442is set on 443.Fn sio_open , 444then the 445.Fn sio_read 446and 447.Fn sio_write 448functions will never block; if no data is available, they will 449return zero immediately. 450.Pp 451Note that non-blocking mode must be used on bidirectional streams. 452For instance, if recording is blocked in 453.Fn sio_read 454then, even if samples can be played, 455.Fn sio_write 456cannot be called and the play buffers may underrun. 457.Pp 458To avoid busy loops when non-blocking mode is used, the 459.Xr poll 2 460system call can be used to check if data can be 461read from or written to the stream. 462The 463.Fn sio_pollfd 464function fills the array 465.Ar pfd 466of 467.Va pollfd 468structures, used by 469.Xr poll 2 , 470with 471.Ar events ; 472the latter is a bit-mask of 473.Va POLLIN 474and 475.Va POLLOUT 476constants; refer to 477.Xr poll 2 478for more details. 479.Fn sio_pollfd 480returns the number of 481.Va pollfd 482structures filled. 483The 484.Fn sio_revents 485function returns the bit-mask set by 486.Xr poll 2 487in the 488.Va pfd 489array of 490.Va pollfd 491structures. 492If 493.Va POLLIN 494is set, 495.Fn sio_read 496can be called without blocking. 497If 498.Va POLLOUT 499is set, 500.Fn sio_write 501can be called without blocking. 502POLLHUP may be set if an error occurs, even if 503it is not selected with 504.Fn sio_pollfd . 505.Pp 506The 507.Fn sio_nfds 508function returns the number of 509.Va pollfd 510structures the caller must preallocate in order to be sure 511that 512.Fn sio_pollfd 513will never overrun. 514.Ss Synchronizing non-audio events to the stream in real-time 515In order to perform actions at precise positions of the stream, 516such as displaying video in sync with the audio stream, 517the application must be notified in real-time of the exact 518position in the stream the hardware is processing. 519.Pp 520The 521.Fn sio_onmove 522function can be used to register the 523.Va cb 524callback function that will be called by the 525.Nm sndio 526library at regular time intervals to notify the application 527the position in the stream changed. 528The 529.Va delta 530argument contains the number of frames the hardware moved in the stream 531since the last call of 532.Va cb . 533When the stream starts, the callback is invoked with a zero 534.Va delta 535argument. 536The value of the 537.Va arg 538pointer is passed to the callback and can contain anything. 539.Pp 540If desired, the application can maintain the current position by 541starting from zero (when 542.Fn sio_start 543is called) and adding to the current position 544.Va delta 545every time 546.Fn cb 547is called. 548.Ss Measuring the latency and buffers usage 549The playback latency is the delay it will take for the 550frame just written to become audible, expressed in number of frames. 551The exact playback 552latency can be obtained by subtracting the current position 553from the number of frames written. 554Once playback is actually started (first sample audible) 555the latency will never exceed the 556.Va bufsz 557parameter (see the sections above). 558There's a phase during which 559.Fn sio_write 560only queues data; 561once there's enough data, actual playback starts. 562During this phase talking about latency is meaningless. 563.Pp 564In any cases, at most 565.Va bufsz 566frames are buffered. 567This value takes into account all buffers, 568including device, kernel and socket buffers. 569The number of frames stored is equal to the number of frames 570written minus the current position. 571.Pp 572The recording latency is obtained similarly, by subtracting 573the number of frames read from the current position. 574.Pp 575It is strongly discouraged to use the latency and/or the buffer 576usage for anything but monitoring. 577Especially, note that 578.Fn sio_write 579might block even if there is buffer space left; 580using the buffer usage to guess if 581.Fn sio_write 582would block is false and leads to unreliable programs \(en consider using 583.Xr poll 2 584for this. 585.Ss Handling buffer overruns and underruns 586When the application cannot accept recorded data fast enough, 587the record buffer (of size 588.Va appbufsz ) 589might overrun; in this case recorded data is lost. 590Similarly if the application cannot provide data to play 591fast enough, the play buffer underruns and silence is played 592instead. 593Depending on the 594.Va xrun 595parameter of the 596.Va sio_par 597structure, the audio subsystem will behave as follows: 598.Bl -tag -width "SIO_IGNORE" 599.It SIO_IGNORE 600The stream is paused during overruns and underruns, 601thus the current position (obtained through 602.Va sio_onmove ) 603stops being incremented. 604Once the overrun and/or underrun condition is gone, the stream is unpaused; 605play and record are always kept in sync. 606With this mode, the application cannot notice 607underruns and/or overruns and shouldn't care about them. 608.Pp 609This mode is the default. 610It's suitable for applications, 611like audio players and telephony, where time 612is not important and overruns or underruns are not short. 613.It SIO_SYNC 614If the play buffer underruns, then silence is played, 615but in order to reach the right position in time, 616the same amount of written samples will be 617discarded once the application is unblocked. 618Similarly, if the record buffer overruns, then 619samples are discarded, but the same amount of silence will be 620returned later. 621The current position (obtained through 622.Va sio_onmove ) 623is still incremented. 624When the play buffer underruns the play latency might become negative; 625when the record buffer overruns, the record latency might become 626larger than 627.Va bufsz . 628.Pp 629This mode is suitable for applications, like music production, 630where time is important and where underruns or overruns are 631short and rare. 632.It SIO_ERROR 633With this mode, on the first play buffer underrun or 634record buffer overrun, the stream is terminated and 635no other function than 636.Fn sio_close 637will succeed. 638.Pp 639This mode is mostly useful for testing; portable 640applications shouldn't depend on it, since it's not available 641on other systems. 642.El 643.Ss Controlling the volume 644The 645.Fn sio_setvol 646function can be used to set playback attenuation. 647The 648.Va vol 649parameter takes a value between 0 (maximum attenuation) 650and 651.Dv SIO_MAXVOL 652(no attenuation). 653It specifies the weight the audio subsystem will 654give to this stream. 655It is not meant to control hardware parameters like 656speaker gain; the 657.Xr mixerctl 1 658interface should be used for that purpose instead. 659.Pp 660An application can use the 661.Fn sio_onvol 662function to register a callback function that 663will be called each time the volume is changed, 664including when 665.Fn sio_setvol 666is used. 667The callback is always invoked when 668.Fn sio_onvol 669is called in order to provide the initial volume. 670An application can safely assume that once 671.Fn sio_onvol 672has returned a non-zero value, 673the callback has been invoked and thus 674the current volume is available. 675If there's no volume setting available, 676.Fn sio_onvol 677returns 0 and the callback is never invoked and calls to 678.Fn sio_setvol 679are ignored. 680.Pp 681The 682.Fn sio_onvol 683function can be called with a NULL argument to check whether 684a volume knob is available. 685.Ss Error handling 686Errors related to the audio subsystem 687(like hardware errors, dropped connections) and 688programming errors (e.g. call to 689.Fn sio_read 690on a play-only stream) are considered fatal. 691Once an error occurs, all functions taking a 692.Va sio_hdl 693argument, except 694.Fn sio_close 695and 696.Fn sio_eof , 697stop working (i.e. always return 0). 698.Pp 699The 700.Fn sio_eof 701function can be used at any stage; 702it returns 0 if there's no pending error, and a non-zero 703value if there's an error. 704.Sh RETURN VALUES 705The 706.Fn sio_open 707function returns the newly created handle on success or NULL 708on failure. 709The 710.Fn sio_setpar , 711.Fn sio_getpar , 712.Fn sio_getcap , 713.Fn sio_start , 714.Fn sio_stop , 715.Fn sio_pollfd 716and 717.Fn sio_setvol 718functions return 1 on success and 0 on failure. 719The 720.Fn sio_read 721and 722.Fn sio_write 723functions return the number of bytes transferred. 724.Sh ENVIRONMENT 725.Bl -tag -width "SNDIO_DEBUGXXX" -compact 726.It Ev AUDIODEVICE 727Device to use if 728.Fn sio_open 729is called with SIO_DEVANY 730.Va name 731argument. 732.It Ev SNDIO_DEBUG 733The debug level: 734may be a value between 0 and 2. 735.El 736.Sh FILES 737.Bl -tag -width "/tmp/aucat-<uid>/aucat0" -compact 738.It Pa /tmp/aucat-<uid>/aucat0 739Default path to 740.Xr sndiod 1 741socket to connect to. 742.It Pa /dev/audio 743Default 744.Xr audio 4 745device to use. 746.El 747.\".Sh EXAMPLES 748.\".Bd -literal -offset indent 749.\".Ed 750.Sh SEE ALSO 751.Xr sndiod 1 , 752.Xr audio 4 , 753.Xr sndio 7 , 754.Xr audio 9 755.Sh BUGS 756The 757.Xr audio 4 758driver cannot drain playback buffers in the background, thus if 759.Nm libsndio 760is used to directly access an 761.Xr audio 4 762device, 763the 764.Fn sio_stop 765function will stop playback immediately. 766.Pp 767The 768.Xr sndiod 1 769server doesn't implement flow control (for performance reasons). 770If the application doesn't consume recorded data fast enough then 771.Dq "control messages" 772are delayed and consequently 773.Va sio_onmove 774callback or volume changes may be delayed. 775.Pp 776The 777.Fn sio_open , 778.Fn sio_setpar , 779.Fn sio_getpar , 780.Fn sio_getcap , 781.Fn sio_start 782and 783.Fn sio_stop 784functions may block for a very short period of time, thus they should 785be avoided in code sections where blocking is not desirable. 786