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