Collect plugin runtime profile statistics.
[ardour.git] / libs / ardour / send.cc
1 /*
2     Copyright (C) 2000 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <iostream>
21 #include <algorithm>
22
23 #include "pbd/xml++.h"
24
25 #include "ardour/amp.h"
26 #include "ardour/boost_debug.h"
27 #include "ardour/buffer_set.h"
28 #include "ardour/debug.h"
29 #include "ardour/delayline.h"
30 #include "ardour/gain_control.h"
31 #include "ardour/io.h"
32 #include "ardour/meter.h"
33 #include "ardour/panner_shell.h"
34 #include "ardour/send.h"
35 #include "ardour/session.h"
36
37 #include "pbd/i18n.h"
38
39 namespace ARDOUR {
40 class AutomationControl;
41 class MuteMaster;
42 class Pannable;
43 }
44
45 using namespace ARDOUR;
46 using namespace PBD;
47 using namespace std;
48
49 PBD::Signal0<void> Send::ChangedLatency;
50
51 string
52 Send::name_and_id_new_send (Session& s, Role r, uint32_t& bitslot, bool ignore_bitslot)
53 {
54         if (ignore_bitslot) {
55                 /* this happens during initial construction of sends from XML,
56                    before they get ::set_state() called. lets not worry about
57                    it.
58                 */
59                 bitslot = 0;
60                 return string ();
61         }
62
63         switch (r) {
64         case Delivery::Aux:
65                 return string_compose (_("aux %1"), (bitslot = s.next_aux_send_id ()) + 1);
66         case Delivery::Listen:
67                 return _("listen"); // no ports, no need for numbering
68         case Delivery::Send:
69                 return string_compose (_("send %1"), (bitslot = s.next_send_id ()) + 1);
70         default:
71                 fatal << string_compose (_("programming error: send created using role %1"), enum_2_string (r)) << endmsg;
72                 abort(); /*NOTREACHED*/
73                 return string();
74         }
75
76 }
77
78 Send::Send (Session& s, boost::shared_ptr<Pannable> p, boost::shared_ptr<MuteMaster> mm, Role r, bool ignore_bitslot)
79         : Delivery (s, p, mm, name_and_id_new_send (s, r, _bitslot, ignore_bitslot), r)
80         , _metering (false)
81         , _delay_in (0)
82         , _delay_out (0)
83         , _remove_on_disconnect (false)
84 {
85         if (_role == Listen) {
86                 /* we don't need to do this but it keeps things looking clean
87                    in a debugger. _bitslot is not used by listen sends.
88                 */
89                 _bitslot = 0;
90         }
91
92         //boost_debug_shared_ptr_mark_interesting (this, "send");
93
94         boost::shared_ptr<AutomationList> gl (new AutomationList (Evoral::Parameter (GainAutomation)));
95         _gain_control = boost::shared_ptr<GainControl> (new GainControl (_session, Evoral::Parameter(GainAutomation), gl));
96         add_control (_gain_control);
97
98         _amp.reset (new Amp (_session, _("Fader"), _gain_control, true));
99         _meter.reset (new PeakMeter (_session, name()));
100
101         _send_delay.reset (new DelayLine (_session, "Send-" + name()));
102         _thru_delay.reset (new DelayLine (_session, "Thru-" + name()));
103
104         if (panner_shell()) {
105                 panner_shell()->Changed.connect_same_thread (*this, boost::bind (&Send::panshell_changed, this));
106         }
107         if (_output) {
108                 _output->changed.connect_same_thread (*this, boost::bind (&Send::snd_output_changed, this, _1, _2));
109         }
110 }
111
112 Send::~Send ()
113 {
114         _session.unmark_send_id (_bitslot);
115 }
116
117 void
118 Send::activate ()
119 {
120         _amp->activate ();
121         _meter->activate ();
122
123         Processor::activate ();
124 }
125
126 void
127 Send::deactivate ()
128 {
129         _amp->deactivate ();
130         _meter->deactivate ();
131         _meter->reset ();
132
133         Processor::deactivate ();
134 }
135
136 samplecnt_t
137 Send::signal_latency () const
138 {
139         if (!_pending_active) {
140                  return 0;
141         }
142         if (_user_latency) {
143                 return _user_latency;
144         }
145         if (_delay_out > _delay_in) {
146                 return _delay_out - _delay_in;
147         }
148         return 0;
149 }
150
151 void
152 Send::update_delaylines ()
153 {
154         if (_role == Listen) {
155                 /* Don't align monitor-listen (just yet).
156                  * They're present on each route, may change positions
157                  * and could potentially signficiantly increase worst-case
158                  * Latency: In PFL mode all tracks/busses would additionally be
159                  * aligned at PFL position.
160                  *
161                  * We should only align active monitor-sends when at least one is active.
162                  */
163                 return;
164         }
165
166         bool changed;
167         if (_delay_out > _delay_in) {
168                 changed = _thru_delay->set_delay(_delay_out - _delay_in);
169                 _send_delay->set_delay(0);
170         } else {
171                 changed = _thru_delay->set_delay(0);
172                 _send_delay->set_delay(_delay_in - _delay_out);
173         }
174
175         if (changed) {
176                 // TODO -- ideally postpone for effective no-op changes
177                 // (in case both  _delay_out and _delay_in are changed by the
178                 // same amount in a single latency-update cycle).
179                 ChangedLatency (); /* EMIT SIGNAL */
180         }
181 }
182
183 void
184 Send::set_delay_in (samplecnt_t delay)
185 {
186         if (_delay_in == delay) {
187                 return;
188         }
189         _delay_in = delay;
190
191         DEBUG_TRACE (DEBUG::LatencyCompensation,
192                         string_compose ("Send::set_delay_in %1: (%2) - %3 = %4\n",
193                                 name (), _delay_in, _delay_out, _delay_in - _delay_out));
194
195         update_delaylines ();
196 }
197
198 void
199 Send::set_delay_out (samplecnt_t delay)
200 {
201         if (_delay_out == delay) {
202                 return;
203         }
204         _delay_out = delay;
205         DEBUG_TRACE (DEBUG::LatencyCompensation,
206                         string_compose ("Send::set_delay_out %1: %2 - (%3) = %4\n",
207                                 name (), _delay_in, _delay_out, _delay_in - _delay_out));
208
209         update_delaylines ();
210 }
211
212 void
213 Send::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool)
214 {
215         if (_output->n_ports() == ChanCount::ZERO) {
216                 _meter->reset ();
217                 _active = _pending_active;
218                 return;
219         }
220
221         if (!_active && !_pending_active) {
222                 _meter->reset ();
223                 _output->silence (nframes);
224                 _active = _pending_active;
225                 return;
226         }
227
228         // we have to copy the input, because deliver_output() may alter the buffers
229         // in-place, which a send must never do.
230
231         BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
232         sendbufs.read_from (bufs, nframes);
233         assert(sendbufs.count() == bufs.count());
234
235         /* gain control */
236
237         _amp->set_gain_automation_buffer (_session.send_gain_automation_buffer ());
238         _amp->setup_gain_automation (start_sample, end_sample, nframes);
239         _amp->run (sendbufs, start_sample, end_sample, speed, nframes, true);
240
241         _send_delay->run (sendbufs, start_sample, end_sample, speed, nframes, true);
242
243         /* deliver to outputs */
244
245         Delivery::run (sendbufs, start_sample, end_sample, speed, nframes, true);
246
247         /* consider metering */
248
249         if (_metering) {
250                 if (_amp->gain_control()->get_value() == 0) {
251                         _meter->reset();
252                 } else {
253                         _meter->run (*_output_buffers, start_sample, end_sample, speed, nframes, true);
254                 }
255         }
256
257         _thru_delay->run (bufs, start_sample, end_sample, speed, nframes, true);
258
259         /* _active was set to _pending_active by Delivery::run() */
260 }
261
262 XMLNode&
263 Send::state ()
264 {
265         XMLNode& node = Delivery::state ();
266
267         node.set_property ("type", "send");
268
269         if (_role != Listen) {
270                 node.set_property ("bitslot", _bitslot);
271         }
272
273         node.set_property ("selfdestruct", _remove_on_disconnect);
274
275         node.add_child_nocopy (_amp->get_state ());
276
277         return node;
278 }
279
280 int
281 Send::set_state (const XMLNode& node, int version)
282 {
283         if (version < 3000) {
284                 return set_state_2X (node, version);
285         }
286
287         XMLProperty const * prop;
288
289         Delivery::set_state (node, version);
290
291         if (node.property ("ignore-bitslot") == 0) {
292
293                 /* don't try to reset bitslot if there is a node for it already: this can cause
294                    issues with the session's accounting of send ID's
295                 */
296
297                 if ((prop = node.property ("bitslot")) == 0) {
298                         if (_role == Delivery::Aux) {
299                                 _bitslot = _session.next_aux_send_id ();
300                         } else if (_role == Delivery::Send) {
301                                 _bitslot = _session.next_send_id ();
302                         } else {
303                                 // bitslot doesn't matter but make it zero anyway
304                                 _bitslot = 0;
305                         }
306                 } else {
307                         if (_role == Delivery::Aux) {
308                                 _session.unmark_aux_send_id (_bitslot);
309                                 _bitslot = string_to<uint32_t>(prop->value());
310                                 _session.mark_aux_send_id (_bitslot);
311                         } else if (_role == Delivery::Send) {
312                                 _session.unmark_send_id (_bitslot);
313                                 _bitslot = string_to<uint32_t>(prop->value());
314                                 _session.mark_send_id (_bitslot);
315                         } else {
316                                 // bitslot doesn't matter but make it zero anyway
317                                 _bitslot = 0;
318                         }
319                 }
320         }
321
322         node.get_property (X_("selfdestruct"), _remove_on_disconnect);
323
324         XMLNodeList nlist = node.children();
325         for (XMLNodeIterator i = nlist.begin(); i != nlist.end(); ++i) {
326                 if ((*i)->name() == X_("Processor")) {
327                         _amp->set_state (**i, version);
328                 }
329         }
330
331         _send_delay->set_name ("Send-" + name());
332         _thru_delay->set_name ("Thru-" + name());
333
334         return 0;
335 }
336
337 int
338 Send::set_state_2X (const XMLNode& node, int /* version */)
339 {
340         /* use the IO's name for the name of the send */
341         XMLNodeList const & children = node.children ();
342
343         XMLNodeList::const_iterator i = children.begin();
344         while (i != children.end() && (*i)->name() != X_("Redirect")) {
345                 ++i;
346         }
347
348         if (i == children.end()) {
349                 return -1;
350         }
351
352         XMLNodeList const & grand_children = (*i)->children ();
353         XMLNodeList::const_iterator j = grand_children.begin ();
354         while (j != grand_children.end() && (*j)->name() != X_("IO")) {
355                 ++j;
356         }
357
358         if (j == grand_children.end()) {
359                 return -1;
360         }
361
362         XMLProperty const * prop = (*j)->property (X_("name"));
363         if (!prop) {
364                 return -1;
365         }
366
367         set_name (prop->value ());
368
369         return 0;
370 }
371
372 bool
373 Send::can_support_io_configuration (const ChanCount& in, ChanCount& out)
374 {
375         /* sends have no impact at all on the channel configuration of the
376            streams passing through the route. so, out == in.
377         */
378
379         out = in;
380         return true;
381 }
382
383 /** Caller must hold process lock */
384 bool
385 Send::configure_io (ChanCount in, ChanCount out)
386 {
387         if (!_amp->configure_io (in, out)) {
388                 return false;
389         }
390
391         if (!Processor::configure_io (in, out)) {
392                 return false;
393         }
394
395         if (!_meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()))) {
396                 return false;
397         }
398
399         if (!_thru_delay->configure_io (in, out)) {
400                 return false;
401         }
402
403         if (!_send_delay->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()))) {
404                 return false;
405         }
406
407         reset_panner ();
408
409         return true;
410 }
411
412 void
413 Send::panshell_changed ()
414 {
415         _meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()));
416 }
417
418 bool
419 Send::set_name (const string& new_name)
420 {
421         string unique_name;
422
423         if (_role == Delivery::Send) {
424                 char buf[32];
425
426                 /* rip any existing numeric part of the name, and append the bitslot
427                  */
428
429                 string::size_type last_letter = new_name.find_last_not_of ("0123456789");
430
431                 if (last_letter != string::npos) {
432                         unique_name = new_name.substr (0, last_letter + 1);
433                 } else {
434                         unique_name = new_name;
435                 }
436
437                 snprintf (buf, sizeof (buf), "%u", (_bitslot + 1));
438                 unique_name += buf;
439
440         } else {
441                 unique_name = new_name;
442         }
443
444         return Delivery::set_name (unique_name);
445 }
446
447 bool
448 Send::display_to_user () const
449 {
450         /* we ignore Deliver::_display_to_user */
451
452         if (_role == Listen) {
453                 /* don't make the monitor/control/listen send visible */
454                 return false;
455         }
456
457         return true;
458 }
459
460 void
461 Send::snd_output_changed (IOChange change, void* /*src*/)
462 {
463         if (change.type & IOChange::ConnectionsChanged) {
464                 if (!_output->connected() && _remove_on_disconnect) {
465                         _remove_on_disconnect = false;
466                         SelfDestruct (); /* EMIT SIGNAL */
467                 }
468         }
469 }