change ownership of the AutomationControl used by Amp.
[ardour.git] / libs / ardour / send.cc
1 /*
2     Copyright (C) 2000 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 #include <algorithm>
22
23 #include "pbd/xml++.h"
24 #include "pbd/boost_debug.h"
25
26 #include "ardour/amp.h"
27 #include "ardour/buffer_set.h"
28 #include "ardour/debug.h"
29 #include "ardour/io.h"
30 #include "ardour/meter.h"
31 #include "ardour/panner_shell.h"
32 #include "ardour/send.h"
33 #include "ardour/session.h"
34
35 #include "i18n.h"
36
37 namespace ARDOUR {
38 class AutomationControl;
39 class MuteMaster;
40 class Pannable;
41 }
42
43 using namespace ARDOUR;
44 using namespace PBD;
45 using namespace std;
46
47 string
48 Send::name_and_id_new_send (Session& s, Role r, uint32_t& bitslot, bool ignore_bitslot)
49 {
50         if (ignore_bitslot) {
51                 /* this happens during initial construction of sends from XML,
52                    before they get ::set_state() called. lets not worry about
53                    it.
54                 */
55                 bitslot = 0;
56                 return string ();
57         }
58
59         switch (r) {
60         case Delivery::Aux:
61                 return string_compose (_("aux %1"), (bitslot = s.next_aux_send_id ()) + 1);
62         case Delivery::Listen:
63                 return _("listen"); // no ports, no need for numbering
64         case Delivery::Send:
65                 return string_compose (_("send %1"), (bitslot = s.next_send_id ()) + 1);
66         default:
67                 fatal << string_compose (_("programming error: send created using role %1"), enum_2_string (r)) << endmsg;
68                 abort(); /*NOTREACHED*/
69                 return string();
70         }
71
72 }
73
74 Send::Send (Session& s, boost::shared_ptr<Pannable> p, boost::shared_ptr<MuteMaster> mm, Role r, bool ignore_bitslot)
75         : Delivery (s, p, mm, name_and_id_new_send (s, r, _bitslot, ignore_bitslot), r)
76         , _metering (false)
77         , _delay_in (0)
78         , _delay_out (0)
79 {
80         if (_role == Listen) {
81                 /* we don't need to do this but it keeps things looking clean
82                    in a debugger. _bitslot is not used by listen sends.
83                 */
84                 _bitslot = 0;
85         }
86
87         //boost_debug_shared_ptr_mark_interesting (this, "send");
88
89         boost::shared_ptr<AutomationList> gl (new AutomationList (Evoral::Parameter (GainAutomation)));
90         _gain_control = boost::shared_ptr<Amp::GainControl> (new Amp::GainControl (_session, Evoral::Parameter(GainAutomation), gl));
91         add_control (_gain_control);
92
93         _amp.reset (new Amp (_session, _("Fader"), _gain_control, true));
94         _meter.reset (new PeakMeter (_session, name()));
95
96         _delayline.reset (new DelayLine (_session, name()));
97
98         if (panner_shell()) {
99                 panner_shell()->Changed.connect_same_thread (*this, boost::bind (&Send::panshell_changed, this));
100         }
101 }
102
103 Send::~Send ()
104 {
105         _session.unmark_send_id (_bitslot);
106 }
107
108 void
109 Send::activate ()
110 {
111         _amp->activate ();
112         _meter->activate ();
113
114         Processor::activate ();
115 }
116
117 void
118 Send::deactivate ()
119 {
120         _amp->deactivate ();
121         _meter->deactivate ();
122         _meter->reset ();
123
124         Processor::deactivate ();
125 }
126
127 void
128 Send::set_delay_in(framecnt_t delay)
129 {
130         if (!_delayline) return;
131         if (_delay_in == delay) {
132                 return;
133         }
134         _delay_in = delay;
135
136         DEBUG_TRACE (DEBUG::LatencyCompensation,
137                         string_compose ("Send::set_delay_in(%1) + %2 = %3\n",
138                                 delay, _delay_out, _delay_out + _delay_in));
139         _delayline.get()->set_delay(_delay_out + _delay_in);
140 }
141
142 void
143 Send::set_delay_out(framecnt_t delay)
144 {
145         if (!_delayline) return;
146         if (_delay_out == delay) {
147                 return;
148         }
149         _delay_out = delay;
150         DEBUG_TRACE (DEBUG::LatencyCompensation,
151                         string_compose ("Send::set_delay_out(%1) + %2 = %3\n",
152                                 delay, _delay_in, _delay_out + _delay_in));
153         _delayline.get()->set_delay(_delay_out + _delay_in);
154 }
155
156 void
157 Send::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
158 {
159         if (_output->n_ports() == ChanCount::ZERO) {
160                 _meter->reset ();
161                 _active = _pending_active;
162                 return;
163         }
164
165         if (!_active && !_pending_active) {
166                 _meter->reset ();
167                 _output->silence (nframes);
168                 _active = _pending_active;
169                 return;
170         }
171
172         // we have to copy the input, because deliver_output() may alter the buffers
173         // in-place, which a send must never do.
174
175         BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
176         sendbufs.read_from (bufs, nframes);
177         assert(sendbufs.count() == bufs.count());
178
179         /* gain control */
180
181         _amp->set_gain_automation_buffer (_session.send_gain_automation_buffer ());
182         _amp->setup_gain_automation (start_frame, end_frame, nframes);
183         _amp->run (sendbufs, start_frame, end_frame, nframes, true);
184
185         _delayline->run (sendbufs, start_frame, end_frame, nframes, true);
186
187         /* deliver to outputs */
188
189         Delivery::run (sendbufs, start_frame, end_frame, nframes, true);
190
191         /* consider metering */
192
193         if (_metering) {
194                 if (_amp->gain_control()->get_value() == 0) {
195                         _meter->reset();
196                 } else {
197                         _meter->run (*_output_buffers, start_frame, end_frame, nframes, true);
198                 }
199         }
200
201         /* _active was set to _pending_active by Delivery::run() */
202 }
203
204 XMLNode&
205 Send::get_state(void)
206 {
207         return state (true);
208 }
209
210 XMLNode&
211 Send::state (bool full)
212 {
213         XMLNode& node = Delivery::state(full);
214         char buf[32];
215
216         node.add_property ("type", "send");
217         snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
218
219         if (_role != Listen) {
220                 node.add_property ("bitslot", buf);
221         }
222
223         node.add_child_nocopy (_amp->state (full));
224
225         return node;
226 }
227
228 int
229 Send::set_state (const XMLNode& node, int version)
230 {
231         if (version < 3000) {
232                 return set_state_2X (node, version);
233         }
234
235         const XMLProperty* prop;
236
237         Delivery::set_state (node, version);
238
239         if (node.property ("ignore-bitslot") == 0) {
240
241                 /* don't try to reset bitslot if there is a node for it already: this can cause
242                    issues with the session's accounting of send ID's
243                 */
244
245                 if ((prop = node.property ("bitslot")) == 0) {
246                         if (_role == Delivery::Aux) {
247                                 _bitslot = _session.next_aux_send_id ();
248                         } else if (_role == Delivery::Send) {
249                                 _bitslot = _session.next_send_id ();
250                         } else {
251                                 // bitslot doesn't matter but make it zero anyway
252                                 _bitslot = 0;
253                         }
254                 } else {
255                         if (_role == Delivery::Aux) {
256                                 _session.unmark_aux_send_id (_bitslot);
257                                 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
258                                 _session.mark_aux_send_id (_bitslot);
259                         } else if (_role == Delivery::Send) {
260                                 _session.unmark_send_id (_bitslot);
261                                 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
262                                 _session.mark_send_id (_bitslot);
263                         } else {
264                                 // bitslot doesn't matter but make it zero anyway
265                                 _bitslot = 0;
266                         }
267                 }
268         }
269
270         XMLNodeList nlist = node.children();
271         for (XMLNodeIterator i = nlist.begin(); i != nlist.end(); ++i) {
272                 if ((*i)->name() == X_("Processor")) {
273                         _amp->set_state (**i, version);
274                 }
275         }
276
277         return 0;
278 }
279
280 int
281 Send::set_state_2X (const XMLNode& node, int /* version */)
282 {
283         /* use the IO's name for the name of the send */
284         XMLNodeList const & children = node.children ();
285
286         XMLNodeList::const_iterator i = children.begin();
287         while (i != children.end() && (*i)->name() != X_("Redirect")) {
288                 ++i;
289         }
290
291         if (i == children.end()) {
292                 return -1;
293         }
294
295         XMLNodeList const & grand_children = (*i)->children ();
296         XMLNodeList::const_iterator j = grand_children.begin ();
297         while (j != grand_children.end() && (*j)->name() != X_("IO")) {
298                 ++j;
299         }
300
301         if (j == grand_children.end()) {
302                 return -1;
303         }
304
305         XMLProperty const * prop = (*j)->property (X_("name"));
306         if (!prop) {
307                 return -1;
308         }
309
310         set_name (prop->value ());
311
312         return 0;
313 }
314
315 bool
316 Send::can_support_io_configuration (const ChanCount& in, ChanCount& out)
317 {
318         /* sends have no impact at all on the channel configuration of the
319            streams passing through the route. so, out == in.
320         */
321
322         out = in;
323         return true;
324 }
325
326 /** Caller must hold process lock */
327 bool
328 Send::configure_io (ChanCount in, ChanCount out)
329 {
330         if (!_amp->configure_io (in, out)) {
331                 return false;
332         }
333
334         if (!Processor::configure_io (in, out)) {
335                 return false;
336         }
337
338         if (!_meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()))) {
339                 return false;
340         }
341
342         if (_delayline && !_delayline->configure_io(in, out)) {
343                 cerr << "send delayline config failed\n";
344                 return false;
345         }
346
347         reset_panner ();
348
349         return true;
350 }
351
352 void
353 Send::panshell_changed ()
354 {
355         _meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()));
356 }
357
358 bool
359 Send::set_name (const string& new_name)
360 {
361         string unique_name;
362
363         if (_role == Delivery::Send) {
364                 char buf[32];
365
366                 /* rip any existing numeric part of the name, and append the bitslot
367                  */
368
369                 string::size_type last_letter = new_name.find_last_not_of ("0123456789");
370
371                 if (last_letter != string::npos) {
372                         unique_name = new_name.substr (0, last_letter + 1);
373                 } else {
374                         unique_name = new_name;
375                 }
376
377                 snprintf (buf, sizeof (buf), "%u", (_bitslot + 1));
378                 unique_name += buf;
379
380         } else {
381                 unique_name = new_name;
382         }
383
384         return Delivery::set_name (unique_name);
385 }
386
387 bool
388 Send::display_to_user () const
389 {
390         /* we ignore Deliver::_display_to_user */
391
392         if (_role == Listen) {
393                 /* don't make the monitor/control/listen send visible */
394                 return false;
395         }
396
397         return true;
398 }
399
400 string
401 Send::value_as_string (boost::shared_ptr<AutomationControl> ac) const
402 {
403         return _amp->value_as_string (ac);
404 }