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