1.\" $OpenBSD: sndiod.8,v 1.17 2024/05/03 16:47:15 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: May 3 2024 $ 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 188Same as 189.Fl f 190except that if the device is disconnected, 191the one given with the previous 192.Fl f 193or 194.Fl F 195option will be used. 196.It Fl f Ar device 197Add this 198.Xr sndio 7 199audio device to devices used for playing and/or recording. 200Preceding per-device options 201.Pq Fl aberwz 202apply to this device. 203Sub-devices 204.Pq Fl s 205that are applied after will be attached to this device. 206Device mode and parameters are determined from sub-devices 207attached to it. 208If no 209.Fl f 210option is used, 211.Nm 212will use 213.Pa rsnd/0 , rsnd/1 , 214.No ... , 215.Pa rsnd/3 . 216.It Fl j Ar flag 217Control whether program channels are joined or expanded if 218the number of channels requested by a program is not equal 219to the device number of channels. 220If the flag is 221.Va off 222then client channels are routed to the corresponding 223device channel, possibly discarding channels not present in the device. 224If the flag is 225.Va on , 226then a single client channel may be sent on multiple device channels, 227or multiple client channels may be sent to a single device channel. 228For instance, this feature could be used for mono to stereo conversions. 229The default is 230.Ar on . 231.It Fl L Ar addr 232Specify a local network address 233.Nm 234should listen on; 235.Nm 236will listen on TCP port 11025+n, where n is the unit number 237specified with 238.Fl U . 239Without this option, 240.Nm 241listens on the 242.Ux Ns -domain 243socket only, and is not reachable from any network. 244If the option argument is 245.Sq - 246then 247.Nm 248will accept connections from any address. 249As the communication is not secure, this 250option is only suitable for local networks where all hosts 251and users are trusted. 252.It Fl m Ar mode 253Set the sub-device mode. 254Valid modes are 255.Ar play , 256.Ar rec , 257and 258.Ar mon , 259corresponding to playback, recording and monitoring. 260A monitoring stream is a fake recording stream corresponding to 261the mix of all playback streams. 262Multiple modes can be specified, separated by commas, 263but the same sub-device cannot be used for both recording and monitoring. 264The default is 265.Ar play , Ns Ar rec 266(i.e. full-duplex). 267.It Fl Q Ar port 268Specify an alternate MIDI port to use. 269If it doesn't work, the one given with the last 270.Fl Q 271or 272.Fl q 273options will be used. 274For instance, this allows a USB MIDI controller to be replaced without 275the need to restart programs using it. 276.It Fl q Ar port 277Expose the given MIDI port. 278This allows multiple programs to share the port. 279If no 280.Fl q 281option is used, 282.Nm 283will use 284.Pa rmidi/0 , rmidi/1 , 285.No ... , 286.Pa rmidi/7 . 287.It Fl r Ar rate 288Attempt to force the device to use this sample rate in Hertz. 289The default is 48000. 290.It Fl s Ar name 291Add 292.Ar name 293to the list of sub-devices to expose. 294This allows clients to use 295.Nm 296instead of the physical audio device for audio input and output 297in order to share the physical device with other clients. 298Defining multiple sub-devices allows splitting a physical audio device 299into sub-devices having different properties (e.g. channel ranges). 300The given 301.Ar name 302corresponds to the 303.Dq option 304part of the 305.Xr sndio 7 306device name string. 307.It Fl t Ar mode 308Select the way clients are controlled by MIDI Machine Control (MMC) 309messages received by 310.Nm . 311If the mode is 312.Va off 313(the default), then programs are not affected by MMC messages. 314If the mode is 315.Va slave , 316then programs are started synchronously by MMC start messages; 317additionally, the server clock is exposed as MIDI Time Code (MTC) 318messages allowing MTC-capable software or hardware to be synchronized 319to audio programs. 320.It Fl U Ar unit 321Unit number. 322Each 323.Nm 324server instance has a unique unit number, 325used in 326.Xr sndio 7 327device names. 328The default is 0. 329.It Fl v Ar volume 330Software volume attenuation of playback. 331The value must be between 1 and 127, 332corresponding to \-42dB and \-0dB attenuation in 1/3dB steps. 333Clients inherit this parameter. 334Reducing the volume in advance allows a client's volume to stay independent 335from the number of clients as long as their number is small enough. 33618 volume units (i.e. \-6dB attenuation) allows the number 337of playback programs to be doubled. 338The default is 127. 339.It Fl w Ar flag 340Control 341.Nm 342behaviour when the maximum volume of the hardware is reached 343and a new program starts playing. 344This happens only when volumes are not properly set using the 345.Fl v 346option. 347If the flag is 348.Va on , 349then the master volume is automatically adjusted to avoid clipping. 350The default is 351.Va off . 352.It Fl z Ar nframes 353The audio device block size in frames. 354This is the number of frames between audio clock ticks, 355i.e. the clock resolution. 356If a sub-device is created with the 357.Fl t 358option, and MTC is used for synchronization, the clock 359resolution must be 96, 100 or 120 ticks per second for maximum 360accuracy. 361For instance, 100 ticks per second at 48000Hz corresponds 362to a 480 frame block size. 363The default is 480 or half of the buffer size 364.Pq Fl b , 365if the buffer size is set. 366.El 367.Pp 368On the command line, 369per-device parameters 370.Pq Fl aberwz 371must precede the device definition 372.Pq Fl f , 373and per-sub-device parameters 374.Pq Fl Ccjmtvx 375must precede the sub-device definition 376.Pq Fl s . 377Sub-device definitions 378.Pq Fl s 379must follow the definition of the device 380.Pq Fl f 381to which they are attached. 382.Pp 383If no audio devices 384.Pq Fl f 385are specified, 386settings are applied as if 387the default device is specified. 388If no sub-devices 389.Pq Fl s 390are specified for a device, a default sub-device is 391created attached to it. 392If a device 393.Pq Fl f 394is defined twice, both definitions are merged: 395parameters of the first one are used but sub-devices 396.Pq Fl s 397of both definitions are created. 398The default 399.Xr sndio 7 400device used by 401.Nm 402is 403.Pa rsnd/0 , 404and the default sub-device exposed by 405.Nm 406is 407.Pa snd/default . 408.Pp 409If 410.Nm 411is sent 412.Dv SIGINT 413or 414.Dv SIGTERM , 415it terminates. 416If 417.Nm 418is sent 419.Dv SIGHUP , 420it reopens all audio devices and MIDI ports. 421.Pp 422By default, when the program cannot accept 423recorded data fast enough or cannot provide data to play fast enough, 424the program is paused, i.e. samples that cannot be written are discarded 425and samples that cannot be read are replaced by silence. 426If a sub-device is created with the 427.Fl t 428option, then recorded samples are discarded, 429but the same amount of silence will be written 430once the program is unblocked, in order to reach the right position in time. 431Similarly silence is played, but the same amount of samples will be discarded 432once the program is unblocked. 433This ensures proper synchronization between programs. 434.Sh MIDI CONTROL 435.Nm 436creates a MIDI port with the same name as the exposed audio 437sub-device to which MIDI programs can connect. 438.Nm 439exposes the audio device clock 440and allows audio device properties to be controlled 441through MIDI. 442.Pp 443A MIDI channel is assigned to each stream, and the volume 444is changed using the standard volume controller (number 7). 445Similarly, when the audio client changes its volume, 446the same MIDI controller message is sent out; it can be used 447for instance for monitoring or as feedback for motorized 448faders. 449.Pp 450The master volume can be changed using the standard master volume 451system exclusive message. 452.Pp 453Streams created with the 454.Fl t 455option are controlled by the following MMC messages: 456.Bl -tag -width relocateXXX -offset indent 457.It relocate 458This message is ignored by audio 459.Nm 460clients, but the given time position is sent to MIDI ports as an MTC 461.Dq "full frame" 462message forcing all MTC-slaves to relocate to the given 463position (see below). 464.It start 465Put all streams in starting mode. 466In this mode, 467.Nm 468waits for all streams to become ready 469to start, and then starts them synchronously. 470Once started, new streams can be created 471.Pq Nm sndiod 472but they will be blocked 473until the next stop-to-start transition. 474.It stop 475Put all streams in stopped mode (the default). 476In this mode, any stream attempting to start playback or recording 477is paused. 478Client streams that are already 479started are not affected until they stop and try to start again. 480.El 481.Pp 482Streams created with the 483.Fl t 484option export the 485.Nm 486device clock using MTC, allowing non-audio 487software or hardware to be synchronized to the audio stream. 488Maximum accuracy is achieved when the number of blocks per 489second is equal to one of the standard MTC clock rates (96, 100 and 120Hz). 490The following sample rates 491.Pq Fl r 492and block sizes 493.Pq Fl z 494are recommended: 495.Pp 496.Bl -bullet -offset indent -compact 497.It 49844100Hz, 441 frames (MTC rate is 100Hz) 499.It 50048000Hz, 400 frames (MTC rate is 120Hz) 501.It 50248000Hz, 480 frames (MTC rate is 100Hz) 503.It 50448000Hz, 500 frames (MTC rate is 96Hz) 505.El 506.Pp 507For instance, the following command will create two devices: 508the default 509.Va snd/default 510and a MIDI-controlled 511.Va snd/mmc : 512.Bd -literal -offset indent 513$ sndiod -r 48000 -z 400 -s default -t slave -s mmc 514.Ed 515.Pp 516Streams connected to 517.Va snd/default 518behave normally, while streams connected to 519.Va snd/mmc 520wait for the MMC start signal and start synchronously. 521Regardless of which device a stream is connected to, 522its playback volume knob is exposed. 523.Sh HOT PLUGGING 524If devices specified with 525.Fl F 526are unavailable when needed or unplugged at runtime, 527.Nm 528will attempt to seamlessly fall back to the last device specified. 529.Pp 530.Nm 531will not automatically switch to specified device that is plugged at runtime. 532Instead, 533.Xr sndioctl 1 534must be used to change the 535.Va server.device 536control. 537.Pp 538For instance, specifying a USB device with 539.Fl F 540following a PCI device with 541.Fl f 542allows 543.Nm 544to use the USB one preferably when it's connected 545and to fall back to the PCI one when it's disconnected. 546.Sh EXAMPLES 547Start server using default parameters, creating an 548additional sub-device for output to channels 2:3 only (rear speakers 549on most cards), exposing the 550.Pa snd/default 551and 552.Pa snd/rear 553devices: 554.Bd -literal -offset indent 555$ sndiod -s default -c 2:3 -s rear 556.Ed 557.Pp 558Start server creating the default sub-device with low volume and 559an additional sub-device for high volume output, exposing the 560.Pa snd/default 561and 562.Pa snd/max 563devices: 564.Bd -literal -offset indent 565$ sndiod -v 65 -s default -v 127 -s max 566.Ed 567.Pp 568Start server configuring the audio device to use 569a 48kHz sample frequency, 240-frame block size, 570and 2-block buffers. 571The corresponding latency is 10ms, which is 572the time it takes the sound to propagate 3.5 meters. 573.Bd -literal -offset indent 574$ sndiod -r 48000 -b 480 -z 240 575.Ed 576.Sh SEE ALSO 577.Xr sndio 7 578.Sh BUGS 579Resampling is low quality; down-sampling especially should be avoided 580when recording. 581.Pp 582If 583.Fl a Ar off 584is used, 585.Nm 586creates sub-devices to expose first 587and then opens the audio hardware on demand. 588Technically, this allows 589.Nm 590to attempt to use one of the sub-devices it exposes as an audio device, 591creating a deadlock. 592There's nothing to prevent the user 593from shooting themselves in the foot by creating such a deadlock. 594