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