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