1 /*
2   Copyright 2008-2011 David Robillard <http://drobilla.net>
3   Copyright 2006-2007 Lars Luthman <lars.luthman@gmail.com>
4 
5   Permission to use, copy, modify, and/or 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   THIS 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 
18 /**
19    @file event.h
20    C API for the LV2 Event extension <http://lv2plug.in/ns/ext/event>.
21 
22    This extension is a generic transport mechanism for time stamped events
23    of any type (e.g. MIDI, OSC, ramps, etc). Each port can transport mixed
24    events of any type; the type of events and timestamps are defined by a URI
25    which is mapped to an integer by the host for performance reasons.
26 
27    This extension requires the host to support the LV2 URI Map extension.
28    Any host which supports this extension MUST guarantee that any call to
29    the LV2 URI Map uri_to_id function with the URI of this extension as the
30    'map' argument returns a value within the range of uint16_t.
31 */
32 
33 #ifndef LV2_EVENT_H
34 #define LV2_EVENT_H
35 
36 #define LV2_EVENT_URI    "http://lv2plug.in/ns/ext/event"
37 #define LV2_EVENT_PREFIX LV2_EVENT_URI "#"
38 
39 #define LV2_EVENT__Event              LV2_EVENT_PREFIX "Event"
40 #define LV2_EVENT__EventPort          LV2_EVENT_PREFIX "EventPort"
41 #define LV2_EVENT__FrameStamp         LV2_EVENT_PREFIX "FrameStamp"
42 #define LV2_EVENT__TimeStamp          LV2_EVENT_PREFIX "TimeStamp"
43 #define LV2_EVENT__generatesTimeStamp LV2_EVENT_PREFIX "generatesTimeStamp"
44 #define LV2_EVENT__generic            LV2_EVENT_PREFIX "generic"
45 #define LV2_EVENT__inheritsEvent      LV2_EVENT_PREFIX "inheritsEvent"
46 #define LV2_EVENT__inheritsTimeStamp  LV2_EVENT_PREFIX "inheritsTimeStamp"
47 #define LV2_EVENT__supportsEvent      LV2_EVENT_PREFIX "supportsEvent"
48 #define LV2_EVENT__supportsTimeStamp  LV2_EVENT_PREFIX "supportsTimeStamp"
49 
50 #define LV2_EVENT_AUDIO_STAMP 0
51 
52 #include <stdint.h>
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 /**
59    The best Pulses Per Quarter Note for tempo-based uint32_t timestamps.
60    Equal to 2^12 * 5 * 7 * 9 * 11 * 13 * 17, which is evenly divisble
61    by all integers from 1 through 18 inclusive, and powers of 2 up to 2^12.
62 */
63 static const uint32_t LV2_EVENT_PPQN = 3136573440U;
64 
65 /**
66    An LV2 event (header only).
67 
68    LV2 events are generic time-stamped containers for any type of event.
69    The type field defines the format of a given event's contents.
70 
71    This struct defines the header of an LV2 event. An LV2 event is a single
72    chunk of POD (plain old data), usually contained in a flat buffer (see
73    LV2_EventBuffer below). Unless a required feature says otherwise, hosts may
74    assume a deep copy of an LV2 event can be created safely using a simple:
75 
76    memcpy(ev_copy, ev, sizeof(LV2_Event) + ev->size);  (or equivalent)
77 */
78 typedef struct {
79 	/**
80 	   The frames portion of timestamp. The units used here can optionally be
81 	   set for a port (with the lv2ev:timeUnits property), otherwise this is
82 	   audio frames, corresponding to the sample_count parameter of the LV2 run
83 	   method (e.g. frame 0 is the first frame for that call to run).
84 	*/
85 	uint32_t frames;
86 
87 	/**
88 	   The sub-frames portion of timestamp. The units used here can optionally
89 	   be set for a port (with the lv2ev:timeUnits property), otherwise this is
90 	   1/(2^32) of an audio frame.
91 	*/
92 	uint32_t subframes;
93 
94 	/**
95 	   The type of this event, as a number which represents some URI
96 	   defining an event type. This value MUST be some value previously
97 	   returned from a call to the uri_to_id function defined in the LV2
98 	   URI map extension (see lv2_uri_map.h).
99 	   There are special rules which must be followed depending on the type
100 	   of an event. If the plugin recognizes an event type, the definition
101 	   of that event type will describe how to interpret the event, and
102 	   any required behaviour. Otherwise, if the type is 0, this event is a
103 	   non-POD event and lv2_event_unref MUST be called if the event is
104 	   'dropped' (see above). Even if the plugin does not understand an event,
105 	   it may pass the event through to an output by simply copying (and NOT
106 	   calling lv2_event_unref). These rules are designed to allow for generic
107 	   event handling plugins and large non-POD events, but with minimal hassle
108 	   on simple plugins that "don't care" about these more advanced features.
109 	*/
110 	uint16_t type;
111 
112 	/**
113 	   The size of the data portion of this event in bytes, which immediately
114 	   follows. The header size (12 bytes) is not included in this value.
115 	*/
116 	uint16_t size;
117 
118 	/* size bytes of data follow here */
119 } LV2_Event;
120 
121 
122 /**
123    A buffer of LV2 events (header only).
124 
125    Like events (which this contains) an event buffer is a single chunk of POD:
126    the entire buffer (including contents) can be copied with a single memcpy.
127    The first contained event begins sizeof(LV2_EventBuffer) bytes after the
128    start of this struct.
129 
130    After this header, the buffer contains an event header (defined by struct
131    LV2_Event), followed by that event's contents (padded to 64 bits), followed
132    by another header, etc:
133 
134    |       |       |       |       |       |       |
135    | | | | | | | | | | | | | | | | | | | | | | | | |
136    |FRAMES |SUBFRMS|TYP|LEN|DATA..DATA..PAD|FRAMES | ...
137 */
138 typedef struct {
139 	/**
140 	   The contents of the event buffer. This may or may not reside in the
141 	   same block of memory as this header, plugins must not assume either.
142 	   The host guarantees this points to at least capacity bytes of allocated
143 	   memory (though only size bytes of that are valid events).
144 	*/
145 	uint8_t* data;
146 
147 	/**
148 	   The size of this event header in bytes (including everything).
149 
150 	   This is to allow for extending this header in the future without
151 	   breaking binary compatibility. Whenever this header is copied,
152 	   it MUST be done using this field (and NOT the sizeof this struct).
153 	*/
154 	uint16_t header_size;
155 
156 	/**
157 	   The type of the time stamps for events in this buffer.
158 	   As a special exception, '0' always means audio frames and subframes
159 	   (1/UINT32_MAX'th of a frame) in the sample rate passed to instantiate.
160 
161 	   INPUTS: The host must set this field to the numeric ID of some URI
162 	   defining the meaning of the frames/subframes fields of contained events
163 	   (obtained by the LV2 URI Map uri_to_id function with the URI of this
164 	   extension as the 'map' argument, see lv2_uri_map.h).  The host must
165 	   never pass a plugin a buffer which uses a stamp type the plugin does not
166 	   'understand'. The value of this field must never change, except when
167 	   connect_port is called on the input port, at which time the host MUST
168 	   have set the stamp_type field to the value that will be used for all
169 	   subsequent run calls.
170 
171 	   OUTPUTS: The plugin may set this to any value that has been returned
172 	   from uri_to_id with the URI of this extension for a 'map' argument.
173 	   When connected to a buffer with connect_port, output ports MUST set this
174 	   field to the type of time stamp they will be writing. On any call to
175 	   connect_port on an event input port, the plugin may change this field on
176 	   any output port, it is the responsibility of the host to check if any of
177 	   these values have changed and act accordingly.
178 	*/
179 	uint16_t stamp_type;
180 
181 	/**
182 	   The number of events in this buffer.
183 
184 	   INPUTS: The host must set this field to the number of events contained
185 	   in the data buffer before calling run(). The plugin must not change
186 	   this field.
187 
188 	   OUTPUTS: The plugin must set this field to the number of events it has
189 	   written to the buffer before returning from run(). Any initial value
190 	   should be ignored by the plugin.
191 	*/
192 	uint32_t event_count;
193 
194 	/**
195 	   The size of the data buffer in bytes.
196 	   This is set by the host and must not be changed by the plugin.
197 	   The host is allowed to change this between run() calls.
198 	*/
199 	uint32_t capacity;
200 
201 	/**
202 	   The size of the initial portion of the data buffer containing data.
203 
204 	   INPUTS: The host must set this field to the number of bytes used
205 	   by all events it has written to the buffer (including headers)
206 	   before calling the plugin's run().
207 	   The plugin must not change this field.
208 
209 	   OUTPUTS: The plugin must set this field to the number of bytes
210 	   used by all events it has written to the buffer (including headers)
211 	   before returning from run().
212 	   Any initial value should be ignored by the plugin.
213 	*/
214 	uint32_t size;
215 } LV2_Event_Buffer;
216 
217 
218 /**
219    Opaque pointer to host data.
220 */
221 typedef void* LV2_Event_Callback_Data;
222 
223 
224 /**
225    Non-POD events feature.
226 
227    To support this feature the host must pass an LV2_Feature struct to the
228    plugin's instantiate method with URI "http://lv2plug.in/ns/ext/event"
229    and data pointed to an instance of this struct.  Note this feature
230    is not mandatory to support the event extension.
231 */
232 typedef struct {
233 	/**
234 	   Opaque pointer to host data.
235 
236 	   The plugin MUST pass this to any call to functions in this struct.
237 	   Otherwise, it must not be interpreted in any way.
238 	*/
239 	LV2_Event_Callback_Data callback_data;
240 
241 	/**
242 	   Take a reference to a non-POD event.
243 
244 	   If a plugin receives an event with type 0, it means the event is a
245 	   pointer to some object in memory and not a flat sequence of bytes
246 	   in the buffer. When receiving a non-POD event, the plugin already
247 	   has an implicit reference to the event. If the event is stored AND
248 	   passed to an output, lv2_event_ref MUST be called on that event.
249 	   If the event is only stored OR passed through, this is not necessary
250 	   (as the plugin already has 1 implicit reference).
251 
252 	   @param event An event received at an input that will not be copied to
253 	   an output or stored in any way.
254 
255 	   @param context The calling context. Like event types, this is a mapped
256 	   URI, see lv2_context.h. Simple plugin with just a run() method should
257 	   pass 0 here (the ID of the 'standard' LV2 run context). The host
258 	   guarantees that this function is realtime safe iff @a context is
259 	   realtime safe.
260 
261 	   PLUGINS THAT VIOLATE THESE RULES MAY CAUSE CRASHES AND MEMORY LEAKS.
262 	*/
263 	uint32_t (*lv2_event_ref)(LV2_Event_Callback_Data callback_data,
264 	                          LV2_Event*              event);
265 
266 	/**
267 	   Drop a reference to a non-POD event.
268 
269 	   If a plugin receives an event with type 0, it means the event is a
270 	   pointer to some object in memory and not a flat sequence of bytes
271 	   in the buffer. If the plugin does not pass the event through to
272 	   an output or store it internally somehow, it MUST call this function
273 	   on the event (more information on using non-POD events below).
274 
275 	   @param event An event received at an input that will not be copied to an
276 	   output or stored in any way.
277 
278 	   @param context The calling context. Like event types, this is a mapped
279 	   URI, see lv2_context.h. Simple plugin with just a run() method should
280 	   pass 0 here (the ID of the 'standard' LV2 run context). The host
281 	   guarantees that this function is realtime safe iff @a context is
282 	   realtime safe.
283 
284 	   PLUGINS THAT VIOLATE THESE RULES MAY CAUSE CRASHES AND MEMORY LEAKS.
285 	*/
286 	uint32_t (*lv2_event_unref)(LV2_Event_Callback_Data callback_data,
287 	                            LV2_Event*              event);
288 } LV2_Event_Feature;
289 
290 #ifdef __cplusplus
291 }  /* extern "C" */
292 #endif
293 
294 #endif /* LV2_EVENT_H */
295