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