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