2 Copyright (C) 2000 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "libardour-config.h"
26 #include "pbd/failed_constructor.h"
27 #include "pbd/xml++.h"
28 #include "pbd/convert.h"
30 #include "ardour/audio_buffer.h"
31 #include "ardour/automation_list.h"
32 #include "ardour/buffer_set.h"
33 #include "ardour/debug.h"
34 #include "ardour/event_type_map.h"
35 #include "ardour/ladspa_plugin.h"
36 #include "ardour/luaproc.h"
37 #include "ardour/plugin.h"
38 #include "ardour/plugin_insert.h"
41 #include "ardour/lv2_plugin.h"
44 #ifdef WINDOWS_VST_SUPPORT
45 #include "ardour/windows_vst_plugin.h"
49 #include "ardour/lxvst_plugin.h"
52 #ifdef AUDIOUNIT_SUPPORT
53 #include "ardour/audio_unit.h"
56 #include "ardour/session.h"
57 #include "ardour/types.h"
62 using namespace ARDOUR;
65 const string PluginInsert::port_automation_node_name = "PortAutomation";
67 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug)
68 : Processor (s, (plug ? plug->name() : string ("toBeRenamed")))
69 , _signal_analysis_collected_nframes(0)
70 , _signal_analysis_collect_nframes_max(0)
75 , _maps_from_state (false)
77 /* the first is the master */
81 create_automatable_parameters ();
82 const ChanCount& sc (sidechain_input_pins ());
83 if (sc.n_total () > 0) {
84 add_sidechain (sc.n_audio ());
89 PluginInsert::~PluginInsert ()
94 PluginInsert::set_strict_io (bool b)
96 bool changed = _strict_io != b;
99 PluginConfigChanged (); /* EMIT SIGNAL */
104 PluginInsert::set_count (uint32_t num)
106 bool require_state = !_plugins.empty();
108 /* this is a bad idea.... we shouldn't do this while active.
109 only a route holding their redirect_lock should be calling this
114 } else if (num > _plugins.size()) {
115 uint32_t diff = num - _plugins.size();
117 for (uint32_t n = 0; n < diff; ++n) {
118 boost::shared_ptr<Plugin> p = plugin_factory (_plugins[0]);
125 /* XXX do something */
128 PluginConfigChanged (); /* EMIT SIGNAL */
130 } else if (num < _plugins.size()) {
131 uint32_t diff = _plugins.size() - num;
132 for (uint32_t n= 0; n < diff; ++n) {
135 PluginConfigChanged (); /* EMIT SIGNAL */
143 PluginInsert::set_outputs (const ChanCount& c)
145 bool changed = (_custom_out != c) && _custom_cfg;
148 PluginConfigChanged (); /* EMIT SIGNAL */
153 PluginInsert::set_custom_cfg (bool b)
155 bool changed = _custom_cfg != b;
158 PluginConfigChanged (); /* EMIT SIGNAL */
163 PluginInsert::add_sidechain (uint32_t n_audio)
165 // caller must hold process lock
169 // TODO add route-name, plugin name and shorten.. (plugin name can be long and conatain odd chars)
170 std::string n = "Sidechain " + id().to_s(); /// XXX
171 SideChain *sc = new SideChain (_session, n);
172 _sidechain = boost::shared_ptr<SideChain> (sc);
173 _sidechain->activate ();
174 for (uint32_t n = 0; n < n_audio; ++n) {
175 _sidechain->input()->add_port ("", owner()); // add a port, don't connect.
177 PluginConfigChanged (); /* EMIT SIGNAL */
182 PluginInsert::del_sidechain ()
188 PluginConfigChanged (); /* EMIT SIGNAL */
193 PluginInsert::control_list_automation_state_changed (Evoral::Parameter which, AutoState s)
195 if (which.type() != PluginAutomation)
198 boost::shared_ptr<AutomationControl> c
199 = boost::dynamic_pointer_cast<AutomationControl>(control (which));
202 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_frame()));
207 PluginInsert::output_streams() const
209 assert (_configured);
210 return _configured_out;
214 PluginInsert::input_streams() const
216 assert (_configured);
217 return _configured_in;
221 PluginInsert::internal_streams() const
223 assert (_configured);
224 return _configured_internal;
228 PluginInsert::internal_output_streams() const
230 assert (!_plugins.empty());
232 PluginInfoPtr info = _plugins.front()->get_info();
234 if (info->reconfigurable_io()) {
235 ChanCount out = _plugins.front()->output_streams ();
236 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, reconfigur(able) output streams = %1\n", out));
239 ChanCount out = info->n_outputs;
240 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, static output streams = %1 for %2 plugins\n", out, _plugins.size()));
241 out.set_audio (out.n_audio() * _plugins.size());
242 out.set_midi (out.n_midi() * _plugins.size());
248 PluginInsert::internal_input_streams() const
250 assert (!_plugins.empty());
254 PluginInfoPtr info = _plugins.front()->get_info();
256 if (info->reconfigurable_io()) {
257 assert (_plugins.size() == 1);
258 in = _plugins.front()->input_streams();
263 DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, input streams = %1, match using %2\n", in, _match.method));
265 if (_match.method == Split) {
267 /* we are splitting 1 processor input to multiple plugin inputs,
268 so we have a maximum of 1 stream of each type.
270 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
271 if (in.get (*t) > 1) {
277 } else if (_match.method == Hide) {
279 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
280 in.set (*t, in.get (*t) - _match.hide.get (*t));
286 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
287 in.set (*t, in.get (*t) * _plugins.size ());
295 PluginInsert::natural_output_streams() const
298 if (is_channelstrip ()) {
299 return _configured_out;
302 return _plugins[0]->get_info()->n_outputs;
306 PluginInsert::natural_input_streams() const
309 if (is_channelstrip ()) {
310 return _configured_in;
313 return _plugins[0]->get_info()->n_inputs;
317 PluginInsert::sidechain_input_pins() const
319 return _cached_sidechain_pins;
323 PluginInsert::has_no_inputs() const
325 return _plugins[0]->get_info()->n_inputs == ChanCount::ZERO;
329 PluginInsert::has_no_audio_inputs() const
331 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
335 PluginInsert::plugin_latency () const {
336 return _plugins.front()->signal_latency ();
340 PluginInsert::is_midi_instrument() const
342 /* XXX more finesse is possible here. VST plugins have a
343 a specific "instrument" flag, for example.
345 PluginInfoPtr pi = _plugins[0]->get_info();
347 return pi->n_inputs.n_midi() != 0 &&
348 pi->n_outputs.n_audio() > 0;
352 PluginInsert::create_automatable_parameters ()
354 assert (!_plugins.empty());
356 set<Evoral::Parameter> a = _plugins.front()->automatable ();
358 for (set<Evoral::Parameter>::iterator i = a.begin(); i != a.end(); ++i) {
359 if (i->type() == PluginAutomation) {
361 Evoral::Parameter param(*i);
363 ParameterDescriptor desc;
364 _plugins.front()->get_parameter_descriptor(i->id(), desc);
366 can_automate (param);
367 boost::shared_ptr<AutomationList> list(new AutomationList(param, desc));
368 boost::shared_ptr<AutomationControl> c (new PluginControl(this, param, desc, list));
370 _plugins.front()->set_automation_control (i->id(), c);
371 } else if (i->type() == PluginPropertyAutomation) {
372 Evoral::Parameter param(*i);
373 const ParameterDescriptor& desc = _plugins.front()->get_property_descriptor(param.id());
374 if (desc.datatype != Variant::NOTHING) {
375 boost::shared_ptr<AutomationList> list;
376 if (Variant::type_is_numeric(desc.datatype)) {
377 list = boost::shared_ptr<AutomationList>(new AutomationList(param, desc));
379 add_control (boost::shared_ptr<AutomationControl> (new PluginPropertyControl(this, param, desc, list)));
384 /** Called when something outside of this host has modified a plugin
385 * parameter. Responsible for propagating the change to two places:
387 * 1) anything listening to the Control itself
388 * 2) any replicated plugins that make up this PluginInsert.
390 * The PluginInsert is connected to the ParameterChangedExternally signal for
391 * the first (primary) plugin, and here broadcasts that change to any others.
393 * XXX We should probably drop this whole replication idea (Paul, October 2015)
394 * since it isn't used by sensible plugin APIs (AU, LV2).
397 PluginInsert::parameter_changed_externally (uint32_t which, float val)
399 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, which));
401 /* First propagation: alter the underlying value of the control,
402 * without telling the plugin(s) that own/use it to set it.
409 boost::shared_ptr<PluginControl> pc = boost::dynamic_pointer_cast<PluginControl> (ac);
412 pc->catch_up_with_external_value (val);
415 /* Second propagation: tell all plugins except the first to
416 update the value of this parameter. For sane plugin APIs,
417 there are no other plugins, so this is a no-op in those
421 Plugins::iterator i = _plugins.begin();
423 /* don't set the first plugin, just all the slaves */
425 if (i != _plugins.end()) {
427 for (; i != _plugins.end(); ++i) {
428 (*i)->set_parameter (which, val);
434 PluginInsert::set_block_size (pframes_t nframes)
437 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
438 if ((*i)->set_block_size (nframes) != 0) {
446 PluginInsert::activate ()
448 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
452 Processor::activate ();
456 PluginInsert::deactivate ()
458 Processor::deactivate ();
460 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
466 PluginInsert::flush ()
468 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
474 PluginInsert::connect_and_run (BufferSet& bufs, pframes_t nframes, framecnt_t offset, bool with_auto, framepos_t now)
476 // TODO: atomically copy maps & _no_inplace
477 PinMappings in_map (_in_map);
478 PinMappings out_map (_out_map);
479 ChanMapping thru_map (_thru_map);
480 if (_mapping_changed) { // ToDo use a counters, increment until match.
481 _no_inplace = check_inplace ();
482 _mapping_changed = false;
485 if (_latency_changed) {
486 /* delaylines are configured with the max possible latency (as reported by the plugin)
487 * so this won't allocate memory (unless the plugin lied about its max latency)
488 * It may still 'click' though, since the fixed delaylines are not de-clicked.
489 * Then again plugin-latency changes are not click-free to begin with.
491 * This is also worst case, there is currently no concept of per-stream latency.
493 * e.g. Two identical latent plugins:
494 * 1st plugin: process left (latent), bypass right.
495 * 2nd plugin: bypass left, process right (latent).
496 * -> currently this yields 2 times latency of the plugin,
498 _latency_changed = false;
499 _delaybuffers.set (ChanCount::max(bufs.count(), _configured_out), plugin_latency ());
502 if (_match.method == Split && !_no_inplace) {
503 // TODO: also use this optimization if one source-buffer
504 // feeds _all_ *connected* inputs.
505 // currently this is *first* buffer to all only --
506 // see PluginInsert::check_inplace
507 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
508 if (_configured_internal.get (*t) == 0) {
512 uint32_t first_idx = in_map[0].get (*t, 0, &valid);
513 assert (valid && first_idx == 0); // check_inplace ensures this
514 /* copy the first stream's buffer contents to the others */
515 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
516 uint32_t idx = in_map[0].get (*t, i, &valid);
519 bufs.get (*t, i).read_from (bufs.get (*t, first_idx), nframes, offset, offset);
523 /* the copy operation produces a linear monotonic input map */
524 in_map[0] = ChanMapping (natural_input_streams ());
527 bufs.set_count(ChanCount::max(bufs.count(), _configured_internal));
528 bufs.set_count(ChanCount::max(bufs.count(), _configured_out));
534 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li, ++n) {
536 boost::shared_ptr<AutomationControl> c
537 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
539 if (c->list() && c->automation_playback()) {
542 const float val = c->list()->rt_safe_eval (now, valid);
545 /* This is the ONLY place where we are
547 * AutomationControl::set_value_unchecked(). We
548 * know that the control is in
549 * automation playback mode, so no
550 * check on writable() is required
551 * (which must be done in AutomationControl::set_value()
554 c->set_value_unchecked(val);
561 /* Calculate if, and how many frames we need to collect for analysis */
562 framecnt_t collect_signal_nframes = (_signal_analysis_collect_nframes_max -
563 _signal_analysis_collected_nframes);
564 if (nframes < collect_signal_nframes) { // we might not get all frames now
565 collect_signal_nframes = nframes;
568 if (collect_signal_nframes > 0) {
570 //std::cerr << "collect input, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
571 //std::cerr << " streams " << internal_input_streams().n_audio() << std::endl;
572 //std::cerr << "filling buffer with " << collect_signal_nframes << " frames at " << _signal_analysis_collected_nframes << std::endl;
574 _signal_analysis_inputs.set_count(internal_input_streams());
576 for (uint32_t i = 0; i < internal_input_streams().n_audio(); ++i) {
577 _signal_analysis_inputs.get_audio(i).read_from(
579 collect_signal_nframes,
580 _signal_analysis_collected_nframes); // offset is for target buffer
585 if (is_channelstrip ()) {
586 if (_configured_in.n_audio() > 0) {
587 ChanMapping mb_in_map (ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2)));
588 ChanMapping mb_out_map (ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2)));
590 _plugins.front()->connect_and_run (bufs, mb_in_map, mb_out_map, nframes, offset);
592 for (uint32_t out = _configured_in.n_audio (); out < bufs.count().get (DataType::AUDIO); ++out) {
593 bufs.get (DataType::AUDIO, out).silence (nframes, offset);
599 // TODO optimize -- build maps once.
601 BufferSet& inplace_bufs = _session.get_noinplace_buffers();
602 ARDOUR::ChanMapping used_outputs;
604 assert (inplace_bufs.count () >= natural_input_streams () + _configured_out);
606 /* build used-output map */
607 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
608 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
609 for (uint32_t out = 0; out < natural_output_streams().get (*t); ++out) {
611 uint32_t out_idx = out_map[pc].get (*t, out, &valid);
613 used_outputs.set (*t, out_idx, 1); // mark as used
618 /* copy thru data to outputs before processing in-place */
619 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
620 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
622 uint32_t in_idx = thru_map.get (*t, out, &valid);
624 uint32_t m = out + natural_input_streams ().get (*t);
625 _delaybuffers.delay (*t, out, inplace_bufs.get (*t, m), bufs.get (*t, in_idx), nframes, offset, offset);
626 used_outputs.set (*t, out, 1); // mark as used
632 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
634 ARDOUR::ChanMapping i_in_map (natural_input_streams());
635 ARDOUR::ChanMapping i_out_map (out_map[pc]);
636 ARDOUR::ChanCount mapped;
638 /* map inputs sequentially */
639 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
640 for (uint32_t in = 0; in < natural_input_streams().get (*t); ++in) {
642 uint32_t in_idx = in_map[pc].get (*t, in, &valid);
643 uint32_t m = mapped.get (*t);
645 inplace_bufs.get (*t, m).read_from (bufs.get (*t, in_idx), nframes, offset, offset);
647 inplace_bufs.get (*t, m).silence (nframes, offset);
649 mapped.set (*t, m + 1);
653 /* outputs are mapped to inplace_bufs after the inputs */
654 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
655 i_out_map.offset_to (*t, natural_input_streams ().get (*t));
658 if ((*i)->connect_and_run (inplace_bufs, i_in_map, i_out_map, nframes, offset)) {
663 /* all instances have completed, now copy data that was written
664 * and zero unconnected buffers */
665 ARDOUR::ChanMapping nonzero_out (used_outputs);
666 if (has_midi_bypass ()) {
667 nonzero_out.set (DataType::MIDI, 0, 1); // Midi bypass.
669 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
670 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
672 used_outputs.get (*t, out, &valid);
674 nonzero_out.get (*t, out, &valid);
676 bufs.get (*t, out).silence (nframes, offset);
679 uint32_t m = out + natural_input_streams ().get (*t);
680 bufs.get (*t, out).read_from (inplace_bufs.get (*t, m), nframes, offset, offset);
685 /* in-place processing */
687 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
688 if ((*i)->connect_and_run(bufs, in_map[pc], out_map[pc], nframes, offset)) {
693 // TODO optimize: store "unconnected" in a fixed set.
694 // it only changes on reconfiguration.
695 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
696 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
698 if (*t == DataType::MIDI && out == 0 && has_midi_bypass ()) {
699 mapped = true; // in-place Midi bypass
701 for (uint32_t pc = 0; pc < get_count() && !mapped; ++pc) {
702 for (uint32_t o = 0; o < natural_output_streams().get (*t); ++o) {
704 uint32_t idx = out_map[pc].get (*t, o, &valid);
705 if (valid && idx == out) {
712 bufs.get (*t, out).silence (nframes, offset);
718 if (collect_signal_nframes > 0) {
720 //std::cerr << " output, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
721 //std::cerr << " streams " << internal_output_streams().n_audio() << std::endl;
723 _signal_analysis_outputs.set_count(internal_output_streams());
725 for (uint32_t i = 0; i < internal_output_streams().n_audio(); ++i) {
726 _signal_analysis_outputs.get_audio(i).read_from(
728 collect_signal_nframes,
729 _signal_analysis_collected_nframes); // offset is for target buffer
732 _signal_analysis_collected_nframes += collect_signal_nframes;
733 assert(_signal_analysis_collected_nframes <= _signal_analysis_collect_nframes_max);
735 if (_signal_analysis_collected_nframes == _signal_analysis_collect_nframes_max) {
736 _signal_analysis_collect_nframes_max = 0;
737 _signal_analysis_collected_nframes = 0;
739 AnalysisDataGathered(&_signal_analysis_inputs,
740 &_signal_analysis_outputs);
746 PluginInsert::silence (framecnt_t nframes)
752 ChanMapping in_map (natural_input_streams ());
753 ChanMapping out_map (natural_output_streams ());
755 // TODO run sidechain (delaylines)
756 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
757 (*i)->connect_and_run (_session.get_scratch_buffers ((*i)->get_info()->n_inputs, true), in_map, out_map, nframes, 0);
762 PluginInsert::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
764 if (_pending_active) {
765 /* run as normal if we are active or moving from inactive to active */
768 // collect sidechain input for complete cycle (!)
769 // TODO we need delaylines here for latency compensation
770 _sidechain->run (bufs, start_frame, end_frame, nframes, true);
773 if (_session.transport_rolling() || _session.bounce_processing()) {
774 automation_run (bufs, start_frame, nframes);
776 connect_and_run (bufs, nframes, 0, false);
780 // TODO use mapping in bypassed mode ?!
781 // -> do we bypass the processor or the plugin
783 // TODO include sidechain??
785 uint32_t in = input_streams ().n_audio ();
786 uint32_t out = output_streams().n_audio ();
788 if (has_no_audio_inputs() || in == 0) {
790 /* silence all (audio) outputs. Should really declick
791 * at the transitions of "active"
794 for (uint32_t n = 0; n < out; ++n) {
795 bufs.get_audio (n).silence (nframes);
798 } else if (out > in) {
800 /* not active, but something has make up for any channel count increase
801 * for now , simply replicate last buffer
803 for (uint32_t n = in; n < out; ++n) {
804 bufs.get_audio(n).read_from(bufs.get_audio(in - 1), nframes);
808 bufs.count().set_audio (out);
811 _active = _pending_active;
813 /* we have no idea whether the plugin generated silence or not, so mark
814 * all buffers appropriately.
819 PluginInsert::automation_run (BufferSet& bufs, framepos_t start, pframes_t nframes)
821 Evoral::ControlEvent next_event (0, 0.0f);
822 framepos_t now = start;
823 framepos_t end = now + nframes;
824 framecnt_t offset = 0;
826 Glib::Threads::Mutex::Lock lm (control_lock(), Glib::Threads::TRY_LOCK);
829 connect_and_run (bufs, nframes, offset, false);
833 if (!find_next_event (now, end, next_event) || _plugins.front()->requires_fixed_sized_buffers()) {
835 /* no events have a time within the relevant range */
837 connect_and_run (bufs, nframes, offset, true, now);
843 framecnt_t cnt = min (((framecnt_t) ceil (next_event.when) - now), (framecnt_t) nframes);
845 connect_and_run (bufs, cnt, offset, true, now);
851 if (!find_next_event (now, end, next_event)) {
856 /* cleanup anything that is left to do */
859 connect_and_run (bufs, nframes, offset, true, now);
864 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
866 if (param.type() != PluginAutomation)
869 if (_plugins.empty()) {
870 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
872 abort(); /*NOTREACHED*/
875 return _plugins[0]->default_value (param.id());
880 PluginInsert::can_reset_all_parameters ()
884 for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
886 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
888 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
892 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
898 if (ac->automation_state() & Play) {
903 return all && (params > 0);
907 PluginInsert::reset_parameters_to_default ()
911 for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
913 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
915 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
919 const float dflt = _plugins[0]->default_value (cid);
920 const float curr = _plugins[0]->get_parameter (cid);
926 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
931 if (ac->automation_state() & Play) {
936 ac->set_value (dflt, Controllable::NoGroup);
941 boost::shared_ptr<Plugin>
942 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
944 boost::shared_ptr<LadspaPlugin> lp;
945 boost::shared_ptr<LuaProc> lua;
947 boost::shared_ptr<LV2Plugin> lv2p;
949 #ifdef WINDOWS_VST_SUPPORT
950 boost::shared_ptr<WindowsVSTPlugin> vp;
953 boost::shared_ptr<LXVSTPlugin> lxvp;
955 #ifdef AUDIOUNIT_SUPPORT
956 boost::shared_ptr<AUPlugin> ap;
959 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
960 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
961 } else if ((lua = boost::dynamic_pointer_cast<LuaProc> (other)) != 0) {
962 return boost::shared_ptr<Plugin> (new LuaProc (*lua));
964 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
965 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
967 #ifdef WINDOWS_VST_SUPPORT
968 } else if ((vp = boost::dynamic_pointer_cast<WindowsVSTPlugin> (other)) != 0) {
969 return boost::shared_ptr<Plugin> (new WindowsVSTPlugin (*vp));
972 } else if ((lxvp = boost::dynamic_pointer_cast<LXVSTPlugin> (other)) != 0) {
973 return boost::shared_ptr<Plugin> (new LXVSTPlugin (*lxvp));
975 #ifdef AUDIOUNIT_SUPPORT
976 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
977 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
981 fatal << string_compose (_("programming error: %1"),
982 X_("unknown plugin type in PluginInsert::plugin_factory"))
984 abort(); /*NOTREACHED*/
985 return boost::shared_ptr<Plugin> ((Plugin*) 0);
989 PluginInsert::set_input_map (uint32_t num, ChanMapping m) {
990 if (num < _in_map.size()) {
991 bool changed = _in_map[num] != m;
993 changed |= sanitize_maps ();
995 PluginMapChanged (); /* EMIT SIGNAL */
996 _mapping_changed = true;
997 _session.set_dirty();
1003 PluginInsert::set_output_map (uint32_t num, ChanMapping m) {
1004 if (num < _out_map.size()) {
1005 bool changed = _out_map[num] != m;
1007 changed |= sanitize_maps ();
1009 PluginMapChanged (); /* EMIT SIGNAL */
1010 _mapping_changed = true;
1011 _session.set_dirty();
1017 PluginInsert::set_thru_map (ChanMapping m) {
1018 bool changed = _thru_map != m;
1020 changed |= sanitize_maps ();
1022 PluginMapChanged (); /* EMIT SIGNAL */
1023 _mapping_changed = true;
1024 _session.set_dirty();
1029 PluginInsert::input_map () const
1033 for (PinMappings::const_iterator i = _in_map.begin (); i != _in_map.end (); ++i, ++pc) {
1034 ChanMapping m (i->second);
1035 const ChanMapping::Mappings& mp ((*i).second.mappings());
1036 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1037 for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1038 rv.set (tm->first, i->first + pc * natural_input_streams().get(tm->first), i->second);
1046 PluginInsert::output_map () const
1050 for (PinMappings::const_iterator i = _out_map.begin (); i != _out_map.end (); ++i, ++pc) {
1051 ChanMapping m (i->second);
1052 const ChanMapping::Mappings& mp ((*i).second.mappings());
1053 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1054 for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1055 rv.set (tm->first, i->first + pc * natural_output_streams().get(tm->first), i->second);
1059 if (has_midi_bypass ()) {
1060 rv.set (DataType::MIDI, 0, 0);
1067 PluginInsert::has_midi_bypass () const
1069 if (_configured_in.n_midi () == 1 && _configured_out.n_midi () == 1
1070 && natural_output_streams ().n_midi () == 0) {
1077 PluginInsert::has_midi_thru () const
1079 if (_configured_in.n_midi () == 1 && _configured_out.n_midi () == 1
1080 && natural_input_streams ().n_midi () == 0 && natural_output_streams ().n_midi () == 0) {
1088 PluginInsert::is_channelstrip () const {
1089 return _plugins.front()->is_channelstrip();
1094 PluginInsert::check_inplace ()
1096 bool inplace_ok = !_plugins.front()->inplace_broken ();
1098 if (_thru_map.n_total () > 0) {
1099 // TODO once midi-bypass is part of the mapping, ignore it
1103 if (_match.method == Split && inplace_ok) {
1104 assert (get_count() == 1);
1105 assert (_in_map.size () == 1);
1106 if (!_out_map[0].is_monotonic ()) {
1109 if (_configured_internal != _configured_in) {
1110 /* no sidechain -- TODO we could allow this with
1111 * some more logic in PluginInsert::connect_and_run().
1113 * PluginInsert::reset_map() already maps it.
1118 for (DataType::iterator t = DataType::begin(); t != DataType::end() && inplace_ok; ++t) {
1119 if (_configured_internal.get (*t) == 0) {
1123 uint32_t first_idx = _in_map[0].get (*t, 0, &valid);
1124 if (!valid || first_idx != 0) {
1125 // so far only allow to copy the *first* stream's buffer to others
1128 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
1129 uint32_t idx = _in_map[0].get (*t, i, &valid);
1130 if (valid && idx != first_idx) {
1139 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: In Place Split Map\n", name()));
1144 for (uint32_t pc = 0; pc < get_count() && inplace_ok ; ++pc) {
1145 if (!_in_map[pc].is_monotonic ()) {
1148 if (!_out_map[pc].is_monotonic ()) {
1152 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: %2\n", name(), inplace_ok ? "In-Place" : "No Inplace Processing"));
1153 return !inplace_ok; // no-inplace
1157 PluginInsert::sanitize_maps ()
1159 bool changed = false;
1160 /* strip dead wood */
1161 PinMappings new_ins;
1162 PinMappings new_outs;
1163 ChanMapping new_thru (_thru_map);
1165 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1167 ChanMapping new_out;
1168 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1169 for (uint32_t i = 0; i < natural_input_streams().get (*t); ++i) {
1171 uint32_t idx = _in_map[pc].get (*t, i, &valid);
1172 if (valid && idx < _configured_internal.get (*t)) {
1173 new_in.set (*t, i, idx);
1176 for (uint32_t o = 0; o < natural_output_streams().get (*t); ++o) {
1178 uint32_t idx = _out_map[pc].get (*t, o, &valid);
1179 if (valid && idx < _configured_out.get (*t)) {
1180 new_out.set (*t, o, idx);
1184 if (_in_map[pc] != new_in || _out_map[pc] != new_out) {
1187 new_ins[pc] = new_in;
1188 new_outs[pc] = new_out;
1191 /* prevent dup output assignments */
1192 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1193 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1194 bool mapped = false;
1195 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1197 uint32_t idx = new_outs[pc].get_src (*t, o, &valid);
1198 if (valid && mapped) {
1199 new_outs[pc].unset (*t, idx);
1207 /* prevent out + thru, existing plugin outputs override thru */
1208 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1209 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1210 bool mapped = false;
1212 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1213 new_outs[pc].get_src (*t, o, &mapped);
1214 if (mapped) { break; }
1216 if (!mapped) { continue; }
1217 uint32_t idx = new_thru.get (*t, o, &valid);
1219 new_thru.unset (*t, idx);
1224 if (has_midi_bypass ()) {
1225 // TODO: include midi-bypass in the thru set,
1226 // remove dedicated handling.
1227 new_thru.unset (DataType::MIDI, 0);
1230 if (_in_map != new_ins || _out_map != new_outs || _thru_map != new_thru) {
1234 _out_map = new_outs;
1235 _thru_map = new_thru;
1241 PluginInsert::reset_map (bool emit)
1244 const PinMappings old_in (_in_map);
1245 const PinMappings old_out (_out_map);
1249 _thru_map = ChanMapping ();
1250 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1251 ChanCount ns_inputs = natural_input_streams() - sidechain_input_pins ();
1252 if (_match.method == Split) {
1253 _in_map[pc] = ChanMapping ();
1254 /* connect no sidechain sinks in round-robin fashion */
1255 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1256 const uint32_t cend = _configured_in.get (*t);
1257 if (cend == 0) { continue; }
1259 for (uint32_t in = 0; in < ns_inputs.get (*t); ++in) {
1260 _in_map[pc].set (*t, in, c);
1265 _in_map[pc] = ChanMapping (ChanCount::min (ns_inputs, _configured_in));
1267 _out_map[pc] = ChanMapping (ChanCount::min (natural_output_streams(), _configured_out));
1269 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1270 const uint32_t nis = natural_input_streams ().get(*t);
1271 const uint32_t stride = nis - sidechain_input_pins().get (*t);
1272 _in_map[pc].offset_to(*t, stride * pc);
1273 _out_map[pc].offset_to(*t, pc * natural_output_streams().get(*t));
1275 // connect side-chains
1276 const uint32_t sc_start = _configured_in.get (*t);
1277 const uint32_t sc_len = _configured_internal.get (*t) - sc_start;
1278 if (sc_len == 0) { continue; }
1280 for (uint32_t in = ns_inputs.get (*t); in < nis; ++in) {
1281 _in_map[pc].set (*t, in, sc_start + c);
1282 c = (c + 1) % sc_len;
1287 if (old_in == _in_map && old_out == _out_map) {
1291 PluginMapChanged (); /* EMIT SIGNAL */
1292 _mapping_changed = true;
1293 _session.set_dirty();
1299 PluginInsert::configure_io (ChanCount in, ChanCount out)
1301 Match old_match = _match;
1303 ChanCount old_internal;
1307 old_in = _configured_in;
1308 old_internal = _configured_internal;
1309 old_out = _configured_out;
1312 _configured_in = in;
1313 _configured_internal = in;
1314 _configured_out = out;
1317 /* TODO hide midi-bypass, and custom outs. Best /fake/ "out" here.
1318 * (currently _sidechain->configure_io always succeeds
1319 * since Processor::configure_io() succeeds)
1321 if (!_sidechain->configure_io (in, out)) {
1322 DEBUG_TRACE (DEBUG::ChanMapping, "Sidechain configuration failed\n");
1325 _configured_internal += _sidechain->input()->n_ports();
1328 /* get plugin configuration */
1329 _match = private_can_support_io_configuration (in, out);
1331 if (DEBUG_ENABLED(DEBUG::ChanMapping)) {
1333 DEBUG_STR_APPEND(a, string_compose ("Match '%1': ", name()));
1334 DEBUG_STR_APPEND(a, _match);
1335 DEBUG_TRACE (DEBUG::ChanMapping, DEBUG_STR(a).str());
1339 /* set the matching method and number of plugins that we will use to meet this configuration */
1340 if (set_count (_match.plugins) == false) {
1341 PluginIoReConfigure (); /* EMIT SIGNAL */
1342 _configured = false;
1346 /* configure plugins */
1347 switch (_match.method) {
1350 if (_plugins.front()->configure_io (natural_input_streams(), out) == false) {
1351 PluginIoReConfigure (); /* EMIT SIGNAL */
1352 _configured = false;
1360 bool const r = _plugins.front()->can_support_io_configuration (in, dout, &useins);
1362 assert (_match.strict_io || dout.n_audio() == out.n_audio()); // sans midi bypass
1363 if (useins.n_audio() == 0) {
1366 if (_plugins.front()->configure_io (useins, dout) == false) {
1367 PluginIoReConfigure (); /* EMIT SIGNAL */
1368 _configured = false;
1374 if (_plugins.front()->configure_io (in, out) == false) {
1375 PluginIoReConfigure (); /* EMIT SIGNAL */
1376 _configured = false;
1382 bool mapping_changed = false;
1383 if (old_in == in && old_out == out
1385 && old_match.method == _match.method
1386 && _in_map.size() == _out_map.size()
1387 && _in_map.size() == get_count ()
1389 /* If the configuration has not changed, keep the mapping */
1390 if (old_internal != _configured_internal) {
1391 mapping_changed = sanitize_maps ();
1393 } else if (_match.custom_cfg && _configured) {
1394 mapping_changed = sanitize_maps ();
1397 if (is_channelstrip ()) { _maps_from_state = false; }
1399 if (_maps_from_state) {
1400 _maps_from_state = false;
1401 mapping_changed = true;
1404 /* generate a new mapping */
1405 mapping_changed = reset_map (false);
1409 if (mapping_changed) {
1410 PluginMapChanged (); /* EMIT SIGNAL */
1413 if (DEBUG_ENABLED(DEBUG::ChanMapping)) {
1416 DEBUG_STR_APPEND(a, "\n--------<<--------\n");
1417 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1419 DEBUG_STR_APPEND(a, "----><----\n");
1421 DEBUG_STR_APPEND(a, string_compose ("Channel Map for %1 plugin %2\n", name(), pc));
1422 DEBUG_STR_APPEND(a, " * Inputs:\n");
1423 DEBUG_STR_APPEND(a, _in_map[pc]);
1424 DEBUG_STR_APPEND(a, " * Outputs:\n");
1425 DEBUG_STR_APPEND(a, _out_map[pc]);
1427 DEBUG_STR_APPEND(a, " * Thru:\n");
1428 DEBUG_STR_APPEND(a, _thru_map);
1429 DEBUG_STR_APPEND(a, "-------->>--------\n");
1430 DEBUG_TRACE (DEBUG::ChanMapping, DEBUG_STR(a).str());
1435 _no_inplace = check_inplace ();
1436 _mapping_changed = false;
1438 if (old_in != in || old_out != out || old_internal != _configured_internal
1439 || (old_match.method != _match.method && (old_match.method == Split || _match.method == Split))
1441 PluginIoReConfigure (); /* EMIT SIGNAL */
1444 _delaybuffers.configure (_configured_out, _plugins.front()->max_latency ());
1445 _latency_changed = true;
1447 // we don't know the analysis window size, so we must work with the
1448 // current buffer size here. each request for data fills in these
1449 // buffers and the analyser makes sure it gets enough data for the
1451 session().ensure_buffer_set (_signal_analysis_inputs, in);
1452 //_signal_analysis_inputs.set_count (in);
1454 session().ensure_buffer_set (_signal_analysis_outputs, out);
1455 //_signal_analysis_outputs.set_count (out);
1457 // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
1460 return Processor::configure_io (in, out);
1463 /** Decide whether this PluginInsert can support a given IO configuration.
1464 * To do this, we run through a set of possible solutions in rough order of
1467 * @param in Required input channel count.
1468 * @param out Filled in with the output channel count if we return true.
1469 * @return true if the given IO configuration can be supported.
1472 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out)
1475 _sidechain->can_support_io_configuration (in, out); // never fails, sets "out"
1477 return private_can_support_io_configuration (in, out).method != Impossible;
1480 /** A private version of can_support_io_configuration which returns the method
1481 * by which the configuration can be matched, rather than just whether or not
1485 PluginInsert::private_can_support_io_configuration (ChanCount const & inx, ChanCount& out) const
1487 if (_plugins.empty()) {
1492 if (is_channelstrip ()) {
1494 return Match (ExactMatch, 1);
1498 /* if a user specified a custom cfg, so be it. */
1501 return Match (ExactMatch, get_count(), _strict_io, true); // XXX
1504 /* try automatic configuration */
1505 Match m = PluginInsert::automatic_can_support_io_configuration (inx, out);
1507 PluginInfoPtr info = _plugins.front()->get_info();
1508 ChanCount inputs = info->n_inputs;
1509 ChanCount outputs = info->n_outputs;
1511 /* handle case strict-i/o */
1512 if (_strict_io && m.method != Impossible) {
1515 /* special case MIDI instruments */
1516 if (is_midi_instrument()) {
1517 // output = midi-bypass + at most master-out channels.
1518 ChanCount max_out (DataType::AUDIO, 2); // TODO use master-out
1519 max_out.set (DataType::MIDI, out.get(DataType::MIDI));
1520 out = ChanCount::min (out, max_out);
1521 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("special case strict-i/o instrument: %1\n", name()));
1527 if (inx.n_audio () != out.n_audio ()) { // ignore midi bypass
1528 /* replicate processor to match output count (generators and such)
1529 * at least enough to feed every output port. */
1530 uint32_t f = 1; // at least one. e.g. control data filters, no in, no out.
1531 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1532 uint32_t nout = outputs.get (*t);
1533 if (nout == 0 || inx.get(*t) == 0) { continue; }
1534 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nout));
1537 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("special case strict-i/o generator: %1\n", name()));
1538 return Match (Replicate, f, _strict_io);
1549 if (m.method != Impossible) {
1553 ChanCount ns_inputs = inputs - sidechain_input_pins ();
1555 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("resolving 'Impossible' match for %1\n", name()));
1557 if (info->reconfigurable_io()) {
1559 // TODO add sidechains here
1560 bool const r = _plugins.front()->can_support_io_configuration (inx, out, &useins);
1562 // houston, we have a problem.
1563 return Match (Impossible, 0);
1565 return Match (Delegate, 1, _strict_io);
1568 ChanCount midi_bypass;
1569 if (inx.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
1570 midi_bypass.set (DataType::MIDI, 1);
1573 // add at least as many plugins so that output count matches input count (w/o sidechain pins)
1575 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1576 uint32_t nin = ns_inputs.get (*t);
1577 uint32_t nout = outputs.get (*t);
1578 if (nin == 0 || inx.get(*t) == 0) { continue; }
1579 // prefer floor() so the count won't overly increase IFF (nin < nout)
1580 f = max (f, (uint32_t) floor (inx.get(*t) / (float)nout));
1582 if (f > 0 && outputs * f >= _configured_out) {
1583 out = outputs * f + midi_bypass;
1584 return Match (Replicate, f, _strict_io);
1587 // add at least as many plugins needed to connect all inputs (w/o sidechain pins)
1589 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1590 uint32_t nin = ns_inputs.get (*t);
1591 if (nin == 0 || inx.get(*t) == 0) { continue; }
1592 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nin));
1595 out = outputs * f + midi_bypass;
1596 return Match (Replicate, f, _strict_io);
1599 // add at least as many plugins needed to connect all inputs
1601 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1602 uint32_t nin = inputs.get (*t);
1603 if (nin == 0 || inx.get(*t) == 0) { continue; }
1604 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nin));
1606 out = outputs * f + midi_bypass;
1607 return Match (Replicate, f, _strict_io);
1610 /* this is the original Ardour 3/4 behavior, mainly for backwards compatibility */
1612 PluginInsert::automatic_can_support_io_configuration (ChanCount const & inx, ChanCount& out) const
1614 if (_plugins.empty()) {
1618 PluginInfoPtr info = _plugins.front()->get_info();
1619 ChanCount in; in += inx;
1620 ChanCount midi_bypass;
1622 if (info->reconfigurable_io()) {
1623 /* Plugin has flexible I/O, so delegate to it */
1624 bool const r = _plugins.front()->can_support_io_configuration (in, out);
1626 return Match (Impossible, 0);
1628 return Match (Delegate, 1);
1631 ChanCount inputs = info->n_inputs;
1632 ChanCount outputs = info->n_outputs;
1633 ChanCount ns_inputs = inputs - sidechain_input_pins ();
1635 if (in.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
1636 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("bypassing midi-data around %1\n", name()));
1637 midi_bypass.set (DataType::MIDI, 1);
1639 if (in.get(DataType::MIDI) == 1 && inputs.get(DataType::MIDI) == 0) {
1640 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("hiding midi-port from plugin %1\n", name()));
1641 in.set(DataType::MIDI, 0);
1644 // add internally provided sidechain ports
1645 ChanCount insc = in + sidechain_input_ports ();
1647 bool no_inputs = true;
1648 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1649 if (inputs.get (*t) != 0) {
1656 /* no inputs so we can take any input configuration since we throw it away */
1657 out = outputs + midi_bypass;
1658 return Match (NoInputs, 1);
1661 /* Plugin inputs match requested inputs + side-chain-ports exactly */
1662 if (inputs == insc) {
1663 out = outputs + midi_bypass;
1664 return Match (ExactMatch, 1);
1667 /* Plugin inputs matches without side-chain-pins */
1668 if (ns_inputs == in) {
1669 out = outputs + midi_bypass;
1670 return Match (ExactMatch, 1);
1673 /* We may be able to run more than one copy of the plugin within this insert
1674 to cope with the insert having more inputs than the plugin.
1675 We allow replication only for plugins with either zero or 1 inputs and outputs
1676 for every valid data type.
1680 bool can_replicate = true;
1681 for (DataType::iterator t = DataType::begin(); t != DataType::end() && can_replicate; ++t) {
1683 // ignore side-chains
1684 uint32_t nin = ns_inputs.get (*t);
1686 // No inputs of this type
1687 if (nin == 0 && in.get(*t) == 0) {
1691 if (nin != 1 || outputs.get (*t) != 1) {
1692 can_replicate = false;
1696 // Potential factor not set yet
1698 f = in.get(*t) / nin;
1701 // Factor for this type does not match another type, can not replicate
1702 if (f != (in.get(*t) / nin)) {
1703 can_replicate = false;
1708 if (can_replicate && f > 0) {
1709 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1710 out.set (*t, outputs.get(*t) * f);
1713 return Match (Replicate, f);
1716 /* If the processor has exactly one input of a given type, and
1717 the plugin has more, we can feed the single processor input
1718 to some or all of the plugin inputs. This is rather
1719 special-case-y, but the 1-to-many case is by far the
1720 simplest. How do I split thy 2 processor inputs to 3
1721 plugin inputs? Let me count the ways ...
1724 bool can_split = true;
1725 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1727 bool const can_split_type = (in.get (*t) == 1 && ns_inputs.get (*t) > 1);
1728 bool const nothing_to_do_for_type = (in.get (*t) == 0 && inputs.get (*t) == 0);
1730 if (!can_split_type && !nothing_to_do_for_type) {
1736 out = outputs + midi_bypass;
1737 return Match (Split, 1);
1740 /* If the plugin has more inputs than we want, we can `hide' some of them
1741 by feeding them silence.
1744 bool could_hide = false;
1745 bool cannot_hide = false;
1746 ChanCount hide_channels;
1748 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1749 if (inputs.get(*t) > in.get(*t)) {
1750 /* there is potential to hide, since the plugin has more inputs of type t than the insert */
1751 hide_channels.set (*t, inputs.get(*t) - in.get(*t));
1753 } else if (inputs.get(*t) < in.get(*t)) {
1754 /* we definitely cannot hide, since the plugin has fewer inputs of type t than the insert */
1759 if (could_hide && !cannot_hide) {
1760 out = outputs + midi_bypass;
1761 return Match (Hide, 1, false, false, hide_channels);
1764 return Match (Impossible, 0);
1769 PluginInsert::get_state ()
1771 return state (true);
1775 PluginInsert::state (bool full)
1777 XMLNode& node = Processor::state (full);
1779 node.add_property("type", _plugins[0]->state_node_name());
1780 node.add_property("unique-id", _plugins[0]->unique_id());
1781 node.add_property("count", string_compose("%1", _plugins.size()));
1783 /* remember actual i/o configuration (for later placeholder
1784 * in case the plugin goes missing) */
1785 node.add_child_nocopy (* _configured_in.state (X_("ConfiguredInput")));
1786 node.add_child_nocopy (* _configured_out.state (X_("ConfiguredOutput")));
1788 /* save custom i/o config */
1789 node.add_property("custom", _custom_cfg ? "yes" : "no");
1790 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1792 snprintf (tmp, sizeof(tmp), "InputMap-%d", pc);
1793 node.add_child_nocopy (* _in_map[pc].state (tmp));
1794 snprintf (tmp, sizeof(tmp), "OutputMap-%d", pc);
1795 node.add_child_nocopy (* _out_map[pc].state (tmp));
1797 node.add_child_nocopy (* _thru_map.state ("ThruMap"));
1800 node.add_child_nocopy (_sidechain->state (full));
1803 _plugins[0]->set_insert_id(this->id());
1804 node.add_child_nocopy (_plugins[0]->get_state());
1806 for (Controls::iterator c = controls().begin(); c != controls().end(); ++c) {
1807 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> ((*c).second);
1809 node.add_child_nocopy (ac->get_state());
1817 PluginInsert::set_control_ids (const XMLNode& node, int version)
1819 const XMLNodeList& nlist = node.children();
1820 XMLNodeConstIterator iter;
1821 set<Evoral::Parameter>::const_iterator p;
1823 for (iter = nlist.begin(); iter != nlist.end(); ++iter) {
1824 if ((*iter)->name() == Controllable::xml_node_name) {
1825 const XMLProperty* prop;
1827 uint32_t p = (uint32_t)-1;
1829 if ((prop = (*iter)->property (X_("symbol"))) != 0) {
1830 boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugins[0]);
1832 p = lv2plugin->port_index(prop->value().c_str());
1836 if (p == (uint32_t)-1 && (prop = (*iter)->property (X_("parameter"))) != 0) {
1837 p = atoi (prop->value());
1840 if (p != (uint32_t)-1) {
1842 /* this may create the new controllable */
1844 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p));
1846 #ifndef NO_PLUGIN_STATE
1850 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
1852 ac->set_state (**iter, version);
1861 PluginInsert::set_state(const XMLNode& node, int version)
1863 XMLNodeList nlist = node.children();
1864 XMLNodeIterator niter;
1865 XMLPropertyList plist;
1866 const XMLProperty *prop;
1867 ARDOUR::PluginType type;
1869 if ((prop = node.property ("type")) == 0) {
1870 error << _("XML node describing plugin is missing the `type' field") << endmsg;
1874 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
1875 type = ARDOUR::LADSPA;
1876 } else if (prop->value() == X_("lv2")) {
1878 } else if (prop->value() == X_("windows-vst")) {
1879 type = ARDOUR::Windows_VST;
1880 } else if (prop->value() == X_("lxvst")) {
1881 type = ARDOUR::LXVST;
1882 } else if (prop->value() == X_("audiounit")) {
1883 type = ARDOUR::AudioUnit;
1884 } else if (prop->value() == X_("luaproc")) {
1887 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
1893 prop = node.property ("unique-id");
1896 #ifdef WINDOWS_VST_SUPPORT
1897 /* older sessions contain VST plugins with only an "id" field.
1900 if (type == ARDOUR::Windows_VST) {
1901 prop = node.property ("id");
1905 #ifdef LXVST_SUPPORT
1906 /*There shouldn't be any older sessions with linuxVST support.. but anyway..*/
1908 if (type == ARDOUR::LXVST) {
1909 prop = node.property ("id");
1915 error << _("Plugin has no unique ID field") << endmsg;
1920 boost::shared_ptr<Plugin> plugin = find_plugin (_session, prop->value(), type);
1922 /* treat linux and windows VST plugins equivalent if they have the same uniqueID
1923 * allow to move sessions windows <> linux */
1924 #ifdef LXVST_SUPPORT
1925 if (plugin == 0 && type == ARDOUR::Windows_VST) {
1926 type = ARDOUR::LXVST;
1927 plugin = find_plugin (_session, prop->value(), type);
1931 #ifdef WINDOWS_VST_SUPPORT
1932 if (plugin == 0 && type == ARDOUR::LXVST) {
1933 type = ARDOUR::Windows_VST;
1934 plugin = find_plugin (_session, prop->value(), type);
1939 error << string_compose(
1940 _("Found a reference to a plugin (\"%1\") that is unknown.\n"
1941 "Perhaps it was removed or moved since it was last used."),
1947 if (type == ARDOUR::Lua) {
1948 XMLNode *ls = node.child (plugin->state_node_name().c_str());
1949 // we need to load the script to set the name and parameters.
1950 boost::shared_ptr<LuaProc> lp = boost::dynamic_pointer_cast<LuaProc>(plugin);
1952 lp->set_script_from_state (*ls);
1956 // The name of the PluginInsert comes from the plugin, nothing else
1957 _name = plugin->get_info()->name;
1961 // Processor::set_state() will set this, but too late
1962 // for it to be available when setting up plugin
1963 // state. We can't call Processor::set_state() until
1964 // the plugins themselves are created and added.
1968 if (_plugins.empty()) {
1969 /* if we are adding the first plugin, we will need to set
1970 up automatable controls.
1972 add_plugin (plugin);
1973 create_automatable_parameters ();
1974 set_control_ids (node, version);
1977 if ((prop = node.property ("count")) != 0) {
1978 sscanf (prop->value().c_str(), "%u", &count);
1981 if (_plugins.size() != count) {
1982 for (uint32_t n = 1; n < count; ++n) {
1983 add_plugin (plugin_factory (plugin));
1987 Processor::set_state (node, version);
1989 PBD::ID new_id = this->id();
1990 PBD::ID old_id = this->id();
1992 if ((prop = node.property ("id")) != 0) {
1993 old_id = prop->value ();
1996 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1998 /* find the node with the type-specific node name ("lv2", "ladspa", etc)
1999 and set all plugins to the same state.
2002 if ((*niter)->name() == plugin->state_node_name()) {
2004 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2005 /* Plugin state can include external files which are named after the ID.
2007 * If regenerate_xml_or_string_ids() is set, the ID will already have
2008 * been changed, so we need to use the old ID from the XML to load the
2009 * state and then update the ID.
2011 * When copying a plugin-state, route_ui takes care of of updating the ID,
2012 * but we need to call set_insert_id() to clear the cached plugin-state
2013 * and force a change.
2015 if (!regenerate_xml_or_string_ids ()) {
2016 (*i)->set_insert_id (new_id);
2018 (*i)->set_insert_id (old_id);
2021 (*i)->set_state (**niter, version);
2023 if (regenerate_xml_or_string_ids ()) {
2024 (*i)->set_insert_id (new_id);
2032 if (version < 3000) {
2034 /* Only 2.X sessions need a call to set_parameter_state() - in 3.X and above
2035 this is all handled by Automatable
2038 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2039 if ((*niter)->name() == "Redirect") {
2040 /* XXX do we need to tackle placement? i think not (pd; oct 16 2009) */
2041 Processor::set_state (**niter, version);
2046 set_parameter_state_2X (node, version);
2049 if ((prop = node.property (X_("custom"))) != 0) {
2050 _custom_cfg = string_is_affirmative (prop->value());
2053 // TODO load/add sidechain
2055 uint32_t in_maps = 0;
2056 uint32_t out_maps = 0;
2057 XMLNodeList kids = node.children ();
2058 for (XMLNodeIterator i = kids.begin(); i != kids.end(); ++i) {
2059 if ((*i)->name() == X_("ConfiguredOutput")) {
2060 _custom_out = ChanCount(**i);
2062 if (strncmp ((*i)->name ().c_str(), X_("InputMap-"), 9) == 0) {
2063 long pc = atol (&((*i)->name().c_str()[9]));
2064 if (pc >=0 && pc <= get_count()) {
2065 _in_map[pc] = ChanMapping (**i);
2069 if (strncmp ((*i)->name ().c_str(), X_("OutputMap-"), 10) == 0) {
2070 long pc = atol (&((*i)->name().c_str()[10]));
2071 if (pc >=0 && pc <= get_count()) {
2072 _out_map[pc] = ChanMapping (**i);
2076 if ((*i)->name () == "ThruMap") {
2077 _thru_map = ChanMapping (**i);
2079 if ((*i)->name () == Processor::state_node_name) {
2083 _sidechain->set_state (**i, version);
2087 if (in_maps == out_maps && out_maps >0 && out_maps == get_count()) {
2088 _maps_from_state = true;
2091 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2095 (*i)->deactivate ();
2103 PluginInsert::update_id (PBD::ID id)
2106 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2107 (*i)->set_insert_id (id);
2112 PluginInsert::set_state_dir (const std::string& d)
2114 // state() only saves the state of the first plugin
2115 _plugins[0]->set_state_dir (d);
2119 PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
2121 XMLNodeList nlist = node.children();
2122 XMLNodeIterator niter;
2124 /* look for port automation node */
2126 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2128 if ((*niter)->name() != port_automation_node_name) {
2134 XMLNodeConstIterator iter;
2139 cnodes = (*niter)->children ("port");
2141 for (iter = cnodes.begin(); iter != cnodes.end(); ++iter){
2145 if ((cprop = child->property("number")) != 0) {
2146 port = cprop->value().c_str();
2148 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
2152 sscanf (port, "%" PRIu32, &port_id);
2154 if (port_id >= _plugins[0]->parameter_count()) {
2155 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
2159 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
2160 control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
2162 if (c && c->alist()) {
2163 if (!child->children().empty()) {
2164 c->alist()->set_state (*child->children().front(), version);
2166 /* In some cases 2.X saves lists with min_yval and max_yval
2167 being FLT_MIN and FLT_MAX respectively. This causes problems
2168 in A3 because these min/max values are used to compute
2169 where GUI control points should be drawn. If we see such
2170 values, `correct' them to the min/max of the appropriate
2174 float min_y = c->alist()->get_min_y ();
2175 float max_y = c->alist()->get_max_y ();
2177 ParameterDescriptor desc;
2178 _plugins.front()->get_parameter_descriptor (port_id, desc);
2180 if (min_y == FLT_MIN) {
2184 if (max_y == FLT_MAX) {
2188 c->alist()->set_yrange (min_y, max_y);
2191 error << string_compose (_("PluginInsert: automatable control %1 not found - ignored"), port_id) << endmsg;
2203 PluginInsert::describe_parameter (Evoral::Parameter param)
2205 if (param.type() == PluginAutomation) {
2206 return _plugins[0]->describe_parameter (param);
2207 } else if (param.type() == PluginPropertyAutomation) {
2208 boost::shared_ptr<AutomationControl> c(automation_control(param));
2209 if (c && !c->desc().label.empty()) {
2210 return c->desc().label;
2213 return Automatable::describe_parameter(param);
2217 PluginInsert::signal_latency() const
2219 if (_user_latency) {
2220 return _user_latency;
2223 return _plugins[0]->signal_latency ();
2227 PluginInsert::type ()
2229 return plugin()->get_info()->type;
2232 PluginInsert::PluginControl::PluginControl (PluginInsert* p,
2233 const Evoral::Parameter& param,
2234 const ParameterDescriptor& desc,
2235 boost::shared_ptr<AutomationList> list)
2236 : AutomationControl (p->session(), param, desc, list, p->describe_parameter(param))
2240 alist()->reset_default (desc.normal);
2242 list->set_interpolation(Evoral::ControlList::Discrete);
2247 set_flags(Controllable::Toggle);
2251 /** @param val `user' value */
2253 PluginInsert::PluginControl::set_value (double user_val, PBD::Controllable::GroupControlDisposition group_override)
2256 _set_value (user_val, group_override);
2260 PluginInsert::PluginControl::set_value_unchecked (double user_val)
2262 /* used only by automation playback */
2263 _set_value (user_val, Controllable::NoGroup);
2267 PluginInsert::PluginControl::_set_value (double user_val, PBD::Controllable::GroupControlDisposition group_override)
2269 /* FIXME: probably should be taking out some lock here.. */
2271 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
2272 (*i)->set_parameter (_list->parameter().id(), user_val);
2275 boost::shared_ptr<Plugin> iasp = _plugin->_impulseAnalysisPlugin.lock();
2277 iasp->set_parameter (_list->parameter().id(), user_val);
2280 AutomationControl::set_value (user_val, group_override);
2284 PluginInsert::PluginControl::catch_up_with_external_value (double user_val)
2286 AutomationControl::set_value (user_val, Controllable::NoGroup);
2290 PluginInsert::PluginControl::get_state ()
2294 XMLNode& node (AutomationControl::get_state());
2295 ss << parameter().id();
2296 node.add_property (X_("parameter"), ss.str());
2298 boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugin->_plugins[0]);
2300 node.add_property (X_("symbol"), lv2plugin->port_symbol (parameter().id()));
2307 /** @return `user' val */
2309 PluginInsert::PluginControl::get_value () const
2311 boost::shared_ptr<Plugin> plugin = _plugin->plugin (0);
2317 return plugin->get_parameter (_list->parameter().id());
2320 PluginInsert::PluginPropertyControl::PluginPropertyControl (PluginInsert* p,
2321 const Evoral::Parameter& param,
2322 const ParameterDescriptor& desc,
2323 boost::shared_ptr<AutomationList> list)
2324 : AutomationControl (p->session(), param, desc, list)
2328 alist()->set_yrange (desc.lower, desc.upper);
2329 alist()->reset_default (desc.normal);
2333 set_flags(Controllable::Toggle);
2338 PluginInsert::PluginPropertyControl::set_value (double user_val, PBD::Controllable::GroupControlDisposition /* group_override*/)
2341 set_value_unchecked (user_val);
2346 PluginInsert::PluginPropertyControl::set_value_unchecked (double user_val)
2348 /* Old numeric set_value(), coerce to appropriate datatype if possible.
2349 This is lossy, but better than nothing until Ardour's automation system
2350 can handle various datatypes all the way down. */
2351 const Variant value(_desc.datatype, user_val);
2352 if (value.type() == Variant::NOTHING) {
2353 error << "set_value(double) called for non-numeric property" << endmsg;
2357 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
2358 (*i)->set_property(_list->parameter().id(), value);
2362 AutomationControl::set_value (user_val, Controllable::NoGroup);
2366 PluginInsert::PluginPropertyControl::get_state ()
2370 XMLNode& node (AutomationControl::get_state());
2371 ss << parameter().id();
2372 node.add_property (X_("property"), ss.str());
2373 node.remove_property (X_("value"));
2379 PluginInsert::PluginPropertyControl::get_value () const
2381 return _value.to_double();
2384 boost::shared_ptr<Plugin>
2385 PluginInsert::get_impulse_analysis_plugin()
2387 boost::shared_ptr<Plugin> ret;
2388 if (_impulseAnalysisPlugin.expired()) {
2389 ret = plugin_factory(_plugins[0]);
2390 ret->configure_io (internal_input_streams (), internal_output_streams ());
2391 _impulseAnalysisPlugin = ret;
2393 ret = _impulseAnalysisPlugin.lock();
2400 PluginInsert::collect_signal_for_analysis (framecnt_t nframes)
2402 // called from outside the audio thread, so this should be safe
2403 // only do audio as analysis is (currently) only for audio plugins
2404 _signal_analysis_inputs.ensure_buffers( DataType::AUDIO, internal_input_streams().n_audio(), nframes);
2405 _signal_analysis_outputs.ensure_buffers( DataType::AUDIO, internal_output_streams().n_audio(), nframes);
2407 _signal_analysis_collected_nframes = 0;
2408 _signal_analysis_collect_nframes_max = nframes;
2411 /** Add a plugin to our list */
2413 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
2415 plugin->set_insert_id (this->id());
2417 if (_plugins.empty()) {
2418 /* first (and probably only) plugin instance - connect to relevant signals */
2420 plugin->ParameterChangedExternally.connect_same_thread (*this, boost::bind (&PluginInsert::parameter_changed_externally, this, _1, _2));
2421 plugin->StartTouch.connect_same_thread (*this, boost::bind (&PluginInsert::start_touch, this, _1));
2422 plugin->EndTouch.connect_same_thread (*this, boost::bind (&PluginInsert::end_touch, this, _1));
2423 plugin->LatencyChanged.connect_same_thread (*this, boost::bind (&PluginInsert::latency_changed, this, _1, _2));
2424 // cache sidechain ports
2425 _cached_sidechain_pins.reset ();
2426 const ChanCount& nis (plugin->get_info()->n_inputs);
2427 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2428 for (uint32_t in = 0; in < nis.get (*t); ++in) {
2429 const Plugin::IOPortDescription& iod (plugin->describe_io_port (*t, true, in));
2430 if (iod.is_sidechain) {
2431 _cached_sidechain_pins.set (*t, 1 + _cached_sidechain_pins.n(*t));
2436 _plugins.push_back (plugin);
2440 PluginInsert::realtime_handle_transport_stopped ()
2442 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2443 (*i)->realtime_handle_transport_stopped ();
2448 PluginInsert::realtime_locate ()
2450 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2451 (*i)->realtime_locate ();
2456 PluginInsert::monitoring_changed ()
2458 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2459 (*i)->monitoring_changed ();
2464 PluginInsert::latency_changed (framecnt_t, framecnt_t)
2466 // this is called in RT context, LatencyChanged is emitted after run()
2467 _latency_changed = true;
2471 PluginInsert::start_touch (uint32_t param_id)
2473 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
2475 ac->start_touch (session().audible_frame());
2480 PluginInsert::end_touch (uint32_t param_id)
2482 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
2484 ac->stop_touch (true, session().audible_frame());
2488 std::ostream& operator<<(std::ostream& o, const ARDOUR::PluginInsert::Match& m)
2491 case PluginInsert::Impossible: o << "Impossible"; break;
2492 case PluginInsert::Delegate: o << "Delegate"; break;
2493 case PluginInsert::NoInputs: o << "NoInputs"; break;
2494 case PluginInsert::ExactMatch: o << "ExactMatch"; break;
2495 case PluginInsert::Replicate: o << "Replicate"; break;
2496 case PluginInsert::Split: o << "Split"; break;
2497 case PluginInsert::Hide: o << "Hide"; break;
2499 o << " cnt: " << m.plugins
2500 << (m.strict_io ? " strict-io" : "")
2501 << (m.custom_cfg ? " custom-cfg" : "");
2502 if (m.method == PluginInsert::Hide) {
2503 o << " hide: " << m.hide;