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