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