| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/*************************************************************************** |
| 3 |
* pluginlv2.cc |
| 4 |
* |
| 5 |
* Sun Feb 7 15:15:24 CET 2016 |
| 6 |
* Copyright 2016 Bent Bisballe Nyeng |
| 7 |
* [email protected] |
| 8 |
****************************************************************************/ |
| 9 |
|
| 10 |
/* |
| 11 |
* This file is part of PluginGizmo. |
| 12 |
* |
| 13 |
* PluginGizmo is free software; you can redistribute it and/or modify |
| 14 |
* it under the terms of the GNU Lesser General Public License as published by |
| 15 |
* the Free Software Foundation; either version 3 of the License, or |
| 16 |
* (at your option) any later version. |
| 17 |
* |
| 18 |
* PluginGizmo is distributed in the hope that it will be useful, |
| 19 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
* GNU Lesser General Public License for more details. |
| 22 |
* |
| 23 |
* You should have received a copy of the GNU Lesser General Public License |
| 24 |
* along with PluginGizmo; if not, write to the Free Software |
| 25 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 26 |
*/ |
| 27 |
#include "pluginlv2.h" |
| 28 |
|
| 29 |
#include <sstream> |
| 30 |
#include <assert.h> |
| 31 |
|
| 32 |
#include "midievent.h" |
| 33 |
|
| 34 |
#define LV2_PLUGIN_URI__atom LV2_PLUGIN_URI "/atom#" |
| 35 |
#define LV2_PLUGIN_URI__instance LV2_PLUGIN_URI "#plugin-instance" |
| 36 |
#define LV2_PLUGIN_URI__ui LV2_PLUGIN_URI "#ui" |
| 37 |
|
| 38 |
#include "lv2/lv2plug.in/ns/ext/atom/util.h" |
| 39 |
#include <lv2/lv2plug.in/ns/ext/midi/midi.h> |
| 40 |
#include <lv2/lv2plug.in/ns/ext/instance-access/instance-access.h> |
| 41 |
|
| 42 |
void PluginLV2::init() |
| 43 |
{ |
| 44 |
for(auto& m : midnamData) |
| 45 |
{ |
| 46 |
m.first = -1; // Mark midnam slot as unsued. |
| 47 |
m.second.reserve(64); // Reserve 64 characters for the midnam name. |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
bool PluginLV2::getFreeWheel() const |
| 52 |
{ |
| 53 |
return free_wheel; |
| 54 |
} |
| 55 |
|
| 56 |
float PluginLV2::getSamplerate() |
| 57 |
{ |
| 58 |
return sample_rate; |
| 59 |
} |
| 60 |
|
| 61 |
std::size_t PluginLV2::getFramesize() |
| 62 |
{ |
| 63 |
return frame_size; |
| 64 |
} |
| 65 |
|
| 66 |
bool PluginLV2::getActive() |
| 67 |
{ |
| 68 |
return active; |
| 69 |
} |
| 70 |
|
| 71 |
float PluginLV2::getLatency() |
| 72 |
{ |
| 73 |
if(latency_port) |
| 74 |
{ |
| 75 |
return *latency_port; |
| 76 |
} |
| 77 |
|
| 78 |
return 0.0f; |
| 79 |
} |
| 80 |
|
| 81 |
void PluginLV2::setLatency(float latency) |
| 82 |
{ |
| 83 |
if(latency_port) |
| 84 |
{ |
| 85 |
*latency_port = latency; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
LV2_Handle PluginLV2::instantiate(const LV2_Descriptor* descriptor, |
| 90 |
double sample_rate, |
| 91 |
const char* bundle_path, |
| 92 |
const LV2_Feature *const *features) |
| 93 |
{ |
| 94 |
PluginLV2* plugin_lv2 = createEffectInstance(); |
| 95 |
|
| 96 |
plugin_lv2->sample_rate = sample_rate; |
| 97 |
|
| 98 |
plugin_lv2->input_event_ports.resize(plugin_lv2->getNumberOfMidiInputs(), |
| 99 |
nullptr); |
| 100 |
plugin_lv2->output_event_ports.resize(plugin_lv2->getNumberOfMidiOutputs(), |
| 101 |
nullptr); |
| 102 |
|
| 103 |
plugin_lv2->input_audio_ports.resize(plugin_lv2->getNumberOfAudioInputs()); |
| 104 |
plugin_lv2->output_audio_ports.resize(plugin_lv2->getNumberOfAudioOutputs()); |
| 105 |
|
| 106 |
for(auto& port : plugin_lv2->output_audio_ports) |
| 107 |
{ |
| 108 |
port = nullptr; |
| 109 |
} |
| 110 |
|
| 111 |
for(auto& port : plugin_lv2->input_audio_ports) |
| 112 |
{ |
| 113 |
port = nullptr; |
| 114 |
} |
| 115 |
|
| 116 |
while(*features != nullptr) |
| 117 |
{ |
| 118 |
std::string uri = (*features)->URI; |
| 119 |
void* data = (*features)->data; |
| 120 |
|
| 121 |
if(uri == LV2_URID__map) |
| 122 |
{ |
| 123 |
plugin_lv2->map = (LV2_URID_Map*)data; |
| 124 |
} |
| 125 |
|
| 126 |
#ifdef DISPLAY_INTERFACE |
| 127 |
if(uri == LV2_INLINEDISPLAY__queue_draw) |
| 128 |
{ |
| 129 |
plugin_lv2->queue_draw = (LV2_Inline_Display*)data; |
| 130 |
} |
| 131 |
#endif |
| 132 |
|
| 133 |
#ifdef MIDNAM_INTERFACE |
| 134 |
if(uri == LV2_MIDNAM__update) |
| 135 |
{ |
| 136 |
plugin_lv2->midnam = (LV2_Midnam*)data; |
| 137 |
} |
| 138 |
#endif |
| 139 |
|
| 140 |
++features; |
| 141 |
} |
| 142 |
|
| 143 |
// Only reported on creation. |
| 144 |
plugin_lv2->onSamplerateChange(sample_rate); |
| 145 |
|
| 146 |
return (LV2_Handle)plugin_lv2; |
| 147 |
} |
| 148 |
|
| 149 |
void PluginLV2::connectPort(LV2_Handle instance, uint32_t port, |
| 150 |
void *data_location) |
| 151 |
{ |
| 152 |
PluginLV2* plugin_lv2 = (PluginLV2*)instance; |
| 153 |
|
| 154 |
if(port == static_cast<uint32_t>(LV2Ports::FreeWheel)) |
| 155 |
{ |
| 156 |
plugin_lv2->free_wheel_port = (float*)data_location; |
| 157 |
if(plugin_lv2->free_wheel_port) |
| 158 |
{ |
| 159 |
plugin_lv2->free_wheel = (*plugin_lv2->free_wheel_port != 0.0f); |
| 160 |
// Signal first time. |
| 161 |
plugin_lv2->onFreeWheelChange(plugin_lv2->free_wheel); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if(port == static_cast<uint32_t>(LV2Ports::Latency)) |
| 166 |
{ |
| 167 |
plugin_lv2->latency_port = (float*)data_location; |
| 168 |
} |
| 169 |
|
| 170 |
uint32_t port_offset = static_cast<uint32_t>(LV2Ports::PortOffset); |
| 171 |
|
| 172 |
if((port >= port_offset) && |
| 173 |
(port < (port_offset + plugin_lv2->getNumberOfMidiInputs()))) |
| 174 |
{ |
| 175 |
int port_index = port - port_offset; |
| 176 |
plugin_lv2->input_event_ports[port_index] = |
| 177 |
(LV2_Atom_Sequence*)data_location; |
| 178 |
} |
| 179 |
|
| 180 |
port_offset += plugin_lv2->getNumberOfMidiInputs(); |
| 181 |
|
| 182 |
if((port >= port_offset) && |
| 183 |
(port < (port_offset + plugin_lv2->getNumberOfMidiOutputs()))) |
| 184 |
{ |
| 185 |
int port_index = port - port_offset; |
| 186 |
plugin_lv2->output_event_ports[port_index] = |
| 187 |
(LV2_Atom_Sequence*)data_location; |
| 188 |
} |
| 189 |
|
| 190 |
port_offset += plugin_lv2->getNumberOfMidiOutputs(); |
| 191 |
|
| 192 |
if((port >= port_offset) && |
| 193 |
(port < (port_offset + plugin_lv2->getNumberOfAudioInputs()))) |
| 194 |
{ |
| 195 |
int port_index = port - port_offset; |
| 196 |
plugin_lv2->input_audio_ports[port_index] = (float*)data_location; |
| 197 |
} |
| 198 |
|
| 199 |
port_offset += plugin_lv2->getNumberOfAudioInputs(); |
| 200 |
|
| 201 |
if((port >= port_offset) && |
| 202 |
(port < (port_offset + plugin_lv2->getNumberOfAudioOutputs()))) |
| 203 |
{ |
| 204 |
int port_index = port - port_offset; |
| 205 |
plugin_lv2->output_audio_ports[port_index] = (float*)data_location; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
class Sequence { |
| 210 |
public: |
| 211 |
Sequence(LV2_URID_Map& map, void* buffer, std::size_t buffer_size); |
| 212 |
void clear(); |
| 213 |
void addMidiEvent(std::size_t pos, const char* data, std::size_t size); |
| 214 |
void* data(); |
| 215 |
|
| 216 |
private: |
| 217 |
void *buffer; |
| 218 |
std::size_t buffer_size; |
| 219 |
LV2_Atom_Sequence *seq; |
| 220 |
LV2_URID_Map& map; |
| 221 |
}; |
| 222 |
|
| 223 |
Sequence::Sequence(LV2_URID_Map& map, void* buffer, std::size_t buffer_size) |
| 224 |
: map(map) |
| 225 |
{ |
| 226 |
this->buffer = buffer; |
| 227 |
this->buffer_size = buffer_size; |
| 228 |
|
| 229 |
seq = (LV2_Atom_Sequence*)buffer; |
| 230 |
|
| 231 |
seq->atom.size = sizeof(LV2_Atom_Sequence_Body); |
| 232 |
seq->atom.type = map.map(map.handle, LV2_ATOM__Sequence); |
| 233 |
seq->body.unit = 0; |
| 234 |
seq->body.pad = 0; |
| 235 |
} |
| 236 |
|
| 237 |
// Keep this to support atom extension from lv2 < 1.10 |
| 238 |
static inline void _lv2_atom_sequence_clear(LV2_Atom_Sequence* seq) |
| 239 |
{ |
| 240 |
seq->atom.size = sizeof(LV2_Atom_Sequence_Body); |
| 241 |
} |
| 242 |
|
| 243 |
void Sequence::clear() |
| 244 |
{ |
| 245 |
_lv2_atom_sequence_clear(seq); |
| 246 |
} |
| 247 |
|
| 248 |
// Keep this to support atom extension from lv2 < 1.10 |
| 249 |
static inline LV2_Atom_Event* |
| 250 |
_lv2_atom_sequence_append_event(LV2_Atom_Sequence* seq, |
| 251 |
uint32_t capacity, |
| 252 |
const LV2_Atom_Event* event) |
| 253 |
{ |
| 254 |
const uint32_t total_size = (uint32_t)sizeof(*event) + event->body.size; |
| 255 |
|
| 256 |
if(capacity - seq->atom.size < total_size) |
| 257 |
{ |
| 258 |
return nullptr; |
| 259 |
} |
| 260 |
|
| 261 |
LV2_Atom_Event* e = lv2_atom_sequence_end(&seq->body, seq->atom.size); |
| 262 |
memcpy(e, event, total_size); |
| 263 |
|
| 264 |
seq->atom.size += lv2_atom_pad_size(total_size); |
| 265 |
|
| 266 |
return e; |
| 267 |
} |
| 268 |
|
| 269 |
void Sequence::addMidiEvent(std::size_t pos, const char* data, std::size_t size) |
| 270 |
{ |
| 271 |
struct MIDINoteEvent { |
| 272 |
LV2_Atom_Event event; |
| 273 |
uint8_t msg[6]; |
| 274 |
}; |
| 275 |
|
| 276 |
MIDINoteEvent ev; |
| 277 |
ev.event.time.frames = pos; |
| 278 |
ev.event.body.type = map.map(map.handle, LV2_MIDI__MidiEvent); |
| 279 |
ev.event.body.size = size; |
| 280 |
|
| 281 |
assert(size <= sizeof(ev.msg)); // Assert that we have room for the message |
| 282 |
|
| 283 |
memcpy(ev.msg, data, size); |
| 284 |
|
| 285 |
_lv2_atom_sequence_append_event(seq, this->buffer_size, &ev.event); |
| 286 |
} |
| 287 |
|
| 288 |
void* Sequence::data() |
| 289 |
{ |
| 290 |
return buffer; |
| 291 |
} |
| 292 |
|
| 293 |
void PluginLV2::run(LV2_Handle instance, uint32_t sample_count) |
| 294 |
{ |
| 295 |
PluginLV2* plugin_lv2 = (PluginLV2*)instance; |
| 296 |
|
| 297 |
// Handle free-wheel state |
| 298 |
if(plugin_lv2->free_wheel_port != nullptr) |
| 299 |
{ |
| 300 |
bool new_free_wheel = *plugin_lv2->free_wheel_port != 0.0f; |
| 301 |
if(new_free_wheel != plugin_lv2->free_wheel) |
| 302 |
{ |
| 303 |
plugin_lv2->free_wheel = new_free_wheel; |
| 304 |
plugin_lv2->onFreeWheelChange(plugin_lv2->free_wheel); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
// Handle frame size |
| 309 |
if(plugin_lv2->frame_size != sample_count) |
| 310 |
{ |
| 311 |
plugin_lv2->frame_size = sample_count; |
| 312 |
plugin_lv2->onFramesizeChange(plugin_lv2->frame_size); |
| 313 |
} |
| 314 |
|
| 315 |
// Convert input lv2 events to input events. |
| 316 |
std::vector<MidiEvent> input_events; |
| 317 |
for(std::size_t port = 0; port < plugin_lv2->getNumberOfMidiInputs(); ++port) |
| 318 |
{ |
| 319 |
if(plugin_lv2->input_event_ports[port] == nullptr) |
| 320 |
{ |
| 321 |
continue; // Not yet connected. |
| 322 |
} |
| 323 |
|
| 324 |
auto& event_port = plugin_lv2->input_event_ports[port]; |
| 325 |
|
| 326 |
for(LV2_Atom_Event* ev = lv2_atom_sequence_begin(&event_port->body); |
| 327 |
!lv2_atom_sequence_is_end(&event_port->body, event_port->atom.size, ev); |
| 328 |
ev = lv2_atom_sequence_next(ev)) |
| 329 |
{ |
| 330 |
if(ev->body.type != plugin_lv2->map->map(plugin_lv2->map->handle, |
| 331 |
LV2_MIDI__MidiEvent)) |
| 332 |
{ |
| 333 |
continue; // not a midi event. |
| 334 |
} |
| 335 |
|
| 336 |
const char* data = (char*)(ev + 1); |
| 337 |
input_events.emplace_back(ev->time.frames, data, ev->body.size); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
std::vector<MidiEvent> output_events; |
| 342 |
|
| 343 |
// Process events and audio |
| 344 |
plugin_lv2->process(plugin_lv2->pos, |
| 345 |
input_events, |
| 346 |
output_events, |
| 347 |
plugin_lv2->input_audio_ports, |
| 348 |
plugin_lv2->output_audio_ports, |
| 349 |
sample_count); |
| 350 |
|
| 351 |
// Convert output events to lv2 events |
| 352 |
if(plugin_lv2->getNumberOfMidiOutputs() > 0) |
| 353 |
{ |
| 354 |
if(plugin_lv2->map != nullptr) |
| 355 |
{ |
| 356 |
if(plugin_lv2->output_event_ports[0] != nullptr) // Not yet connected? |
| 357 |
{ |
| 358 |
auto& event_port = plugin_lv2->output_event_ports[0]; // TODO: Split? |
| 359 |
Sequence seq(*plugin_lv2->map, |
| 360 |
&event_port->body + 1, |
| 361 |
event_port->atom.size); |
| 362 |
for(auto midi_event : output_events) |
| 363 |
{ |
| 364 |
seq.addMidiEvent(midi_event.getTime(), |
| 365 |
midi_event.getData(), |
| 366 |
midi_event.getSize()); |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
plugin_lv2->pos += sample_count; |
| 373 |
|
| 374 |
#ifdef MIDNAM_INTERFACE |
| 375 |
if(plugin_lv2->midnam && plugin_lv2->midnam_changed.load()) |
| 376 |
{ |
| 377 |
plugin_lv2->midnam->update(plugin_lv2->midnam->handle); |
| 378 |
plugin_lv2->midnam_changed.store(false); |
| 379 |
} |
| 380 |
#endif |
| 381 |
|
| 382 |
#ifdef DISPLAY_INTERFACE |
| 383 |
if(plugin_lv2->queue_draw) |
| 384 |
{ |
| 385 |
plugin_lv2->queue_draw->queue_draw(plugin_lv2->queue_draw->handle); |
| 386 |
} |
| 387 |
#endif |
| 388 |
} |
| 389 |
|
| 390 |
void PluginLV2::activate(LV2_Handle instance) |
| 391 |
{ |
| 392 |
PluginLV2* plugin_lv2 = (PluginLV2*)instance; |
| 393 |
plugin_lv2->active = true; |
| 394 |
plugin_lv2->onActiveChange(plugin_lv2->active); |
| 395 |
} |
| 396 |
|
| 397 |
void PluginLV2::deactivate(LV2_Handle instance) |
| 398 |
{ |
| 399 |
PluginLV2* plugin_lv2 = (PluginLV2*)instance; |
| 400 |
plugin_lv2->active = false; |
| 401 |
plugin_lv2->onActiveChange(plugin_lv2->active); |
| 402 |
} |
| 403 |
|
| 404 |
void PluginLV2::cleanup(LV2_Handle instance) |
| 405 |
{ |
| 406 |
PluginLV2* plugin_lv2 = (PluginLV2*)instance; |
| 407 |
delete plugin_lv2; |
| 408 |
} |
| 409 |
|
| 410 |
// |
| 411 |
// State handling |
| 412 |
// |
| 413 |
|
| 414 |
LV2_State_Status PluginLV2::save(LV2_Handle instance, |
| 415 |
LV2_State_Store_Function store, |
| 416 |
LV2_State_Handle handle, |
| 417 |
uint32_t flags, |
| 418 |
const LV2_Feature *const * features) |
| 419 |
{ |
| 420 |
PluginLV2* plugin_lv2 = (PluginLV2*)instance; |
| 421 |
|
| 422 |
if(plugin_lv2->map == nullptr) |
| 423 |
{ |
| 424 |
// Missing urid feature? |
| 425 |
return LV2_STATE_ERR_NO_FEATURE; |
| 426 |
} |
| 427 |
|
| 428 |
std::string config = plugin_lv2->onStateSave(); |
| 429 |
|
| 430 |
store(handle, |
| 431 |
plugin_lv2->map->map(plugin_lv2->map->handle, |
| 432 |
LV2_PLUGIN_URI__atom "config"), |
| 433 |
config.data(), |
| 434 |
config.length(), |
| 435 |
plugin_lv2->map->map(plugin_lv2->map->handle, LV2_ATOM__Chunk), |
| 436 |
LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE); |
| 437 |
|
| 438 |
return LV2_STATE_SUCCESS; |
| 439 |
} |
| 440 |
|
| 441 |
LV2_State_Status PluginLV2::restore(LV2_Handle instance, |
| 442 |
LV2_State_Retrieve_Function retrieve, |
| 443 |
LV2_State_Handle handle, |
| 444 |
uint32_t flags, |
| 445 |
const LV2_Feature *const * features) |
| 446 |
{ |
| 447 |
PluginLV2* plugin_lv2 = (PluginLV2*)instance; |
| 448 |
|
| 449 |
if(plugin_lv2->map == nullptr) |
| 450 |
{ |
| 451 |
// Missing urid feature? |
| 452 |
return LV2_STATE_ERR_NO_FEATURE; |
| 453 |
} |
| 454 |
|
| 455 |
std::size_t size; |
| 456 |
uint32_t type; |
| 457 |
|
| 458 |
const char* data = |
| 459 |
(const char*)retrieve(handle, |
| 460 |
plugin_lv2->map->map(plugin_lv2->map->handle, |
| 461 |
LV2_PLUGIN_URI__atom "config"), |
| 462 |
&size, &type, &flags); |
| 463 |
|
| 464 |
if(data && size) |
| 465 |
{ |
| 466 |
std::string config; |
| 467 |
|
| 468 |
config.append(data, size); |
| 469 |
plugin_lv2->onStateRestore(config); |
| 470 |
} |
| 471 |
|
| 472 |
return LV2_STATE_SUCCESS; |
| 473 |
} |
| 474 |
|
| 475 |
static LV2_State_Interface persist = { |
| 476 |
PluginLV2::save, |
| 477 |
PluginLV2::restore |
| 478 |
}; |
| 479 |
|
| 480 |
LV2_Inline_Display_Image_Surface* PluginLV2::inlineRender(LV2_Handle instance, |
| 481 |
uint32_t w, |
| 482 |
uint32_t max_h) |
| 483 |
{ |
| 484 |
PluginLV2* plugin_lv2 = (PluginLV2*)instance; |
| 485 |
|
| 486 |
plugin_lv2->onInlineRedraw(w, max_h, plugin_lv2->drawContext); |
| 487 |
|
| 488 |
plugin_lv2->surf.width = plugin_lv2->drawContext.width; |
| 489 |
plugin_lv2->surf.height = plugin_lv2->drawContext.height; |
| 490 |
plugin_lv2->surf.stride = plugin_lv2->surf.width * 4; // stride is in bytes |
| 491 |
|
| 492 |
plugin_lv2->surf.data = plugin_lv2->drawContext.data; |
| 493 |
|
| 494 |
return &plugin_lv2->surf; |
| 495 |
} |
| 496 |
|
| 497 |
void PluginLV2::setMidnamData(const std::vector<std::pair<int, std::string>>& midnam) |
| 498 |
{ |
| 499 |
auto idx = 0u; |
| 500 |
for(const auto& m : midnam) |
| 501 |
{ |
| 502 |
midnamData[idx].first = m.first; |
| 503 |
// Duplicate name making sure no reallocation is being performed. |
| 504 |
midnamData[idx].second = |
| 505 |
m.second.substr(0, midnamData[idx].second.capacity() - 1); |
| 506 |
idx++; |
| 507 |
} |
| 508 |
for(;idx < this->midnamData.size(); ++idx) |
| 509 |
{ |
| 510 |
midnamData[idx].first = -1; // Mark as unused |
| 511 |
} |
| 512 |
midnam_changed.store(true); |
| 513 |
} |
| 514 |
|
| 515 |
#ifdef MIDNAM_INTERFACE |
| 516 |
char* PluginLV2::MidnamFile (LV2_Handle instance) |
| 517 |
{ |
| 518 |
PluginLV2* plugin_lv2 = (PluginLV2*)instance; |
| 519 |
std::stringstream ss; |
| 520 |
ss << |
| 521 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 522 |
"<!DOCTYPE MIDINameDocument PUBLIC \"-//MIDI Manufacturers Association//DTD MIDINameDocument 1.0//EN\" \"http://dev.midi.org/dtds/MIDINameDocument10.dtd\">\n" |
| 523 |
"<MIDINameDocument>\n" |
| 524 |
" <Author/>\n" |
| 525 |
" <MasterDeviceNames>\n" |
| 526 |
" <Manufacturer>" << plugin_lv2->getVendorString() << "</Manufacturer>\n" |
| 527 |
" <Model>" << plugin_lv2->getProductString() << ":" << ((const void *) instance) << "</Model>\n"; //<< needs to match MidnamModel() |
| 528 |
|
| 529 |
ss << " <CustomDeviceMode Name=\"Default\">\n"; |
| 530 |
ss << " <ChannelNameSetAssignments>\n"; |
| 531 |
for (int c = 0; c < 16; ++c) |
| 532 |
{ |
| 533 |
ss << " <ChannelNameSetAssign Channel=\"" << (c + 1) << "\" NameSet=\"Presets\"/>\n"; |
| 534 |
} |
| 535 |
ss << " </ChannelNameSetAssignments>\n"; |
| 536 |
ss << " </CustomDeviceMode>\n"; |
| 537 |
|
| 538 |
|
| 539 |
ss << |
| 540 |
" <ChannelNameSet Name=\"Presets\">\n" |
| 541 |
" <AvailableForChannels>\n"; |
| 542 |
for (int c = 0; c < 16; ++c) { |
| 543 |
ss << " <AvailableChannel Channel=\"" << (c + 1) << "\" Available=\"true\"/>\n"; |
| 544 |
} |
| 545 |
ss << |
| 546 |
" </AvailableForChannels>\n" |
| 547 |
" <UsesNoteNameList Name=\"Notes\"/>\n" |
| 548 |
" <PatchBank Name=\"User Patches\">\n" |
| 549 |
" <PatchNameList Name=\"User Patches\"/>\n" |
| 550 |
" </PatchBank>\n" |
| 551 |
" </ChannelNameSet>\n" |
| 552 |
" <NoteNameList Name=\"Notes\">\n"; |
| 553 |
// TODO: Fill in empty slots for the notes that are not in the map |
| 554 |
// Does Ardour preserve the note names for the unspecified notes? |
| 555 |
// ... and is this a bug?? |
| 556 |
for(const auto& m : plugin_lv2->midnamData) |
| 557 |
{ |
| 558 |
if(m.first == -1) |
| 559 |
{ |
| 560 |
continue; |
| 561 |
} |
| 562 |
ss << " <Note Number=\"" << m.first << "\" Name=\""<< m.second << "\"/>\n"; |
| 563 |
} |
| 564 |
|
| 565 |
ss << |
| 566 |
" </NoteNameList>\n" |
| 567 |
" </MasterDeviceNames>\n" |
| 568 |
"</MIDINameDocument>"; |
| 569 |
|
| 570 |
return strdup (ss.str().c_str()); |
| 571 |
} |
| 572 |
|
| 573 |
char* PluginLV2::MidnamModel(LV2_Handle instance) |
| 574 |
{ |
| 575 |
PluginLV2* plugin_lv2 = (PluginLV2*)instance; |
| 576 |
char* rv = (char*) malloc (64 * sizeof (char)); |
| 577 |
snprintf(rv, 64, "%s:%p", |
| 578 |
plugin_lv2->getProductString().data(), (void*) instance); |
| 579 |
rv[63] = 0; |
| 580 |
return rv; |
| 581 |
} |
| 582 |
|
| 583 |
void PluginLV2::MidnamFree (char* v) |
| 584 |
{ |
| 585 |
free (v); |
| 586 |
} |
| 587 |
#endif |
| 588 |
|
| 589 |
const void* PluginLV2::extensionData(const char *uri) |
| 590 |
{ |
| 591 |
if(!strcmp(uri, LV2_STATE__interface)) |
| 592 |
{ |
| 593 |
return &persist; |
| 594 |
} |
| 595 |
|
| 596 |
#ifdef DISPLAY_INTERFACE |
| 597 |
static const LV2_Inline_Display_Interface display = { inlineRender }; |
| 598 |
if(!strcmp(uri, LV2_INLINEDISPLAY__interface)) |
| 599 |
{ |
| 600 |
return &display; |
| 601 |
} |
| 602 |
#endif |
| 603 |
|
| 604 |
#ifdef MIDNAM_INTERFACE |
| 605 |
static const LV2_Midnam_Interface midnam = { MidnamFile, MidnamModel, MidnamFree }; |
| 606 |
if (!strcmp (uri, LV2_MIDNAM__interface)) |
| 607 |
{ |
| 608 |
return &midnam; |
| 609 |
} |
| 610 |
#endif |
| 611 |
|
| 612 |
return nullptr; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
// |
| 617 |
// GUI |
| 618 |
// |
| 619 |
LV2UI_Handle PluginLV2::uiInstantiate(const LV2UI_Descriptor*descriptor, |
| 620 |
const char * plugin_uri, |
| 621 |
const char * bundle_path, |
| 622 |
LV2UI_Write_Function write_function, |
| 623 |
LV2UI_Controller controller, |
| 624 |
LV2UI_Widget* widget, |
| 625 |
const LV2_Feature * const * features) |
| 626 |
{ |
| 627 |
LV2_Handle instance = nullptr; |
| 628 |
void* parent = nullptr; |
| 629 |
LV2UI_Resize* resize = nullptr; |
| 630 |
|
| 631 |
while(*features != nullptr) { |
| 632 |
std::string uri = (*features)->URI; |
| 633 |
void *data = (*features)->data; |
| 634 |
|
| 635 |
if(uri == LV2_INSTANCE_ACCESS_URI) |
| 636 |
{ |
| 637 |
instance = (LV2_Handle)data; |
| 638 |
} |
| 639 |
|
| 640 |
if(uri == LV2_UI__parent) |
| 641 |
{ |
| 642 |
parent = data; |
| 643 |
} |
| 644 |
|
| 645 |
if(uri == LV2_UI__resize) |
| 646 |
{ |
| 647 |
resize = (LV2UI_Resize*)data; |
| 648 |
} |
| 649 |
|
| 650 |
features++; |
| 651 |
} |
| 652 |
|
| 653 |
if(instance == nullptr) |
| 654 |
{ |
| 655 |
return nullptr; |
| 656 |
} |
| 657 |
|
| 658 |
PluginLV2* plugin_lv2 = (PluginLV2*)instance; |
| 659 |
|
| 660 |
// Do we have a GUI? |
| 661 |
if(plugin_lv2->hasGUI() == false) |
| 662 |
{ |
| 663 |
return nullptr; |
| 664 |
} |
| 665 |
|
| 666 |
plugin_lv2->resize = resize; |
| 667 |
|
| 668 |
*widget = plugin_lv2->createWindow(parent); |
| 669 |
|
| 670 |
return plugin_lv2; |
| 671 |
} |
| 672 |
|
| 673 |
void PluginLV2::resizeWindow(std::size_t width, std::size_t height) |
| 674 |
{ |
| 675 |
if(resize) |
| 676 |
{ |
| 677 |
resize->ui_resize(resize->handle, width, height); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
void PluginLV2::closeWindow() |
| 682 |
{ |
| 683 |
} |
| 684 |
|
| 685 |
static const LV2_Descriptor descriptor = { |
| 686 |
LV2_PLUGIN_URI, |
| 687 |
PluginLV2::instantiate, |
| 688 |
PluginLV2::connectPort, |
| 689 |
PluginLV2::activate, |
| 690 |
PluginLV2::run, |
| 691 |
PluginLV2::deactivate, |
| 692 |
PluginLV2::cleanup, |
| 693 |
PluginLV2::extensionData |
| 694 |
}; |
| 695 |
|
| 696 |
void PluginLV2::uiCleanup(LV2UI_Handle handle) |
| 697 |
{ |
| 698 |
PluginLV2* plugin_lv2 = (PluginLV2*)handle; |
| 699 |
plugin_lv2->onDestroyWindow () ; |
| 700 |
|
| 701 |
} |
| 702 |
|
| 703 |
int PluginLV2::uiIdle(LV2UI_Handle handle) |
| 704 |
{ |
| 705 |
PluginLV2* plugin_lv2 = (PluginLV2*)handle; |
| 706 |
plugin_lv2->onIdle(); |
| 707 |
return 0; |
| 708 |
} |
| 709 |
|
| 710 |
static const LV2UI_Idle_Interface idle_iface = { |
| 711 |
PluginLV2::uiIdle |
| 712 |
}; |
| 713 |
|
| 714 |
const void* PluginLV2::uiExtensionData(const char* uri) |
| 715 |
{ |
| 716 |
if(!strcmp(uri, LV2_UI__idleInterface)) |
| 717 |
{ |
| 718 |
return &idle_iface; |
| 719 |
} |
| 720 |
|
| 721 |
return NULL; |
| 722 |
} |
| 723 |
|
| 724 |
static LV2UI_Descriptor ui_descriptor = { |
| 725 |
LV2_PLUGIN_URI__ui, |
| 726 |
PluginLV2::uiInstantiate, |
| 727 |
PluginLV2::uiCleanup, |
| 728 |
nullptr,//PluginLV2::ui_port_event, |
| 729 |
PluginLV2::uiExtensionData |
| 730 |
}; |
| 731 |
|
| 732 |
#ifdef __cplusplus |
| 733 |
extern "C" { |
| 734 |
#endif |
| 735 |
|
| 736 |
LV2_SYMBOL_EXPORT |
| 737 |
const LV2_Descriptor* lv2_descriptor(uint32_t index) |
| 738 |
{ |
| 739 |
switch (index) { |
| 740 |
case 0: |
| 741 |
return &descriptor; |
| 742 |
default: |
| 743 |
return nullptr; |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
LV2_SYMBOL_EXPORT |
| 748 |
const LV2UI_Descriptor *lv2ui_descriptor(uint32_t index) |
| 749 |
{ |
| 750 |
switch(index) { |
| 751 |
case 0: |
| 752 |
return &ui_descriptor; |
| 753 |
default: |
| 754 |
return nullptr; |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
// |
| 759 |
// DynManifest experiments failed, but the code is kept here for nostalgic |
| 760 |
// reasons :-) |
| 761 |
// |
| 762 |
// |
| 763 |
//int lv2_dyn_manifest_open(LV2_Dyn_Manifest_Handle* handle, |
| 764 |
// const LV2_Feature* const* features) |
| 765 |
//{ |
| 766 |
// *handle = createEffectInstance(); |
| 767 |
// return 0; |
| 768 |
//} |
| 769 |
// |
| 770 |
//int lv2_dyn_manifest_get_subjects(LV2_Dyn_Manifest_Handle handle, FILE* fp) |
| 771 |
//{ |
| 772 |
//// PluginLV2* plugin_lv2 = (PluginLV2*)handle; |
| 773 |
// |
| 774 |
// fprintf(fp, "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n"); |
| 775 |
// fprintf(fp, "<" LV2_PLUGIN_URI "> a lv2:Plugin .\n"); |
| 776 |
// return 0; |
| 777 |
//} |
| 778 |
// |
| 779 |
//int lv2_dyn_manifest_get_data(LV2_Dyn_Manifest_Handle handle, FILE* fp, |
| 780 |
// const char* uri) |
| 781 |
//{ |
| 782 |
// //PluginLV2* plugin_lv2 = (PluginLV2*)handle; |
| 783 |
// printf("%s '%s'\n", __PRETTY_FUNCTION__, uri); |
| 784 |
// return 0; |
| 785 |
//} |
| 786 |
// |
| 787 |
//void lv2_dyn_manifest_close(LV2_Dyn_Manifest_Handle handle) |
| 788 |
//{ |
| 789 |
// PluginLV2* plugin_lv2 = (PluginLV2*)handle; |
| 790 |
// delete plugin_lv2; |
| 791 |
//} |
| 792 |
|
| 793 |
#ifdef __cplusplus |
| 794 |
} |
| 795 |
#endif |