getdetune() coding style cleanup
[zyn.git] / lv2-miditype.h
blob465d5d5e2db5640b10ba1bcb18379925e1993a26
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
27 /** This data structure is used to contain the MIDI events for one run()
28 cycle. The port buffer for a LV2 port that has the datatype
29 <http://ll-plugins.nongnu.org/lv2/ext/miditype> should be a pointer
30 to an instance of this struct.
32 To store two Note On events on MIDI channel 0 in a buffer, with timestamps
33 12 and 35.5, you could use something like this code (assuming that
34 midi_data is a variable of type LV2_MIDI):
35 @code
37 size_t buffer_offset = 0;
38 *(double*)(midi_data->data + buffer_offset) = 12;
39 buffer_offset += sizeof(double);
40 *(size_t*)(midi_data->data + buffer_offset) = 3;
41 buffer_offset += sizeof(size_t);
42 midi_data->data[buffer_offset++] = 0x90;
43 midi_data->data[buffer_offset++] = 0x48;
44 midi_data->data[buffer_offset++] = 0x64;
45 ++midi_data->event_count;
47 *(double*)(midi_data->data + buffer_offset) = 35.5;
48 buffer_offset += sizeof(double);
49 *(size_t*)(midi_data->data + buffer_offset) = 3;
50 buffer_offset += sizeof(size_t);
51 midi_data->data[buffer_offset++] = 0x90;
52 midi_data->data[buffer_offset++] = 0x55;
53 midi_data->data[buffer_offset++] = 0x64;
54 ++midi_data->event_count;
56 midi_data->size = buffer_offset;
58 @endcode
60 This would be done by the host in the case of an input port, and by the
61 plugin in the case of an output port. Whoever is writing events to the
62 buffer must also take care not to exceed the capacity of the data buffer.
64 To read events from a buffer, you could do something like this:
65 @code
67 size_t buffer_offset = 0;
68 uint32_t i;
69 for (i = 0; i < midi_data->event_count; ++i) {
70 double timestamp = *(double*)(midi_data->data + buffer_offset);
71 buffer_offset += sizeof(double);
72 size_t size = *(size_t*)(midi_data->data + buffer_offset);
73 buffer_offset += sizeof(size_t);
74 do_something_with_event(timestamp, size,
75 midi_data->data + buffer_offset);
76 buffer_offset += size;
79 @endcode
81 typedef struct {
83 /** The number of MIDI events in the data buffer.
84 INPUT PORTS: It's the host's responsibility to set this field to the
85 number of MIDI events contained in the data buffer before calling the
86 plugin's run() function. The plugin may not change this field.
87 OUTPUT PORTS: It's the plugin's responsibility to set this field to the
88 number of MIDI events it has stored in the data buffer before returning
89 from the run() function. Any initial value should be ignored by the
90 plugin.
92 uint32_t event_count;
94 /** The size of the data buffer in bytes. It is set by the host and may not
95 be changed by the plugin. The host is allowed to change this between
96 run() calls.
98 uint32_t capacity;
100 /** The size of the initial part of the data buffer that actually contains
101 data.
102 INPUT PORTS: It's the host's responsibility to set this field to the
103 number of bytes used by all MIDI events it has written to the buffer
104 (including timestamps and size fields) before calling the plugin's
105 run() function. The plugin may not change this field.
106 OUTPUT PORTS: It's the plugin's responsibility to set this field to
107 the number of bytes used by all MIDI events it has written to the
108 buffer (including timestamps and size fields) before returning from
109 the run() function. Any initial value should be ignored by the plugin.
111 uint32_t size;
113 /** The data buffer that is used to store MIDI events. The events are packed
114 after each other, and the format of each event is as follows:
116 First there is a timestamp, which should have the type "double",
117 i.e. have the same bit size as a double and the same bit layout as a
118 double (whatever that is on the current platform). This timestamp gives
119 the offset from the beginning of the current cycle, in frames, that
120 the MIDI event occurs on. It must be strictly smaller than the 'nframes'
121 parameter to the current run() call. The MIDI events in the buffer must
122 be ordered by their timestamp, e.g. an event with a timestamp of 123.23
123 must be stored after an event with a timestamp of 65.0.
125 The second part of the event is a size field, which should have the type
126 "size_t" (as defined in the standard C header stddef.h). It should
127 contain the size of the MIDI data for this event, i.e. the number of
128 bytes used to store the actual MIDI event. The bytes used by the
129 timestamp and the size field should not be counted.
131 The third part of the event is the actual MIDI data. There are some
132 requirements that must be followed:
134 * Running status is not allowed. Every event must have its own status
135 byte.
136 * Note On events with velocity 0 are not allowed. These events are
137 equivalent to Note Off in standard MIDI streams, but in order to make
138 plugins and hosts easier to write, as well as more efficient, only
139 proper Note Off events are allowed as Note Off.
140 * "Realtime events" (status bytes 0xF8 to 0xFF) are allowed, but may not
141 occur inside other events like they are allowed to in hardware MIDI
142 streams.
143 * All events must be fully contained in a single data buffer, i.e. events
144 may not "wrap around" by storing the first few bytes in one buffer and
145 then wait for the next run() call to store the rest of the event. If
146 there isn't enough space in the current data buffer to store an event,
147 the event will either have to wait until next run() call, be ignored,
148 or compensated for in some more clever way.
149 * All events must be valid MIDI events. This means for example that
150 only the first byte in each event (the status byte) may have the eighth
151 bit set, that Note On and Note Off events are always 3 bytes long etc.
152 The MIDI writer (host or plugin) is responsible for writing valid MIDI
153 events to the buffer, and the MIDI reader (plugin or host) can assume
154 that all events are valid.
156 On a platform where double is 8 bytes and size_t is 4 bytes, the data
157 buffer layout for a 3-byte event followed by a 4-byte event may look
158 something like this:
159 _______________________________________________________________
160 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ...
161 |TIMESTAMP 1 |SIZE 1 |DATA |TIMESTAMP 2 |SIZE 2 |DATA | ...
164 unsigned char* data;
166 } LV2_MIDI;
170 #endif