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