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