1.\" $OpenBSD: sndiod.8,v 1.2 2016/01/18 11:38:07 ratchov Exp $ 2.\" 3.\" Copyright (c) 2006-2012 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: January 18 2016 $ 18.Dt SNDIOD 8 19.Os 20.Sh NAME 21.Nm sndiod 22.Nd audio/MIDI server 23.Sh SYNOPSIS 24.Nm sndiod 25.Bk -words 26.Op Fl d 27.Op Fl a Ar flag 28.Op Fl b Ar nframes 29.Op Fl C Ar min : Ns Ar max 30.Op Fl c Ar min : Ns Ar max 31.Op Fl e Ar enc 32.Op Fl f Ar device 33.Op Fl j Ar flag 34.Op Fl L Ar addr 35.Op Fl m Ar mode 36.Op Fl q Ar port 37.Op Fl r Ar rate 38.Op Fl s Ar name 39.Op Fl t Ar mode 40.Op Fl U Ar unit 41.Op Fl v Ar volume 42.Op Fl w Ar flag 43.Op Fl z Ar nframes 44.Ek 45.Sh DESCRIPTION 46The 47.Nm 48daemon is an intermediate layer between 49audio or MIDI programs and the hardware. 50It performs the necessary audio processing to 51allow any program to work on any supported hardware. 52By default, 53.Nm 54accepts connections from programs 55running on the same system only; 56it initializes only when programs are using its services, 57allowing 58.Nm 59to consume a negligible amount of system resources the rest of the time. 60Systems with no audio hardware can use 61.Nm 62to keep hot-pluggable devices usable by default at 63virtually no cost. 64.Pp 65.Nm 66operates as follows: it exposes at least one 67.Em sub-device 68that any number of audio programs can connect to and use as if it was 69audio hardware. 70During playback, 71.Nm 72receives audio data concurrently from all programs, mixes it and sends 73the result to the hardware device. 74Similarly, during recording it duplicates audio data recorded 75from the device and sends it to all programs. 76Since audio data flows through the 77.Nm 78process, it has the opportunity to process audio data on the fly: 79.Pp 80.Bl -bullet -offset indent -compact 81.It 82Change the sound encoding to overcome incompatibilities between 83software and hardware. 84.It 85Route the sound from one channel to another, 86join stereo or split mono. 87.It 88Control the per-application playback volume as well as the 89master volume. 90.It 91Monitor the sound being played, allowing one program to record 92what other programs play. 93.El 94.Pp 95Processing is configured on a per sub-device basis, meaning that 96the sound of all programs connected to the same sub-device will be 97processed according to the same configuration. 98Multiple sub-devices can be defined, allowing multiple configurations 99to coexist. 100The user selects the configuration a given program will use 101by selecting the sub-device the program uses. 102.Pp 103.Nm 104exposes MIDI thru boxes (hubs), 105allowing programs to send MIDI messages to each other 106or to hardware MIDI ports in a uniform way. 107.Pp 108Finally, 109.Nm 110exposes a control MIDI port usable for: 111.Pp 112.Bl -bullet -offset indent -compact 113.It 114Volume control. 115.It 116Common clock source for audio and MIDI programs. 117.It 118Start, stop and relocate groups of audio programs. 119.El 120.Pp 121The options are as follows: 122.Bl -tag -width Ds 123.It Fl a Ar flag 124Control whether 125.Nm 126opens the audio device or the MIDI port only when needed or keeps 127it open all the time. 128If the flag is 129.Va on 130then the audio device or MIDI port is kept open all the time, ensuring 131no other program can steal it. 132If the flag is 133.Va off , 134then it's automatically closed, allowing other programs to have direct 135access to the audio device, or the device to be disconnected. 136The default is 137.Va off . 138.It Fl b Ar nframes 139The buffer size of the audio device in frames. 140A frame consists of one sample for each channel in the stream. 141This is the number of frames that will be buffered before being played 142and thus controls the playback latency. 143The default is 7680 or twice the block size 144.Pq Fl z , 145if the block size is set. 146.It Xo 147.Fl C Ar min : Ns Ar max , 148.Fl c Ar min : Ns Ar max 149.Xc 150The range of channel numbers for recording and playback directions, 151respectively any client is allowed to use. 152This is a subset of the audio device channels. 153The default is 0:1, i.e. stereo. 154.It Fl d 155Enable debugging to standard error, and do not disassociate from the 156controlling terminal. 157Can be specified multiple times to further increase log verbosity. 158.It Fl e Ar enc 159Attempt to configure the device to use this encoding. 160The default is 161.Va s16 . 162Encoding names use the following scheme: signedness 163.Po 164.Va s 165or 166.Va u 167.Pc 168followed 169by the precision in bits, the byte-order 170.Po 171.Va le 172or 173.Va be 174.Pc , 175the number of 176bytes per sample, and the alignment 177.Po 178.Va msb 179or 180.Va lsb 181.Pc . 182Only the signedness and the precision are mandatory. 183Examples: 184.Va u8 , s16le , s24le3 , s24le4lsb . 185.It Fl f Ar device 186Add this 187.Xr sndio 7 188audio device to devices used for playing and/or recording. 189Preceding per-device options 190.Pq Fl aberwz 191apply to this device. 192Sub-devices 193.Pq Fl s 194that are applied after will be attached to this device. 195Device mode and parameters are determined from sub-devices 196attached to it. 197.It Fl j Ar flag 198Control whether program channels are joined or expanded if 199the number of channels requested by a program is not equal 200to the device number of channels. 201If the flag is 202.Va off 203then client channels are routed to the corresponding 204device channel, possibly discarding channels not present in the device. 205If the flag is 206.Va on , 207then a single client channel may be sent on multiple device channels, 208or multiple client channels may be sent to a single device channel. 209For instance, this feature could be used for mono to stereo conversions. 210The default is 211.Ar on . 212.It Fl L Ar addr 213Specify a local network address 214.Nm 215should listen on; 216.Nm 217will listen on TCP port 11025+n, where n is the unit number 218specified with 219.Fl U . 220Without this option, 221.Nm 222listens on the 223.Ux Ns -domain 224socket only, and is not reachable from any network. 225If the option argument is 226.Sq - 227then 228.Nm 229will accept connections from any address. 230As the communication is not secure, this 231option is only suitable for local networks where all hosts 232and users are trusted. 233.It Fl m Ar mode 234Set the sub-device mode. 235Valid modes are 236.Ar play , 237.Ar rec , 238and 239.Ar mon , 240corresponding to playback, recording and monitoring. 241A monitoring stream is a fake recording stream corresponding to 242the mix of all playback streams. 243Multiple modes can be specified, separated by commas, 244but the same sub-device cannot be used for both recording and monitoring. 245The default is 246.Ar play , Ns Ar rec 247(i.e. full-duplex). 248.It Fl q Ar port 249Expose the given MIDI port. 250This allows multiple programs to share the port. 251.It Fl r Ar rate 252Attempt to force the device to use this sample rate in Hertz. 253The default is 48000. 254.It Fl s Ar name 255Add 256.Ar name 257to the list of sub-devices to expose. 258This allows clients to use 259.Nm 260instead of the physical audio device for audio input and output 261in order to share the physical device with other clients. 262Defining multiple sub-devices allows splitting a physical audio device 263into sub-devices having different properties (e.g. channel ranges). 264The given 265.Ar name 266corresponds to the 267.Dq option 268part of the 269.Xr sndio 7 270device name string. 271.It Fl t Ar mode 272Select the way clients are controlled by MIDI Machine Control (MMC) 273messages received by 274.Nm . 275If the mode is 276.Va off 277(the default), then programs are not affected by MMC messages. 278If the mode is 279.Va slave , 280then programs are started synchronously by MMC start messages; 281additionally, the server clock is exposed as MIDI Time Code (MTC) 282messages allowing MTC-capable software or hardware to be synchronized 283to audio programs. 284.It Fl U Ar unit 285Unit number. 286Each 287.Nm 288server instance has an unique unit number, 289used in 290.Xr sndio 7 291device names. 292The default is 0. 293.It Fl v Ar volume 294Software volume attenuation of playback. 295The value must be between 1 and 127, 296corresponding to \-42dB and \-0dB attenuation in 1/3dB steps. 297Clients inherit this parameter. 298Reducing the volume in advance allows a client's volume to stay independent 299from the number of clients as long as their number is small enough. 30018 volume units (i.e. \-6dB attenuation) allows the number 301of playback programs to be doubled. 302The default is 118 i.e. \-3dB. 303.It Fl w Ar flag 304Control 305.Nm 306behaviour when the maximum volume of the hardware is reached 307and a new program starts playing. 308This happens only when volumes are not properly set using the 309.Fl v 310option. 311If the flag is 312.Va on , 313then the master volume is automatically adjusted to avoid clipping. 314Using 315.Va off 316makes sense in the rare situation where all programs lower their volumes. 317The default is 318.Va on . 319.It Fl z Ar nframes 320The audio device block size in frames. 321This is the number of frames between audio clock ticks, 322i.e. the clock resolution. 323If a sub-device is created with the 324.Fl t 325option, and MTC is used for synchronization, the clock 326resolution must be 96, 100 or 120 ticks per second for maximum 327accuracy. 328For instance, 100 ticks per second at 48000Hz corresponds 329to a 480 frame block size. 330The default is 960 or half of the buffer size 331.Pq Fl b , 332if the buffer size is set. 333.El 334.Pp 335On the command line, 336per-device parameters 337.Pq Fl aberwz 338must precede the device definition 339.Pq Fl f , 340and per-sub-device parameters 341.Pq Fl Ccjmtvx 342must precede the sub-device definition 343.Pq Fl s . 344Sub-device definitions 345.Pq Fl s 346must follow the definition of the device 347.Pq Fl f 348to which they are attached. 349.Pp 350If no audio devices 351.Pq Fl f 352are specified, 353settings are applied as if 354the default device is specified. 355If no sub-devices 356.Pq Fl s 357are specified for a device, a default sub-device is 358created attached to it. 359If a device 360.Pq Fl f 361is defined twice, both definitions are merged: 362parameters of the first one are used but sub-devices 363.Pq Fl s 364of both definitions are created. 365The default 366.Xr sndio 7 367device used by 368.Nm 369is 370.Pa rsnd/0 , 371and the default sub-device exposed by 372.Nm 373is 374.Pa snd/0 . 375.Pp 376If 377.Nm 378is sent 379.Dv SIGHUP , 380.Dv SIGINT 381or 382.Dv SIGTERM , 383it terminates. 384.Pp 385By default, when the program cannot accept 386recorded data fast enough or cannot provide data to play fast enough, 387the program is paused, i.e. samples that cannot be written are discarded 388and samples that cannot be read are replaced by silence. 389If a sub-device is created with the 390.Fl t 391option, then recorded samples are discarded, 392but the same amount of silence will be written 393once the program is unblocked, in order to reach the right position in time. 394Similarly silence is played, but the same amount of samples will be discarded 395once the program is unblocked. 396This ensures proper synchronization between programs. 397.Sh MIDI CONTROL 398.Nm 399creates a MIDI port with the same name as the exposed audio 400sub-device to which MIDI programs can connect. 401.Nm 402exposes the audio device clock 403and allows audio device properties to be controlled 404through MIDI. 405.Pp 406A MIDI channel is assigned to each stream, and the volume 407is changed using the standard volume controller (number 7). 408Similarly, when the audio client changes its volume, 409the same MIDI controller message is sent out; it can be used 410for instance for monitoring or as feedback for motorized 411faders. 412.Pp 413The master volume can be changed using the standard master volume 414system exclusive message. 415.Pp 416Streams created with the 417.Fl t 418option are controlled by the following MMC messages: 419.Bl -tag -width relocateXXX -offset indent 420.It relocate 421This message is ignored by audio 422.Nm 423clients, but the given time position is sent to MIDI ports as an MTC 424.Dq "full frame" 425message forcing all MTC-slaves to relocate to the given 426position (see below). 427.It start 428Put all streams in starting mode. 429In this mode, 430.Nm 431waits for all streams to become ready 432to start, and then starts them synchronously. 433Once started, new streams can be created 434.Pq Nm sndiod 435but they will be blocked 436until the next stop-to-start transition. 437.It stop 438Put all streams in stopped mode (the default). 439In this mode, any stream attempting to start playback or recording 440is paused. 441Client streams that are already 442started are not affected until they stop and try to start again. 443.El 444.Pp 445Streams created with the 446.Fl t 447option export the 448.Nm 449device clock using MTC, allowing non-audio 450software or hardware to be synchronized to the audio stream. 451Maximum accuracy is achieved when the number of blocks per 452second is equal to one of the standard MTC clock rates (96, 100 and 120Hz). 453The following sample rates 454.Pq Fl r 455and block sizes 456.Pq Fl z 457are recommended: 458.Pp 459.Bl -bullet -offset indent -compact 460.It 46144100Hz, 441 frames (MTC rate is 100Hz) 462.It 46348000Hz, 400 frames (MTC rate is 120Hz) 464.It 46548000Hz, 480 frames (MTC rate is 100Hz) 466.It 46748000Hz, 500 frames (MTC rate is 96Hz) 468.El 469.Pp 470For instance, the following command will create two devices: 471the default 472.Va snd/0 473and a MIDI-controlled 474.Va snd/0.mmc : 475.Bd -literal -offset indent 476$ sndiod -r 48000 -z 400 -s default -t slave -s mmc 477.Ed 478.Pp 479Streams connected to 480.Va snd/0 481behave normally, while streams connected to 482.Va snd/0.mmc 483wait for the MMC start signal and start synchronously. 484Regardless of which device a stream is connected to, 485its playback volume knob is exposed. 486.Sh EXAMPLES 487Start server using default parameters, creating an 488additional sub-device for output to channels 2:3 only (rear speakers 489on most cards), exposing the 490.Pa snd/0 491and 492.Pa snd/0.rear 493devices: 494.Bd -literal -offset indent 495$ sndiod -s default -c 2:3 -s rear 496.Ed 497.Pp 498Start server creating the default sub-device with low volume and 499an additional sub-device for high volume output, exposing the 500.Pa snd/0 501and 502.Pa snd/0.max 503devices: 504.Bd -literal -offset indent 505$ sndiod -v 65 -s default -v 127 -s max 506.Ed 507.Pp 508Start server configuring the audio device to use 509a 48kHz sample frequency, 240-frame block size, 510and 2-block buffers. 511The corresponding latency is 10ms, which is 512the time it takes the sound to propagate 3.5 meters. 513.Bd -literal -offset indent 514$ sndiod -r 48000 -b 480 -z 240 515.Ed 516.Sh SEE ALSO 517.Xr sndio 7 518.Sh BUGS 519Resampling is low quality; down-sampling especially should be avoided 520when recording. 521.Pp 522Processing is done using 16-bit arithmetic, 523thus samples with more than 16 bits are rounded. 52416 bits (i.e. 97dB dynamic) are largely enough for most applications though. 525Processing precision can be increased to 24-bit at compilation time though. 526.Pp 527If 528.Fl a Ar off 529is used, 530.Nm 531creates sub-devices to expose first 532and then opens the audio hardware on demand. 533Technically, this allows 534.Nm 535to attempt to use one of the sub-devices it exposes as an audio device, 536creating a deadlock. 537There's nothing to prevent the user 538from shooting himself in the foot by creating such a deadlock. 539