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