Prevent deadlock when removing routes w/o engine
[ardour.git] / libs / ardour / internal_send.cc
1 /*
2     Copyright (C) 2009 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 "pbd/error.h"
21 #include "pbd/failed_constructor.h"
22 #include "pbd/types_convert.h"
23
24 #include "ardour/amp.h"
25 #include "ardour/audio_buffer.h"
26 #include "ardour/delayline.h"
27 #include "ardour/internal_return.h"
28 #include "ardour/internal_send.h"
29 #include "ardour/meter.h"
30 #include "ardour/panner_shell.h"
31 #include "ardour/route.h"
32 #include "ardour/session.h"
33 #include "ardour/audioengine.h"
34
35 #include "pbd/i18n.h"
36
37 namespace ARDOUR { class MuteMaster; class Pannable; }
38
39 using namespace PBD;
40 using namespace ARDOUR;
41 using namespace std;
42
43 PBD::Signal1<void, pframes_t> InternalSend::CycleStart;
44
45 InternalSend::InternalSend (Session& s,
46                 boost::shared_ptr<Pannable> p,
47                 boost::shared_ptr<MuteMaster> mm,
48                 boost::shared_ptr<Route> sendfrom,
49                 boost::shared_ptr<Route> sendto,
50                 Delivery::Role role,
51                 bool ignore_bitslot)
52         : Send (s, p, mm, role, ignore_bitslot)
53         , _send_from (sendfrom)
54         , _allow_feedback (false)
55 {
56         if (sendto) {
57                 if (use_target (sendto)) {
58                         throw failed_constructor();
59                 }
60         }
61
62         init_gain ();
63
64         _send_from->DropReferences.connect_same_thread (source_connection, boost::bind (&InternalSend::send_from_going_away, this));
65         CycleStart.connect_same_thread (*this, boost::bind (&InternalSend::cycle_start, this, _1));
66 }
67
68 InternalSend::~InternalSend ()
69 {
70         if (_send_to) {
71                 _send_to->remove_send_from_internal_return (this);
72         }
73 }
74
75 void
76 InternalSend::init_gain ()
77 {
78         if (_role == Listen) {
79                 /* send to monitor bus is always at unity */
80                 _gain_control->set_value (GAIN_COEFF_UNITY, PBD::Controllable::NoGroup);
81         } else {
82                 /* aux sends start at -inf dB */
83                 _gain_control->set_value (GAIN_COEFF_ZERO, PBD::Controllable::NoGroup);
84         }
85 }
86
87 int
88 InternalSend::use_target (boost::shared_ptr<Route> sendto)
89 {
90         if (_send_to) {
91                 _send_to->remove_send_from_internal_return (this);
92         }
93
94         _send_to = sendto;
95
96         _send_to->add_send_to_internal_return (this);
97
98         mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
99         mixbufs.set_count (_send_to->internal_return()->input_streams());
100
101         _meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()));
102
103         _send_delay->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()));
104
105         reset_panner ();
106
107         set_name (sendto->name());
108         _send_to_id = _send_to->id();
109
110         target_connections.drop_connections ();
111
112         _send_to->DropReferences.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_going_away, this));
113         _send_to->PropertyChanged.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_property_changed, this, _1));
114         _send_to->io_changed.connect_same_thread (target_connections, boost::bind (&InternalSend::target_io_changed, this));
115
116         return 0;
117 }
118
119 void
120 InternalSend::target_io_changed ()
121 {
122         assert (_send_to);
123         mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
124         mixbufs.set_count (_send_to->internal_return()->input_streams());
125         reset_panner();
126 }
127
128 void
129 InternalSend::send_from_going_away ()
130 {
131         _send_from.reset();
132 }
133
134 void
135 InternalSend::send_to_going_away ()
136 {
137         target_connections.drop_connections ();
138         _send_to.reset ();
139         _send_to_id = "0";
140 }
141
142 void
143 InternalSend::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool)
144 {
145         if ((!_active && !_pending_active) || !_send_to) {
146                 _meter->reset ();
147                 return;
148         }
149
150         // we have to copy the input, because we may alter the buffers with the amp
151         // in-place, which a send must never do.
152
153         if (_panshell && !_panshell->bypassed() && role() != Listen) {
154                 if (mixbufs.count ().n_audio () > 0) {
155                         _panshell->run (bufs, mixbufs, start_sample, end_sample, nframes);
156                 }
157
158                 /* non-audio data will not have been copied by the panner, do it now
159                  * if there are more buffers available than send buffers, ignore them,
160                  * if there are less, copy the last as IO::copy_to_output does. */
161
162                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
163                         if (*t != DataType::AUDIO) {
164                                 BufferSet::iterator o = mixbufs.begin(*t);
165                                 BufferSet::iterator i = bufs.begin(*t);
166
167                                 while (i != bufs.end(*t) && o != mixbufs.end(*t)) {
168                                         o->read_from (*i, nframes);
169                                         ++i;
170                                         ++o;
171                                 }
172                                 while (o != mixbufs.end(*t)) {
173                                         o->silence(nframes, 0);
174                                         ++o;
175                                 }
176                         }
177                 }
178         } else {
179                 if (role() == Listen) {
180                         /* We're going to the monitor bus, so discard MIDI data */
181
182                         uint32_t const bufs_audio = bufs.count().get (DataType::AUDIO);
183                         uint32_t const mixbufs_audio = mixbufs.count().get (DataType::AUDIO);
184
185                         /* monitor-section has same number of channels as master-bus (on creation).
186                          *
187                          * There is no clear answer what should happen when trying to PFL or AFL
188                          * a track that has more channels (bufs_audio from source-track is
189                          * larger than mixbufs).
190                          *
191                          * There are two options:
192                          *  1: discard additional channels    (current)
193                          * OR
194                          *  2: require the monitor-section to have at least as many channels
195                          * as the largest count of any route
196                          */
197                         //assert (mixbufs.available().get (DataType::AUDIO) >= bufs_audio);
198
199                         /* Copy bufs into mixbufs, going round bufs more than once if necessary
200                            to ensure that every mixbuf gets some data.
201                         */
202
203                         uint32_t j = 0;
204                         uint32_t i = 0;
205                         for (i = 0; i < mixbufs_audio && j < bufs_audio; ++i) {
206                                 mixbufs.get_audio(i).read_from (bufs.get_audio(j), nframes);
207                                 ++j;
208
209                                 if (j == bufs_audio) {
210                                         j = 0;
211                                 }
212                         }
213                         /* in case or MIDI track with 0 audio channels */
214                         for (; i < mixbufs_audio; ++i) {
215                                 mixbufs.get_audio(i).silence (nframes);
216                         }
217
218                 } else {
219                         assert (mixbufs.available() >= bufs.count());
220                         mixbufs.read_from (bufs, nframes);
221                 }
222         }
223
224         /* gain control */
225
226         gain_t tgain = target_gain ();
227
228         if (tgain != _current_gain) {
229
230                 /* target gain has changed */
231
232                 _current_gain = Amp::apply_gain (mixbufs, _session.nominal_sample_rate(), nframes, _current_gain, tgain);
233
234         } else if (tgain == GAIN_COEFF_ZERO) {
235
236                 /* we were quiet last time, and we're still supposed to be quiet.
237                 */
238
239                 _meter->reset ();
240                 Amp::apply_simple_gain (mixbufs, nframes, GAIN_COEFF_ZERO);
241                 goto out;
242
243         } else if (tgain != GAIN_COEFF_UNITY) {
244
245                 /* target gain has not changed, but is not zero or unity */
246                 Amp::apply_simple_gain (mixbufs, nframes, tgain);
247         }
248
249         _amp->set_gain_automation_buffer (_session.send_gain_automation_buffer ());
250         _amp->setup_gain_automation (start_sample, end_sample, nframes);
251         _amp->run (mixbufs, start_sample, end_sample, speed, nframes, true);
252
253         _send_delay->run (mixbufs, start_sample, end_sample, speed, nframes, true);
254
255         /* consider metering */
256
257         if (_metering) {
258                 if (_amp->gain_control()->get_value() == GAIN_COEFF_ZERO) {
259                         _meter->reset();
260                 } else {
261                         _meter->run (mixbufs, start_sample, end_sample, speed, nframes, true);
262                 }
263         }
264
265         _thru_delay->run (bufs, start_sample, end_sample, speed, nframes, true);
266
267         /* target will pick up our output when it is ready */
268
269   out:
270         _active = _pending_active;
271 }
272
273 int
274 InternalSend::set_block_size (pframes_t nframes)
275 {
276         if (_send_to) {
277                 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), nframes);
278         }
279
280         return 0;
281 }
282
283 void
284 InternalSend::set_allow_feedback (bool yn)
285 {
286         _allow_feedback = yn;
287         _send_from->processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
288 }
289
290 bool
291 InternalSend::feeds (boost::shared_ptr<Route> other) const
292 {
293         if (_role == Listen || !_allow_feedback) {
294                 return _send_to == other;
295         }
296         return false;
297 }
298
299 XMLNode&
300 InternalSend::state ()
301 {
302         XMLNode& node (Send::state ());
303
304         /* this replaces any existing "type" property */
305
306         node.set_property ("type", "intsend");
307
308         if (_send_to) {
309                 node.set_property ("target", _send_to->id());
310         }
311         node.set_property ("allow-feedback", _allow_feedback);
312
313         return node;
314 }
315
316 int
317 InternalSend::set_state (const XMLNode& node, int version)
318 {
319         init_gain ();
320
321         Send::set_state (node, version);
322
323         if (node.get_property ("target", _send_to_id)) {
324
325                 /* if we're loading a session, the target route may not have been
326                    create yet. make sure we defer till we are sure that it should
327                    exist.
328                 */
329
330                 if (!IO::connecting_legal) {
331                         IO::ConnectingLegal.connect_same_thread (connect_c, boost::bind (&InternalSend::connect_when_legal, this));
332                 } else {
333                         connect_when_legal ();
334                 }
335         }
336
337         node.get_property (X_("allow-feedback"), _allow_feedback);
338
339         return 0;
340 }
341
342 int
343 InternalSend::connect_when_legal ()
344 {
345         connect_c.disconnect ();
346
347         if (_send_to_id == "0") {
348                 /* it vanished before we could connect */
349                 return 0;
350         }
351
352         boost::shared_ptr<Route> sendto;
353
354         if ((sendto = _session.route_by_id (_send_to_id)) == 0) {
355                 error << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endmsg;
356                 cerr << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endl;
357                 return -1;
358         }
359
360         return use_target (sendto);
361 }
362
363 bool
364 InternalSend::can_support_io_configuration (const ChanCount& in, ChanCount& out)
365 {
366         out = in;
367         return true;
368 }
369
370 uint32_t
371 InternalSend::pan_outs () const
372 {
373         /* the number of targets for our panner is determined by what we are
374            sending to, if anything.
375         */
376
377         if (_send_to) {
378                 return _send_to->internal_return()->input_streams().n_audio();
379         }
380
381         return 1; /* zero is more accurate, but 1 is probably safer as a way to
382                    * say "don't pan"
383                    */
384 }
385
386 bool
387 InternalSend::configure_io (ChanCount in, ChanCount out)
388 {
389         bool ret = Send::configure_io (in, out);
390         set_block_size (_session.engine().samples_per_cycle());
391         return ret;
392 }
393
394 bool
395 InternalSend::set_name (const string& str)
396 {
397         /* rules for external sends don't apply to us */
398         return IOProcessor::set_name (str);
399 }
400
401 string
402 InternalSend::display_name () const
403 {
404         if (_role == Aux) {
405                 return string_compose (X_("%1"), _name);
406         } else {
407                 return _name;
408         }
409 }
410
411 bool
412 InternalSend::visible () const
413 {
414         if (_role == Aux) {
415                 return true;
416         }
417
418         return false;
419 }
420
421 void
422 InternalSend::send_to_property_changed (const PropertyChange& what_changed)
423 {
424         if (what_changed.contains (Properties::name)) {
425                 set_name (_send_to->name ());
426         }
427 }
428
429 void
430 InternalSend::set_can_pan (bool yn)
431 {
432         if (_panshell) {
433                 _panshell->set_bypassed (!yn);
434         }
435 }
436
437 void
438 InternalSend::cycle_start (pframes_t /*nframes*/)
439 {
440         for (BufferSet::audio_iterator b = mixbufs.audio_begin(); b != mixbufs.audio_end(); ++b) {
441                 b->prepare ();
442         }
443 }