1 /****************************************************************************
3 lv2-miditype.h - header file for using MIDI in LV2 plugins
5 Copyright (C) 2006 Lars Luthman <lars.luthman@gmail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307 USA
21 ****************************************************************************/
23 #ifndef LV2_MIDITYPE_H
24 #define LV2_MIDITYPE_H
30 /** This data structure is used to contain the MIDI events for one run()
31 cycle. The port buffer for a LV2 port that has the datatype
32 <http://ll-plugins.nongnu.org/lv2/ext/miditype> should be a pointer
33 to an instance of this struct.
35 To store two Note On events on MIDI channel 0 in a buffer, with timestamps
36 12 and 35.5, you could use something like this code (assuming that
37 midi_data is a variable of type LV2_MIDI):
40 size_t buffer_offset = 0;
41 *(double*)(midi_data->data + buffer_offset) = 12;
42 buffer_offset += sizeof(double);
43 *(size_t*)(midi_data->data + buffer_offset) = 3;
44 buffer_offset += sizeof(size_t);
45 midi_data->data[buffer_offset++] = 0x90;
46 midi_data->data[buffer_offset++] = 0x48;
47 midi_data->data[buffer_offset++] = 0x64;
48 ++midi_data->event_count;
50 *(double*)(midi_data->data + buffer_offset) = 35.5;
51 buffer_offset += sizeof(double);
52 *(size_t*)(midi_data->data + buffer_offset) = 3;
53 buffer_offset += sizeof(size_t);
54 midi_data->data[buffer_offset++] = 0x90;
55 midi_data->data[buffer_offset++] = 0x55;
56 midi_data->data[buffer_offset++] = 0x64;
57 ++midi_data->event_count;
59 midi_data->size = buffer_offset;
63 This would be done by the host in the case of an input port, and by the
64 plugin in the case of an output port. Whoever is writing events to the
65 buffer must also take care not to exceed the capacity of the data buffer.
67 To read events from a buffer, you could do something like this:
70 size_t buffer_offset = 0;
72 for (i = 0; i < midi_data->event_count; ++i) {
73 double timestamp = *(double*)(midi_data->data + buffer_offset);
74 buffer_offset += sizeof(double);
75 size_t size = *(size_t*)(midi_data->data + buffer_offset);
76 buffer_offset += sizeof(size_t);
77 do_something_with_event(timestamp, size,
78 midi_data->data + buffer_offset);
79 buffer_offset += size;
86 /** The number of MIDI events in the data buffer.
87 INPUT PORTS: It's the host's responsibility to set this field to the
88 number of MIDI events contained in the data buffer before calling the
89 plugin's run() function. The plugin may not change this field.
90 OUTPUT PORTS: It's the plugin's responsibility to set this field to the
91 number of MIDI events it has stored in the data buffer before returning
92 from the run() function. Any initial value should be ignored by the
97 /** The size of the data buffer in bytes. It is set by the host and may not
98 be changed by the plugin. The host is allowed to change this between
103 /** The size of the initial part of the data buffer that actually contains
105 INPUT PORTS: It's the host's responsibility to set this field to the
106 number of bytes used by all MIDI events it has written to the buffer
107 (including timestamps and size fields) before calling the plugin's
108 run() function. The plugin may not change this field.
109 OUTPUT PORTS: It's the plugin's responsibility to set this field to
110 the number of bytes used by all MIDI events it has written to the
111 buffer (including timestamps and size fields) before returning from
112 the run() function. Any initial value should be ignored by the plugin.
116 /** The data buffer that is used to store MIDI events. The events are packed
117 after each other, and the format of each event is as follows:
119 First there is a timestamp, which should have the type "double",
120 i.e. have the same bit size as a double and the same bit layout as a
121 double (whatever that is on the current platform). This timestamp gives
122 the offset from the beginning of the current cycle, in frames, that
123 the MIDI event occurs on. It must be strictly smaller than the 'nframes'
124 parameter to the current run() call. The MIDI events in the buffer must
125 be ordered by their timestamp, e.g. an event with a timestamp of 123.23
126 must be stored after an event with a timestamp of 65.0.
128 The second part of the event is a size field, which should have the type
129 "size_t" (as defined in the standard C header stddef.h). It should
130 contain the size of the MIDI data for this event, i.e. the number of
131 bytes used to store the actual MIDI event. The bytes used by the
132 timestamp and the size field should not be counted.
134 The third part of the event is the actual MIDI data. There are some
135 requirements that must be followed:
137 * Running status is not allowed. Every event must have its own status
139 * Note On events with velocity 0 are not allowed. These events are
140 equivalent to Note Off in standard MIDI streams, but in order to make
141 plugins and hosts easier to write, as well as more efficient, only
142 proper Note Off events are allowed as Note Off.
143 * "Realtime events" (status bytes 0xF8 to 0xFF) are allowed, but may not
144 occur inside other events like they are allowed to in hardware MIDI
146 * All events must be fully contained in a single data buffer, i.e. events
147 may not "wrap around" by storing the first few bytes in one buffer and
148 then wait for the next run() call to store the rest of the event. If
149 there isn't enough space in the current data buffer to store an event,
150 the event will either have to wait until next run() call, be ignored,
151 or compensated for in some more clever way.
152 * All events must be valid MIDI events. This means for example that
153 only the first byte in each event (the status byte) may have the eighth
154 bit set, that Note On and Note Off events are always 3 bytes long etc.
155 The MIDI writer (host or plugin) is responsible for writing valid MIDI
156 events to the buffer, and the MIDI reader (plugin or host) can assume
157 that all events are valid.
159 On a platform where double is 8 bytes and size_t is 4 bytes, the data
160 buffer layout for a 3-byte event followed by a 4-byte event may look
162 _______________________________________________________________
163 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ...
164 |TIMESTAMP 1 |SIZE 1 |DATA |TIMESTAMP 2 |SIZE 2 |DATA | ...