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