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