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