fix clicking when processors become active/inactive; reduce crazy 2.5sec delay for...
[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 <iostream>
21
22 #include "pbd/error.h"
23 #include "pbd/failed_constructor.h"
24
25 #include "ardour/amp.h"
26 #include "ardour/internal_send.h"
27 #include "ardour/meter.h"
28 #include "ardour/route.h"
29 #include "ardour/session.h"
30
31 #include "i18n.h"
32
33 using namespace PBD;
34 using namespace ARDOUR;
35
36 InternalSend::InternalSend (Session& s, boost::shared_ptr<MuteMaster> mm, boost::shared_ptr<Route> sendto, Delivery::Role role)
37         : Send (s, mm, role)
38         , _send_to (sendto)
39 {
40         if ((target = _send_to->get_return_buffer ()) == 0) {
41                 throw failed_constructor();
42         }
43
44         set_name (sendto->name());
45         
46         _send_to->GoingAway.connect (mem_fun (*this, &InternalSend::send_to_going_away));
47 }
48
49 InternalSend::InternalSend (Session& s, boost::shared_ptr<MuteMaster> mm, const XMLNode& node)
50         : Send (s, mm, node, Delivery::Aux /* will be reset in set_state() */)
51 {
52         set_state (node);
53 }
54
55 InternalSend::~InternalSend ()
56 {
57         if (_send_to) {
58                 _send_to->release_return_buffer ();
59         }
60
61         connect_c.disconnect ();
62 }
63
64 void
65 InternalSend::send_to_going_away ()
66 {
67         target = 0;
68         _send_to.reset ();
69         _send_to_id = "0";
70 }
71
72 void
73 InternalSend::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
74 {
75         if ((!_active && !_pending_active) || !target || !_send_to) {
76                 _meter->reset ();
77                 return;
78         }
79
80         // we have to copy the input, because we may alter the buffers with the amp
81         // in-place, which a send must never do.
82         
83         BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
84         sendbufs.read_from (bufs, nframes);
85         assert(sendbufs.count() == bufs.count());
86
87         /* gain control */
88
89         gain_t tgain = target_gain ();
90         
91         if (tgain != _current_gain) {
92                 
93                 /* target gain has changed */
94
95                 Amp::apply_gain (sendbufs, nframes, _current_gain, tgain);
96                 _current_gain = tgain;
97
98         } else if (tgain == 0.0) {
99
100                 /* we were quiet last time, and we're still supposed to be quiet.
101                 */
102
103                 _meter->reset ();
104                 Amp::apply_simple_gain (sendbufs, nframes, 0.0);
105                 goto out;
106
107         } else if (tgain != 1.0) {
108
109                 /* target gain has not changed, but is not zero or unity */
110                 Amp::apply_simple_gain (sendbufs, nframes, tgain);
111         }
112
113         
114         // Can't automate gain for sends or returns yet because we need different buffers
115         // so that we don't overwrite the main automation data for the route amp
116         // _amp->setup_gain_automation (start_frame, end_frame, nframes);
117
118         _amp->run (sendbufs, start_frame, end_frame, nframes);
119
120         /* consider metering */
121         
122         if (_metering) {
123                 if (_amp->gain_control()->get_value() == 0) {
124                         _meter->reset();
125                 } else {
126                         _meter->run (sendbufs, start_frame, end_frame, nframes);
127                 }
128         }
129
130         /* deliver to target */
131
132         target->merge_from (sendbufs, nframes);
133
134   out:
135         _active = _pending_active;
136 }
137
138 bool
139 InternalSend::feeds (boost::shared_ptr<Route> other) const
140 {
141         return _send_to == other;
142 }
143
144 XMLNode&
145 InternalSend::state (bool full)
146 {
147         XMLNode& node (Send::state (full));
148
149         /* this replaces any existing property */
150
151         node.add_property ("type", "intsend");
152
153         if (_send_to) {
154                 node.add_property ("target", _send_to->id().to_s());
155         }
156         
157         return node;
158 }
159
160 XMLNode&
161 InternalSend::get_state()
162 {
163         return state (true);
164 }
165
166 int
167 InternalSend::set_state (const XMLNode& node)
168 {
169         const XMLProperty* prop;
170
171         Send::set_state (node);
172
173         if ((prop = node.property ("target")) != 0) {
174
175                 _send_to_id = prop->value();
176
177                 /* if we're loading a session, the target route may not have been
178                    create yet. make sure we defer till we are sure that it should
179                    exist.
180                 */
181
182                 if (!IO::connecting_legal) {
183                         connect_c = IO::ConnectingLegal.connect (mem_fun (*this, &InternalSend::connect_when_legal));
184                 } else {
185                         connect_when_legal ();
186                 }
187         }
188         
189         return 0;
190 }
191
192 int
193 InternalSend::connect_when_legal ()
194 {
195         connect_c.disconnect ();
196
197         if (_send_to_id == "0") {
198                 /* it vanished before we could connect */
199                 return 0;
200         }
201
202         if ((_send_to = _session.route_by_id (_send_to_id)) == 0) {
203                 error << X_("cannot find route to connect to") << endmsg;
204                 return -1;
205         } 
206         
207         if ((target = _send_to->get_return_buffer ()) == 0) {
208                 error << X_("target for internal send has no return buffer") << endmsg;
209                 return -1;
210         }
211
212         return 0;
213 }
214
215 bool 
216 InternalSend::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
217 {
218         out = in;
219         return true;
220 }
221
222 bool
223 InternalSend::set_name (const std::string& str)
224 {
225         /* rules for external sends don't apply to us */
226         return IOProcessor::set_name (str);
227 }
228
229 std::string
230 InternalSend::display_name () const
231 {
232         if (_role == Aux) {
233                 return string_compose (X_("aux-%1"), _name);
234         } else {
235                 return _name;
236         }
237 }
238
239 bool
240 InternalSend::visible () const
241 {
242         if (_role == Aux) {
243                 return true;
244         }
245
246         return false; 
247 }