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