Do a topological sort of the route list before passing it to
[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.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 "i18n.h"
35
36 using namespace PBD;
37 using namespace ARDOUR;
38 using namespace std;
39
40 InternalSend::InternalSend (Session& s, boost::shared_ptr<Pannable> p, boost::shared_ptr<MuteMaster> mm, boost::shared_ptr<Route> sendto, Delivery::Role role)
41         : Send (s, p, mm, role)
42 {
43         if (sendto) {
44                 if (use_target (sendto)) {
45                         throw failed_constructor();
46                 }
47         }
48
49         init_gain ();
50 }
51
52 InternalSend::~InternalSend ()
53 {
54         if (_send_to) {
55                 _send_to->remove_send_from_internal_return (this);
56         }
57 }
58
59 void
60 InternalSend::init_gain ()
61 {
62         if (_role == Listen) {
63                 /* send to monitor bus is always at unity */
64                 _amp->set_gain (1.0, this);
65         } else {
66                 /* aux sends start at -inf dB */
67                 _amp->set_gain (0, this);
68         }
69 }
70
71 int
72 InternalSend::use_target (boost::shared_ptr<Route> sendto)
73 {
74         if (_send_to) {
75                 _send_to->remove_send_from_internal_return (this);
76         }
77
78         _send_to = sendto;
79
80         _send_to->add_send_to_internal_return (this);
81
82         mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
83         mixbufs.set_count (_send_to->internal_return()->input_streams());
84
85         reset_panner ();
86
87         set_name (sendto->name());
88         _send_to_id = _send_to->id();
89
90         target_connections.drop_connections ();
91
92         _send_to->DropReferences.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_going_away, this));
93         _send_to->PropertyChanged.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_property_changed, this, _1));;
94
95         return 0;
96 }
97
98 void
99 InternalSend::send_to_going_away ()
100 {
101         target_connections.drop_connections ();
102         _send_to.reset ();
103         _send_to_id = "0";
104 }
105
106 void
107 InternalSend::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
108 {
109         if ((!_active && !_pending_active) || !_send_to) {
110                 _meter->reset ();
111                 return;
112         }
113
114         // we have to copy the input, because we may alter the buffers with the amp
115         // in-place, which a send must never do.
116
117         if (_panshell && !_panshell->bypassed()) {
118                 _panshell->run (bufs, mixbufs, start_frame, end_frame, nframes);
119         } else {
120                 if (role() == Listen) {
121                         /* We're going to the monitor bus, so discard MIDI data */
122                         assert (mixbufs.available().get (DataType::AUDIO) >= bufs.count().get (DataType::AUDIO));
123                         mixbufs.read_from (bufs, nframes, DataType::AUDIO);
124                 } else {
125                         assert (mixbufs.available() >= bufs.count());
126                         mixbufs.read_from (bufs, nframes);
127                 }
128         }
129
130         /* gain control */
131
132         gain_t tgain = target_gain ();
133
134         if (tgain != _current_gain) {
135
136                 /* target gain has changed */
137
138                 Amp::apply_gain (mixbufs, nframes, _current_gain, tgain);
139                 _current_gain = tgain;
140
141         } else if (tgain == 0.0) {
142
143                 /* we were quiet last time, and we're still supposed to be quiet.
144                 */
145
146                 _meter->reset ();
147                 Amp::apply_simple_gain (mixbufs, nframes, 0.0);
148                 goto out;
149
150         } else if (tgain != 1.0) {
151
152                 /* target gain has not changed, but is not zero or unity */
153                 Amp::apply_simple_gain (mixbufs, nframes, tgain);
154         }
155
156         // Can't automate gain for sends or returns yet because we need different buffers
157         // so that we don't overwrite the main automation data for the route amp
158         // _amp->setup_gain_automation (start_frame, end_frame, nframes);
159
160         _amp->run (mixbufs, start_frame, end_frame, nframes, true);
161
162         /* consider metering */
163
164         if (_metering) {
165                 if (_amp->gain_control()->get_value() == 0) {
166                         _meter->reset();
167                 } else {
168                         _meter->run (mixbufs, start_frame, end_frame, nframes, true);
169                 }
170         }
171
172         /* target will pick up our output when it is ready */
173
174   out:
175         _active = _pending_active;
176 }
177
178 int
179 InternalSend::set_block_size (pframes_t nframes)
180 {
181         if (_send_to) {
182                 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), nframes);
183         }
184
185         return 0;
186 }
187
188 bool
189 InternalSend::feeds (boost::shared_ptr<Route> other) const
190 {
191         return _send_to == other;
192 }
193
194 XMLNode&
195 InternalSend::state (bool full)
196 {
197         XMLNode& node (Send::state (full));
198
199         /* this replaces any existing "type" property */
200
201         node.add_property ("type", "intsend");
202
203         if (_send_to) {
204                 node.add_property ("target", _send_to->id().to_s());
205         }
206
207         return node;
208 }
209
210 XMLNode&
211 InternalSend::get_state()
212 {
213         return state (true);
214 }
215
216 int
217 InternalSend::set_state (const XMLNode& node, int version)
218 {
219         const XMLProperty* prop;
220
221         init_gain ();
222
223         Send::set_state (node, version);
224
225         if ((prop = node.property ("target")) != 0) {
226
227                 _send_to_id = prop->value();
228
229                 /* if we're loading a session, the target route may not have been
230                    create yet. make sure we defer till we are sure that it should
231                    exist.
232                 */
233
234                 if (!IO::connecting_legal) {
235                         IO::ConnectingLegal.connect_same_thread (connect_c, boost::bind (&InternalSend::connect_when_legal, this));
236                 } else {
237                         connect_when_legal ();
238                 }
239         }
240
241         return 0;
242 }
243
244 int
245 InternalSend::connect_when_legal ()
246 {
247         connect_c.disconnect ();
248
249         if (_send_to_id == "0") {
250                 /* it vanished before we could connect */
251                 return 0;
252         }
253
254         boost::shared_ptr<Route> sendto;
255
256         if ((sendto = _session.route_by_id (_send_to_id)) == 0) {
257                 error << X_("cannot find route to connect to") << endmsg;
258                 return -1;
259         }
260
261         return use_target (sendto);
262 }
263
264 bool
265 InternalSend::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
266 {
267         out = in;
268         return true;
269 }
270
271 uint32_t
272 InternalSend::pan_outs () const
273 {
274         /* the number of targets for our panner is determined by what we are
275            sending to, if anything.
276         */
277
278         if (_send_to) {
279                 return _send_to->internal_return()->input_streams().n_audio();
280         }
281
282         return 1; /* zero is more accurate, but 1 is probably safer as a way to
283                    * say "don't pan"
284                    */
285 }
286
287 bool
288 InternalSend::configure_io (ChanCount in, ChanCount out)
289 {
290         bool ret = Send::configure_io (in, out);
291         set_block_size (_session.engine().frames_per_cycle());
292         return ret;
293 }
294
295 bool
296 InternalSend::set_name (const string& str)
297 {
298         /* rules for external sends don't apply to us */
299         return IOProcessor::set_name (str);
300 }
301
302 string
303 InternalSend::display_name () const
304 {
305         if (_role == Aux) {
306                 return string_compose (X_("aux-%1"), _name);
307         } else {
308                 return _name;
309         }
310 }
311
312 bool
313 InternalSend::visible () const
314 {
315         if (_role == Aux) {
316                 return true;
317         }
318
319         return false;
320 }
321
322 void
323 InternalSend::send_to_property_changed (const PropertyChange& what_changed)
324 {
325         if (what_changed.contains (Properties::name)) {
326                 set_name (_send_to->name ());
327         }
328 }
329
330 void
331 InternalSend::set_can_pan (bool yn)
332 {
333         if (_panshell) {
334                 _panshell->set_bypassed (!yn);
335         }
336 }
337
338 void
339 InternalSend::cycle_start (pframes_t nframes)
340 {
341         Delivery::cycle_start (nframes);
342
343         for (BufferSet::audio_iterator b = mixbufs.audio_begin(); b != mixbufs.audio_end(); ++b) {
344                 (*b).prepare ();
345         }
346 }