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