2 * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2018 Len Ovens <len@ovenwerks.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "pbd/xml++.h"
28 #include "ardour/amp.h"
29 #include "ardour/boost_debug.h"
30 #include "ardour/buffer_set.h"
31 #include "ardour/debug.h"
32 #include "ardour/delayline.h"
33 #include "ardour/gain_control.h"
34 #include "ardour/io.h"
35 #include "ardour/meter.h"
36 #include "ardour/panner_shell.h"
37 #include "ardour/send.h"
38 #include "ardour/session.h"
43 class AutomationControl;
48 using namespace ARDOUR;
52 PBD::Signal0<void> LatentSend::ChangedLatency;
54 LatentSend::LatentSend ()
61 Send::name_and_id_new_send (Session& s, Role r, uint32_t& bitslot, bool ignore_bitslot)
64 /* this happens during initial construction of sends from XML,
65 before they get ::set_state() called. lets not worry about
74 return string_compose (_("aux %1"), (bitslot = s.next_aux_send_id ()) + 1);
75 case Delivery::Listen:
76 return _("listen"); // no ports, no need for numbering
78 return string_compose (_("send %1"), (bitslot = s.next_send_id ()) + 1);
79 case Delivery::Foldback:
80 return string_compose (_("foldback %1"), (bitslot = s.next_aux_send_id ()) + 1);
82 fatal << string_compose (_("programming error: send created using role %1"), enum_2_string (r)) << endmsg;
83 abort(); /*NOTREACHED*/
89 Send::Send (Session& s, boost::shared_ptr<Pannable> p, boost::shared_ptr<MuteMaster> mm, Role r, bool ignore_bitslot)
90 : Delivery (s, p, mm, name_and_id_new_send (s, r, _bitslot, ignore_bitslot), r)
92 , _remove_on_disconnect (false)
94 if (_role == Listen) {
95 /* we don't need to do this but it keeps things looking clean
96 in a debugger. _bitslot is not used by listen sends.
101 //boost_debug_shared_ptr_mark_interesting (this, "send");
103 boost::shared_ptr<AutomationList> gl (new AutomationList (Evoral::Parameter (GainAutomation)));
104 _gain_control = boost::shared_ptr<GainControl> (new GainControl (_session, Evoral::Parameter(GainAutomation), gl));
105 add_control (_gain_control);
107 _amp.reset (new Amp (_session, _("Fader"), _gain_control, true));
108 _meter.reset (new PeakMeter (_session, name()));
110 _send_delay.reset (new DelayLine (_session, "Send-" + name()));
111 _thru_delay.reset (new DelayLine (_session, "Thru-" + name()));
113 if (panner_shell()) {
114 panner_shell()->Changed.connect_same_thread (*this, boost::bind (&Send::panshell_changed, this));
117 _output->changed.connect_same_thread (*this, boost::bind (&Send::snd_output_changed, this, _1, _2));
123 _session.unmark_send_id (_bitslot);
132 Processor::activate ();
139 _meter->deactivate ();
142 Processor::deactivate ();
146 Send::signal_latency () const
148 if (!_pending_active) {
151 if (_delay_out > _delay_in) {
152 return _delay_out - _delay_in;
158 Send::update_delaylines ()
160 if (_role == Listen) {
161 /* Don't align monitor-listen (just yet).
162 * They're present on each route, may change positions
163 * and could potentially signficiantly increase worst-case
164 * Latency: In PFL mode all tracks/busses would additionally be
165 * aligned at PFL position.
167 * We should only align active monitor-sends when at least one is active.
173 if (_delay_out > _delay_in) {
174 changed = _thru_delay->set_delay(_delay_out - _delay_in);
175 _send_delay->set_delay(0);
177 changed = _thru_delay->set_delay(0);
178 _send_delay->set_delay(_delay_in - _delay_out);
182 // TODO -- ideally postpone for effective no-op changes
183 // (in case both _delay_out and _delay_in are changed by the
184 // same amount in a single latency-update cycle).
185 ChangedLatency (); /* EMIT SIGNAL */
190 Send::set_delay_in (samplecnt_t delay)
192 if (_delay_in == delay) {
197 DEBUG_TRACE (DEBUG::LatencyCompensation,
198 string_compose ("Send::set_delay_in %1: (%2) - %3 = %4\n",
199 name (), _delay_in, _delay_out, _delay_in - _delay_out));
201 update_delaylines ();
205 Send::set_delay_out (samplecnt_t delay, size_t /*bus*/)
207 if (_delay_out == delay) {
211 DEBUG_TRACE (DEBUG::LatencyCompensation,
212 string_compose ("Send::set_delay_out %1: %2 - (%3) = %4\n",
213 name (), _delay_in, _delay_out, _delay_in - _delay_out));
215 update_delaylines ();
219 Send::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool)
221 if (_output->n_ports() == ChanCount::ZERO) {
223 _active = _pending_active;
227 if (!_active && !_pending_active) {
229 _output->silence (nframes);
230 _active = _pending_active;
234 // we have to copy the input, because deliver_output() may alter the buffers
235 // in-place, which a send must never do.
237 BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
238 sendbufs.read_from (bufs, nframes);
239 assert(sendbufs.count() == bufs.count());
243 _amp->set_gain_automation_buffer (_session.send_gain_automation_buffer ());
244 _amp->setup_gain_automation (start_sample, end_sample, nframes);
245 _amp->run (sendbufs, start_sample, end_sample, speed, nframes, true);
247 _send_delay->run (sendbufs, start_sample, end_sample, speed, nframes, true);
249 /* deliver to outputs */
251 Delivery::run (sendbufs, start_sample, end_sample, speed, nframes, true);
253 /* consider metering */
256 if (_amp->gain_control()->get_value() == 0) {
259 _meter->run (*_output_buffers, start_sample, end_sample, speed, nframes, true);
263 _thru_delay->run (bufs, start_sample, end_sample, speed, nframes, true);
265 /* _active was set to _pending_active by Delivery::run() */
271 XMLNode& node = Delivery::state ();
273 node.set_property ("type", "send");
275 if (_role != Listen) {
276 node.set_property ("bitslot", _bitslot);
279 node.set_property ("selfdestruct", _remove_on_disconnect);
281 node.add_child_nocopy (_amp->get_state ());
287 Send::set_state (const XMLNode& node, int version)
289 if (version < 3000) {
290 return set_state_2X (node, version);
293 XMLProperty const * prop;
295 Delivery::set_state (node, version);
297 if (node.property ("ignore-bitslot") == 0) {
299 /* don't try to reset bitslot if there is a node for it already: this can cause
300 issues with the session's accounting of send ID's
303 if ((prop = node.property ("bitslot")) == 0) {
304 if (_role == Delivery::Aux || _role == Delivery::Foldback) {
305 _bitslot = _session.next_aux_send_id ();
306 } else if (_role == Delivery::Send) {
307 _bitslot = _session.next_send_id ();
309 // bitslot doesn't matter but make it zero anyway
313 if (_role == Delivery::Aux || _role == Delivery::Foldback) {
314 _session.unmark_aux_send_id (_bitslot);
315 _bitslot = string_to<uint32_t>(prop->value());
316 _session.mark_aux_send_id (_bitslot);
317 } else if (_role == Delivery::Send) {
318 _session.unmark_send_id (_bitslot);
319 _bitslot = string_to<uint32_t>(prop->value());
320 _session.mark_send_id (_bitslot);
322 // bitslot doesn't matter but make it zero anyway
328 node.get_property (X_("selfdestruct"), _remove_on_disconnect);
330 XMLNodeList nlist = node.children();
331 for (XMLNodeIterator i = nlist.begin(); i != nlist.end(); ++i) {
332 if ((*i)->name() == X_("Processor")) {
333 _amp->set_state (**i, version);
337 _send_delay->set_name ("Send-" + name());
338 _thru_delay->set_name ("Thru-" + name());
344 Send::set_state_2X (const XMLNode& node, int /* version */)
346 /* use the IO's name for the name of the send */
347 XMLNodeList const & children = node.children ();
349 XMLNodeList::const_iterator i = children.begin();
350 while (i != children.end() && (*i)->name() != X_("Redirect")) {
354 if (i == children.end()) {
358 XMLNodeList const & grand_children = (*i)->children ();
359 XMLNodeList::const_iterator j = grand_children.begin ();
360 while (j != grand_children.end() && (*j)->name() != X_("IO")) {
364 if (j == grand_children.end()) {
368 XMLProperty const * prop = (*j)->property (X_("name"));
373 set_name (prop->value ());
379 Send::can_support_io_configuration (const ChanCount& in, ChanCount& out)
381 /* sends have no impact at all on the channel configuration of the
382 streams passing through the route. so, out == in.
389 /** Caller must hold process lock */
391 Send::configure_io (ChanCount in, ChanCount out)
393 if (!_amp->configure_io (in, out)) {
397 if (!Processor::configure_io (in, out)) {
401 if (!_meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()))) {
405 if (!_thru_delay->configure_io (in, out)) {
409 if (!_send_delay->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()))) {
419 Send::panshell_changed ()
421 _meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()));
425 Send::set_name (const string& new_name)
429 if (_role == Delivery::Send) {
432 /* rip any existing numeric part of the name, and append the bitslot
435 string::size_type last_letter = new_name.find_last_not_of ("0123456789");
437 if (last_letter != string::npos) {
438 unique_name = new_name.substr (0, last_letter + 1);
440 unique_name = new_name;
443 snprintf (buf, sizeof (buf), "%u", (_bitslot + 1));
447 unique_name = new_name;
450 return Delivery::set_name (unique_name);
454 Send::display_to_user () const
456 /* we ignore Deliver::_display_to_user */
458 if (_role == Listen || _role == Foldback) {
459 /* don't make the monitor/control/listen send visible */
467 Send::snd_output_changed (IOChange change, void* /*src*/)
469 if (change.type & IOChange::ConnectionsChanged) {
470 if (!_output->connected() && _remove_on_disconnect) {
471 _remove_on_disconnect = false;
472 SelfDestruct (); /* EMIT SIGNAL */