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