2 Copyright (C) 2008-2012 Paul Davis
3 Author: David Robillard
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <giomm/file.h>
29 #include <glib/gprintf.h>
32 #include <boost/utility.hpp>
34 #include "pbd/compose.h"
35 #include "pbd/error.h"
36 #include "pbd/xml++.h"
38 #include "libardour-config.h"
40 #include "ardour/types.h"
41 #include "ardour/audio_buffer.h"
42 #include "ardour/audioengine.h"
43 #include "ardour/debug.h"
44 #include "ardour/lv2_plugin.h"
45 #include "ardour/session.h"
46 #include "ardour/tempo.h"
47 #include "ardour/worker.h"
52 #include <lilv/lilv.h>
54 #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
55 #include "lv2/lv2plug.in/ns/ext/atom/forge.h"
56 #include "lv2/lv2plug.in/ns/ext/log/log.h"
57 #include "lv2/lv2plug.in/ns/ext/midi/midi.h"
58 #include "lv2/lv2plug.in/ns/ext/port-props/port-props.h"
59 #include "lv2/lv2plug.in/ns/ext/presets/presets.h"
60 #include "lv2/lv2plug.in/ns/ext/state/state.h"
61 #include "lv2/lv2plug.in/ns/ext/time/time.h"
62 #include "lv2/lv2plug.in/ns/ext/worker/worker.h"
63 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
65 #include "lv2/lv2plug.in/ns/ext/buf-size/buf-size.h"
66 #include "lv2/lv2plug.in/ns/ext/options/options.h"
69 #include "lv2_evbuf.h"
72 #include <suil/suil.h>
75 /** The number of MIDI buffers that will fit in a UI/worker comm buffer.
76 This needs to be roughly the number of cycles the UI will get around to
77 actually processing the traffic. Lower values are flakier but save memory.
79 static const size_t NBUFS = 4;
82 using namespace ARDOUR;
85 URIMap LV2Plugin::_uri_map;
87 LV2Plugin::URIDs LV2Plugin::urids = {
88 _uri_map.uri_to_id(LV2_ATOM__Chunk),
89 _uri_map.uri_to_id(LV2_ATOM__Path),
90 _uri_map.uri_to_id(LV2_ATOM__Sequence),
91 _uri_map.uri_to_id(LV2_ATOM__eventTransfer),
92 _uri_map.uri_to_id(LV2_LOG__Error),
93 _uri_map.uri_to_id(LV2_LOG__Note),
94 _uri_map.uri_to_id(LV2_LOG__Warning),
95 _uri_map.uri_to_id(LV2_MIDI__MidiEvent),
96 _uri_map.uri_to_id(LV2_TIME__Position),
97 _uri_map.uri_to_id(LV2_TIME__bar),
98 _uri_map.uri_to_id(LV2_TIME__barBeat),
99 _uri_map.uri_to_id(LV2_TIME__beatUnit),
100 _uri_map.uri_to_id(LV2_TIME__beatsPerBar),
101 _uri_map.uri_to_id(LV2_TIME__beatsPerMinute),
102 _uri_map.uri_to_id(LV2_TIME__frame),
103 _uri_map.uri_to_id(LV2_TIME__speed)
106 class LV2World : boost::noncopyable {
113 LilvNode* atom_AtomPort;
114 LilvNode* atom_Chunk;
115 LilvNode* atom_Sequence;
116 LilvNode* atom_bufferType;
117 LilvNode* atom_eventTransfer;
118 LilvNode* atom_supports;
119 LilvNode* ev_EventPort;
120 LilvNode* ext_logarithmic;
121 LilvNode* lv2_AudioPort;
122 LilvNode* lv2_ControlPort;
123 LilvNode* lv2_InputPort;
124 LilvNode* lv2_OutputPort;
125 LilvNode* lv2_enumeration;
126 LilvNode* lv2_inPlaceBroken;
127 LilvNode* lv2_integer;
128 LilvNode* lv2_sampleRate;
129 LilvNode* lv2_toggled;
130 LilvNode* midi_MidiEvent;
131 LilvNode* rdfs_comment;
132 LilvNode* time_Position;
134 LilvNode* ui_external;
137 static LV2World _world;
139 /* worker extension */
141 /** Called by the plugin to schedule non-RT work. */
142 static LV2_Worker_Status
143 work_schedule(LV2_Worker_Schedule_Handle handle,
147 LV2Plugin* plugin = (LV2Plugin*)handle;
148 if (plugin->session().engine().freewheeling()) {
149 // Freewheeling, do the work immediately in this (audio) thread
150 return (LV2_Worker_Status)plugin->work(size, data);
152 // Enqueue message for the worker thread
153 return plugin->worker()->schedule(size, data) ?
154 LV2_WORKER_SUCCESS : LV2_WORKER_ERR_UNKNOWN;
158 /** Called by the plugin to respond to non-RT work. */
159 static LV2_Worker_Status
160 work_respond(LV2_Worker_Respond_Handle handle,
164 LV2Plugin* plugin = (LV2Plugin*)handle;
165 if (plugin->session().engine().freewheeling()) {
166 // Freewheeling, respond immediately in this (audio) thread
167 return (LV2_Worker_Status)plugin->work_response(size, data);
169 // Enqueue response for the worker
170 return plugin->worker()->respond(size, data) ?
171 LV2_WORKER_SUCCESS : LV2_WORKER_ERR_UNKNOWN;
178 log_vprintf(LV2_Log_Handle handle,
184 const int ret = g_vasprintf(&str, fmt, args);
185 if (type == LV2Plugin::urids.log_Error) {
186 error << str << endmsg;
187 } else if (type == LV2Plugin::urids.log_Warning) {
188 warning << str << endmsg;
189 } else if (type == LV2Plugin::urids.log_Note) {
190 info << str << endmsg;
192 // TODO: Toggleable log:Trace message support
197 log_printf(LV2_Log_Handle handle,
199 const char* fmt, ...)
203 const int ret = log_vprintf(handle, type, fmt, args);
208 struct LV2Plugin::Impl {
209 Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0)
214 /** Find the LV2 input port with the given designation.
215 * If found, bufptrs[port_index] will be set to bufptr.
217 LilvPort* designated_input (const char* uri, void** bufptrs[], void** bufptr);
219 const LilvPlugin* plugin;
221 const LilvNode* ui_type;
224 LilvInstance* instance;
225 const LV2_Worker_Interface* work_iface;
227 LV2_Atom_Forge forge;
230 LV2Plugin::LV2Plugin (AudioEngine& engine,
232 const void* c_plugin,
234 : Plugin (engine, session)
241 init(c_plugin, rate);
244 LV2Plugin::LV2Plugin (const LV2Plugin& other)
250 , _insert_id(other._insert_id)
252 init(other._impl->plugin, other._sample_rate);
254 for (uint32_t i = 0; i < parameter_count(); ++i) {
255 _control_data[i] = other._shadow_data[i];
256 _shadow_data[i] = other._shadow_data[i];
261 LV2Plugin::init(const void* c_plugin, framecnt_t rate)
263 DEBUG_TRACE(DEBUG::LV2, "init\n");
265 _impl->plugin = (const LilvPlugin*)c_plugin;
267 _impl->ui_type = NULL;
272 _atom_ev_buffers = 0;
274 _bpm_control_port = 0;
275 _freewheel_control_port = 0;
276 _latency_control_port = 0;
277 _position_seq_port_idx = std::numeric_limits<uint32_t>::max();
278 _next_cycle_start = std::numeric_limits<framepos_t>::max();
279 _next_cycle_speed = 1.0;
280 _block_length = _engine.frames_per_cycle();
281 _seq_size = _engine.raw_buffer_size(DataType::MIDI);
283 _was_activated = false;
284 _has_state_interface = false;
286 _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
287 _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
288 _make_path_feature.URI = LV2_STATE__makePath;
289 _log_feature.URI = LV2_LOG__log;
290 _work_schedule_feature.URI = LV2_WORKER__schedule;
291 _work_schedule_feature.data = NULL;
293 const LilvPlugin* plugin = _impl->plugin;
295 LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE__interface);
296 LilvNode* state_uri = lilv_new_uri(_world.world, LV2_STATE_URI);
297 _has_state_interface =
298 // What plugins should have (lv2:extensionData state:Interface)
299 lilv_plugin_has_extension_data(plugin, state_iface_uri)
300 // What some outdated/incorrect ones have
301 || lilv_plugin_has_feature(plugin, state_uri);
302 lilv_node_free(state_uri);
303 lilv_node_free(state_iface_uri);
305 _features = (LV2_Feature**)calloc(10, sizeof(LV2_Feature*));
306 _features[0] = &_instance_access_feature;
307 _features[1] = &_data_access_feature;
308 _features[2] = &_make_path_feature;
309 _features[3] = _uri_map.uri_map_feature();
310 _features[4] = _uri_map.urid_map_feature();
311 _features[5] = _uri_map.urid_unmap_feature();
312 _features[6] = &_log_feature;
314 lv2_atom_forge_init(&_impl->forge, _uri_map.urid_map());
316 unsigned n_features = 7;
318 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
319 LV2_Options_Option options[] = {
320 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__minBlockLength),
321 sizeof(int32_t), atom_Int, &_block_length },
322 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__maxBlockLength),
323 sizeof(int32_t), atom_Int, &_block_length },
324 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__sequenceSize),
325 sizeof(int32_t), atom_Int, &_seq_size },
326 { LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL }
329 _options_feature.URI = LV2_OPTIONS__options;
330 _options_feature.data = options;
331 _features[n_features++] = &_options_feature;
334 LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
335 sizeof(LV2_State_Make_Path));
336 make_path->handle = this;
337 make_path->path = &lv2_state_make_path;
338 _make_path_feature.data = make_path;
340 LV2_Log_Log* log = (LV2_Log_Log*)malloc(sizeof(LV2_Log_Log));
342 log->printf = &log_printf;
343 log->vprintf = &log_vprintf;
344 _log_feature.data = log;
346 LilvNode* worker_schedule = lilv_new_uri(_world.world, LV2_WORKER__schedule);
347 if (lilv_plugin_has_feature(plugin, worker_schedule)) {
348 LV2_Worker_Schedule* schedule = (LV2_Worker_Schedule*)malloc(
349 sizeof(LV2_Worker_Schedule));
350 size_t buf_size = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
351 _worker = new Worker(this, buf_size);
352 schedule->handle = this;
353 schedule->schedule_work = work_schedule;
354 _work_schedule_feature.data = schedule;
355 _features[n_features++] = &_work_schedule_feature;
357 lilv_node_free(worker_schedule);
359 _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
360 _impl->name = lilv_plugin_get_name(plugin);
361 _impl->author = lilv_plugin_get_author_name(plugin);
363 if (_impl->instance == 0) {
364 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
365 throw failed_constructor();
368 _instance_access_feature.data = (void*)_impl->instance->lv2_handle;
369 _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
370 _data_access_feature.data = &_data_access_extension_data;
372 _impl->work_iface = (const LV2_Worker_Interface*)extension_data(
373 LV2_WORKER__interface);
375 if (lilv_plugin_has_feature(plugin, _world.lv2_inPlaceBroken)) {
376 error << string_compose(
377 _("LV2: \"%1\" cannot be used, since it cannot do inplace processing"),
378 lilv_node_as_string(_impl->name)) << endmsg;
379 lilv_node_free(_impl->name);
380 lilv_node_free(_impl->author);
381 throw failed_constructor();
386 const uint32_t num_ports = this->num_ports();
387 for (uint32_t i = 0; i < num_ports; ++i) {
388 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, i);
391 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_OutputPort)) {
392 flags |= PORT_OUTPUT;
393 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_InputPort)) {
396 error << string_compose(
397 "LV2: \"%1\" port %2 is neither input nor output",
398 lilv_node_as_string(_impl->name), i) << endmsg;
399 throw failed_constructor();
402 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_ControlPort)) {
403 flags |= PORT_CONTROL;
404 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_AudioPort)) {
406 } else if (lilv_port_is_a(_impl->plugin, port, _world.ev_EventPort)) {
408 } else if (lilv_port_is_a(_impl->plugin, port, _world.atom_AtomPort)) {
409 LilvNodes* buffer_types = lilv_port_get_value(
410 _impl->plugin, port, _world.atom_bufferType);
411 LilvNodes* atom_supports = lilv_port_get_value(
412 _impl->plugin, port, _world.atom_supports);
414 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
415 if (lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
416 flags |= PORT_MESSAGE;
420 if (lilv_nodes_contains(atom_supports, _world.time_Position)) {
421 _position_seq_port_idx = i;
424 lilv_nodes_free(buffer_types);
425 lilv_nodes_free(atom_supports);
427 error << string_compose(
428 "LV2: \"%1\" port %2 has no known data type",
429 lilv_node_as_string(_impl->name), i) << endmsg;
430 throw failed_constructor();
433 _port_flags.push_back(flags);
436 _control_data = new float[num_ports];
437 _shadow_data = new float[num_ports];
438 _defaults = new float[num_ports];
439 _ev_buffers = new LV2_Evbuf*[num_ports];
440 memset(_ev_buffers, 0, sizeof(LV2_Evbuf*) * num_ports);
442 const bool latent = lilv_plugin_has_latency(plugin);
443 const uint32_t latency_index = (latent)
444 ? lilv_plugin_get_latency_port_index(plugin)
447 // Build an array of pointers to special parameter buffers
448 void*** params = new void**[num_ports];
449 for (uint32_t i = 0; i < num_ports; ++i) {
452 _impl->designated_input (LV2_TIME__beatsPerMinute, params, (void**)&_bpm_control_port);
453 _impl->designated_input (LV2_CORE__freeWheeling, params, (void**)&_freewheel_control_port);
455 for (uint32_t i = 0; i < num_ports; ++i) {
456 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
457 const LilvNode* sym = lilv_port_get_symbol(plugin, port);
459 // Store index in map so we can look up index by symbol
460 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
462 // Get range and default value if applicable
463 if (parameter_is_control(i)) {
465 lilv_port_get_range(plugin, port, &def, NULL, NULL);
466 _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
467 if (lilv_port_has_property (plugin, port, _world.lv2_sampleRate)) {
468 _defaults[i] *= _session.frame_rate ();
472 lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
474 if (latent && i == latency_index) {
475 _latency_control_port = &_control_data[i];
476 *_latency_control_port = 0;
479 if (parameter_is_input(i)) {
480 _shadow_data[i] = default_value(i);
482 *params[i] = (void*)&_shadow_data[i];
492 LilvUIs* uis = lilv_plugin_get_uis(plugin);
493 if (lilv_uis_size(uis) > 0) {
495 // Look for embeddable UI
496 LILV_FOREACH(uis, u, uis) {
497 const LilvUI* this_ui = lilv_uis_get(uis, u);
498 const LilvNode* this_ui_type = NULL;
499 if (lilv_ui_is_supported(this_ui,
503 // TODO: Multiple UI support
505 _impl->ui_type = this_ui_type;
510 // Look for Gtk native UI
511 LILV_FOREACH(uis, i, uis) {
512 const LilvUI* ui = lilv_uis_get(uis, i);
513 if (lilv_ui_is_a(ui, _world.ui_GtkUI)) {
515 _impl->ui_type = _world.ui_GtkUI;
521 // If Gtk UI is not available, try to find external UI
523 LILV_FOREACH(uis, i, uis) {
524 const LilvUI* ui = lilv_uis_get(uis, i);
525 if (lilv_ui_is_a(ui, _world.ui_external)) {
527 _impl->ui_type = _world.ui_external;
534 allocate_atom_event_buffers();
535 latency_compute_run();
538 LV2Plugin::~LV2Plugin ()
540 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
545 lilv_instance_free(_impl->instance);
546 lilv_node_free(_impl->name);
547 lilv_node_free(_impl->author);
550 free(_make_path_feature.data);
551 free(_work_schedule_feature.data);
557 if (_atom_ev_buffers) {
558 LV2_Evbuf** b = _atom_ev_buffers;
563 free(_atom_ev_buffers);
566 delete [] _control_data;
567 delete [] _shadow_data;
568 delete [] _ev_buffers;
572 LV2Plugin::is_external_ui() const
577 return lilv_ui_is_a(_impl->ui, _world.ui_external);
581 LV2Plugin::ui_is_resizable () const
583 const LilvNode* s = lilv_ui_get_uri(_impl->ui);
584 LilvNode* p = lilv_new_uri(_world.world, LV2_CORE__optionalFeature);
585 LilvNode* fs = lilv_new_uri(_world.world, LV2_UI__fixedSize);
586 LilvNode* nrs = lilv_new_uri(_world.world, LV2_UI__noUserResize);
588 LilvNodes* fs_matches = lilv_world_find_nodes(_world.world, s, p, fs);
589 LilvNodes* nrs_matches = lilv_world_find_nodes(_world.world, s, p, nrs);
591 lilv_nodes_free(nrs_matches);
592 lilv_nodes_free(fs_matches);
597 return !fs_matches && !nrs_matches;
601 LV2Plugin::unique_id() const
603 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
607 LV2Plugin::uri() const
609 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
613 LV2Plugin::label() const
615 return lilv_node_as_string(_impl->name);
619 LV2Plugin::name() const
621 return lilv_node_as_string(_impl->name);
625 LV2Plugin::maker() const
627 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
631 LV2Plugin::num_ports() const
633 return lilv_plugin_get_num_ports(_impl->plugin);
637 LV2Plugin::parameter_count() const
639 return lilv_plugin_get_num_ports(_impl->plugin);
643 LV2Plugin::default_value(uint32_t port)
645 return _defaults[port];
649 LV2Plugin::port_symbol(uint32_t index) const
651 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
653 error << name() << ": Invalid port index " << index << endmsg;
656 const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
657 return lilv_node_as_string(sym);
661 LV2Plugin::port_index (const char* symbol) const
663 const map<string, uint32_t>::const_iterator i = _port_indices.find(symbol);
664 if (i != _port_indices.end()) {
667 warning << string_compose(_("LV2: Unknown port %1"), symbol) << endmsg;
673 LV2Plugin::set_parameter(uint32_t which, float val)
675 DEBUG_TRACE(DEBUG::LV2, string_compose(
676 "%1 set parameter %2 to %3\n", name(), which, val));
678 if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
679 if (get_parameter (which) == val) {
683 _shadow_data[which] = val;
685 warning << string_compose(
686 _("Illegal parameter number used with plugin \"%1\". "
687 "This is a bug in either %2 or the LV2 plugin <%3>"),
688 name(), PROGRAM_NAME, unique_id()) << endmsg;
691 Plugin::set_parameter(which, val);
695 LV2Plugin::get_parameter(uint32_t which) const
697 if (parameter_is_input(which)) {
698 return (float)_shadow_data[which];
700 return (float)_control_data[which];
706 LV2Plugin::get_docs() const
708 LilvNodes* comments = lilv_plugin_get_value(_impl->plugin, _world.rdfs_comment);
710 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
711 lilv_nodes_free(comments);
719 LV2Plugin::get_parameter_docs(uint32_t which) const
721 LilvNodes* comments = lilv_port_get_value(
723 lilv_plugin_get_port_by_index(_impl->plugin, which),
724 _world.rdfs_comment);
727 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
728 lilv_nodes_free(comments);
736 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
739 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
740 if (parameter_is_control(x)) {
752 LV2Plugin::extension_data(const char* uri) const
754 return lilv_instance_get_extension_data(_impl->instance, uri);
758 LV2Plugin::c_plugin()
760 return _impl->plugin;
766 return (const void*)_impl->ui;
770 LV2Plugin::c_ui_type()
772 return (const void*)_impl->ui_type;
775 /** Directory for all plugin state. */
777 LV2Plugin::plugin_dir() const
779 return Glib::build_filename(_session.plugins_dir(), _insert_id.to_s());
782 /** Directory for files created by the plugin (except during save). */
784 LV2Plugin::scratch_dir() const
786 return Glib::build_filename(plugin_dir(), "scratch");
789 /** Directory for snapshots of files in the scratch directory. */
791 LV2Plugin::file_dir() const
793 return Glib::build_filename(plugin_dir(), "files");
796 /** Directory to save state snapshot version @c num into. */
798 LV2Plugin::state_dir(unsigned num) const
800 return Glib::build_filename(plugin_dir(), string_compose("state%1", num));
803 /** Implementation of state:makePath for files created at instantiation time.
804 * Note this is not used for files created at save time (Lilv deals with that).
807 LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
810 LV2Plugin* me = (LV2Plugin*)handle;
811 if (me->_insert_id == PBD::ID("0")) {
812 warning << string_compose(
813 "File path \"%1\" requested but LV2 %2 has no insert ID",
814 path, me->name()) << endmsg;
815 return g_strdup(path);
818 const std::string abs_path = Glib::build_filename(me->scratch_dir(), path);
819 const std::string dirname = Glib::path_get_dirname(abs_path);
820 g_mkdir_with_parents(dirname.c_str(), 0744);
822 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
825 return g_strndup(abs_path.c_str(), abs_path.length());
829 remove_directory(const std::string& path)
831 if (!Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) {
832 warning << string_compose("\"%1\" is not a directory", path) << endmsg;
836 Glib::RefPtr<Gio::File> dir = Gio::File::create_for_path(path);
837 Glib::RefPtr<Gio::FileEnumerator> e = dir->enumerate_children();
838 Glib::RefPtr<Gio::FileInfo> fi;
839 while ((fi = e->next_file())) {
840 if (fi->get_type() == Gio::FILE_TYPE_DIRECTORY) {
841 remove_directory(fi->get_name());
843 dir->get_child(fi->get_name())->remove();
850 LV2Plugin::add_state(XMLNode* root) const
852 assert(_insert_id != PBD::ID("0"));
856 LocaleGuard lg(X_("POSIX"));
858 for (uint32_t i = 0; i < parameter_count(); ++i) {
859 if (parameter_is_input(i) && parameter_is_control(i)) {
860 child = new XMLNode("Port");
861 child->add_property("symbol", port_symbol(i));
862 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
863 child->add_property("value", string(buf));
864 root->add_child_nocopy(*child);
868 if (_has_state_interface) {
869 // Provisionally increment state version and create directory
870 const std::string new_dir = state_dir(++_state_version);
871 g_mkdir_with_parents(new_dir.c_str(), 0744);
873 LilvState* state = lilv_state_new_from_instance(
877 scratch_dir().c_str(),
879 _session.externals_dir().c_str(),
882 const_cast<LV2Plugin*>(this),
886 if (!_impl->state || !lilv_state_equals(state, _impl->state)) {
887 lilv_state_save(_world.world,
889 _uri_map.urid_unmap(),
895 lilv_state_free(_impl->state);
896 _impl->state = state;
898 // State is identical, decrement version and nuke directory
899 lilv_state_free(state);
900 remove_directory(new_dir);
904 root->add_property("state-dir", string_compose("state%1", _state_version));
908 static inline const LilvNode*
909 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
911 LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
912 return vs ? lilv_nodes_get_first(vs) : NULL;
916 LV2Plugin::find_presets()
918 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
919 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
920 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
922 LilvNodes* presets = lilv_plugin_get_related(_impl->plugin, pset_Preset);
923 LILV_FOREACH(nodes, i, presets) {
924 const LilvNode* preset = lilv_nodes_get(presets, i);
925 lilv_world_load_resource(_world.world, preset);
926 const LilvNode* name = get_value(_world.world, preset, rdfs_label);
928 _presets.insert(std::make_pair(lilv_node_as_string(preset),
929 Plugin::PresetRecord(
930 lilv_node_as_string(preset),
931 lilv_node_as_string(name))));
933 warning << string_compose(
934 _("Plugin \"%1\% preset \"%2%\" is missing a label\n"),
935 lilv_node_as_string(lilv_plugin_get_uri(_impl->plugin)),
936 lilv_node_as_string(preset)) << endmsg;
939 lilv_nodes_free(presets);
941 lilv_node_free(rdfs_label);
942 lilv_node_free(pset_Preset);
943 lilv_node_free(lv2_appliesTo);
947 LV2Plugin::load_preset(PresetRecord r)
949 std::map<std::string,uint32_t>::iterator it;
951 LilvNode* lv2_port = lilv_new_uri(_world.world, LILV_NS_LV2 "port");
952 LilvNode* lv2_symbol = lilv_new_uri(_world.world, LILV_NS_LV2 "symbol");
953 LilvNode* preset = lilv_new_uri(_world.world, r.uri.c_str());
954 LilvNode* pset_value = lilv_new_uri(_world.world, LV2_PRESETS__value);
956 LilvNodes* ports = lilv_world_find_nodes(_world.world, preset, lv2_port, NULL);
957 LILV_FOREACH(nodes, i, ports) {
958 const LilvNode* port = lilv_nodes_get(ports, i);
959 const LilvNode* symbol = get_value(_world.world, port, lv2_symbol);
960 const LilvNode* value = get_value(_world.world, port, pset_value);
961 if (value && lilv_node_is_float(value)) {
962 it = _port_indices.find(lilv_node_as_string(symbol));
963 if (it != _port_indices.end()) {
964 set_parameter(it->second,lilv_node_as_float(value));
968 lilv_nodes_free(ports);
970 lilv_node_free(pset_value);
971 lilv_node_free(preset);
972 lilv_node_free(lv2_symbol);
973 lilv_node_free(lv2_port);
975 Plugin::load_preset(r);
981 ARDOUR::lv2plugin_get_port_value(const char* port_symbol,
986 LV2Plugin *plugin = (LV2Plugin *) user_data;
988 uint32_t index = plugin->port_index(port_symbol);
989 if (index != (uint32_t) -1) {
990 if (plugin->parameter_is_input(index) && plugin->parameter_is_control(index)) {
992 *size = sizeof(float);
993 *type = plugin->_uri_map.uri_to_id(LV2_ATOM__Float);
994 value = &plugin->_shadow_data[index];
1006 LV2Plugin::do_save_preset(string name)
1008 string pset_uri = uri();
1012 string save_dir = Glib::build_filename(
1013 Glib::get_home_dir(),
1014 Glib::build_filename(".lv2", "presets")
1017 LilvState* state = lilv_state_new_from_instance(
1020 _uri_map.urid_map(),
1021 scratch_dir().c_str(), // file_dir
1024 save_dir.c_str(), // save_dir
1025 lv2plugin_get_port_value, // get_value
1026 (void*) this, // user_data
1027 LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE, // flags
1028 _features // features
1031 lilv_state_set_label(state, name.c_str());
1033 _world.world, // world
1034 _uri_map.urid_map(), // map
1035 _uri_map.urid_unmap(), // unmap
1037 pset_uri.c_str(), // uri
1038 save_dir.c_str(), // dir
1039 (name + ".ttl").c_str() // filename
1042 lilv_state_free(state);
1047 LV2Plugin::do_remove_preset(string name)
1049 string preset_file = Glib::build_filename(
1050 Glib::get_home_dir(),
1051 Glib::build_filename(
1052 Glib::build_filename(".lv2", "presets"),
1056 unlink(preset_file.c_str());
1060 LV2Plugin::has_editor() const
1062 return _impl->ui != NULL;
1066 LV2Plugin::has_message_output() const
1068 for (uint32_t i = 0; i < num_ports(); ++i) {
1069 if ((_port_flags[i] & (PORT_MESSAGE|PORT_ATOM)) && _port_flags[i] & PORT_OUTPUT) {
1077 LV2Plugin::write_to(RingBuffer<uint8_t>* dest,
1081 const uint8_t* body)
1083 const uint32_t buf_size = sizeof(UIMessage) + size;
1084 uint8_t buf[buf_size];
1086 UIMessage* msg = (UIMessage*)buf;
1088 msg->protocol = protocol;
1090 memcpy(msg + 1, body, size);
1092 return (dest->write(buf, buf_size) == buf_size);
1096 LV2Plugin::write_from_ui(uint32_t index,
1099 const uint8_t* body)
1102 _from_ui = new RingBuffer<uint8_t>(
1103 _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS);
1106 if (!write_to(_from_ui, index, protocol, size, body)) {
1107 error << "Error writing from UI to plugin" << endmsg;
1114 LV2Plugin::write_to_ui(uint32_t index,
1117 const uint8_t* body)
1119 if (!write_to(_to_ui, index, protocol, size, body)) {
1120 error << "Error writing from plugin to UI" << endmsg;
1127 LV2Plugin::enable_ui_emmission()
1130 _to_ui = new RingBuffer<uint8_t>(
1131 _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS);
1136 LV2Plugin::emit_to_ui(void* controller, UIMessageSink sink)
1142 uint32_t read_space = _to_ui->read_space();
1143 while (read_space > sizeof(UIMessage)) {
1145 if (_to_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
1146 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1149 uint8_t body[msg.size];
1150 if (_to_ui->read(body, msg.size) != msg.size) {
1151 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1155 sink(controller, msg.index, msg.size, msg.protocol, body);
1157 read_space -= sizeof(msg) + msg.size;
1162 LV2Plugin::work(uint32_t size, const void* data)
1164 return _impl->work_iface->work(
1165 _impl->instance->lv2_handle, work_respond, this, size, data);
1169 LV2Plugin::work_response(uint32_t size, const void* data)
1171 return _impl->work_iface->work_response(
1172 _impl->instance->lv2_handle, size, data);
1176 LV2Plugin::set_insert_info(const PluginInsert* insert)
1178 _insert_id = insert->id();
1182 LV2Plugin::set_state(const XMLNode& node, int version)
1185 const XMLProperty* prop;
1186 XMLNodeConstIterator iter;
1191 LocaleGuard lg(X_("POSIX"));
1193 if (node.name() != state_node_name()) {
1194 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
1198 if (version < 3000) {
1199 nodes = node.children("port");
1201 nodes = node.children("Port");
1204 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
1208 if ((prop = child->property("symbol")) != 0) {
1209 sym = prop->value().c_str();
1211 warning << _("LV2: port has no symbol, ignored") << endmsg;
1215 map<string, uint32_t>::iterator i = _port_indices.find(sym);
1217 if (i != _port_indices.end()) {
1218 port_id = i->second;
1220 warning << _("LV2: port has unknown index, ignored") << endmsg;
1224 if ((prop = child->property("value")) != 0) {
1225 value = prop->value().c_str();
1227 warning << _("LV2: port has no value, ignored") << endmsg;
1231 set_parameter(port_id, atof(value));
1235 if ((prop = node.property("state-dir")) != 0) {
1236 if (sscanf(prop->value().c_str(), "state%u", &_state_version) != 1) {
1237 error << string_compose(
1238 "LV2: failed to parse state version from \"%1\"",
1239 prop->value()) << endmsg;
1242 std::string state_file = Glib::build_filename(
1244 Glib::build_filename(prop->value(), "state.ttl"));
1246 LilvState* state = lilv_state_new_from_file(
1247 _world.world, _uri_map.urid_map(), NULL, state_file.c_str());
1249 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
1252 latency_compute_run();
1254 return Plugin::set_state(node, version);
1258 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
1260 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
1262 LilvNode *def, *min, *max;
1263 lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
1265 desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.lv2_integer);
1266 desc.toggled = lilv_port_has_property(_impl->plugin, port, _world.lv2_toggled);
1267 desc.logarithmic = lilv_port_has_property(_impl->plugin, port, _world.ext_logarithmic);
1268 desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.lv2_sampleRate);
1269 desc.label = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
1270 desc.lower = min ? lilv_node_as_float(min) : 0.0f;
1271 desc.upper = max ? lilv_node_as_float(max) : 1.0f;
1272 if (desc.sr_dependent) {
1273 desc.lower *= _session.frame_rate ();
1274 desc.upper *= _session.frame_rate ();
1277 desc.min_unbound = false; // TODO: LV2 extension required
1278 desc.max_unbound = false; // TODO: LV2 extension required
1280 if (desc.integer_step) {
1282 desc.smallstep = 0.1;
1283 desc.largestep = 10.0;
1285 const float delta = desc.upper - desc.lower;
1286 desc.step = delta / 1000.0f;
1287 desc.smallstep = delta / 10000.0f;
1288 desc.largestep = delta / 10.0f;
1291 desc.enumeration = lilv_port_has_property(_impl->plugin, port, _world.lv2_enumeration);
1293 lilv_node_free(def);
1294 lilv_node_free(min);
1295 lilv_node_free(max);
1301 LV2Plugin::describe_parameter(Evoral::Parameter which)
1303 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
1304 LilvNode* name = lilv_port_get_name(_impl->plugin,
1305 lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
1306 string ret(lilv_node_as_string(name));
1307 lilv_node_free(name);
1315 LV2Plugin::signal_latency() const
1317 if (_latency_control_port) {
1318 return (framecnt_t)floor(*_latency_control_port);
1324 set<Evoral::Parameter>
1325 LV2Plugin::automatable() const
1327 set<Evoral::Parameter> ret;
1329 for (uint32_t i = 0; i < parameter_count(); ++i) {
1330 if (parameter_is_input(i) && parameter_is_control(i)) {
1331 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
1339 LV2Plugin::activate()
1341 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
1343 if (!_was_activated) {
1344 lilv_instance_activate(_impl->instance);
1345 _was_activated = true;
1350 LV2Plugin::deactivate()
1352 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
1354 if (_was_activated) {
1355 lilv_instance_deactivate(_impl->instance);
1356 _was_activated = false;
1361 LV2Plugin::cleanup()
1363 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
1367 lilv_instance_free(_impl->instance);
1368 _impl->instance = NULL;
1372 LV2Plugin::allocate_atom_event_buffers()
1374 /* reserve local scratch buffers for ATOM event-queues */
1375 const LilvPlugin* p = _impl->plugin;
1377 /* count non-MIDI atom event-ports
1378 * TODO: nicely ask drobilla to make a lilv_ call for that
1380 int count_atom_out = 0;
1381 int count_atom_in = 0;
1382 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
1383 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
1384 if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
1385 LilvNodes* buffer_types = lilv_port_get_value(
1386 p, port, _world.atom_bufferType);
1387 LilvNodes* atom_supports = lilv_port_get_value(
1388 p, port, _world.atom_supports);
1390 if (!lilv_nodes_contains(buffer_types, _world.atom_Sequence)
1391 || !lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
1392 if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
1395 if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
1399 lilv_nodes_free(buffer_types);
1400 lilv_nodes_free(atom_supports);
1404 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 need buffers for %2 atom-in and %3 atom-out event-ports\n",
1405 name(), count_atom_in, count_atom_out));
1407 const int total_atom_buffers = (count_atom_in + count_atom_out);
1408 if (_atom_ev_buffers || total_atom_buffers == 0) {
1412 DEBUG_TRACE(DEBUG::LV2, string_compose("allocate %1 atom_ev_buffers\n", total_atom_buffers));
1413 _atom_ev_buffers = (LV2_Evbuf**) malloc((total_atom_buffers + 1) * sizeof(LV2_Evbuf*));
1414 for (int i = 0; i < total_atom_buffers; ++i ) {
1415 _atom_ev_buffers[i] = lv2_evbuf_new(32768, LV2_EVBUF_ATOM,
1416 LV2Plugin::urids.atom_Chunk, LV2Plugin::urids.atom_Sequence);
1418 _atom_ev_buffers[total_atom_buffers] = 0;
1422 /** Write an ardour position/time/tempo/meter as an LV2 event.
1423 * @return true on success.
1426 write_position(LV2_Atom_Forge* forge,
1428 const TempoMetric& t,
1429 Timecode::BBT_Time& bbt,
1431 framepos_t position,
1434 uint8_t pos_buf[256];
1435 lv2_atom_forge_set_buffer(forge, pos_buf, sizeof(pos_buf));
1436 LV2_Atom_Forge_Frame frame;
1437 lv2_atom_forge_blank(forge, &frame, 1, LV2Plugin::urids.time_Position);
1438 lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_frame, 0);
1439 lv2_atom_forge_long(forge, position);
1440 lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_speed, 0);
1441 lv2_atom_forge_float(forge, speed);
1442 lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_barBeat, 0);
1443 lv2_atom_forge_float(forge, bbt.beats - 1 +
1444 (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
1445 lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_bar, 0);
1446 lv2_atom_forge_float(forge, bbt.bars - 1);
1447 lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_beatUnit, 0);
1448 lv2_atom_forge_float(forge, t.meter().note_divisor());
1449 lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_beatsPerBar, 0);
1450 lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
1451 lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_beatsPerMinute, 0);
1452 lv2_atom_forge_float(forge, t.tempo().beats_per_minute());
1454 LV2_Evbuf_Iterator end = lv2_evbuf_end(buf);
1455 const LV2_Atom* const atom = (const LV2_Atom*)pos_buf;
1456 return lv2_evbuf_write(&end, offset, 0, atom->type, atom->size,
1457 (const uint8_t*)(atom + 1));
1461 LV2Plugin::connect_and_run(BufferSet& bufs,
1462 ChanMapping in_map, ChanMapping out_map,
1463 pframes_t nframes, framecnt_t offset)
1465 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
1466 Plugin::connect_and_run(bufs, in_map, out_map, nframes, offset);
1468 cycles_t then = get_cycles();
1470 TempoMap& tmap = _session.tempo_map();
1471 Metrics::const_iterator metric_i = tmap.metrics_end();
1472 TempoMetric tmetric = tmap.metric_at(_session.transport_frame(), &metric_i);
1474 if (_freewheel_control_port) {
1475 *_freewheel_control_port = _session.engine().freewheeling();
1478 if (_bpm_control_port) {
1479 *_bpm_control_port = tmetric.tempo().beats_per_minute();
1482 ChanCount bufs_count;
1483 bufs_count.set(DataType::AUDIO, 1);
1484 bufs_count.set(DataType::MIDI, 1);
1485 BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
1486 BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
1487 uint32_t const num_ports = parameter_count();
1489 uint32_t audio_in_index = 0;
1490 uint32_t audio_out_index = 0;
1491 uint32_t midi_in_index = 0;
1492 uint32_t midi_out_index = 0;
1493 uint32_t atom_port_index = 0;
1495 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
1498 PortFlags flags = _port_flags[port_index];
1499 if (flags & PORT_AUDIO) {
1500 if (flags & PORT_INPUT) {
1501 index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
1503 ? bufs.get_audio(index).data(offset)
1504 : silent_bufs.get_audio(0).data(offset);
1506 index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
1508 ? bufs.get_audio(index).data(offset)
1509 : scratch_bufs.get_audio(0).data(offset);
1511 } else if (flags & (PORT_EVENT|PORT_MESSAGE)) {
1512 /* FIXME: The checks here for bufs.count().n_midi() > index shouldn't
1513 be necessary, but the mapping is illegal in some cases. Ideally
1514 that should be fixed, but this is easier...
1516 if (flags & PORT_INPUT) {
1517 index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
1518 _ev_buffers[port_index] = (valid && bufs.count().n_midi() > index)
1519 ? bufs.get_lv2_midi(true, index, flags & PORT_EVENT)
1520 : silent_bufs.get_lv2_midi(true, 0, flags & PORT_EVENT);
1521 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
1523 index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
1524 _ev_buffers[port_index] = (valid && bufs.count().n_midi() > index)
1525 ? bufs.get_lv2_midi(false, index, flags & PORT_EVENT)
1526 : scratch_bufs.get_lv2_midi(false, 0, flags & PORT_EVENT);
1527 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
1529 } else if (flags & (PORT_ATOM)) {
1530 assert(_atom_ev_buffers && _atom_ev_buffers[atom_port_index]);
1531 if (flags & PORT_INPUT) {
1532 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], true);
1533 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
1535 if (port_index == _position_seq_port_idx) {
1536 Timecode::BBT_Time bbt;
1537 if (_session.transport_frame() != _next_cycle_start ||
1538 _session.transport_speed() != _next_cycle_speed) {
1539 // Something has changed, write the position at cycle start
1540 tmap.bbt_time(_session.transport_frame(), bbt);
1541 write_position(&_impl->forge, _ev_buffers[port_index],
1542 tmetric, bbt, _session.transport_speed(),
1543 _session.transport_frame(), 0);
1546 // Write a position event for every metric change within this cycle
1547 while (++metric_i != tmap.metrics_end() &&
1548 (*metric_i)->frame() < _session.transport_frame() + nframes) {
1549 MetricSection* section = *metric_i;
1550 tmetric.set_metric(section);
1551 bbt = section->start();
1552 write_position(&_impl->forge, _ev_buffers[port_index],
1553 tmetric, bbt, _session.transport_speed(),
1555 section->frame() - _session.transport_frame());
1559 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], false);
1560 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
1562 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
1565 continue; // Control port, leave buffer alone
1567 lilv_instance_connect_port(_impl->instance, port_index, buf);
1570 // Read messages from UI and push into appropriate buffers
1572 uint32_t read_space = _from_ui->read_space();
1573 while (read_space > sizeof(UIMessage)) {
1575 if (_from_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
1576 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
1579 uint8_t body[msg.size];
1580 if (_from_ui->read(body, msg.size) != msg.size) {
1581 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
1584 if (msg.protocol == urids.atom_eventTransfer) {
1585 LV2_Evbuf* buf = _ev_buffers[msg.index];
1586 LV2_Evbuf_Iterator i = lv2_evbuf_end(buf);
1587 const LV2_Atom* const atom = (const LV2_Atom*)body;
1588 if (!lv2_evbuf_write(&i, nframes, 0, atom->type, atom->size,
1589 (const uint8_t*)(atom + 1))) {
1590 error << "Failed to write data to LV2 event buffer\n";
1593 error << "Received unknown message type from UI" << endmsg;
1595 read_space -= sizeof(UIMessage) + msg.size;
1602 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
1603 PortFlags flags = _port_flags[port_index];
1605 // Flush MIDI (write back to Ardour MIDI buffers)
1606 if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_MESSAGE))) {
1607 const uint32_t buf_index = out_map.get(
1608 DataType::MIDI, midi_out_index++, &valid);
1610 bufs.flush_lv2_midi(true, buf_index);
1614 // Write messages to UI
1615 if (_to_ui && (flags & PORT_OUTPUT) && (flags & (PORT_MESSAGE|PORT_ATOM))) {
1616 LV2_Evbuf* buf = _ev_buffers[port_index];
1617 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
1618 lv2_evbuf_is_valid(i);
1619 i = lv2_evbuf_next(i)) {
1620 uint32_t frames, subframes, type, size;
1622 lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
1623 write_to_ui(port_index, urids.atom_eventTransfer,
1624 size + sizeof(LV2_Atom),
1625 data - sizeof(LV2_Atom));
1630 cycles_t now = get_cycles();
1631 set_cycles((uint32_t)(now - then));
1633 // Update expected transport information for next cycle so we can detect changes
1634 _next_cycle_speed = _session.transport_speed();
1635 _next_cycle_start = _session.transport_frame() + (nframes * _next_cycle_speed);
1641 LV2Plugin::parameter_is_control(uint32_t param) const
1643 assert(param < _port_flags.size());
1644 return _port_flags[param] & PORT_CONTROL;
1648 LV2Plugin::parameter_is_audio(uint32_t param) const
1650 assert(param < _port_flags.size());
1651 return _port_flags[param] & PORT_AUDIO;
1655 LV2Plugin::parameter_is_event(uint32_t param) const
1657 assert(param < _port_flags.size());
1658 return _port_flags[param] & PORT_EVENT;
1662 LV2Plugin::parameter_is_output(uint32_t param) const
1664 assert(param < _port_flags.size());
1665 return _port_flags[param] & PORT_OUTPUT;
1669 LV2Plugin::parameter_is_input(uint32_t param) const
1671 assert(param < _port_flags.size());
1672 return _port_flags[param] & PORT_INPUT;
1676 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
1679 if (param < parameter_count()) {
1680 snprintf(buf, len, "%.3f", get_parameter(param));
1687 boost::shared_ptr<Plugin::ScalePoints>
1688 LV2Plugin::get_scale_points(uint32_t port_index) const
1690 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
1691 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
1693 boost::shared_ptr<Plugin::ScalePoints> ret;
1698 ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
1700 LILV_FOREACH(scale_points, i, points) {
1701 const LilvScalePoint* p = lilv_scale_points_get(points, i);
1702 const LilvNode* label = lilv_scale_point_get_label(p);
1703 const LilvNode* value = lilv_scale_point_get_value(p);
1704 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
1705 ret->insert(make_pair(lilv_node_as_string(label),
1706 lilv_node_as_float(value)));
1710 lilv_scale_points_free(points);
1715 LV2Plugin::run(pframes_t nframes)
1717 uint32_t const N = parameter_count();
1718 for (uint32_t i = 0; i < N; ++i) {
1719 if (parameter_is_control(i) && parameter_is_input(i)) {
1720 _control_data[i] = _shadow_data[i];
1724 lilv_instance_run(_impl->instance, nframes);
1726 if (_impl->work_iface) {
1727 _worker->emit_responses();
1728 if (_impl->work_iface->end_run) {
1729 _impl->work_iface->end_run(_impl->instance->lv2_handle);
1735 LV2Plugin::latency_compute_run()
1737 if (!_latency_control_port) {
1741 // Run the plugin so that it can set its latency parameter
1745 uint32_t port_index = 0;
1746 uint32_t in_index = 0;
1747 uint32_t out_index = 0;
1749 const framecnt_t bufsize = 1024;
1750 float buffer[bufsize];
1752 memset(buffer, 0, sizeof(float) * bufsize);
1754 // FIXME: Ensure plugins can handle in-place processing
1758 while (port_index < parameter_count()) {
1759 if (parameter_is_audio(port_index)) {
1760 if (parameter_is_input(port_index)) {
1761 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1763 } else if (parameter_is_output(port_index)) {
1764 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1776 LV2Plugin::Impl::designated_input (const char* uri, void** bufptrs[], void** bufptr)
1778 LilvPort* port = NULL;
1779 LilvNode* designation = lilv_new_uri(_world.world, uri);
1780 port = lilv_plugin_get_port_by_designation(
1781 plugin, _world.lv2_InputPort, designation);
1782 lilv_node_free(designation);
1784 bufptrs[lilv_port_get_index(plugin, port)] = bufptr;
1789 LV2World::LV2World()
1790 : world(lilv_world_new())
1792 lilv_world_load_all(world);
1793 atom_AtomPort = lilv_new_uri(world, LV2_ATOM__AtomPort);
1794 atom_Chunk = lilv_new_uri(world, LV2_ATOM__Chunk);
1795 atom_Sequence = lilv_new_uri(world, LV2_ATOM__Sequence);
1796 atom_bufferType = lilv_new_uri(world, LV2_ATOM__bufferType);
1797 atom_supports = lilv_new_uri(world, LV2_ATOM__supports);
1798 atom_eventTransfer = lilv_new_uri(world, LV2_ATOM__eventTransfer);
1799 ev_EventPort = lilv_new_uri(world, LILV_URI_EVENT_PORT);
1800 ext_logarithmic = lilv_new_uri(world, LV2_PORT_PROPS__logarithmic);
1801 lv2_AudioPort = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
1802 lv2_ControlPort = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
1803 lv2_InputPort = lilv_new_uri(world, LILV_URI_INPUT_PORT);
1804 lv2_OutputPort = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
1805 lv2_inPlaceBroken = lilv_new_uri(world, LV2_CORE__inPlaceBroken);
1806 lv2_integer = lilv_new_uri(world, LV2_CORE__integer);
1807 lv2_sampleRate = lilv_new_uri(world, LV2_CORE__sampleRate);
1808 lv2_toggled = lilv_new_uri(world, LV2_CORE__toggled);
1809 lv2_enumeration = lilv_new_uri(world, LV2_CORE__enumeration);
1810 midi_MidiEvent = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
1811 rdfs_comment = lilv_new_uri(world, LILV_NS_RDFS "comment");
1812 time_Position = lilv_new_uri(world, LV2_TIME__Position);
1813 ui_GtkUI = lilv_new_uri(world, LV2_UI__GtkUI);
1814 ui_external = lilv_new_uri(world, "http://lv2plug.in/ns/extensions/ui#external");
1817 LV2World::~LV2World()
1819 lilv_node_free(ui_external);
1820 lilv_node_free(ui_GtkUI);
1821 lilv_node_free(midi_MidiEvent);
1822 lilv_node_free(lv2_toggled);
1823 lilv_node_free(lv2_sampleRate);
1824 lilv_node_free(lv2_integer);
1825 lilv_node_free(lv2_inPlaceBroken);
1826 lilv_node_free(lv2_OutputPort);
1827 lilv_node_free(lv2_InputPort);
1828 lilv_node_free(lv2_ControlPort);
1829 lilv_node_free(lv2_AudioPort);
1830 lilv_node_free(ext_logarithmic);
1831 lilv_node_free(ev_EventPort);
1832 lilv_node_free(atom_eventTransfer);
1833 lilv_node_free(atom_bufferType);
1834 lilv_node_free(atom_Sequence);
1835 lilv_node_free(atom_Chunk);
1836 lilv_node_free(atom_AtomPort);
1839 LV2PluginInfo::LV2PluginInfo (const void* c_plugin)
1840 : _c_plugin(c_plugin)
1845 LV2PluginInfo::~LV2PluginInfo()
1849 LV2PluginInfo::load(Session& session)
1854 plugin.reset(new LV2Plugin(session.engine(), session,
1855 (const LilvPlugin*)_c_plugin,
1856 session.frame_rate()));
1858 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
1860 } catch (failed_constructor& err) {
1861 return PluginPtr((Plugin*)0);
1868 LV2PluginInfo::discover()
1870 PluginInfoList* plugs = new PluginInfoList;
1871 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
1873 info << "LV2: Discovering " << lilv_plugins_size(plugins) << " plugins" << endmsg;
1875 LILV_FOREACH(plugins, i, plugins) {
1876 const LilvPlugin* p = lilv_plugins_get(plugins, i);
1877 LV2PluginInfoPtr info(new LV2PluginInfo((const void*)p));
1879 LilvNode* name = lilv_plugin_get_name(p);
1880 if (!name || !lilv_plugin_get_port_by_index(p, 0)) {
1881 warning << "Ignoring invalid LV2 plugin "
1882 << lilv_node_as_string(lilv_plugin_get_uri(p))
1889 info->name = string(lilv_node_as_string(name));
1890 lilv_node_free(name);
1892 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
1893 const LilvNode* label = lilv_plugin_class_get_label(pclass);
1894 info->category = lilv_node_as_string(label);
1896 LilvNode* author_name = lilv_plugin_get_author_name(p);
1897 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
1898 lilv_node_free(author_name);
1900 info->path = "/NOPATH"; // Meaningless for LV2
1902 /* count atom-event-ports that feature
1903 * atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent>
1905 * TODO: nicely ask drobilla to make a lilv_ call for that
1907 int count_midi_out = 0;
1908 int count_midi_in = 0;
1909 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
1910 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
1911 if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
1912 LilvNodes* buffer_types = lilv_port_get_value(
1913 p, port, _world.atom_bufferType);
1914 LilvNodes* atom_supports = lilv_port_get_value(
1915 p, port, _world.atom_supports);
1917 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)
1918 && lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
1919 if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
1922 if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
1926 lilv_nodes_free(buffer_types);
1927 lilv_nodes_free(atom_supports);
1931 info->n_inputs.set_audio(
1932 lilv_plugin_get_num_ports_of_class(
1933 p, _world.lv2_InputPort, _world.lv2_AudioPort, NULL));
1934 info->n_inputs.set_midi(
1935 lilv_plugin_get_num_ports_of_class(
1936 p, _world.lv2_InputPort, _world.ev_EventPort, NULL)
1939 info->n_outputs.set_audio(
1940 lilv_plugin_get_num_ports_of_class(
1941 p, _world.lv2_OutputPort, _world.lv2_AudioPort, NULL));
1942 info->n_outputs.set_midi(
1943 lilv_plugin_get_num_ports_of_class(
1944 p, _world.lv2_OutputPort, _world.ev_EventPort, NULL)
1947 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
1948 info->index = 0; // Meaningless for LV2
1950 plugs->push_back(info);