xref: /netbsd-src/share/man/man4/midi.4 (revision 01869ca4d24a86379a68731bf9706a9f0820fe4e)
1.\" $NetBSD: midi.4,v 1.32 2017/07/03 21:30:58 wiz Exp $
2.\"
3.\" Copyright (c) 1999-2017 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Lennart Augustsson and Chapman Flack.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd April 28, 2017
31.Dt MIDI 4
32.Os
33.Sh NAME
34.Nm midi
35.Nd device-independent MIDI driver layer
36.Sh SYNOPSIS
37.Cd "midi* at midibus?"
38.Cd "midi* at pcppi?"
39.Cd "pseudo-device sequencer"
40.Pp
41.In sys/types.h
42.In sys/midiio.h
43.Sh DESCRIPTION
44The
45.Nm
46driver is the machine independent layer over anything that can
47source or sink a
48.Tn MIDI
49data stream, whether a physical
50.Tn "MIDI IN"
51or
52.Tn "MIDI OUT"
53jack on a soundcard, cabled to some external synthesizer or input controller,
54an on-board programmable tone generator, or a single jack, synthesizer, or
55controller component within a complex
56.Tn USB
57or
58.Tn IEEE1394
59.Tn MIDI
60device that has several
61such components and appears as several
62.Tn MIDI
63streams.
64.Ss Concepts
65One
66.Tn MIDI
67data stream is a unidirectional stream of
68.Tn MIDI
69messages, as could be carried over one
70.Tn MIDI
71cable in the
72.Tn "MIDI 1.0"
73specification.
74Many
75.Tn MIDI
76messages
77carry a four-bit channel number, creating up to 16
78.Tn MIDI
79channels within a single
80.Tn MIDI
81stream.
82There may be multiple consumers of a
83.Tn MIDI
84stream, each configured
85to react only to messages on specific channels; the sets of channels different
86consumers react to need not be disjoint.
87Many modern devices such as multitimbral keyboards and tone generators
88listen on all 16 channels, or may be viewed as collections of 16
89independent consumers each listening on one channel.
90.Tn MIDI
91defines some messages that take no channel number, and
92apply to all consumers of the
93stream on which they are sent.
94For an inbound stream,
95.Nm
96is a promiscuous
97receiver, capturing all messages regardless of channel number.
98For an outbound stream, the writer can specify a channel number
99per message; there is no notion of binding the stream to one
100destination channel in advance.
101.Pp
102A single
103.Nm
104device instance is the endpoint of one outbound stream, one
105inbound stream, or one of each.
106In the third case, the write and read sides are independent
107.Tn MIDI
108streams.
109For example, a soundcard driver may map its
110.Tn "MIDI OUT"
111and
112.Tn "MIDI IN"
113jacks to the write and read sides of a single device instance, but
114those jacks can be cabled to completely different pieces of gear.
115Information from
116.Xr dmesg 8 ,
117and a diagram of any external
118.Tn MIDI
119cabling, will help clarify the mapping.
120.Ss "Underlying drivers and MIDI protocol"
121Drivers
122.Nm
123can attach include soundcard drivers, many of
124which support a
125.Tn UART
126resembling Roland's
127.Tn MPU401
128and handled by
129.Xr mpu 4 ,
130.Tn "USB MIDI"
131devices via
132.Xr umidi 4 ,
133and on-board devices that can make sounds,
134whether a lowly
135.Tn PC
136speaker or a Yamaha
137.Tn OPL .
138Serial port and
139.Tn IEEE1394
140connections are currently science fiction.
141.Pp
142The
143.Tn MIDI
144protocol permits some forms of message compression such as running
145status and hidden note-off.
146Received messages on inbound streams are always canonicalized by
147.Nm
148before presentation to higher layers.
149Messages for transmission are accepted by
150.Nm
151in any valid form.
152.Ss "Device access"
153Access to
154.Nm
155device instances can be through the raw device nodes,
156.Pa /dev/rmidiN ,
157or through the sequencer,
158.Pa /dev/music .
159.Ss "Raw MIDI access"
160A
161.Pa /dev/rmidiN
162device supports
163.Xr read 2 ,
164.Xr write 2 ,
165.Xr ioctl 2 ,
166.Xr select 2 Ns / Ns
167.Xr poll 2
168and the corresponding
169.Xr kevent 2
170filters,
171and may be opened
172only when it is not already open.
173It may be opened in
174.Dv O_RDONLY ,
175.Dv O_WRONLY ,
176or
177.Dv O_RDWR
178mode, but a later
179.Xr read 2
180or
181.Xr write 2
182will return \-1 if the device has no associated
183input or output stream, respectively.
184.Pp
185Bytes written are passed as quickly as possible to the underlying driver
186as complete
187.Tn MIDI
188messages; a maximum of two bytes at the end of a
189.Xr write 2
190may remain buffered if they do not complete a message, until
191completed by a following
192.Xr write 2 .
193.Pp
194A
195.Xr read 2
196will not block or return
197.Er EWOULDBLOCK
198when it could immediately return any nonzero count, and
199.Tn MIDI
200messages received are available to
201.Xr read 2
202as soon as they are complete,
203with a maximum of two received bytes remaining buffered if they do not
204complete a message.
205.Pp
206As all
207.Tn MIDI
208messages are three bytes or fewer except for System Exclusive,
209which can have arbitrary length, these rules imply that System Exclusive
210messages are the only ones of which some bytes can be delivered before
211all are available.
212.Pp
213System Realtime messages are passed with minimum delay in either direction,
214ahead of any possible buffered incomplete message.
215As a result, they will never interrupt any
216.Tn MIDI
217message except possibly System Exclusive.
218.Pp
219A
220.Xr read 2
221with a buffer large enough to accommodate the first complete
222message available will be satisfied with as many complete messages as
223will fit.
224A buffer too small for the first complete
225message will be filled to capacity.
226Therefore, an application that reads from an
227.Pa rmidi
228device with buffers of three bytes or larger need never parse
229across read boundaries to assemble a received message, except possibly in
230the case of a System Exclusive message.
231However, if the application reads through a buffering layer such as
232.Xr fread 3 ,
233this property will not be preserved.
234.Pp
235The
236.Nm
237driver itself supports the
238.Xr ioctl 2
239operations
240.Dv FIOASYNC ,
241.Dv FIONBIO ,
242and
243.Dv FIONREAD .
244Underlying devices may support others.
245The value returned for
246.Dv FIONREAD
247reflects the size in bytes of complete messages
248(or System Exclusive chunks) ready to read.
249If the
250.Xr ioctl 2
251returns
252.Va n
253and a
254.Xr read 2
255of size
256.Va n
257is issued,
258.Va n
259bytes will be read, but if a
260.Xr read 2
261of
262size
263.Va m
264<
265.Va n
266is issued, fewer than
267.Va m
268bytes may be read if
269.Va m
270does not fall
271on a message/chunk boundary.
272.Pp
273Raw
274.Tn MIDI
275access can be used to receive bulk dumps from synthesizers, download
276bulk data to them, and so on.
277Simple patching of one device to another can be
278done at the command line, as with
279.Dl $ cat -u 0<>/dev/rmidi0 1>&0
280which will loop all messages received on the input stream of
281.Pa rmidi0
282input stream  back to its output
283stream in real time.
284However, an attempt to record and play back music with
285.Dl $ cat /dev/rmidiN >foo; cat foo >/dev/rmidiN
286will be disappointing.
287The file
288.Pa foo
289will contain all of the notes that were played, but because
290.Tn MIDI
291messages carry
292no explicit timing, the
293.Sq "playback"
294will reproduce them all at once, as fast as
295they can be transmitted.
296To preserve timing information, the sequencer device can be used.
297.Ss "Active Sensing"
298The
299.Tn MIDI
300protocol includes a keepalive function called Active Sensing.
301In any receiver that has
302.Em not
303received at least one Active Sense
304.Tn MIDI
305message, the
306feature is suppressed and no timeout applies.
307If at least one such message has
308been received, the lapse of any subsequent 300 ms interval without receipt of
309any message reflects loss of communication, and the receiver should silence any
310currently sounding notes and return to non-Active-Sensing behavior.
311A sender using Active Sensing generally avoids 300 ms gaps in
312transmission by sending Active Sense messages (which have no other
313effect) as needed when there is no other traffic to send in the
314interval.
315This feature can be important for
316.Tn MIDI ,
317which relies on separate Note On and Note Off messages, to avoid notes stuck on
318indefinitely if communication is interrupted before a Note Off
319message arrives.
320.Pp
321This protocol is supported in
322.Nm .
323An outbound stream will be kept alive
324by sending Active Sense messages as needed, beginning after any real
325traffic is sent on the stream, and continuing until the stream is closed.
326On an inbound stream, if any Active Sense has been received, then a process
327reading an
328.Pa rmidi
329device will see an end-of-file indication if the input timeout elapses.
330The stream remains open, the driver reverts to enforcing no timeout, and the
331process may continue to read for more input.
332Subsequent receipt of an
333Active Sense message will re-arm the timeout.
334As received Active Sense messages are handled by
335.Nm ,
336they are not included among messages read from the
337.Pa /dev/rmidiN
338device.
339.Pp
340These rules support end-to-end Active Sensing behavior in simple cases
341without special action in an application.
342For example, in
343.Dl $ cat -u /dev/rmidi0 >/dev/rmidi1
344if the input stream to
345.Pa rmidi0
346is lost, the
347.Xr cat 1
348command exits; on the
349.Xr close 2
350of
351.Pa rmidi1 ,
352.Nm
353ceases to send Active Sense messages, and the receiving
354device will detect the loss and silence any outstanding notes.
355.Ss "Access through the sequencer"
356To play music using the raw
357.Tn MIDI
358.Tn API
359would require an application to
360issue many small writes with very precise timing.
361The sequencer device,
362.Pa /dev/music ,
363can manage the timing of
364.Tn MIDI
365data in the kernel, to avoid
366such demanding real-time constraints on a user process.
367.Pp
368The
369.Pa /dev/music
370device can be opened only when it is not already open.
371When opened, the sequencer internally opens all
372.Tn MIDI
373instances existing
374in the system that are not already open at their raw nodes; any attempts
375to open them at their raw nodes while the sequencer is open will fail.
376All access to the corresponding
377.Tn MIDI
378streams will then be through the
379sequencer.
380.Pp
381Reads and writes of
382.Pa /dev/music
383pass eight-byte event structures defined in
384.In sys/midiio.h
385(which see for their documentation and examples of use).
386Some events correspond to
387.Tn MIDI
388messages, and carry an integer
389.Va device
390field to identify one of the
391.Tn MIDI
392devices opened by the sequencer.
393Other events carry timing information interpreted or generated by the
394sequencer itself.
395.Pp
396A message received on an input stream is wrapped in a sequencer event
397along with the device index of the stream it arrived on, and queued for
398the reader of
399.Pa /dev/music .
400If a measurable time interval passed since the
401last preceding message, a timing event that represents a delay for that interval
402is queued ahead of the received event.
403The sequencer handles output events by
404interpreting any timing event, and routing any
405.Tn MIDI
406message event at the proper time to
407an underlying output stream according to its
408.Va device
409index.
410Therefore
411.Dl $ cat /dev/music >foo; cat foo >/dev/music
412can be expected to capture and reproduce an input performance including
413timing.
414.Pp
415The process of playing back a complex
416.Tn MIDI
417file is illustrated below.
418The file may contain several tracks\(emfour, in this example\(emof
419.Tn MIDI
420events, each marked with a device index and a time stamp, that may
421overlap in time.
422In the example,
423.Va a ,
424.Va b ,
425and
426.Va c
427are device indices of
428the three output
429.Tn MIDI
430streams; the left-hand digit in each input event represents a
431.Tn MIDI
432channel on the selected stream, and the right-hand digit represents
433a time for the event's occurrence.
434As illustrated, the input tracks are not firmly associated with
435output streams; any track may contain events for any stream.
436.Bd -literal
437     |      |     a2|4     |
438   a0|3     |     c1|3   c0|3
439     |    b0|2    b1|2     |
440     |    b1|1      |    c0|1
441   a0|0     |     b0|0     |
442     v      v       v      v
443  +---------------------------+
444  | merge to 1 ordered stream |
445  | user code, eg midiplay(1) |
446  +---------------------------+
447              b1|2
448              b0|2
449              c0|1
450              b1|1
451              b0|0
452              a0|0
453                v
454  _______+-------------+_______user
455         | /dev/music  |     kernel
456         | (sequencer) |
457         +-------------+
458           |    1    0
459     +-----'    |    '-----.
460     0          0          |
461     v          v          v
462  +-------+ +--------+ +---------+
463  |midi(4)| |midi(4) | |midi(4)  |
464  |rmidia | |rmidib  | |rmidic   |
465  +-------+ +--------+ +---------+
466  | mpu(4)| |umidi(4)| |midisyn  |
467  +-------+ +--------+ +---------+
468  |  HW   |     |      | opl(4)  |
469  | MIDI  |     U      +---------+
470  | UART  |      S     | internal|
471  +-------+       B    |   tone  |
472      |           |    |generator|
473      v           |    +---------+
474   external       v
475  MIDI device  external
476              MIDI device
477.Ed
478.Pp
479A user process must merge the tracks into a single stream of sequencer
480.Tn MIDI
481and timing events in order by desired timing.
482The sequencer obeys the timing events and distributes the
483.Tn MIDI
484events to the three destinations,
485in this case two external devices connected to a sound card
486.Tn UART
487and a
488.Tn USB
489interface, and an
490.Tn OPL
491tone generator on a sound card.
492.Sh NOTES
493Use of
494.Xr select 2 Ns / Ns
495.Xr poll 2
496with the sequencer is supported, however, there is no guarantee that a
497.Xr write 2
498will not block or return
499.Er EWOULDBLOCK
500if it begins with a timer-wait event, even if
501.Xr select 2 Ns / Ns
502.Xr poll 2
503reported the sequencer writable.
504.Pp
505The delivery of a realtime message ahead of buffered bytes of an incomplete
506message may cause the realtime message to seem, in a saved byte stream, to have
507arrived up to 640 us earlier than it really did, at
508.Tn MIDI
5091.0 data rates.
510Higher data rates make the effect less significant.
511.Pp
512Another sequencer device,
513.Pa /dev/sequencer ,
514is provided only for backward
515compatibility with an obsolete
516.Tn OSS
517interface in which some sequencer events
518were four-byte records.
519It is not further documented here, and the
520.Pa /dev/music
521.Tn API
522should be used in new code.
523The
524.Pa /dev/sequencer
525emulation is implemented only for writing, and that might not be complete.
526.Sh IMPLEMENTATION NOTES
527Some hardware devices supporting
528.Nm
529lack transmit-ready interrupts, and some have the capability in
530hardware but currently lack driver support.
531They can be recognized by the annotation
532.Li "(CPU-intensive output)"
533in
534.Xr dmesg 8 .
535While suitable for music playback, they may have an objectionable impact on
536system responsiveness during bulk transmission such as patch downloads, and
537are best avoided for that purpose if other suitable devices are present.
538.Pp
539Buffer space in
540.Nm
541itself is adequate for about 200 ms of traffic at
542.Tn "MIDI 1.0"
543data rates, per stream.
544.Pp
545Event counters record bytes and messages discarded because of protocol
546errors or buffer overruns, and can be viewed with
547.Li "vmstat -e" .
548They can be useful in diagnosing flaky cables and other communication
549problems.
550.Pp
551.\" These two paragraphs really belong not here but in a midi(9) man page,
552.\" which should one day exist.
553A raw sound generator uses the
554.Sy midisyn
555layer to present a
556.Tn MIDI
557message-driven interface attachable by
558.Nm .
559.Pp
560While
561.Nm
562accepts messages for transmission in any valid mixture of compressed
563or canonical form, they are always presented to an underlying driver
564in the form it prefers.
565Drivers for simple
566.Tn UART Ns -like
567devices
568register their preference for a compressed byte stream, while those like
569.Xr umidi 4 ,
570which uses a packet protocol, or
571.Sy midisyn ,
572which interprets complete
573messages, register for intact canonical messages.
574This design eliminates the
575need for compression and canonicalization logic from all layers above and below
576.Nm
577itself.
578.Sh FILES
579.Bl -tag -width /dev/sequencer -compact
580.It Pa /dev/rmidiN
581.It Pa /dev/music
582.It Pa /dev/sequencer
583.El
584.Sh ERRORS
585In addition to other errors documented for the
586.Xr write 2
587family of system calls,
588.Er EPROTO
589can be returned if the bytes to be written on a raw
590.Nm
591device would violate
592.Tn MIDI
593protocol.
594.Sh SEE ALSO
595.Xr midiplay 1 ,
596.Xr midirecord 1 ,
597.Xr ioctl 2 ,
598.Xr ossaudio 3 ,
599.Xr audio 4 ,
600.Xr mpu 4 ,
601.Xr opl 4 ,
602.Xr umidi 4
603.Pp
604For ports using the ISA bus:
605.Xr cms 4 ,
606.Xr pcppi 4 ,
607.Xr sb 4
608.Pp
609For ports using the PCI bus:
610.Xr autri 4 ,
611.Xr clcs 4 ,
612.Xr eap 4
613.Sh HISTORY
614The
615.Nm
616driver first appeared in
617.Nx 1.4 .
618It was overhauled and this manual page rewritten for
619.Nx 4.0 .
620.Sh BUGS
621Some
622.Tn OSS
623sequencer events and
624.Xr ioctl 2
625operations are unimplemented, as
626.In sys/midiio.h
627notes.
628.Pp
629.Tn OSS
630source-compatible sequencer macros should be added to
631.In sys/soundcard.h ,
632implemented with the
633.Nx
634ones in
635.In sys/midiio.h ,
636so sources written for OSS can be easily compiled.
637.Pp
638The sequencer blocks (or returns
639.Er EWOULDBLOCK )
640only when its buffer physically fills, which can represent an arbitrary
641latency because of buffered timing events.
642As a result, interrupting a process writing the sequencer may not
643interrupt music playback for a considerable time.
644The sequencer could enforce a reasonable latency bound
645by examining timing events as they are enqueued and blocking appropriately.
646.Pp
647.Dv FIOASYNC
648enables signal delivery to the calling process only;
649.Dv FIOSETOWN
650is not supported.
651.Pp
652The sequencer can only be a timing master, but does not send timing messages
653to synchronize any slave device; it cannot be slaved to timing messages
654received on any interface (which would presumably require a PLL algorithm
655similar to NTP's, and expertise in that area to implement it).
656The sequencer ignores timing messages received on any interface
657and does not pass them along to the reading process, and the OSS
658operations to change that behavior are unimplemented.
659.Pp
660The
661.Dv SEQUENCER_TMR_TIMEBASE
662.Xr ioctl 2
663will report successfully setting any
664timebase up to ridiculously high resolutions, though the actual
665resolution, and therefore jitter, is constrained by
666.Xr hz 9 .
667Comparable sequencer implementations typically allow a selection from available
668sources of time interrupts that may be programmable.
669.Pp
670The device number in a sequencer event is treated on
671.Xr write 2
672as index into
673the array of
674.Tn MIDI
675devices the sequencer has opened, but on
676.Xr read 2
677as the
678unit number of the source
679.Tn MIDI
680device; these are usually the same if the
681sequencer has opened all the
682.Tn MIDI
683devices (that is, none was already open
684at its raw node when the sequencer was opened), but might not be the same
685otherwise.
686.Pp
687There is at present no way to make reception nonpromiscuous,
688should anyone have a reason to want to.
689.Pp
690There should be ways to override default Active Sense behavior.
691As one obvious
692case, if an application is seen to send Active Sense explicitly,
693.Nm
694should refrain
695from adding its own.
696On receive, there should be an option to pass Active Sense through
697rather than interpreting it, for apps that wish to handle or ignore it
698themselves and never see
699.Dv EOF .
700.Pp
701When a
702.Nm
703stream is open by the sequencer, Active Sense messages received on the stream
704are passed to the sequencer and not interpreted by
705.Nm .
706The sequencer at present neither does anything itself with Active Sense
707messages received, nor supports the
708.Tn OSS
709.Tn API
710for making them available to the user process.
711.Pp
712System Exclusive messages can be received by reading a raw device, but
713not by reading the sequencer; they are discarded on receipt when the
714stream is open by the sequencer, rather than being presented as the
715OSS-defined sequencer events.
716.Pp
717.Sy midisyn
718is too rudimentary at present to get satisfactory results from any
719onboard synth.
720It lacks the required special interpretation of the
721General
722.Tn MIDI
723percussion channel in GM mode.
724More devices should be supported; some sound cards with synthesis
725capability have
726.Nx
727drivers that implement the
728.Xr audio 4
729but not the
730.Sy midisyn
731interface.
732Voice stealing algorithm does not follow the General
733.Tn MIDI
734Developer Guidelines.
735.Pp
736.Tn ALSA
737sequencer compatibility is lacking, but becoming important to applications.
738It would require the function of merging multiple tracks into a single ordered
739stream to be moved from user space into the sequencer.
740Assuming the sequencer driven by periodic interrupts, timing wheels
741could be used as in
742.Xr hardclock 9
743itself.
744Similar functionality will be in OSS4; with the right infrastructure
745it should be possible to support both.
746When merging
747.Tn MIDI
748streams, a notion of transaction
749is needed to group critical message sequences.
750If
751.Tn ALSA
752or
753.Tn OSS4
754have no such notion, it should be provided as an upward-compatible
755extension.
756.Pp
757I would rather have
758.Xr open 2
759itself return an error (by the POSIX description
760.Er ENODEV
761looks most appropriate) if a read or write mode
762is requested that is not supported by the instance, rather than letting
763.Xr open 2
764succeed and
765.Xr read 2
766or
767.Xr write 2
768return \-1, but so help me, the latter seems
769the more common
770.Ux
771practice.
772