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.
27 #include <giomm/file.h>
28 #include <glib/gprintf.h>
31 #include <boost/utility.hpp>
33 #include "pbd/compose.h"
34 #include "pbd/error.h"
35 #include "pbd/xml++.h"
37 #include "libardour-config.h"
39 #include "ardour/types.h"
40 #include "ardour/audio_buffer.h"
41 #include "ardour/audioengine.h"
42 #include "ardour/debug.h"
43 #include "ardour/lv2_plugin.h"
44 #include "ardour/session.h"
45 #include "ardour/tempo.h"
46 #include "ardour/worker.h"
51 #include <lilv/lilv.h>
53 #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
54 #include "lv2/lv2plug.in/ns/ext/log/log.h"
55 #include "lv2/lv2plug.in/ns/ext/port-props/port-props.h"
56 #include "lv2/lv2plug.in/ns/ext/presets/presets.h"
57 #include "lv2/lv2plug.in/ns/ext/state/state.h"
58 #include "lv2/lv2plug.in/ns/ext/time/time.h"
59 #include "lv2/lv2plug.in/ns/ext/worker/worker.h"
60 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
62 #include "lv2/lv2plug.in/ns/ext/buf-size/buf-size.h"
63 #include "lv2/lv2plug.in/ns/ext/options/options.h"
66 #include "lv2_evbuf.h"
69 #include <suil/suil.h>
73 using namespace ARDOUR;
76 URIMap LV2Plugin::_uri_map;
77 uint32_t LV2Plugin::_midi_event_type = _uri_map.uri_to_id(
78 "http://lv2plug.in/ns/ext/midi#MidiEvent");
79 uint32_t LV2Plugin::_chunk_type = _uri_map.uri_to_id(
81 uint32_t LV2Plugin::_sequence_type = _uri_map.uri_to_id(
83 uint32_t LV2Plugin::_event_transfer_type = _uri_map.uri_to_id(
84 LV2_ATOM__eventTransfer);
85 uint32_t LV2Plugin::_path_type = _uri_map.uri_to_id(
87 uint32_t LV2Plugin::_log_Error = _uri_map.uri_to_id(
89 uint32_t LV2Plugin::_log_Warning = _uri_map.uri_to_id(
91 uint32_t LV2Plugin::_log_Note = _uri_map.uri_to_id(
94 class LV2World : boost::noncopyable {
101 LilvNode* atom_AtomPort;
102 LilvNode* atom_Chunk;
103 LilvNode* atom_Sequence;
104 LilvNode* atom_bufferType;
105 LilvNode* atom_supports;
106 LilvNode* atom_eventTransfer;
107 LilvNode* ev_EventPort;
108 LilvNode* ext_logarithmic;
109 LilvNode* lv2_AudioPort;
110 LilvNode* lv2_ControlPort;
111 LilvNode* lv2_InputPort;
112 LilvNode* lv2_OutputPort;
113 LilvNode* lv2_enumeration;
114 LilvNode* lv2_inPlaceBroken;
115 LilvNode* lv2_integer;
116 LilvNode* lv2_sampleRate;
117 LilvNode* lv2_toggled;
118 LilvNode* midi_MidiEvent;
119 LilvNode* rdfs_comment;
121 LilvNode* ui_external;
124 static LV2World _world;
126 /* worker extension */
128 /** Called by the plugin to schedule non-RT work. */
129 static LV2_Worker_Status
130 work_schedule(LV2_Worker_Schedule_Handle handle,
134 LV2Plugin* plugin = (LV2Plugin*)handle;
135 if (plugin->session().engine().freewheeling()) {
136 // Freewheeling, do the work immediately in this (audio) thread
137 return (LV2_Worker_Status)plugin->work(size, data);
139 // Enqueue message for the worker thread
140 return plugin->worker()->schedule(size, data) ?
141 LV2_WORKER_SUCCESS : LV2_WORKER_ERR_UNKNOWN;
145 /** Called by the plugin to respond to non-RT work. */
146 static LV2_Worker_Status
147 work_respond(LV2_Worker_Respond_Handle handle,
151 LV2Plugin* plugin = (LV2Plugin*)handle;
152 if (plugin->session().engine().freewheeling()) {
153 // Freewheeling, respond immediately in this (audio) thread
154 return (LV2_Worker_Status)plugin->work_response(size, data);
156 // Enqueue response for the worker
157 return plugin->worker()->respond(size, data) ?
158 LV2_WORKER_SUCCESS : LV2_WORKER_ERR_UNKNOWN;
165 log_vprintf(LV2_Log_Handle handle,
171 const int ret = g_vasprintf(&str, fmt, args);
172 if (type == LV2Plugin::_log_Error) {
173 error << str << endmsg;
174 } else if (type == LV2Plugin::_log_Warning) {
175 warning << str << endmsg;
176 } else if (type == LV2Plugin::_log_Note) {
177 info << str << endmsg;
179 // TODO: Togglable log:Trace message support
184 log_printf(LV2_Log_Handle handle,
186 const char* fmt, ...)
190 const int ret = log_vprintf(handle, type, fmt, args);
195 struct LV2Plugin::Impl {
196 Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0)
201 /** Find the LV2 input port with the given designation.
202 * If found, bufptrs[port_index] will be set to bufptr.
204 LilvPort* designated_input (const char* uri, void** bufptrs[], void** bufptr);
206 const LilvPlugin* plugin;
208 const LilvNode* ui_type;
211 LilvInstance* instance;
212 const LV2_Worker_Interface* work_iface;
216 LV2Plugin::LV2Plugin (AudioEngine& engine,
218 const void* c_plugin,
220 : Plugin (engine, session)
227 init(c_plugin, rate);
230 LV2Plugin::LV2Plugin (const LV2Plugin& other)
236 , _insert_id(other._insert_id)
238 init(other._impl->plugin, other._sample_rate);
240 for (uint32_t i = 0; i < parameter_count(); ++i) {
241 _control_data[i] = other._shadow_data[i];
242 _shadow_data[i] = other._shadow_data[i];
247 LV2Plugin::init(const void* c_plugin, framecnt_t rate)
249 DEBUG_TRACE(DEBUG::LV2, "init\n");
251 _impl->plugin = (const LilvPlugin*)c_plugin;
253 _impl->ui_type = NULL;
258 _atom_ev_buffers = 0;
260 _bpm_control_port = 0;
261 _freewheel_control_port = 0;
262 _latency_control_port = 0;
263 _block_length = _engine.frames_per_cycle();
264 _seq_size = _engine.raw_buffer_size(DataType::MIDI);
266 _was_activated = false;
267 _has_state_interface = false;
269 _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
270 _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
271 _make_path_feature.URI = LV2_STATE__makePath;
272 _log_feature.URI = LV2_LOG__log;
273 _work_schedule_feature.URI = LV2_WORKER__schedule;
274 _work_schedule_feature.data = NULL;
276 const LilvPlugin* plugin = _impl->plugin;
278 LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE__interface);
279 LilvNode* state_uri = lilv_new_uri(_world.world, LV2_STATE_URI);
280 _has_state_interface =
281 // What plugins should have (lv2:extensionData state:Interface)
282 lilv_plugin_has_extension_data(plugin, state_iface_uri)
283 // What some outdated/incorrect ones have
284 || lilv_plugin_has_feature(plugin, state_uri);
285 lilv_node_free(state_uri);
286 lilv_node_free(state_iface_uri);
288 _features = (LV2_Feature**)calloc(10, sizeof(LV2_Feature*));
289 _features[0] = &_instance_access_feature;
290 _features[1] = &_data_access_feature;
291 _features[2] = &_make_path_feature;
292 _features[3] = _uri_map.uri_map_feature();
293 _features[4] = _uri_map.urid_map_feature();
294 _features[5] = _uri_map.urid_unmap_feature();
295 _features[6] = &_log_feature;
297 unsigned n_features = 7;
299 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
300 LV2_Options_Option options[] = {
301 { _uri_map.uri_to_id(LV2_BUF_SIZE__minBlockLength),
302 sizeof(int32_t), atom_Int, &_block_length },
303 { _uri_map.uri_to_id(LV2_BUF_SIZE__maxBlockLength),
304 sizeof(int32_t), atom_Int, &_block_length },
305 { _uri_map.uri_to_id(LV2_BUF_SIZE__sequenceSize),
306 sizeof(int32_t), atom_Int, &_seq_size },
310 _options_feature.URI = LV2_OPTIONS__options;
311 _options_feature.data = options;
312 _features[n_features++] = &_options_feature;
315 LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
316 sizeof(LV2_State_Make_Path));
317 make_path->handle = this;
318 make_path->path = &lv2_state_make_path;
319 _make_path_feature.data = make_path;
321 LV2_Log_Log* log = (LV2_Log_Log*)malloc(sizeof(LV2_Log_Log));
323 log->printf = &log_printf;
324 log->vprintf = &log_vprintf;
325 _log_feature.data = log;
327 LilvNode* worker_schedule = lilv_new_uri(_world.world, LV2_WORKER__schedule);
328 if (lilv_plugin_has_feature(plugin, worker_schedule)) {
329 LV2_Worker_Schedule* schedule = (LV2_Worker_Schedule*)malloc(
330 sizeof(LV2_Worker_Schedule));
331 _worker = new Worker(this, 4096);
332 schedule->handle = this;
333 schedule->schedule_work = work_schedule;
334 _work_schedule_feature.data = schedule;
335 _features[n_features++] = &_work_schedule_feature;
337 lilv_node_free(worker_schedule);
339 _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
340 _impl->name = lilv_plugin_get_name(plugin);
341 _impl->author = lilv_plugin_get_author_name(plugin);
343 if (_impl->instance == 0) {
344 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
345 throw failed_constructor();
348 _instance_access_feature.data = (void*)_impl->instance->lv2_handle;
349 _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
350 _data_access_feature.data = &_data_access_extension_data;
352 _impl->work_iface = (const LV2_Worker_Interface*)extension_data(
353 LV2_WORKER__interface);
355 if (lilv_plugin_has_feature(plugin, _world.lv2_inPlaceBroken)) {
356 error << string_compose(
357 _("LV2: \"%1\" cannot be used, since it cannot do inplace processing"),
358 lilv_node_as_string(_impl->name)) << endmsg;
359 lilv_node_free(_impl->name);
360 lilv_node_free(_impl->author);
361 throw failed_constructor();
366 const uint32_t num_ports = this->num_ports();
367 for (uint32_t i = 0; i < num_ports; ++i) {
368 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, i);
371 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_OutputPort)) {
372 flags |= PORT_OUTPUT;
373 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_InputPort)) {
376 error << string_compose(
377 "LV2: \"%1\" port %2 is neither input nor output",
378 lilv_node_as_string(_impl->name), i) << endmsg;
379 throw failed_constructor();
382 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_ControlPort)) {
383 flags |= PORT_CONTROL;
384 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_AudioPort)) {
386 } else if (lilv_port_is_a(_impl->plugin, port, _world.ev_EventPort)) {
388 } else if (lilv_port_is_a(_impl->plugin, port, _world.atom_AtomPort)) {
389 LilvNodes* buffer_types = lilv_port_get_value(
390 _impl->plugin, port, _world.atom_bufferType);
391 LilvNodes* atom_supports = lilv_port_get_value(
392 _impl->plugin, port, _world.atom_supports);
394 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)
395 && lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)
397 flags |= PORT_MESSAGE;
401 lilv_nodes_free(buffer_types);
402 lilv_nodes_free(atom_supports);
404 error << string_compose(
405 "LV2: \"%1\" port %2 has no known data type",
406 lilv_node_as_string(_impl->name), i) << endmsg;
407 throw failed_constructor();
410 _port_flags.push_back(flags);
413 _control_data = new float[num_ports];
414 _shadow_data = new float[num_ports];
415 _defaults = new float[num_ports];
416 _ev_buffers = new LV2_Evbuf*[num_ports];
417 memset(_ev_buffers, 0, sizeof(LV2_Evbuf*) * num_ports);
419 const bool latent = lilv_plugin_has_latency(plugin);
420 const uint32_t latency_index = (latent)
421 ? lilv_plugin_get_latency_port_index(plugin)
424 // Build an array of pointers to special parameter buffers
425 void*** params = new void**[num_ports];
426 for (uint32_t i = 0; i < num_ports; ++i) {
429 _impl->designated_input (LV2_TIME__beatsPerMinute, params, (void**)&_bpm_control_port);
430 _impl->designated_input (LV2_CORE__freeWheeling, params, (void**)&_freewheel_control_port);
432 for (uint32_t i = 0; i < num_ports; ++i) {
433 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
434 const LilvNode* sym = lilv_port_get_symbol(plugin, port);
436 // Store index in map so we can look up index by symbol
437 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
439 // Get range and default value if applicable
440 if (parameter_is_control(i)) {
442 lilv_port_get_range(plugin, port, &def, NULL, NULL);
443 _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
444 if (lilv_port_has_property (plugin, port, _world.lv2_sampleRate)) {
445 _defaults[i] *= _session.frame_rate ();
449 lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
451 if (latent && i == latency_index) {
452 _latency_control_port = &_control_data[i];
453 *_latency_control_port = 0;
456 if (parameter_is_input(i)) {
457 _shadow_data[i] = default_value(i);
459 *params[i] = (void*)&_shadow_data[i];
469 LilvUIs* uis = lilv_plugin_get_uis(plugin);
470 if (lilv_uis_size(uis) > 0) {
472 // Look for embeddable UI
473 LILV_FOREACH(uis, u, uis) {
474 const LilvUI* this_ui = lilv_uis_get(uis, u);
475 const LilvNode* this_ui_type = NULL;
476 if (lilv_ui_is_supported(this_ui,
480 // TODO: Multiple UI support
482 _impl->ui_type = this_ui_type;
487 // Look for Gtk native UI
488 LILV_FOREACH(uis, i, uis) {
489 const LilvUI* ui = lilv_uis_get(uis, i);
490 if (lilv_ui_is_a(ui, _world.ui_GtkUI)) {
492 _impl->ui_type = _world.ui_GtkUI;
498 // If Gtk UI is not available, try to find external UI
500 LILV_FOREACH(uis, i, uis) {
501 const LilvUI* ui = lilv_uis_get(uis, i);
502 if (lilv_ui_is_a(ui, _world.ui_external)) {
504 _impl->ui_type = _world.ui_external;
511 allocate_atom_event_buffers();
512 latency_compute_run();
515 LV2Plugin::~LV2Plugin ()
517 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
522 lilv_instance_free(_impl->instance);
523 lilv_node_free(_impl->name);
524 lilv_node_free(_impl->author);
527 free(_make_path_feature.data);
528 free(_work_schedule_feature.data);
534 if (_atom_ev_buffers) {
535 LV2_Evbuf** b = _atom_ev_buffers;
540 free(_atom_ev_buffers);
543 delete [] _control_data;
544 delete [] _shadow_data;
545 delete [] _ev_buffers;
549 LV2Plugin::is_external_ui() const
554 return lilv_ui_is_a(_impl->ui, _world.ui_external);
558 LV2Plugin::ui_is_resizable () const
560 const LilvNode* s = lilv_ui_get_uri(_impl->ui);
561 LilvNode* p = lilv_new_uri(_world.world, LV2_CORE__optionalFeature);
562 LilvNode* fs = lilv_new_uri(_world.world, LV2_UI__fixedSize);
563 LilvNode* nrs = lilv_new_uri(_world.world, LV2_UI__noUserResize);
565 LilvNodes* fs_matches = lilv_world_find_nodes(_world.world, s, p, fs);
566 LilvNodes* nrs_matches = lilv_world_find_nodes(_world.world, s, p, nrs);
568 lilv_nodes_free(nrs_matches);
569 lilv_nodes_free(fs_matches);
574 return !fs_matches && !nrs_matches;
578 LV2Plugin::unique_id() const
580 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
584 LV2Plugin::uri() const
586 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
590 LV2Plugin::label() const
592 return lilv_node_as_string(_impl->name);
596 LV2Plugin::name() const
598 return lilv_node_as_string(_impl->name);
602 LV2Plugin::maker() const
604 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
608 LV2Plugin::num_ports() const
610 return lilv_plugin_get_num_ports(_impl->plugin);
614 LV2Plugin::parameter_count() const
616 return lilv_plugin_get_num_ports(_impl->plugin);
620 LV2Plugin::default_value(uint32_t port)
622 return _defaults[port];
626 LV2Plugin::port_symbol(uint32_t index) const
628 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
630 error << name() << ": Invalid port index " << index << endmsg;
633 const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
634 return lilv_node_as_string(sym);
638 LV2Plugin::port_index (const char* symbol) const
640 const map<string, uint32_t>::const_iterator i = _port_indices.find(symbol);
641 if (i != _port_indices.end()) {
644 warning << string_compose(_("LV2: Unknown port %1"), symbol) << endmsg;
650 LV2Plugin::set_parameter(uint32_t which, float val)
652 DEBUG_TRACE(DEBUG::LV2, string_compose(
653 "%1 set parameter %2 to %3\n", name(), which, val));
655 if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
656 if (get_parameter (which) == val) {
660 _shadow_data[which] = val;
662 warning << string_compose(
663 _("Illegal parameter number used with plugin \"%1\". "
664 "This is a bug in either %2 or the LV2 plugin <%3>"),
665 name(), PROGRAM_NAME, unique_id()) << endmsg;
668 Plugin::set_parameter(which, val);
672 LV2Plugin::get_parameter(uint32_t which) const
674 if (parameter_is_input(which)) {
675 return (float)_shadow_data[which];
677 return (float)_control_data[which];
683 LV2Plugin::get_docs() const
685 LilvNodes* comments = lilv_plugin_get_value(_impl->plugin, _world.rdfs_comment);
687 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
688 lilv_nodes_free(comments);
696 LV2Plugin::get_parameter_docs(uint32_t which) const
698 LilvNodes* comments = lilv_port_get_value(
700 lilv_plugin_get_port_by_index(_impl->plugin, which),
701 _world.rdfs_comment);
704 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
705 lilv_nodes_free(comments);
713 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
716 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
717 if (parameter_is_control(x)) {
729 LV2Plugin::extension_data(const char* uri) const
731 return lilv_instance_get_extension_data(_impl->instance, uri);
735 LV2Plugin::c_plugin()
737 return _impl->plugin;
743 return (const void*)_impl->ui;
747 LV2Plugin::c_ui_type()
749 return (const void*)_impl->ui_type;
752 /** Directory for all plugin state. */
754 LV2Plugin::plugin_dir() const
756 return Glib::build_filename(_session.plugins_dir(), _insert_id.to_s());
759 /** Directory for files created by the plugin (except during save). */
761 LV2Plugin::scratch_dir() const
763 return Glib::build_filename(plugin_dir(), "scratch");
766 /** Directory for snapshots of files in the scratch directory. */
768 LV2Plugin::file_dir() const
770 return Glib::build_filename(plugin_dir(), "files");
773 /** Directory to save state snapshot version @c num into. */
775 LV2Plugin::state_dir(unsigned num) const
777 return Glib::build_filename(plugin_dir(), string_compose("state%1", num));
780 /** Implementation of state:makePath for files created at instantiation time.
781 * Note this is not used for files created at save time (Lilv deals with that).
784 LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
787 LV2Plugin* me = (LV2Plugin*)handle;
788 if (me->_insert_id == PBD::ID("0")) {
789 warning << string_compose(
790 "File path \"%1\" requested but LV2 %2 has no insert ID",
791 path, me->name()) << endmsg;
792 return g_strdup(path);
795 const std::string abs_path = Glib::build_filename(me->scratch_dir(), path);
796 const std::string dirname = Glib::path_get_dirname(abs_path);
797 g_mkdir_with_parents(dirname.c_str(), 0744);
799 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
802 std::cerr << "MAKE PATH " << path
803 << " => " << g_strndup(abs_path.c_str(), abs_path.length())
805 return g_strndup(abs_path.c_str(), abs_path.length());
809 remove_directory(const std::string& path)
811 if (!Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) {
812 warning << string_compose("\"%1\" is not a directory", path) << endmsg;
816 Glib::RefPtr<Gio::File> dir = Gio::File::create_for_path(path);
817 Glib::RefPtr<Gio::FileEnumerator> e = dir->enumerate_children();
818 Glib::RefPtr<Gio::FileInfo> fi;
819 while ((fi = e->next_file())) {
820 if (fi->get_type() == Gio::FILE_TYPE_DIRECTORY) {
821 remove_directory(fi->get_name());
823 dir->get_child(fi->get_name())->remove();
830 LV2Plugin::add_state(XMLNode* root) const
832 assert(_insert_id != PBD::ID("0"));
836 LocaleGuard lg(X_("POSIX"));
838 for (uint32_t i = 0; i < parameter_count(); ++i) {
839 if (parameter_is_input(i) && parameter_is_control(i)) {
840 child = new XMLNode("Port");
841 child->add_property("symbol", port_symbol(i));
842 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
843 child->add_property("value", string(buf));
844 root->add_child_nocopy(*child);
848 if (_has_state_interface) {
849 cout << "LV2 " << name() << " has state interface" << endl;
850 // Provisionally increment state version and create directory
851 const std::string new_dir = state_dir(++_state_version);
852 g_mkdir_with_parents(new_dir.c_str(), 0744);
854 cout << "NEW DIR: " << new_dir << endl;
856 LilvState* state = lilv_state_new_from_instance(
860 scratch_dir().c_str(),
862 _session.externals_dir().c_str(),
869 if (!_impl->state || !lilv_state_equals(state, _impl->state)) {
870 lilv_state_save(_world.world,
872 _uri_map.urid_unmap(),
878 lilv_state_free(_impl->state);
879 _impl->state = state;
881 cout << "Saved LV2 state to " << state_dir(_state_version) << endl;
883 // State is identical, decrement version and nuke directory
884 cout << "LV2 state identical, not saving" << endl;
885 lilv_state_free(state);
886 remove_directory(new_dir);
890 root->add_property("state-dir", string_compose("state%1", _state_version));
892 cout << "LV2 " << name() << " has no state interface." << endl;
896 static inline const LilvNode*
897 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
899 LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
900 return vs ? lilv_nodes_get_first(vs) : NULL;
904 LV2Plugin::find_presets()
906 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
907 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
908 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
910 LilvNodes* presets = lilv_plugin_get_related(_impl->plugin, pset_Preset);
911 LILV_FOREACH(nodes, i, presets) {
912 const LilvNode* preset = lilv_nodes_get(presets, i);
913 lilv_world_load_resource(_world.world, preset);
914 const LilvNode* name = get_value(_world.world, preset, rdfs_label);
916 _presets.insert(std::make_pair(lilv_node_as_string(preset),
917 Plugin::PresetRecord(
918 lilv_node_as_string(preset),
919 lilv_node_as_string(name))));
921 warning << string_compose(
922 _("Plugin \"%1\% preset \"%2%\" is missing a label\n"),
923 lilv_node_as_string(lilv_plugin_get_uri(_impl->plugin)),
924 lilv_node_as_string(preset)) << endmsg;
927 lilv_nodes_free(presets);
929 lilv_node_free(rdfs_label);
930 lilv_node_free(pset_Preset);
931 lilv_node_free(lv2_appliesTo);
935 LV2Plugin::load_preset(PresetRecord r)
937 std::map<std::string,uint32_t>::iterator it;
939 LilvNode* lv2_port = lilv_new_uri(_world.world, LILV_NS_LV2 "port");
940 LilvNode* lv2_symbol = lilv_new_uri(_world.world, LILV_NS_LV2 "symbol");
941 LilvNode* preset = lilv_new_uri(_world.world, r.uri.c_str());
942 LilvNode* pset_value = lilv_new_uri(_world.world, LV2_PRESETS__value);
944 LilvNodes* ports = lilv_world_find_nodes(_world.world, preset, lv2_port, NULL);
945 LILV_FOREACH(nodes, i, ports) {
946 const LilvNode* port = lilv_nodes_get(ports, i);
947 const LilvNode* symbol = get_value(_world.world, port, lv2_symbol);
948 const LilvNode* value = get_value(_world.world, port, pset_value);
949 if (value && lilv_node_is_float(value)) {
950 it = _port_indices.find(lilv_node_as_string(symbol));
951 if (it != _port_indices.end()) {
952 set_parameter(it->second,lilv_node_as_float(value));
956 lilv_nodes_free(ports);
958 lilv_node_free(pset_value);
959 lilv_node_free(preset);
960 lilv_node_free(lv2_symbol);
961 lilv_node_free(lv2_port);
963 Plugin::load_preset(r);
969 ARDOUR::lv2plugin_get_port_value(const char* port_symbol,
974 // cerr << "get_port_value(" << port_symbol << ", ...) ... ";
975 LV2Plugin *plugin = (LV2Plugin *) user_data;
977 uint32_t index = plugin->port_index(port_symbol);
978 if (index != (uint32_t) -1) {
979 if (plugin->parameter_is_input(index) && plugin->parameter_is_control(index)) {
981 *size = sizeof(float);
982 *type = plugin->_uri_map.uri_to_id(LV2_ATOM__Float);
983 value = &plugin->_shadow_data[index];
984 // cerr << "index="<< index << ",*size=" << *size << ",*type=" << *type << ",*value=" << *value << endl;
988 // cerr << "port is not input control port! ";
991 // cerr << "returning NULL!" << endl;
998 LV2Plugin::do_save_preset(string name)
1000 // cerr << "LV2Plugin::do_save_preset(" << name << ")" << endl;
1002 string pset_uri = uri();
1006 string save_dir = Glib::build_filename(
1007 Glib::get_home_dir(),
1008 Glib::build_filename(".lv2", "presets")
1011 LilvState* state = lilv_state_new_from_instance(
1014 _uri_map.urid_map(),
1015 scratch_dir().c_str(), // file_dir
1018 save_dir.c_str(), // save_dir
1019 lv2plugin_get_port_value, // get_value
1020 (void*) this, // user_data
1021 LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE, // flags
1022 _features // features
1025 lilv_state_set_label(state, name.c_str());
1027 _world.world, // world
1028 _uri_map.urid_map(), // map
1029 _uri_map.urid_unmap(), // unmap
1031 pset_uri.c_str(), // uri
1032 save_dir.c_str(), // dir
1033 (name + ".ttl").c_str() // filename
1036 lilv_state_free(state);
1041 LV2Plugin::do_remove_preset(string name)
1043 string preset_file = Glib::build_filename(
1044 Glib::get_home_dir(),
1045 Glib::build_filename(
1046 Glib::build_filename(".lv2", "presets"),
1050 unlink(preset_file.c_str());
1054 LV2Plugin::has_editor() const
1056 return _impl->ui != NULL;
1060 LV2Plugin::has_message_output() const
1062 for (uint32_t i = 0; i < num_ports(); ++i) {
1063 if ((_port_flags[i] & (PORT_MESSAGE|PORT_ATOM)) && _port_flags[i] & PORT_OUTPUT) {
1071 LV2Plugin::atom_eventTransfer() const
1073 return _event_transfer_type;
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 if (dest->write(buf, buf_size) != buf_size) {
1093 error << "Error writing to UI=>Plugin RingBuffer" << endmsg;
1098 LV2Plugin::write_from_ui(uint32_t index,
1101 const uint8_t* body)
1104 _from_ui = new RingBuffer<uint8_t>(4096);
1107 write_to(_from_ui, index, protocol, size, body);
1111 LV2Plugin::write_to_ui(uint32_t index,
1114 const uint8_t* body)
1116 write_to(_to_ui, index, protocol, size, body);
1120 LV2Plugin::enable_ui_emmission()
1123 _to_ui = new RingBuffer<uint8_t>(4096);
1128 LV2Plugin::emit_to_ui(void* controller, UIMessageSink sink)
1134 uint32_t read_space = _to_ui->read_space();
1135 while (read_space > sizeof(UIMessage)) {
1137 if (_to_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
1138 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1141 uint8_t body[msg.size];
1142 if (_to_ui->read(body, msg.size) != msg.size) {
1143 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1147 sink(controller, msg.index, msg.size, msg.protocol, body);
1149 read_space -= sizeof(msg) + msg.size;
1154 LV2Plugin::work(uint32_t size, const void* data)
1156 return _impl->work_iface->work(
1157 _impl->instance->lv2_handle, work_respond, this, size, data);
1161 LV2Plugin::work_response(uint32_t size, const void* data)
1163 return _impl->work_iface->work_response(
1164 _impl->instance->lv2_handle, size, data);
1168 LV2Plugin::set_insert_info(const PluginInsert* insert)
1170 _insert_id = insert->id();
1174 LV2Plugin::set_state(const XMLNode& node, int version)
1177 const XMLProperty* prop;
1178 XMLNodeConstIterator iter;
1183 LocaleGuard lg(X_("POSIX"));
1185 if (node.name() != state_node_name()) {
1186 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
1190 if (version < 3000) {
1191 nodes = node.children("port");
1193 nodes = node.children("Port");
1196 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
1200 if ((prop = child->property("symbol")) != 0) {
1201 sym = prop->value().c_str();
1203 warning << _("LV2: port has no symbol, ignored") << endmsg;
1207 map<string, uint32_t>::iterator i = _port_indices.find(sym);
1209 if (i != _port_indices.end()) {
1210 port_id = i->second;
1212 warning << _("LV2: port has unknown index, ignored") << endmsg;
1216 if ((prop = child->property("value")) != 0) {
1217 value = prop->value().c_str();
1219 warning << _("LV2: port has no value, ignored") << endmsg;
1223 set_parameter(port_id, atof(value));
1227 if ((prop = node.property("state-dir")) != 0) {
1228 if (sscanf(prop->value().c_str(), "state%u", &_state_version) != 1) {
1229 error << string_compose(
1230 "LV2: failed to parse state version from \"%1\"",
1231 prop->value()) << endmsg;
1234 std::string state_file = Glib::build_filename(
1236 Glib::build_filename(prop->value(), "state.ttl"));
1238 cout << "Loading LV2 state from " << state_file << endl;
1239 LilvState* state = lilv_state_new_from_file(
1240 _world.world, _uri_map.urid_map(), NULL, state_file.c_str());
1242 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
1245 latency_compute_run();
1247 return Plugin::set_state(node, version);
1251 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
1253 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
1255 LilvNode *def, *min, *max;
1256 lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
1258 desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.lv2_integer);
1259 desc.toggled = lilv_port_has_property(_impl->plugin, port, _world.lv2_toggled);
1260 desc.logarithmic = lilv_port_has_property(_impl->plugin, port, _world.ext_logarithmic);
1261 desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.lv2_sampleRate);
1262 desc.label = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
1263 desc.lower = min ? lilv_node_as_float(min) : 0.0f;
1264 desc.upper = max ? lilv_node_as_float(max) : 1.0f;
1265 if (desc.sr_dependent) {
1266 desc.lower *= _session.frame_rate ();
1267 desc.upper *= _session.frame_rate ();
1270 desc.min_unbound = false; // TODO: LV2 extension required
1271 desc.max_unbound = false; // TODO: LV2 extension required
1273 if (desc.integer_step) {
1275 desc.smallstep = 0.1;
1276 desc.largestep = 10.0;
1278 const float delta = desc.upper - desc.lower;
1279 desc.step = delta / 1000.0f;
1280 desc.smallstep = delta / 10000.0f;
1281 desc.largestep = delta / 10.0f;
1284 desc.enumeration = lilv_port_has_property(_impl->plugin, port, _world.lv2_enumeration);
1286 lilv_node_free(def);
1287 lilv_node_free(min);
1288 lilv_node_free(max);
1294 LV2Plugin::describe_parameter(Evoral::Parameter which)
1296 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
1297 LilvNode* name = lilv_port_get_name(_impl->plugin,
1298 lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
1299 string ret(lilv_node_as_string(name));
1300 lilv_node_free(name);
1308 LV2Plugin::signal_latency() const
1310 if (_latency_control_port) {
1311 return (framecnt_t)floor(*_latency_control_port);
1317 set<Evoral::Parameter>
1318 LV2Plugin::automatable() const
1320 set<Evoral::Parameter> ret;
1322 for (uint32_t i = 0; i < parameter_count(); ++i) {
1323 if (parameter_is_input(i) && parameter_is_control(i)) {
1324 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
1332 LV2Plugin::activate()
1334 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
1336 if (!_was_activated) {
1337 lilv_instance_activate(_impl->instance);
1338 _was_activated = true;
1343 LV2Plugin::deactivate()
1345 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
1347 if (_was_activated) {
1348 lilv_instance_deactivate(_impl->instance);
1349 _was_activated = false;
1354 LV2Plugin::cleanup()
1356 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
1360 lilv_instance_free(_impl->instance);
1361 _impl->instance = NULL;
1365 LV2Plugin::allocate_atom_event_buffers()
1367 /* reserve local scratch buffers for ATOM event-queues */
1368 const LilvPlugin* p = _impl->plugin;
1370 /* count non-MIDI atom event-ports
1371 * TODO: nicely ask drobilla to make a lilv_ call for that
1373 int count_atom_out = 0;
1374 int count_atom_in = 0;
1375 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
1376 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
1377 if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
1378 LilvNodes* buffer_types = lilv_port_get_value(
1379 p, port, _world.atom_bufferType);
1380 LilvNodes* atom_supports = lilv_port_get_value(
1381 p, port, _world.atom_supports);
1383 if (!lilv_nodes_contains(buffer_types, _world.atom_Sequence)
1384 || !lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
1385 if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
1388 if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
1392 lilv_nodes_free(buffer_types);
1393 lilv_nodes_free(atom_supports);
1397 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 need buffers for %2 atom-in and %3 atom-out event-ports\n",
1398 name(), count_atom_in, count_atom_out));
1400 const int total_atom_buffers = (count_atom_in + count_atom_out);
1401 if (_atom_ev_buffers || total_atom_buffers == 0) {
1405 DEBUG_TRACE(DEBUG::LV2, string_compose("allocate %1 atom_ev_buffers\n", total_atom_buffers));
1406 _atom_ev_buffers = (LV2_Evbuf**) malloc((total_atom_buffers + 1) * sizeof(LV2_Evbuf*));
1407 for (int i = 0; i < total_atom_buffers; ++i ) {
1408 _atom_ev_buffers[i] = lv2_evbuf_new(32768, LV2_EVBUF_ATOM,
1409 LV2Plugin::_chunk_type, LV2Plugin::_sequence_type);
1411 _atom_ev_buffers[total_atom_buffers] = 0;
1416 LV2Plugin::connect_and_run(BufferSet& bufs,
1417 ChanMapping in_map, ChanMapping out_map,
1418 pframes_t nframes, framecnt_t offset)
1420 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
1421 Plugin::connect_and_run(bufs, in_map, out_map, nframes, offset);
1423 cycles_t then = get_cycles();
1425 if (_freewheel_control_port) {
1426 *_freewheel_control_port = _session.engine().freewheeling ();
1429 if (_bpm_control_port) {
1430 TempoMap& tmap (_session.tempo_map ());
1431 Tempo tempo = tmap.tempo_at (_session.transport_frame () + offset);
1432 *_bpm_control_port = tempo.beats_per_minute ();
1435 ChanCount bufs_count;
1436 bufs_count.set(DataType::AUDIO, 1);
1437 bufs_count.set(DataType::MIDI, 1);
1438 BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
1439 BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
1440 uint32_t const num_ports = parameter_count();
1442 uint32_t audio_in_index = 0;
1443 uint32_t audio_out_index = 0;
1444 uint32_t midi_in_index = 0;
1445 uint32_t midi_out_index = 0;
1446 uint32_t atom_port_index = 0;
1448 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
1451 PortFlags flags = _port_flags[port_index];
1452 if (flags & PORT_AUDIO) {
1453 if (flags & PORT_INPUT) {
1454 index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
1456 ? bufs.get_audio(index).data(offset)
1457 : silent_bufs.get_audio(0).data(offset);
1459 index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
1461 ? bufs.get_audio(index).data(offset)
1462 : scratch_bufs.get_audio(0).data(offset);
1464 } else if (flags & (PORT_EVENT|PORT_MESSAGE)) {
1465 /* FIXME: The checks here for bufs.count().n_midi() > index shouldn't
1466 be necessary, but the mapping is illegal in some cases. Ideally
1467 that should be fixed, but this is easier...
1469 if (flags & PORT_INPUT) {
1470 index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
1471 _ev_buffers[port_index] = (valid && bufs.count().n_midi() > index)
1472 ? bufs.get_lv2_midi(true, index, flags & PORT_EVENT)
1473 : silent_bufs.get_lv2_midi(true, 0, flags & PORT_EVENT);
1474 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
1476 index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
1477 _ev_buffers[port_index] = (valid && bufs.count().n_midi() > index)
1478 ? bufs.get_lv2_midi(false, index, flags & PORT_EVENT)
1479 : scratch_bufs.get_lv2_midi(false, 0, flags & PORT_EVENT);
1480 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
1482 } else if (flags & (PORT_ATOM)) {
1483 assert(_atom_ev_buffers && _atom_ev_buffers[atom_port_index]);
1484 if (flags & PORT_INPUT) {
1485 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], true);
1486 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
1488 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], false);
1489 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
1491 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
1494 continue; // Control port, leave buffer alone
1496 lilv_instance_connect_port(_impl->instance, port_index, buf);
1499 // Read messages from UI and push into appropriate buffers
1501 uint32_t read_space = _from_ui->read_space();
1502 while (read_space > sizeof(UIMessage)) {
1504 if (_from_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
1505 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
1508 uint8_t body[msg.size];
1509 if (_from_ui->read(body, msg.size) != msg.size) {
1510 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
1513 if (msg.protocol == _event_transfer_type) {
1514 LV2_Evbuf* buf = _ev_buffers[msg.index];
1515 LV2_Evbuf_Iterator i = lv2_evbuf_end(buf);
1516 const LV2_Atom* const atom = (const LV2_Atom*)body;
1517 if (!lv2_evbuf_write(&i, nframes, 0, atom->type, atom->size,
1518 (const uint8_t*)(atom + 1))) {
1519 cerr << "LV2: failed to write data to event buffer\n";
1522 error << "Received unknown message type from UI" << endmsg;
1524 read_space -= sizeof(UIMessage) + msg.size;
1531 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
1532 PortFlags flags = _port_flags[port_index];
1534 // Flush MIDI (write back to Ardour MIDI buffers)
1535 if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_MESSAGE))) {
1536 const uint32_t buf_index = out_map.get(
1537 DataType::MIDI, midi_out_index++, &valid);
1539 bufs.flush_lv2_midi(true, buf_index);
1543 // Write messages to UI
1544 if (_to_ui && (flags & PORT_OUTPUT) && (flags & (PORT_MESSAGE|PORT_ATOM))) {
1545 LV2_Evbuf* buf = _ev_buffers[port_index];
1546 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
1547 lv2_evbuf_is_valid(i);
1548 i = lv2_evbuf_next(i)) {
1549 uint32_t frames, subframes, type, size;
1551 lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
1552 write_to_ui(port_index, _event_transfer_type,
1553 size + sizeof(LV2_Atom),
1554 data - sizeof(LV2_Atom));
1559 cycles_t now = get_cycles();
1560 set_cycles((uint32_t)(now - then));
1566 LV2Plugin::parameter_is_control(uint32_t param) const
1568 assert(param < _port_flags.size());
1569 return _port_flags[param] & PORT_CONTROL;
1573 LV2Plugin::parameter_is_audio(uint32_t param) const
1575 assert(param < _port_flags.size());
1576 return _port_flags[param] & PORT_AUDIO;
1580 LV2Plugin::parameter_is_event(uint32_t param) const
1582 assert(param < _port_flags.size());
1583 return _port_flags[param] & PORT_EVENT;
1587 LV2Plugin::parameter_is_output(uint32_t param) const
1589 assert(param < _port_flags.size());
1590 return _port_flags[param] & PORT_OUTPUT;
1594 LV2Plugin::parameter_is_input(uint32_t param) const
1596 assert(param < _port_flags.size());
1597 return _port_flags[param] & PORT_INPUT;
1601 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
1604 if (param < parameter_count()) {
1605 snprintf(buf, len, "%.3f", get_parameter(param));
1612 boost::shared_ptr<Plugin::ScalePoints>
1613 LV2Plugin::get_scale_points(uint32_t port_index) const
1615 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
1616 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
1618 boost::shared_ptr<Plugin::ScalePoints> ret;
1623 ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
1625 LILV_FOREACH(scale_points, i, points) {
1626 const LilvScalePoint* p = lilv_scale_points_get(points, i);
1627 const LilvNode* label = lilv_scale_point_get_label(p);
1628 const LilvNode* value = lilv_scale_point_get_value(p);
1629 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
1630 ret->insert(make_pair(lilv_node_as_string(label),
1631 lilv_node_as_float(value)));
1635 lilv_scale_points_free(points);
1640 LV2Plugin::run(pframes_t nframes)
1642 uint32_t const N = parameter_count();
1643 for (uint32_t i = 0; i < N; ++i) {
1644 if (parameter_is_control(i) && parameter_is_input(i)) {
1645 _control_data[i] = _shadow_data[i];
1649 lilv_instance_run(_impl->instance, nframes);
1651 if (_impl->work_iface) {
1652 _worker->emit_responses();
1653 if (_impl->work_iface->end_run) {
1654 _impl->work_iface->end_run(_impl->instance->lv2_handle);
1660 LV2Plugin::latency_compute_run()
1662 if (!_latency_control_port) {
1666 // Run the plugin so that it can set its latency parameter
1670 uint32_t port_index = 0;
1671 uint32_t in_index = 0;
1672 uint32_t out_index = 0;
1674 const framecnt_t bufsize = 1024;
1675 float buffer[bufsize];
1677 memset(buffer, 0, sizeof(float) * bufsize);
1679 // FIXME: Ensure plugins can handle in-place processing
1683 while (port_index < parameter_count()) {
1684 if (parameter_is_audio(port_index)) {
1685 if (parameter_is_input(port_index)) {
1686 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1688 } else if (parameter_is_output(port_index)) {
1689 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1701 LV2Plugin::Impl::designated_input (const char* uri, void** bufptrs[], void** bufptr)
1703 LilvPort* port = NULL;
1704 LilvNode* designation = lilv_new_uri(_world.world, uri);
1705 port = lilv_plugin_get_port_by_designation(
1706 plugin, _world.lv2_InputPort, designation);
1707 lilv_node_free(designation);
1709 bufptrs[lilv_port_get_index(plugin, port)] = bufptr;
1714 LV2World::LV2World()
1715 : world(lilv_world_new())
1717 lilv_world_load_all(world);
1718 atom_AtomPort = lilv_new_uri(world, LV2_ATOM__AtomPort);
1719 atom_Chunk = lilv_new_uri(world, LV2_ATOM__Chunk);
1720 atom_Sequence = lilv_new_uri(world, LV2_ATOM__Sequence);
1721 atom_bufferType = lilv_new_uri(world, LV2_ATOM__bufferType);
1722 atom_supports = lilv_new_uri(world, LV2_ATOM__supports);
1723 atom_eventTransfer = lilv_new_uri(world, LV2_ATOM__eventTransfer);
1724 ev_EventPort = lilv_new_uri(world, LILV_URI_EVENT_PORT);
1725 ext_logarithmic = lilv_new_uri(world, LV2_PORT_PROPS__logarithmic);
1726 lv2_AudioPort = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
1727 lv2_ControlPort = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
1728 lv2_InputPort = lilv_new_uri(world, LILV_URI_INPUT_PORT);
1729 lv2_OutputPort = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
1730 lv2_inPlaceBroken = lilv_new_uri(world, LV2_CORE__inPlaceBroken);
1731 lv2_integer = lilv_new_uri(world, LV2_CORE__integer);
1732 lv2_sampleRate = lilv_new_uri(world, LV2_CORE__sampleRate);
1733 lv2_toggled = lilv_new_uri(world, LV2_CORE__toggled);
1734 lv2_enumeration = lilv_new_uri(world, LV2_CORE__enumeration);
1735 midi_MidiEvent = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
1736 rdfs_comment = lilv_new_uri(world, LILV_NS_RDFS "comment");
1737 ui_GtkUI = lilv_new_uri(world, LV2_UI__GtkUI);
1738 ui_external = lilv_new_uri(world, "http://lv2plug.in/ns/extensions/ui#external");
1741 LV2World::~LV2World()
1743 lilv_node_free(ui_external);
1744 lilv_node_free(ui_GtkUI);
1745 lilv_node_free(midi_MidiEvent);
1746 lilv_node_free(lv2_toggled);
1747 lilv_node_free(lv2_sampleRate);
1748 lilv_node_free(lv2_integer);
1749 lilv_node_free(lv2_inPlaceBroken);
1750 lilv_node_free(lv2_OutputPort);
1751 lilv_node_free(lv2_InputPort);
1752 lilv_node_free(lv2_ControlPort);
1753 lilv_node_free(lv2_AudioPort);
1754 lilv_node_free(ext_logarithmic);
1755 lilv_node_free(ev_EventPort);
1756 lilv_node_free(atom_eventTransfer);
1757 lilv_node_free(atom_bufferType);
1758 lilv_node_free(atom_Sequence);
1759 lilv_node_free(atom_Chunk);
1760 lilv_node_free(atom_AtomPort);
1763 LV2PluginInfo::LV2PluginInfo (const void* c_plugin)
1764 : _c_plugin(c_plugin)
1769 LV2PluginInfo::~LV2PluginInfo()
1773 LV2PluginInfo::load(Session& session)
1778 plugin.reset(new LV2Plugin(session.engine(), session,
1779 (const LilvPlugin*)_c_plugin,
1780 session.frame_rate()));
1782 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
1784 } catch (failed_constructor& err) {
1785 return PluginPtr((Plugin*)0);
1792 LV2PluginInfo::discover()
1794 PluginInfoList* plugs = new PluginInfoList;
1795 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
1797 info << "LV2: Discovering " << lilv_plugins_size(plugins) << " plugins" << endmsg;
1799 LILV_FOREACH(plugins, i, plugins) {
1800 const LilvPlugin* p = lilv_plugins_get(plugins, i);
1801 LV2PluginInfoPtr info(new LV2PluginInfo((const void*)p));
1803 LilvNode* name = lilv_plugin_get_name(p);
1805 cerr << "LV2: invalid plugin\n";
1811 info->name = string(lilv_node_as_string(name));
1812 lilv_node_free(name);
1814 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
1815 const LilvNode* label = lilv_plugin_class_get_label(pclass);
1816 info->category = lilv_node_as_string(label);
1818 LilvNode* author_name = lilv_plugin_get_author_name(p);
1819 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
1820 lilv_node_free(author_name);
1822 info->path = "/NOPATH"; // Meaningless for LV2
1824 /* count atom-event-ports that feature
1825 * atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent>
1827 * TODO: nicely ask drobilla to make a lilv_ call for that
1829 int count_midi_out = 0;
1830 int count_midi_in = 0;
1831 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
1832 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
1833 if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
1834 LilvNodes* buffer_types = lilv_port_get_value(
1835 p, port, _world.atom_bufferType);
1836 LilvNodes* atom_supports = lilv_port_get_value(
1837 p, port, _world.atom_supports);
1839 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)
1840 && lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
1841 if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
1844 if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
1848 lilv_nodes_free(buffer_types);
1849 lilv_nodes_free(atom_supports);
1853 info->n_inputs.set_audio(
1854 lilv_plugin_get_num_ports_of_class(
1855 p, _world.lv2_InputPort, _world.lv2_AudioPort, NULL));
1856 info->n_inputs.set_midi(
1857 lilv_plugin_get_num_ports_of_class(
1858 p, _world.lv2_InputPort, _world.ev_EventPort, NULL)
1861 info->n_outputs.set_audio(
1862 lilv_plugin_get_num_ports_of_class(
1863 p, _world.lv2_OutputPort, _world.lv2_AudioPort, NULL));
1864 info->n_outputs.set_midi(
1865 lilv_plugin_get_num_ports_of_class(
1866 p, _world.lv2_OutputPort, _world.ev_EventPort, NULL)
1869 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
1870 info->index = 0; // Meaningless for LV2
1872 plugs->push_back(info);