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/event_type_map.h"
34 #include "ardour/gain_control.h"
35 #include "ardour/io.h"
36 #include "ardour/meter.h"
37 #include "ardour/panner_shell.h"
38 #include "ardour/send.h"
39 #include "ardour/session.h"
40
41 #include "pbd/i18n.h"
42
43 namespace ARDOUR {
44 class AutomationControl;
45 class MuteMaster;
46 class Pannable;
47 }
48
49 using namespace ARDOUR;
50 using namespace PBD;
51 using namespace std;
52
53 PBD::Signal0<void> LatentSend::ChangedLatency;
54
LatentSend()55 LatentSend::LatentSend ()
56 : _delay_in (0)
57 , _delay_out (0)
58 {
59 }
60
61 string
name_and_id_new_send(Session & s,Role r,uint32_t & bitslot,bool ignore_bitslot)62 Send::name_and_id_new_send (Session& s, Role r, uint32_t& bitslot, bool ignore_bitslot)
63 {
64 if (ignore_bitslot) {
65 /* this happens during initial construction of sends from XML,
66 before they get ::set_state() called. lets not worry about
67 it.
68 */
69 bitslot = 0;
70 return string ();
71 }
72
73 switch (r) {
74 case Delivery::Aux:
75 return string_compose (_("aux %1"), (bitslot = s.next_aux_send_id ()));
76 case Delivery::Listen:
77 bitslot = 0; /* unused */
78 return _("listen"); // no ports, no need for numbering
79 case Delivery::Send:
80 return string_compose (_("send %1"), (bitslot = s.next_send_id ()));
81 case Delivery::Foldback:
82 return string_compose (_("foldback %1"), (bitslot = s.next_aux_send_id ()));
83 default:
84 fatal << string_compose (_("programming error: send created using role %1"), enum_2_string (r)) << endmsg;
85 abort(); /*NOTREACHED*/
86 return string();
87 }
88
89 }
90
Send(Session & s,boost::shared_ptr<Pannable> p,boost::shared_ptr<MuteMaster> mm,Role r,bool ignore_bitslot)91 Send::Send (Session& s, boost::shared_ptr<Pannable> p, boost::shared_ptr<MuteMaster> mm, Role r, bool ignore_bitslot)
92 : Delivery (s, p, mm, name_and_id_new_send (s, r, _bitslot, ignore_bitslot), r)
93 , _metering (false)
94 , _remove_on_disconnect (false)
95 {
96 //boost_debug_shared_ptr_mark_interesting (this, "send");
97
98 boost::shared_ptr<AutomationList> gl (new AutomationList (Evoral::Parameter (BusSendLevel)));
99 _gain_control = boost::shared_ptr<GainControl> (new GainControl (_session, Evoral::Parameter(BusSendLevel), gl));
100 _gain_control->set_flag (Controllable::InlineControl);
101 add_control (_gain_control);
102
103 _amp.reset (new Amp (_session, _("Fader"), _gain_control, true));
104 _meter.reset (new PeakMeter (_session, name()));
105
106 _send_delay.reset (new DelayLine (_session, "Send-" + name()));
107 _thru_delay.reset (new DelayLine (_session, "Thru-" + name()));
108
109 if (panner_shell()) {
110 panner_shell()->Changed.connect_same_thread (*this, boost::bind (&Send::panshell_changed, this));
111 panner_shell()->PannableChanged.connect_same_thread (*this, boost::bind (&Send::pannable_changed, this));
112 }
113 if (_output) {
114 _output->changed.connect_same_thread (*this, boost::bind (&Send::snd_output_changed, this, _1, _2));
115 }
116 }
117
~Send()118 Send::~Send ()
119 {
120 _session.unmark_send_id (_bitslot);
121 }
122
123 void
activate()124 Send::activate ()
125 {
126 _amp->activate ();
127 _meter->activate ();
128
129 Processor::activate ();
130 }
131
132 void
deactivate()133 Send::deactivate ()
134 {
135 _amp->deactivate ();
136 _meter->deactivate ();
137 _meter->reset ();
138
139 Processor::deactivate ();
140 }
141
142 samplecnt_t
signal_latency() const143 Send::signal_latency () const
144 {
145 if (!_pending_active) {
146 return 0;
147 }
148 if (_delay_out > _delay_in) {
149 return _delay_out - _delay_in;
150 }
151 return 0;
152 }
153
154 void
update_delaylines()155 Send::update_delaylines ()
156 {
157 if (_role == Listen) {
158 /* Don't align monitor-listen (just yet).
159 * They're present on each route, may change positions
160 * and could potentially signficiantly increase worst-case
161 * Latency: In PFL mode all tracks/busses would additionally be
162 * aligned at PFL position.
163 *
164 * We should only align active monitor-sends when at least one is active.
165 */
166 return;
167 }
168
169 bool changed;
170 if (_delay_out > _delay_in) {
171 changed = _thru_delay->set_delay(_delay_out - _delay_in);
172 _send_delay->set_delay(0);
173 } else {
174 changed = _thru_delay->set_delay(0);
175 _send_delay->set_delay(_delay_in - _delay_out);
176 }
177
178 if (changed) {
179 // TODO -- ideally postpone for effective no-op changes
180 // (in case both _delay_out and _delay_in are changed by the
181 // same amount in a single latency-update cycle).
182 ChangedLatency (); /* EMIT SIGNAL */
183 }
184 }
185
186 void
set_delay_in(samplecnt_t delay)187 Send::set_delay_in (samplecnt_t delay)
188 {
189 if (_delay_in == delay) {
190 return;
191 }
192 _delay_in = delay;
193
194 DEBUG_TRACE (DEBUG::LatencyDelayLine,
195 string_compose ("Send::set_delay_in %1: (%2) - %3 = %4\n",
196 name (), _delay_in, _delay_out, _delay_in - _delay_out));
197
198 update_delaylines ();
199 }
200
201 void
set_delay_out(samplecnt_t delay,size_t)202 Send::set_delay_out (samplecnt_t delay, size_t /*bus*/)
203 {
204 if (_delay_out == delay) {
205 return;
206 }
207 _delay_out = delay;
208 DEBUG_TRACE (DEBUG::LatencyDelayLine,
209 string_compose ("Send::set_delay_out %1: %2 - (%3) = %4\n",
210 name (), _delay_in, _delay_out, _delay_in - _delay_out));
211
212 update_delaylines ();
213 }
214
215 void
run(BufferSet & bufs,samplepos_t start_sample,samplepos_t end_sample,double speed,pframes_t nframes,bool)216 Send::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool)
217 {
218 if (_output->n_ports() == ChanCount::ZERO) {
219 _meter->reset ();
220 _active = _pending_active;
221 return;
222 }
223
224 if (!_active && !_pending_active) {
225 _meter->reset ();
226 _output->silence (nframes);
227 _active = _pending_active;
228 return;
229 }
230
231 // we have to copy the input, because deliver_output() may alter the buffers
232 // in-place, which a send must never do.
233
234 BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
235 sendbufs.read_from (bufs, nframes);
236 assert(sendbufs.count() == bufs.count());
237
238 /* gain control */
239
240 _amp->set_gain_automation_buffer (_session.send_gain_automation_buffer ());
241 _amp->setup_gain_automation (start_sample, end_sample, nframes);
242 _amp->run (sendbufs, start_sample, end_sample, speed, nframes, true);
243
244 _send_delay->run (sendbufs, start_sample, end_sample, speed, nframes, true);
245
246 /* deliver to outputs */
247
248 Delivery::run (sendbufs, start_sample, end_sample, speed, nframes, true);
249
250 /* consider metering */
251
252 if (_metering) {
253 if (_amp->gain_control()->get_value() == 0) {
254 _meter->reset();
255 } else {
256 _meter->run (*_output_buffers, start_sample, end_sample, speed, nframes, true);
257 }
258 }
259
260 _thru_delay->run (bufs, start_sample, end_sample, speed, nframes, true);
261
262 /* _active was set to _pending_active by Delivery::run() */
263 }
264
265 XMLNode&
state()266 Send::state ()
267 {
268 XMLNode& node = Delivery::state ();
269
270 node.set_property ("type", "send");
271
272 if (_role != Listen) {
273 node.set_property ("bitslot", _bitslot);
274 }
275
276 node.set_property ("selfdestruct", _remove_on_disconnect);
277
278 node.add_child_nocopy (_gain_control->get_state());
279
280 return node;
281 }
282
283 int
set_state(const XMLNode & node,int version)284 Send::set_state (const XMLNode& node, int version)
285 {
286 if (version < 3000) {
287 return set_state_2X (node, version);
288 }
289
290 XMLNode* gain_node;
291
292 if ((gain_node = node.child (Controllable::xml_node_name.c_str ())) != 0) {
293 _gain_control->set_state (*gain_node, version);
294 }
295
296 if (version <= 6000) {
297 XMLNode const* nn = &node;
298
299 #ifdef MIXBUS
300 /* This was also broken in mixbus 6.0 */
301 if (version <= 6000)
302 #else
303 /* version 5: Gain Control was owned by the Amp */
304 if (version < 6000)
305 #endif
306 {
307 XMLNode* processor = node.child ("Processor");
308 if (processor) {
309 nn = processor;
310 if ((gain_node = nn->child (Controllable::xml_node_name.c_str ())) != 0) {
311 _gain_control->set_state (*gain_node, version);
312 _gain_control->set_flags (Controllable::InlineControl);
313 }
314 }
315 }
316
317 /* convert GainAutomation to BusSendLevel
318 *
319 * (early Ardour 6.0-pre0 and Mixbus 6.0 used "BusSendLevel"
320 * control with GainAutomation, so we check version <= 6000.
321 * New A6 sessions do not have a GainAutomation parameter,
322 * so this is safe.)
323 *
324 * Normally this is restored via
325 * Delivery::set_state() -> Processor::set_state()
326 * -> Automatable::set_automation_xml_state()
327 */
328 XMLNodeList nlist;
329 XMLNode* automation = nn->child ("Automation");
330 if (automation) {
331 nlist = automation->children();
332 } else if (0 != (automation = node.child ("Automation"))) {
333 nlist = automation->children();
334 }
335 for (XMLNodeIterator i = nlist.begin(); i != nlist.end(); ++i) {
336 if ((*i)->name() != "AutomationList") {
337 continue;
338 }
339 XMLProperty const* id_prop = (*i)->property("automation-id");
340 if (!id_prop) {
341 continue;
342 }
343 Evoral::Parameter param = EventTypeMap::instance().from_symbol (id_prop->value());
344 if (param.type() != GainAutomation) {
345 continue;
346 }
347 XMLNode xn (**i);
348 xn.set_property ("automation-id", EventTypeMap::instance().to_symbol(Evoral::Parameter (BusSendLevel)));
349 _gain_control->alist()->set_state (xn, version);
350 break;
351 }
352 }
353
354 Delivery::set_state (node, version);
355
356 if (node.property ("ignore-bitslot") == 0) {
357 XMLProperty const* prop;
358
359 /* don't try to reset bitslot if there is a node for it already: this can cause
360 issues with the session's accounting of send ID's
361 */
362
363 if ((prop = node.property ("bitslot")) == 0) {
364 if (_role == Delivery::Aux || _role == Delivery::Foldback) {
365 _bitslot = _session.next_aux_send_id ();
366 } else if (_role == Delivery::Send) {
367 _bitslot = _session.next_send_id ();
368 } else {
369 // bitslot doesn't matter but make it zero anyway
370 _bitslot = 0;
371 }
372 } else {
373 if (_role == Delivery::Aux || _role == Delivery::Foldback) {
374 _session.unmark_aux_send_id (_bitslot);
375 _bitslot = string_to<uint32_t>(prop->value());
376 _session.mark_aux_send_id (_bitslot);
377 } else if (_role == Delivery::Send) {
378 _session.unmark_send_id (_bitslot);
379 _bitslot = string_to<uint32_t>(prop->value());
380 _session.mark_send_id (_bitslot);
381 } else {
382 // bitslot doesn't matter but make it zero anyway
383 _bitslot = 0;
384 }
385 }
386 }
387
388 node.get_property (X_("selfdestruct"), _remove_on_disconnect);
389
390 _send_delay->set_name ("Send-" + name());
391 _thru_delay->set_name ("Thru-" + name());
392
393 return 0;
394 }
395
396 int
set_state_2X(const XMLNode & node,int)397 Send::set_state_2X (const XMLNode& node, int /* version */)
398 {
399 /* use the IO's name for the name of the send */
400 XMLNodeList const & children = node.children ();
401
402 XMLNodeList::const_iterator i = children.begin();
403 while (i != children.end() && (*i)->name() != X_("Redirect")) {
404 ++i;
405 }
406
407 if (i == children.end()) {
408 return -1;
409 }
410
411 XMLNodeList const & grand_children = (*i)->children ();
412 XMLNodeList::const_iterator j = grand_children.begin ();
413 while (j != grand_children.end() && (*j)->name() != X_("IO")) {
414 ++j;
415 }
416
417 if (j == grand_children.end()) {
418 return -1;
419 }
420
421 XMLProperty const * prop = (*j)->property (X_("name"));
422 if (!prop) {
423 return -1;
424 }
425
426 set_name (prop->value ());
427
428 return 0;
429 }
430
431 bool
has_panner() const432 Send::has_panner () const
433 {
434 /* see InternalSend::run() and Delivery::run */
435 if (_panshell && role () != Listen && _panshell->panner()) {
436 return true; // !_panshell->bypassed ()
437 }
438 return false;
439 }
440
441 bool
panner_linked_to_route() const442 Send::panner_linked_to_route () const
443 {
444 return _panshell ? _panshell->is_linked_to_route() : false;
445 }
446
447 void
set_panner_linked_to_route(bool onoff)448 Send::set_panner_linked_to_route (bool onoff) {
449 if (_panshell) {
450 _panshell->set_linked_to_route (onoff);
451 }
452 }
453
454 bool
can_support_io_configuration(const ChanCount & in,ChanCount & out)455 Send::can_support_io_configuration (const ChanCount& in, ChanCount& out)
456 {
457 /* sends have no impact at all on the channel configuration of the
458 streams passing through the route. so, out == in.
459 */
460
461 out = in;
462 return true;
463 }
464
465 /** Caller must hold process lock */
466 bool
configure_io(ChanCount in,ChanCount out)467 Send::configure_io (ChanCount in, ChanCount out)
468 {
469 if (!_amp->configure_io (in, out)) {
470 return false;
471 }
472
473 if (!Processor::configure_io (in, out)) {
474 return false;
475 }
476
477 if (!_meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()))) {
478 return false;
479 }
480
481 if (!_thru_delay->configure_io (in, out)) {
482 return false;
483 }
484
485 if (!_send_delay->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()))) {
486 return false;
487 }
488
489 reset_panner ();
490
491 return true;
492 }
493
494 void
panshell_changed()495 Send::panshell_changed ()
496 {
497 _meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()));
498 }
499
500 void
pannable_changed()501 Send::pannable_changed ()
502 {
503 PropertyChanged (PBD::PropertyChange ()); /* EMIT SIGNAL */
504 }
505
506 bool
set_name(const string & new_name)507 Send::set_name (const string& new_name)
508 {
509 string unique_name;
510
511 if (_role == Delivery::Send) {
512 unique_name = validate_name (new_name, string_compose (_("send %1"), _bitslot));
513
514 if (unique_name.empty ()) {
515 return false;
516 }
517 } else {
518 unique_name = new_name;
519 }
520
521 return Delivery::set_name (unique_name);
522 }
523
524 bool
display_to_user() const525 Send::display_to_user () const
526 {
527 /* we ignore Deliver::_display_to_user */
528
529 if (_role == Listen || _role == Foldback) {
530 /* don't make the monitor/control/listen send visible */
531 return false;
532 }
533
534 return true;
535 }
536
537 void
snd_output_changed(IOChange change,void *)538 Send::snd_output_changed (IOChange change, void* /*src*/)
539 {
540 if (change.type & IOChange::ConnectionsChanged) {
541 if (!_output->connected() && _remove_on_disconnect) {
542 _remove_on_disconnect = false;
543 SelfDestruct (); /* EMIT SIGNAL */
544 }
545 }
546 }
547