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