2 * Copyright (C) 2000-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2006 Jesse Chappell <jesse@essej.net>
5 * Copyright (C) 2007-2014 David Robillard <d@drobilla.net>
6 * Copyright (C) 2007-2017 Tim Mayberry <mojofunk@gmail.com>
7 * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
8 * Copyright (C) 2013-2015 John Emmas <john@creativepost.co.uk>
9 * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "libardour-config.h"
34 #include <cstdio> // so libraptor doesn't complain
47 #include "pbd/compose.h"
48 #include "pbd/error.h"
49 #include "pbd/xml++.h"
51 #include "ardour/buffer_set.h"
52 #include "ardour/chan_count.h"
53 #include "ardour/chan_mapping.h"
54 #include "ardour/data_type.h"
55 #include "ardour/luaproc.h"
56 #include "ardour/midi_buffer.h"
57 #include "ardour/midi_state_tracker.h"
58 #include "ardour/plugin.h"
59 #include "ardour/plugin_manager.h"
60 #include "ardour/port.h"
61 #include "ardour/session.h"
62 #include "ardour/types.h"
64 #ifdef AUDIOUNIT_SUPPORT
65 #include "ardour/audio_unit.h"
69 #include "ardour/lv2_plugin.h"
72 #include "pbd/stl_delete.h"
78 using namespace ARDOUR;
81 namespace ARDOUR { class AudioEngine; }
83 PBD::Signal2<void, std::string, Plugin*> Plugin::PresetsChanged;
86 PluginInfo::needs_midi_input () const
88 return (n_inputs.n_midi() != 0);
91 Plugin::Plugin (AudioEngine& e, Session& s)
96 , _for_impulse_analysis (false)
97 , _have_presets (false)
98 , _have_pending_stop_events (false)
99 , _parameter_changed_since_last_preset (false)
100 , _immediate_events(6096) // FIXME: size?
102 _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
105 Plugin::Plugin (const Plugin& other)
106 : StatefulDestructible()
108 , _engine (other._engine)
109 , _session (other._session)
110 , _info (other._info)
112 , _owner (other._owner)
113 , _for_impulse_analysis (false)
114 , _have_presets (false)
115 , _have_pending_stop_events (false)
116 , _last_preset (other._last_preset)
117 , _parameter_changed_since_last_preset (false)
118 , _immediate_events(6096) // FIXME: size?
120 _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
128 Plugin::remove_preset (string name)
130 Plugin::PresetRecord const * p = preset_by_label (name);
132 PBD::error << _("Trying to remove nonexistent preset.") << endmsg;
136 PBD::error << _("Cannot remove plugin factory preset.") << endmsg;
140 do_remove_preset (name);
141 _presets.erase (p->uri);
143 _last_preset.uri = "";
144 _parameter_changed_since_last_preset = false;
145 _have_presets = false;
146 PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
147 PresetRemoved (); /* EMIT SIGNAL */
151 Plugin::save_preset (string name)
153 if (preset_by_label (name)) {
154 PBD::error << _("Preset with given name already exists.") << endmsg;
155 return Plugin::PresetRecord ();
158 string const uri = do_save_preset (name);
161 _presets.insert (make_pair (uri, PresetRecord (uri, name)));
162 _have_presets = false;
163 PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
164 PresetAdded (); /* EMIT SIGNAL */
167 return PresetRecord (uri, name);
171 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
173 PluginManager& mgr (PluginManager::instance());
174 PluginInfoList plugs;
178 plugs = mgr.lua_plugin_info();
182 plugs = mgr.ladspa_plugin_info();
187 plugs = mgr.lv2_plugin_info();
191 #ifdef WINDOWS_VST_SUPPORT
192 case ARDOUR::Windows_VST:
193 plugs = mgr.windows_vst_plugin_info();
199 plugs = mgr.lxvst_plugin_info();
203 #ifdef MACVST_SUPPORT
205 plugs = mgr.mac_vst_plugin_info();
209 #ifdef AUDIOUNIT_SUPPORT
210 case ARDOUR::AudioUnit:
211 plugs = mgr.au_plugin_info();
216 return PluginPtr ((Plugin *) 0);
219 PluginInfoList::iterator i;
221 for (i = plugs.begin(); i != plugs.end(); ++i) {
222 if (identifier == (*i)->unique_id){
223 return (*i)->load (session);
227 #ifdef WINDOWS_VST_SUPPORT
228 /* hmm, we didn't find it. could be because in older versions of Ardour.
229 we used to store the name of a VST plugin, not its unique ID. so try
233 for (i = plugs.begin(); i != plugs.end(); ++i) {
234 if (identifier == (*i)->name){
235 return (*i)->load (session);
241 /* hmm, we didn't find it. could be because in older versions of Ardour.
242 we used to store the name of a VST plugin, not its unique ID. so try
246 for (i = plugs.begin(); i != plugs.end(); ++i) {
247 if (identifier == (*i)->name){
248 return (*i)->load (session);
253 return PluginPtr ((Plugin*) 0);
257 Plugin::output_streams () const
259 /* LADSPA & VST should not get here because they do not
260 return "infinite" i/o counts.
262 return ChanCount::ZERO;
266 Plugin::input_streams () const
268 /* LADSPA & VST should not get here because they do not
269 return "infinite" i/o counts.
271 return ChanCount::ZERO;
274 Plugin::IOPortDescription
275 Plugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
277 std::stringstream ss;
279 case DataType::AUDIO:
280 ss << _("Audio") << " ";
283 ss << _("Midi") << " ";
290 ss << _("In") << " ";
292 ss << _("Out") << " ";
295 std::stringstream gn;
299 gn << (id / 2 + 1) << " L/R";
301 Plugin::IOPortDescription iod (ss.str());
302 iod.group_name = gn.str();
306 PluginOutputConfiguration
307 Plugin::possible_output () const
309 PluginOutputConfiguration oc;
311 oc.insert (_info->n_outputs.n_audio ());
316 const Plugin::PresetRecord *
317 Plugin::preset_by_label (const string& label)
319 if (!_have_presets) {
321 _have_presets = true;
325 for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
326 if (i->second.label == label) {
334 const Plugin::PresetRecord *
335 Plugin::preset_by_uri (const string& uri)
337 if (!_have_presets) {
339 _have_presets = true;
342 map<string, PresetRecord>::const_iterator pr = _presets.find (uri);
343 if (pr != _presets.end()) {
351 Plugin::write_immediate_event (size_t size, const uint8_t* buf)
353 if (!Evoral::midi_event_is_valid (buf, size)) {
356 return (_immediate_events.write (0, Evoral::MIDI_EVENT, size, buf) == size);
360 Plugin::connect_and_run (BufferSet& bufs,
361 samplepos_t /*start*/, samplepos_t /*end*/, double /*speed*/,
362 ChanMapping const& /*in_map*/, ChanMapping const& /*out_map*/,
363 pframes_t nframes, samplecnt_t /*offset*/)
365 if (bufs.count().n_midi() > 0) {
367 if (_immediate_events.read_space() && nframes > 0) {
368 _immediate_events.read (bufs.get_midi (0), 0, 1, nframes - 1, true);
371 /* Track notes that we are sending to the plugin */
372 const MidiBuffer& b = bufs.get_midi (0);
374 _tracker.track (b.begin(), b.end());
376 if (_have_pending_stop_events) {
377 /* Transmit note-offs that are pending from the last transport stop */
378 bufs.merge_from (_pending_stop_events, 0);
379 _have_pending_stop_events = false;
387 Plugin::realtime_handle_transport_stopped ()
393 Plugin::realtime_locate ()
399 Plugin::monitoring_changed ()
405 Plugin::resolve_midi ()
407 /* Create note-offs for any active notes and put them in _pending_stop_events, to be picked
408 up on the next call to connect_and_run ().
411 _pending_stop_events.get_midi(0).clear ();
412 _tracker.resolve_notes (_pending_stop_events.get_midi (0), 0);
413 _have_pending_stop_events = true;
416 vector<Plugin::PresetRecord>
417 Plugin::get_presets ()
419 vector<PresetRecord> p;
421 if (!_have_presets) {
423 _have_presets = true;
426 for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
427 p.push_back (i->second);
434 Plugin::load_preset (PresetRecord r)
437 _parameter_changed_since_last_preset = false;
439 _session.set_dirty ();
440 PresetLoaded (); /* EMIT SIGNAL */
445 Plugin::clear_preset ()
447 _last_preset.uri = "";
448 _last_preset.label = "";
449 _parameter_changed_since_last_preset = false;
451 _session.set_dirty ();
452 PresetLoaded (); /* EMIT SIGNAL */
456 Plugin::set_parameter (uint32_t /* which */, float /* value */)
458 _parameter_changed_since_last_preset = true;
459 PresetDirty (); /* EMIT SIGNAL */
463 Plugin::parameter_changed_externally (uint32_t which, float /* value */)
465 _parameter_changed_since_last_preset = true;
466 _session.set_dirty ();
467 ParameterChangedExternally (which, get_parameter (which)); /* EMIT SIGNAL */
468 PresetDirty (); /* EMIT SIGNAL */
472 Plugin::set_state (const XMLNode& node, int /*version*/)
474 std::string preset_uri;
475 const Plugin::PresetRecord* r = 0;
476 if (node.get_property (X_("last-preset-uri"), preset_uri)) {
477 r = preset_by_uri (preset_uri);
481 node.get_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset); // XXX
483 _last_preset.uri = "";
484 _last_preset.valid = false;
492 XMLNode* root = new XMLNode (state_node_name ());
494 root->set_property (X_("last-preset-uri"), _last_preset.uri);
495 root->set_property (X_("last-preset-label"), _last_preset.label);
496 root->set_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset);
504 Plugin::set_info (PluginInfoPtr info)
510 Plugin::parameter_label (uint32_t which) const
512 if (which >= parameter_count ()) {
515 ParameterDescriptor pd;
516 get_parameter_descriptor (which, pd);
521 PluginInfo::is_effect () const
523 return (!is_instrument () && !is_utility () && !is_analyzer ());
527 PluginInfo::is_instrument () const
529 if (category == "Instrument") {
533 // second check: if we have midi input and audio output, we're likely an instrument
534 return (n_inputs.n_midi() != 0) && (n_outputs.n_audio() > 0) && (n_inputs.n_audio() == 0);
538 PluginInfo::is_utility () const
540 /* XXX beware of translations, e.g. LV2 categories */
541 return (category == "Utility" || category == "MIDI" || category == "Generator");
545 PluginInfo::is_analyzer () const
547 return (category == "Analyser" || category == "Anaylsis" || category == "Analyzer");