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