1 .\" $NetBSD: midi.4,v 1.28 2008/04/30 13:10:54 martin Exp $
3 .\" Copyright (c) 1999-2006 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Lennart Augustsson and Chapman Flack.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
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.
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.
35 .Nd device-independent MIDI driver layer
37 .Cd "midi* at midibus?"
39 .Cd "pseudo-device sequencer"
46 driver is the machine independent layer over anything that can
49 data stream, whether a physical
53 jack on a soundcard, cabled to some external synthesizer or input controller,
54 an on-board programmable tone generator, or a single jack, synthesizer, or
55 controller component within a complex
60 device that has several
61 such components and appears as several
67 data stream is a unidirectional stream of
69 messages, as could be carried over one
77 carry a four-bit channel number, creating up to 16
79 channels within a single
82 There may be multiple consumers of a
84 stream, each configured
85 to react only to messages on specific channels; the sets of channels different
86 consumers react to need not be disjoint.
87 Many modern devices such as multitimbral keyboards and tone generators
88 listen on all 16 channels, or may be viewed as collections of 16
89 independent consumers each listening on one channel.
91 defines some messages that take no channel number, and
92 apply to all consumers of the
93 stream on which they are sent.
94 For an inbound stream,
97 receiver, capturing all messages regardless of channel number.
98 For an outbound stream, the writer can specify a channel number
99 per message; there is no notion of binding the stream to one
100 destination channel in advance.
104 device instance is the endpoint of one outbound stream, one
105 inbound stream, or one of each.
106 In the third case, the write and read sides are independent
109 For example, a soundcard driver may map its
113 jacks to the write and read sides of a single device instance, but
114 those jacks can be cabled to completely different pieces of gear.
117 and a diagram of any external
119 cabling, will help clarify the mapping.
120 .Ss "Underlying drivers and MIDI protocol"
123 can attach include soundcard drivers, many of
133 and on-board devices that can make sounds,
140 connections are currently science fiction.
144 protocol permits some forms of message compression such as running
145 status and hidden note-off.
146 Received messages on inbound streams are always canonicalized by
148 before presentation to higher layers.
149 Messages for transmission are accepted by
155 device instances can be through the raw device nodes,
157 or through the sequencer,
159 .Ss "Raw MIDI access"
168 and the corresponding
172 only when it is not already open.
182 will return \-1 if the device has no associated
183 input or output stream, respectively.
185 Bytes written are passed as quickly as possible to the underlying driver
188 messages; a maximum of two bytes at the end of a
190 may remain buffered if they do not complete a message, until
191 completed by a following
196 will not block or return
198 when it could immediately return any nonzero count, and
200 messages received are available to
202 as soon as they are complete,
203 with a maximum of two received bytes remaining buffered if they do not
208 messages are three bytes or fewer except for System Exclusive,
209 which can have arbitrary length, these rules imply that System Exclusive
210 messages are the only ones of which some bytes can be delivered before
213 System Realtime messages are passed with minimum delay in either direction,
214 ahead of any possible buffered incomplete message.
215 As a result, they will never interrupt any
217 message except possibly System Exclusive.
221 with a buffer large enough to accommodate the first complete
222 message available will be satisfied with as many complete messages as
224 A buffer too small for the first complete
225 message will be filled to capacity.
226 Therefore, an application that reads from an
228 device with buffers of three bytes or larger need never parse
229 across read boundaries to assemble a received message, except possibly in
230 the case of a System Exclusive message.
231 However, if the application reads through a buffering layer such as
233 this property will not be preserved.
237 driver itself supports the
244 Underlying devices may support others.
245 The value returned for
247 reflects the size in bytes of complete messages
248 (or System Exclusive chunks) ready to read.
259 bytes will be read, but if a
266 is issued, fewer than
271 on a message/chunk boundary.
275 access can be used to receive bulk dumps from synthesizers, download
276 bulk data to them, and so on.
277 Simple patching of one device to another can be
278 done at the command line, as with
279 .Dl $ cat -u 0\*[Lt]\*[Gt]/dev/rmidi0 1\*[Gt]\*[Am]0
280 which will loop all messages received on the input stream of
282 input stream back to its output
284 However, an attempt to record and play back music with
285 .Dl $ cat /dev/rmidiN \*[Gt]foo; cat foo \*[Gt]/dev/rmidiN
286 will be disappointing.
289 will contain all of the notes that were played, but because
292 no explicit timing, the
294 will reproduce them all at once, as fast as
295 they can be transmitted.
296 To preserve timing information, the sequencer device can be used.
300 protocol includes a keepalive function called Active Sensing.
301 In any receiver that has
303 received at least one Active Sense
306 feature is suppressed and no timeout applies.
307 If at least one such message has
308 been received, the lapse of any subsequent 300 ms interval without receipt of
309 any message reflects loss of communication, and the receiver should silence any
310 currently sounding notes and return to non-Active-Sensing behavior.
311 A sender using Active Sensing generally avoids 300 ms gaps in
312 transmission by sending Active Sense messages (which have no other
313 effect) as needed when there is no other traffic to send in the
315 This feature can be important for
317 which relies on separate Note On and Note Off messages, to avoid notes stuck on
318 indefinitely if communication is interrupted before a Note Off
321 This protocol is supported in
323 An outbound stream will be kept alive
324 by sending Active Sense messages as needed, beginning after any real
325 traffic is sent on the stream, and continuing until the stream is closed.
326 On an inbound stream, if any Active Sense has been received, then a process
329 device will see an end-of-file indication if the input timeout elapses.
330 The stream remains open, the driver reverts to enforcing no timeout, and the
331 process may continue to read for more input.
332 Subsequent receipt of an
333 Active Sense message will re-arm the timeout.
334 As received Active Sense messages are handled by
336 they are not included among messages read from the
340 These rules support end-to-end Active Sensing behavior in simple cases
341 without special action in an application.
343 .Dl $ cat -u /dev/rmidi0 \*[Gt]/dev/rmidi1
344 if the input stream to
348 command exits; on the
353 ceases to send Active Sense messages, and the receiving
354 device will detect the loss and silence any outstanding notes.
355 .Ss "Access through the sequencer"
356 To play music using the raw
359 would require an application to
360 issue many small writes with very precise timing.
361 The sequencer device,
363 can manage the timing of
365 data in the kernel, to avoid
366 such demanding real-time constraints on a user process.
370 device can be opened only when it is not already open.
371 When opened, the sequencer internally opens all
374 in the system that are not already open at their raw nodes; any attempts
375 to open them at their raw nodes while the sequencer is open will fail.
376 All access to the corresponding
378 streams will then be through the
383 pass eight-byte event structures defined in
385 (which see for their documentation and examples of use).
386 Some events correspond to
388 messages, and carry an integer
390 field to identify one of the
392 devices opened by the sequencer.
393 Other events carry timing information interpreted or generated by the
396 A message received on an input stream is wrapped in a sequencer event
397 along with the device index of the stream it arrived on, and queued for
400 If a measurable time interval passed since the
401 last preceding message, a timing event that represents a delay for that interval
402 is queued ahead of the received event.
403 The sequencer handles output events by
404 interpreting any timing event, and routing any
406 message event at the proper time to
407 an underlying output stream according to its
411 .Dl $ cat /dev/music \*[Gt]foo; cat foo \*[Gt]/dev/music
412 can be expected to capture and reproduce an input performance including
415 The process of playing back a complex
417 file is illustrated below.
418 The file may contain several tracks\(emfour, in this example\(emof
420 events, each marked with a device index and a time stamp, that may
427 are device indices of
430 streams; the left-hand digit in each input event represents a
432 channel on the selected stream, and the right-hand digit represents
433 a time for the event's occurrence.
434 As illustrated, the input tracks are not firmly associated with
435 output streams; any track may contain events for any stream.
443 +---------------------------+
444 | merge to 1 ordered stream |
445 | user code, eg midiplay(1) |
446 +---------------------------+
454 _______+-------------+_______user
455 | /dev/music | kernel
462 +-------+ +--------+ +---------+
463 |midi(4)| |midi(4) | |midi(4) |
464 |rmidia | |rmidib | |rmidic |
465 +-------+ +--------+ +---------+
466 | mpu(4)| |umidi(4)| |midisyn |
467 +-------+ +--------+ +---------+
469 | MIDI | U +---------+
470 | UART | S | internal|
479 A user process must merge the tracks into a single stream of sequencer
481 and timing events in order by desired timing.
482 The sequencer obeys the timing events and distributes the
484 events to the three destinations,
485 in this case two external devices connected to a sound card
491 tone generator on a sound card.
496 with the sequencer is supported, however, there is no guarantee that a
498 will not block or return
500 if it begins with a timer-wait event, even if
503 reported the sequencer writable.
505 The delivery of a realtime message ahead of buffered bytes of an incomplete
506 message may cause the realtime message to seem, in a saved byte stream, to have
507 arrived up to 640 us earlier than it really did, at
510 Higher data rates make the effect less significant.
512 Another sequencer device,
514 is provided only for backward
515 compatibility with an obsolete
517 interface in which some sequencer events
518 were four-byte records.
519 It is not further documented here, and the
522 should be used in new code.
525 emulation is implemented only for writing, and that might not be complete.
526 .Sh IMPLEMENTATION NOTES
527 Some hardware devices supporting
529 lack transmit-ready interrupts, and some have the capability in
530 hardware but currently lack driver support.
531 They can be recognized by the annotation
532 .Li "(CPU-intensive output)"
535 While suitable for music playback, they may have an objectionable impact on
536 system responsiveness during bulk transmission such as patch downloads, and
537 are best avoided for that purpose if other suitable devices are present.
541 itself is adequate for about 200 ms of traffic at
543 data rates, per stream.
545 Event counters record bytes and messages discarded because of protocol
546 errors or buffer overruns, and can be viewed with
548 They can be useful in diagnosing flaky cables and other communication
551 .\" These two paragraphs really belong not here but in a midi(9) man page,
552 .\" which should one day exist.
553 A raw sound generator uses the
557 message-driven interface attachable by
562 accepts messages for transmission in any valid mixture of compressed
563 or canonical form, they are always presented to an underlying driver
564 in the form it prefers.
568 register their preference for a compressed byte stream, while those like
570 which uses a packet protocol, or
572 which interprets complete
573 messages, register for intact canonical messages.
574 This design eliminates the
575 need for compression and canonicalization logic from all layers above and below
579 .Bl -tag -width /dev/sequencer -compact
582 .It Pa /dev/sequencer
585 In addition to other errors documented for the
587 family of system calls,
589 can be returned if the bytes to be written on a raw
603 For ports using the ISA bus:
608 For ports using the PCI bus:
615 driver first appeared in
617 It was overhauled and this manual page rewritten for
624 operations are unimplemented, as
629 source-compatible sequencer macros should be added to
630 .Aq Pa sys/soundcard.h ,
634 .Aq Pa sys/midiio.h ,
635 so sources written for OSS can be easily compiled.
637 The sequencer blocks (or returns
639 only when its buffer physically fills, which can represent an arbitrary
640 latency because of buffered timing events.
641 As a result, interrupting a process writing the sequencer may not
642 interrupt music playback for a considerable time.
643 The sequencer could enforce a reasonable latency bound
644 by examining timing events as they are enqueued and blocking appropriately.
647 enables signal delivery to the calling process only;
651 The sequencer can only be a timing master, but does not send timing messages
652 to synchronize any slave device; it cannot be slaved to timing messages
653 received on any interface (which would presumably require a PLL algorithm
654 similar to NTP's, and expertise in that area to implement it).
655 The sequencer ignores timing messages received on any interface
656 and does not pass them along to the reading process, and the OSS
657 operations to change that behavior are unimplemented.
660 .Dv SEQUENCER_TMR_TIMEBASE
662 will report successfully setting any
663 timebase up to ridiculously high resolutions, though the actual
664 resolution, and therefore jitter, is constrained by
666 Comparable sequencer implementations typically allow a selection from available
667 sources of time interrupts that may be programmable.
669 The device number in a sequencer event is treated on
674 devices the sequencer has opened, but on
677 unit number of the source
679 device; these are usually the same if the
680 sequencer has opened all the
682 devices (that is, none was already open
683 at its raw node when the sequencer was opened), but might not be the same
686 There is at present no way to make reception nonpromiscuous,
687 should anyone have a reason to want to.
689 There should be ways to override default Active Sense behavior.
691 case, if an application is seen to send Active Sense explicitly,
695 On receive, there should be an option to pass Active Sense through
696 rather than interpreting it, for apps that wish to handle or ignore it
697 themselves and never see
702 stream is open by the sequencer, Active Sense messages received on the stream
703 are passed to the sequencer and not interpreted by
705 The sequencer at present neither does anything itself with Active Sense
706 messages received, nor supports the
709 for making them available to the user process.
711 System Exclusive messages can be received by reading a raw device, but
712 not by reading the sequencer; they are discarded on receipt when the
713 stream is open by the sequencer, rather than being presented as the
714 OSS-defined sequencer events.
717 is too rudimentary at present to get satisfactory results from any
719 It lacks the required special interpretation of the
722 percussion channel in GM mode.
723 More devices should be supported; some sound cards with synthesis
726 drivers that implement the
731 Voice stealing algorithm does not follow the General
733 Developer Guidelines.
736 sequencer compatibility is lacking, but becoming important to applications.
737 It would require the function of merging multiple tracks into a single ordered
738 stream to be moved from user space into the sequencer.
739 Assuming the sequencer driven by periodic interrupts, timing wheels
743 Similar functionality will be in OSS4; with the right infrastructure
744 it should be possible to support both.
747 streams, a notion of transaction
748 is needed to group critical message sequences.
753 have no such notion, it should be provided as an upward-compatible
758 itself return an error (by the POSIX description
760 looks most appropriate) if a read or write mode
761 is requested that is not supported by the instance, rather than letting
767 return \-1, but so help me, the latter seems