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