NO-OP: whitespace
[ardour.git] / libs / ardour / send.cc
1 /*
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>
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <iostream>
24 #include <algorithm>
25
26 #include "pbd/xml++.h"
27
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"
39
40 #include "pbd/i18n.h"
41
42 namespace ARDOUR {
43 class AutomationControl;
44 class MuteMaster;
45 class Pannable;
46 }
47
48 using namespace ARDOUR;
49 using namespace PBD;
50 using namespace std;
51
52 PBD::Signal0<void> LatentSend::ChangedLatency;
53
54 LatentSend::LatentSend ()
55                 : _delay_in (0)
56                 , _delay_out (0)
57 {
58 }
59
60 string
61 Send::name_and_id_new_send (Session& s, Role r, uint32_t& bitslot, bool ignore_bitslot)
62 {
63         if (ignore_bitslot) {
64                 /* this happens during initial construction of sends from XML,
65                    before they get ::set_state() called. lets not worry about
66                    it.
67                 */
68                 bitslot = 0;
69                 return string ();
70         }
71
72         switch (r) {
73         case Delivery::Aux:
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
77         case Delivery::Send:
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);
81         default:
82                 fatal << string_compose (_("programming error: send created using role %1"), enum_2_string (r)) << endmsg;
83                 abort(); /*NOTREACHED*/
84                 return string();
85         }
86
87 }
88
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)
91         , _metering (false)
92         , _remove_on_disconnect (false)
93 {
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.
97                 */
98                 _bitslot = 0;
99         }
100
101         //boost_debug_shared_ptr_mark_interesting (this, "send");
102
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);
106
107         _amp.reset (new Amp (_session, _("Fader"), _gain_control, true));
108         _meter.reset (new PeakMeter (_session, name()));
109
110         _send_delay.reset (new DelayLine (_session, "Send-" + name()));
111         _thru_delay.reset (new DelayLine (_session, "Thru-" + name()));
112
113         if (panner_shell()) {
114                 panner_shell()->Changed.connect_same_thread (*this, boost::bind (&Send::panshell_changed, this));
115         }
116         if (_output) {
117                 _output->changed.connect_same_thread (*this, boost::bind (&Send::snd_output_changed, this, _1, _2));
118         }
119 }
120
121 Send::~Send ()
122 {
123         _session.unmark_send_id (_bitslot);
124 }
125
126 void
127 Send::activate ()
128 {
129         _amp->activate ();
130         _meter->activate ();
131
132         Processor::activate ();
133 }
134
135 void
136 Send::deactivate ()
137 {
138         _amp->deactivate ();
139         _meter->deactivate ();
140         _meter->reset ();
141
142         Processor::deactivate ();
143 }
144
145 samplecnt_t
146 Send::signal_latency () const
147 {
148         if (!_pending_active) {
149                  return 0;
150         }
151         if (_delay_out > _delay_in) {
152                 return _delay_out - _delay_in;
153         }
154         return 0;
155 }
156
157 void
158 Send::update_delaylines ()
159 {
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.
166                  *
167                  * We should only align active monitor-sends when at least one is active.
168                  */
169                 return;
170         }
171
172         bool changed;
173         if (_delay_out > _delay_in) {
174                 changed = _thru_delay->set_delay(_delay_out - _delay_in);
175                 _send_delay->set_delay(0);
176         } else {
177                 changed = _thru_delay->set_delay(0);
178                 _send_delay->set_delay(_delay_in - _delay_out);
179         }
180
181         if (changed) {
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 */
186         }
187 }
188
189 void
190 Send::set_delay_in (samplecnt_t delay)
191 {
192         if (_delay_in == delay) {
193                 return;
194         }
195         _delay_in = delay;
196
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));
200
201         update_delaylines ();
202 }
203
204 void
205 Send::set_delay_out (samplecnt_t delay, size_t /*bus*/)
206 {
207         if (_delay_out == delay) {
208                 return;
209         }
210         _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));
214
215         update_delaylines ();
216 }
217
218 void
219 Send::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool)
220 {
221         if (_output->n_ports() == ChanCount::ZERO) {
222                 _meter->reset ();
223                 _active = _pending_active;
224                 return;
225         }
226
227         if (!_active && !_pending_active) {
228                 _meter->reset ();
229                 _output->silence (nframes);
230                 _active = _pending_active;
231                 return;
232         }
233
234         // we have to copy the input, because deliver_output() may alter the buffers
235         // in-place, which a send must never do.
236
237         BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
238         sendbufs.read_from (bufs, nframes);
239         assert(sendbufs.count() == bufs.count());
240
241         /* gain control */
242
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);
246
247         _send_delay->run (sendbufs, start_sample, end_sample, speed, nframes, true);
248
249         /* deliver to outputs */
250
251         Delivery::run (sendbufs, start_sample, end_sample, speed, nframes, true);
252
253         /* consider metering */
254
255         if (_metering) {
256                 if (_amp->gain_control()->get_value() == 0) {
257                         _meter->reset();
258                 } else {
259                         _meter->run (*_output_buffers, start_sample, end_sample, speed, nframes, true);
260                 }
261         }
262
263         _thru_delay->run (bufs, start_sample, end_sample, speed, nframes, true);
264
265         /* _active was set to _pending_active by Delivery::run() */
266 }
267
268 XMLNode&
269 Send::state ()
270 {
271         XMLNode& node = Delivery::state ();
272
273         node.set_property ("type", "send");
274
275         if (_role != Listen) {
276                 node.set_property ("bitslot", _bitslot);
277         }
278
279         node.set_property ("selfdestruct", _remove_on_disconnect);
280
281         node.add_child_nocopy (_amp->get_state ());
282
283         return node;
284 }
285
286 int
287 Send::set_state (const XMLNode& node, int version)
288 {
289         if (version < 3000) {
290                 return set_state_2X (node, version);
291         }
292
293         XMLProperty const * prop;
294
295         Delivery::set_state (node, version);
296
297         if (node.property ("ignore-bitslot") == 0) {
298
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
301                 */
302
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 ();
308                         } else {
309                                 // bitslot doesn't matter but make it zero anyway
310                                 _bitslot = 0;
311                         }
312                 } else {
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);
321                         } else {
322                                 // bitslot doesn't matter but make it zero anyway
323                                 _bitslot = 0;
324                         }
325                 }
326         }
327
328         node.get_property (X_("selfdestruct"), _remove_on_disconnect);
329
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);
334                 }
335         }
336
337         _send_delay->set_name ("Send-" + name());
338         _thru_delay->set_name ("Thru-" + name());
339
340         return 0;
341 }
342
343 int
344 Send::set_state_2X (const XMLNode& node, int /* version */)
345 {
346         /* use the IO's name for the name of the send */
347         XMLNodeList const & children = node.children ();
348
349         XMLNodeList::const_iterator i = children.begin();
350         while (i != children.end() && (*i)->name() != X_("Redirect")) {
351                 ++i;
352         }
353
354         if (i == children.end()) {
355                 return -1;
356         }
357
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")) {
361                 ++j;
362         }
363
364         if (j == grand_children.end()) {
365                 return -1;
366         }
367
368         XMLProperty const * prop = (*j)->property (X_("name"));
369         if (!prop) {
370                 return -1;
371         }
372
373         set_name (prop->value ());
374
375         return 0;
376 }
377
378 bool
379 Send::can_support_io_configuration (const ChanCount& in, ChanCount& out)
380 {
381         /* sends have no impact at all on the channel configuration of the
382            streams passing through the route. so, out == in.
383         */
384
385         out = in;
386         return true;
387 }
388
389 /** Caller must hold process lock */
390 bool
391 Send::configure_io (ChanCount in, ChanCount out)
392 {
393         if (!_amp->configure_io (in, out)) {
394                 return false;
395         }
396
397         if (!Processor::configure_io (in, out)) {
398                 return false;
399         }
400
401         if (!_meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()))) {
402                 return false;
403         }
404
405         if (!_thru_delay->configure_io (in, out)) {
406                 return false;
407         }
408
409         if (!_send_delay->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()))) {
410                 return false;
411         }
412
413         reset_panner ();
414
415         return true;
416 }
417
418 void
419 Send::panshell_changed ()
420 {
421         _meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()));
422 }
423
424 bool
425 Send::set_name (const string& new_name)
426 {
427         string unique_name;
428
429         if (_role == Delivery::Send) {
430                 char buf[32];
431
432                 /* rip any existing numeric part of the name, and append the bitslot
433                  */
434
435                 string::size_type last_letter = new_name.find_last_not_of ("0123456789");
436
437                 if (last_letter != string::npos) {
438                         unique_name = new_name.substr (0, last_letter + 1);
439                 } else {
440                         unique_name = new_name;
441                 }
442
443                 snprintf (buf, sizeof (buf), "%u", (_bitslot + 1));
444                 unique_name += buf;
445
446         } else {
447                 unique_name = new_name;
448         }
449
450         return Delivery::set_name (unique_name);
451 }
452
453 bool
454 Send::display_to_user () const
455 {
456         /* we ignore Deliver::_display_to_user */
457
458         if (_role == Listen || _role == Foldback) {
459                 /* don't make the monitor/control/listen send visible */
460                 return false;
461         }
462
463         return true;
464 }
465
466 void
467 Send::snd_output_changed (IOChange change, void* /*src*/)
468 {
469         if (change.type & IOChange::ConnectionsChanged) {
470                 if (!_output->connected() && _remove_on_disconnect) {
471                         _remove_on_disconnect = false;
472                         SelfDestruct (); /* EMIT SIGNAL */
473                 }
474         }
475 }