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