2 * LV2-related helper classes and functions
4 * Copyright (C) 2001-2008 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 #ifndef CALF_LV2HELPERS_H
22 #define CALF_LV2HELPERS_H
26 #include <calf/lv2_event.h>
27 #include <calf/lv2_uri_map.h>
32 /// URI map feature pointer (previously in a mixin, but polymorphic ports made it necessary for most plugins)
33 LV2_URI_Map_Feature
*uri_map
;
39 /// Map an URI through an URI map
40 uint32_t map_uri(const char *ns
, const char *URI
)
43 return uri_map
->uri_to_id(uri_map
->callback_data
, ns
, URI
);
46 /// Called on instantiation for every LV2 feature sent by a host
47 void use_feature(const char *URI
, void *data
) {
48 if (!strcmp(URI
, LV2_URI_MAP_URI
))
50 uri_map
= (LV2_URI_Map_Feature
*)data
;
54 virtual void map_uris()
57 virtual ~uri_map_access() {}
60 /// A mixin for adding the event feature and URI map to the small plugin
62 class event_mixin
: public T
65 /// Event feature pointer
66 LV2_Event_Feature
*event_feature
;
67 virtual void use_feature(const char *URI
, void *data
) {
68 if (!strcmp(URI
, LV2_EVENT_URI
))
70 event_feature
= (LV2_Event_Feature
*)data
;
72 T::use_feature(URI
, data
);
74 /// Create a reference
75 inline void ref_event(LV2_Event
*event
) { event_feature
->lv2_event_ref(event_feature
->callback_data
, event
); }
76 /// Destroy a reference
77 inline void unref_event(LV2_Event
*event
) { event_feature
->lv2_event_unref(event_feature
->callback_data
, event
); }
80 /// A mixin for adding the URI map and MIDI event type retrieval to small plugins
82 class midi_mixin
: public virtual event_mixin
<T
>
85 /// MIDI event ID, as resolved using the URI map feature
86 uint32_t midi_event_type
;
87 virtual void map_uris() {
88 midi_event_type
= this->map_uri("http://lv2plug.in/ns/ext/event", "http://lv2plug.in/ns/ext/midi#MidiEvent");
89 printf("MIDI event type = %d\n", midi_event_type
);
90 event_mixin
<T
>::map_uris();
94 /// A mixin for adding the URI map and MIDI event type retrieval to small plugins
96 class message_mixin
: public virtual event_mixin
<T
>
99 /// MIDI event ID, as resolved using the URI map feature
100 uint32_t message_event_type
;
101 virtual void map_uris() {
102 message_event_type
= this->map_uri("http://lv2plug.in/ns/ext/event", "http://lv2plug.in/ns/dev/msg#MessageEvent");
103 printf("Message event type = %d\n", message_event_type
);
104 event_mixin
<T
>::map_uris();
108 /// LV2 event structure + payload as 0-length array for easy access
109 struct lv2_event
: public LV2_Event
112 inline lv2_event
&operator=(const lv2_event
&src
) {
113 *(LV2_Event
*)this = (const LV2_Event
&)src
;
114 memcpy(data
, src
.data
, src
.size
);
117 /// Returns a 64-bit timestamp for easy and inefficient comparison
118 inline uint64_t timestamp() const {
119 return ((uint64_t)frames
<< 32) | subframes
;
122 /// forbid default constructor - this object cannot be constructed, only obtained via cast from LV2_Event* (or &) to lv2_event* (or &)
124 /// forbid copy constructor - see default constructor
125 lv2_event(const lv2_event
&) {}
128 /// A read-only iterator-like object for reading from event buffers
129 class event_port_read_iterator
132 const LV2_Event_Buffer
*buffer
;
135 /// Default constructor creating a useless iterator you can assign to
136 event_port_read_iterator()
142 /// Create an iterator based on specified buffer and index/offset values
143 event_port_read_iterator(const LV2_Event_Buffer
*_buffer
, uint32_t _offset
= 0)
149 /// Are any data left to be read?
150 inline operator bool() const {
151 return offset
< buffer
->size
;
155 inline const lv2_event
&operator*() const {
156 return *(const lv2_event
*)(buffer
->data
+ offset
);
158 /// Pointer to member
159 inline const lv2_event
*operator->() const {
163 /// Move to the next element
164 inline event_port_read_iterator
operator++() {
165 offset
+= ((**this).size
+ 19) &~7;
169 /// Move to the next element
170 inline event_port_read_iterator
operator++(int) {
171 event_port_read_iterator old
= *this;
172 offset
+= ((**this).size
+ 19) &~7;
177 /// A write-only iterator-like object for writing to event buffers
178 class event_port_write_iterator
181 LV2_Event_Buffer
*buffer
;
183 /// Default constructor creating a useless iterator you can assign to
184 event_port_write_iterator()
189 /// Create a write iterator based on specified buffer and index/offset values
190 event_port_write_iterator(LV2_Event_Buffer
*_buffer
)
195 /// @return the remaining buffer space
196 inline uint32_t space_left() const {
197 return buffer
->capacity
- buffer
->size
;
199 /// @return write pointer
200 inline lv2_event
&operator*() {
201 return *(lv2_event
*)(buffer
->data
+ buffer
->size
);
203 /// Pointer to member
204 inline lv2_event
*operator->() {
207 /// Move to the next element after the current one has been written (must be called after each write)
208 inline event_port_write_iterator
operator++() {
209 buffer
->size
+= ((**this).size
+ 19) &~7;
210 buffer
->event_count
++;
213 /// Move to the next element after the current one has been written
214 inline lv2_event
*operator++(int) {
215 lv2_event
*ptr
= &**this;
216 buffer
->size
+= ((**this).size
+ 19) &~7;
217 buffer
->event_count
++;
222 template<class Iter1
, class Iter2
>
223 class event_port_merge_iterator
229 event_port_merge_iterator() {}
230 event_port_merge_iterator(const Iter1
&_first
, const Iter2
&_second
)
235 /// @retval true if any of the iterators have any data left
236 inline operator bool() const {
237 return ((bool)first
) || ((bool)second
);
239 inline bool select_first() const
245 return first
->timestamp() < second
->timestamp();
247 /// Returns the earliest of (*first, *second)
248 inline const lv2_event
&operator*() const {
254 assert((bool)second
);
257 /// Pointer to member
258 inline const lv2_event
*operator->() const {
262 inline event_port_merge_iterator
operator++() {
269 /// Postfix increment
270 inline event_port_merge_iterator
operator++(int) {
271 event_port_merge_iterator ptr
= *this;