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