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