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