1.\" $OpenBSD: midi.4,v 1.29 2016/08/31 13:54:58 jmc Exp $ 2.\" 3.\" Copyright (c) 2006 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.\" 17.Dd $Mdocdate: August 31 2016 $ 18.Dt MIDI 4 19.Os 20.Sh NAME 21.Nm midi 22.Nd raw device independent interface to MIDI ports 23.Sh SYNOPSIS 24.Cd "midi* at autri?" 25.Cd "midi* at eap?" 26.Cd "midi* at envy?" 27.Cd "midi* at mpu?" 28.Cd "midi* at sb?" 29.Cd "midi* at umidi?" 30.Cd "midi* at ym?" 31.Sh DESCRIPTION 32The 33.Nm 34driver makes MIDI ports available to user applications. 35A 36.Nm 37device corresponds to 2 MIDI ports: one input port and one 38output port. 39Data received on the input port is not interpreted and is passed 40to the user program as-is. 41Similarly, data issued by the user program is sent as-is to the 42output port. 43.Pp 44Only one process may hold open a 45.Nm 46device at a given time, although file descriptors may be shared 47between processes once the first open completes. 48If it is opened read-only (write-only) only the input (output) 49MIDI port is available. 50.Ss Writing to the device 51A process can send raw MIDI data to the output port by using the 52.Xr write 2 53system call. 54Data is queued and the system call returns immediately; the data 55is sent as fast as possible to the output MIDI port. 56However, if the in-kernel buffer is full or the requested amount 57is too large, then 58.Xr write 2 59may block. 60The current size of the in-kernel buffer is 1024 bytes, which 61ensures that 62.Xr write 2 63isn't blocking in most situations. 64.Ss Reading from the device 65Data received from the input MIDI port is stored into the 66in-kernel buffer. 67A process can retrieve its contents by using 68the 69.Xr read 2 70system call. 71If there is less data than the amount requested for reading, then 72a shorter amount is returned. 73If no data is available, then the 74.Xr read 2 75system call will block until data is received, 76and then return immediately. 77.Pp 78The MIDI protocol has been designed for real-time performance and 79doesn't support flow control. 80An application must be able to read the incoming data fast enough 81(the MIDI standard's maximum rate is 3125 bytes per second). 82The kernel can buffer up to 1024 bytes; once the buffer is full 83input will be silently discarded. 84.Ss Polling the device 85A process can use the 86.Xr poll 2 87system call to poll for the following events: 88.Bl -tag -width POLLOUT 89.It Dv POLLIN 90The in-kernel input buffer isn't empty, i.e. at least one byte is 91available for reading. 92A subsequent call to 93.Xr read 2 94will not be blocking. 95.It Dv POLLOUT 96The in-kernel output buffer is empty, thus a subsequent call to 97.Xr write 2 98will not be blocking if a reasonable amount of data is written 99(currently less that 1024 bytes). 100.El 101.Pp 102Using the 103.Xr poll 2 104system call is the recommended way to handle multiple 105.Nm 106devices in a real-time MIDI application. 107.Ss Non-blocking I/O 108If the 109.Nm 110device is opened with the O_NONBLOCK flag (see 111.Xr open 2 ) , 112then subsequent calls to 113.Xr read 2 114or 115.Xr write 2 116will never block. 117The 118.Xr write 2 119system call may write less bytes than requested, or may return 120EAGAIN if no data could be sent or queued. 121Similarly, the 122.Xr read 2 123system call may return EAGAIN if no input is available. 124.Pp 125Note that even if non-blocking I/O is not selected, 126.Xr read 2 127and 128.Xr write 2 129system calls are non-blocking when the kernel buffers permit it. 130.Sh FILES 131.Bl -tag -width /dev/rmidim -compact 132.It Pa /dev/rmidi* 133.Nm 134devices 135.El 136.Sh EXAMPLES 137The following command could record the memory dump of a 138synthesizer in a file: 139.Pp 140.Dl $ cat -u /dev/rmidi2 >dumpfile 141.Pp 142A MIDI keyboard could be connected to a synthesizer by the 143command: 144.Pp 145.Dl $ cat -u /dev/rmidi1 >/dev/rmidi2 146.Pp 147The input port could be connected to the output port by the 148command: 149.Pp 150.Dl $ cat -u <>/dev/rmidi1 >&0 151.Pp 152The following example reads MIDI timing events from an input 153device, MIDI common and voice events from another input device, and 154sends the result to a third (output) device. 155.Bd -literal -offset indent 156#define BUFSIZE 0x100 157#define ISTIMING(c) ((c) == 0xf8 || (c) == 0xfa || (c) == 0xfc) 158#define ISCOMMON(c) ((c) < 0xf8) 159 160int ofd; 161struct pollfd ifd[2]; 162unsigned char ibuf[BUFSIZE], obuf[2 * BUFSIZE]; 163ssize_t iused, oused, i; 164 165ifd[0].events = ifd[1].events = POLLIN; 166for (;;) { 167 oused = 0; 168 if (poll(ifd, 2, -1) == -1) 169 errx(1, "poll"); 170 if (ifd[0].revents & POLLIN) { 171 if ((iused = read(ifd[0].fd, ibuf, BUFSIZE)) == -1) 172 errx(1, "read"); 173 for (i = 0; i < iused; i++) 174 if (ISTIMING(ibuf[i])) 175 obuf[oused++] = ibuf[i]; 176 } 177 if (ifd[1].revents & POLLIN) { 178 if ((iused = read(ifd[1].fd, ibuf, BUFSIZE)) == -1) 179 errx(1, "read"); 180 for (i = 0; i < iused; i++) 181 if (ISCOMMON(ibuf[i])) 182 obuf[oused++] = ibuf[i]; 183 } 184 if (write(ofd, obuf, oused) == -1) 185 errx(1, "write"); 186} 187.Ed 188.Pp 189In the above example, unless kernel buffers are full, processing 190is done in real-time without any noticeable latency; as expected, 191the only blocking system call is 192.Xr poll 2 . 193.Sh ERRORS 194If 195.Xr open 2 , 196.Xr read 2 , 197.Xr write 2 , 198or 199.Xr poll 2 200fail then 201.Xr errno 2 202may be set to one of: 203.Bl -tag -width Er 204.It Bq Er ENXIO 205The device is opened read-only (write-only) but 206.Xr write 2 207.Pf ( Xr read 2 ) 208was called. 209.It Bq Er EIO 210The device is being detached while a process has been trying to 211read or write (for instance an 212.Xr umidi 4 213device has been unplugged). 214.It Bq Er EAGAIN 215Non-blocking I/O was selected and the output buffer is full (on 216writing) or the input buffer is empty (on reading). 217.It Bq Er EBUSY 218The device is already open by another process. 219.El 220.Sh SEE ALSO 221.Xr autri 4 , 222.Xr eap 4 , 223.Xr envy 4 , 224.Xr mpu 4 , 225.Xr sb 4 , 226.Xr umidi 4 227.Sh HISTORY 228The 229.Nm 230driver first appeared in 231.Ox 2.5 . 232.Sh AUTHORS 233.An -nosplit 234The 235.Nm 236driver was originally written by 237.An Lennart Augustsson 238and later largely rewritten by 239.An Alexandre Ratchov . 240.Sh CAVEATS 241MIDI hardware was designed for real time performance and software 242using such hardware must be able to process MIDI events without 243any noticeable latency (typically no more than 5ms, which 244is the time it takes for sound to propagate 1.75 meters). 245.Pp 246The 247.Ox 248.Nm 249driver processes data fast enough, however if a MIDI application 250tries to write data faster than the hardware is able to process it 251(typically 3125 bytes per second), then kernel buffers may become 252full and the application may be blocked. 253.Pp 254The other common reason for MIDI data being delayed is the system 255load. 256Processes cannot be preempted while running in kernel mode. 257If there are too much processes running concurrently (especially 258if they are running a lot of expensive system calls) then the 259scheduling of a real-time MIDI application may be delayed. 260Even on low-end machines this delay hardly reaches a few 261milliseconds provided that the system load is reasonable. 262.Pp 263A real-time MIDI application can avoid being swapped by locking 264its memory (see 265.Xr mlock 2 266and 267.Xr mlockall 2 ) . 268.Sh BUGS 269For a given device, even if the physical MIDI input and output 270ports are independent, there is no way for one process to use the 271input MIDI port and for another process to use the output MIDI 272port at the same time. 273