move speed quietning code into Delivery, where it belongs.
[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         _meter.reset (new PeakMeter (_session, name()));
90
91         _delayline.reset (new DelayLine (_session, name()));
92
93         add_control (_amp->gain_control ());
94
95         if (panner_shell()) {
96                 panner_shell()->Changed.connect_same_thread (*this, boost::bind (&Send::panshell_changed, this));
97         }
98 }
99
100 Send::~Send ()
101 {
102         _session.unmark_send_id (_bitslot);
103 }
104
105 void
106 Send::activate ()
107 {
108         _amp->activate ();
109         _meter->activate ();
110
111         Processor::activate ();
112 }
113
114 void
115 Send::deactivate ()
116 {
117         _amp->deactivate ();
118         _meter->deactivate ();
119         _meter->reset ();
120
121         Processor::deactivate ();
122 }
123
124 void
125 Send::set_delay_in(framecnt_t delay)
126 {
127         if (!_delayline) return;
128         if (_delay_in == delay) {
129                 return;
130         }
131         _delay_in = delay;
132
133         DEBUG_TRACE (DEBUG::LatencyCompensation,
134                         string_compose ("Send::set_delay_in(%1) + %2 = %3\n",
135                                 delay, _delay_out, _delay_out + _delay_in));
136         _delayline.get()->set_delay(_delay_out + _delay_in);
137 }
138
139 void
140 Send::set_delay_out(framecnt_t delay)
141 {
142         if (!_delayline) return;
143         if (_delay_out == delay) {
144                 return;
145         }
146         _delay_out = delay;
147         DEBUG_TRACE (DEBUG::LatencyCompensation,
148                         string_compose ("Send::set_delay_out(%1) + %2 = %3\n",
149                                 delay, _delay_in, _delay_out + _delay_in));
150         _delayline.get()->set_delay(_delay_out + _delay_in);
151 }
152
153 void
154 Send::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
155 {
156         if (_output->n_ports() == ChanCount::ZERO) {
157                 _meter->reset ();
158                 _active = _pending_active;
159                 return;
160         }
161
162         if (!_active && !_pending_active) {
163                 _meter->reset ();
164                 _output->silence (nframes);
165                 _active = _pending_active;
166                 return;
167         }
168
169         // we have to copy the input, because deliver_output() may alter the buffers
170         // in-place, which a send must never do.
171
172         BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
173         sendbufs.read_from (bufs, nframes);
174         assert(sendbufs.count() == bufs.count());
175
176         /* automatable gain control */
177
178         _amp->set_gain_automation_buffer (_session.send_gain_automation_buffer ());
179         _amp->setup_gain_automation (start_frame, end_frame, nframes);
180         _amp->run (sendbufs, start_frame, end_frame, nframes, true);
181
182         _delayline->run (sendbufs, start_frame, end_frame, nframes, true);
183
184         /* deliver to outputs */
185
186         Delivery::run (sendbufs, start_frame, end_frame, nframes, true);
187
188         /* consider metering */
189
190         if (_metering) {
191                 if (_amp->gain_control()->get_value() == 0) {
192                         _meter->reset();
193                 } else {
194                         _meter->run (*_output_buffers, start_frame, end_frame, nframes, true);
195                 }
196         }
197
198         /* _active was set to _pending_active by Delivery::run() */
199 }
200
201 XMLNode&
202 Send::get_state(void)
203 {
204         return state (true);
205 }
206
207 XMLNode&
208 Send::state (bool full)
209 {
210         XMLNode& node = Delivery::state(full);
211         char buf[32];
212
213         node.add_property ("type", "send");
214         snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
215
216         if (_role != Listen) {
217                 node.add_property ("bitslot", buf);
218         }
219
220         node.add_child_nocopy (_amp->state (full));
221
222         return node;
223 }
224
225 int
226 Send::set_state (const XMLNode& node, int version)
227 {
228         if (version < 3000) {
229                 return set_state_2X (node, version);
230         }
231
232         const XMLProperty* prop;
233
234         Delivery::set_state (node, version);
235
236         if (node.property ("ignore-bitslot") == 0) {
237
238                 /* don't try to reset bitslot if there is a node for it already: this can cause
239                    issues with the session's accounting of send ID's
240                 */
241                 
242                 if ((prop = node.property ("bitslot")) == 0) {
243                         if (_role == Delivery::Aux) {
244                                 _bitslot = _session.next_aux_send_id ();
245                         } else if (_role == Delivery::Send) {
246                                 _bitslot = _session.next_send_id ();
247                         } else {
248                                 // bitslot doesn't matter but make it zero anyway
249                                 _bitslot = 0;
250                         }
251                 } else {
252                         if (_role == Delivery::Aux) {
253                                 _session.unmark_aux_send_id (_bitslot);
254                                 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
255                                 _session.mark_aux_send_id (_bitslot);
256                         } else if (_role == Delivery::Send) {
257                                 _session.unmark_send_id (_bitslot);
258                                 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
259                                 _session.mark_send_id (_bitslot);
260                         } else {
261                                 // bitslot doesn't matter but make it zero anyway
262                                 _bitslot = 0;
263                         }
264                 }
265         }
266         
267         XMLNodeList nlist = node.children();
268         for (XMLNodeIterator i = nlist.begin(); i != nlist.end(); ++i) {
269                 if ((*i)->name() == X_("Processor")) {
270                         _amp->set_state (**i, version);
271                 }
272         }
273
274         return 0;
275 }
276
277 int
278 Send::set_state_2X (const XMLNode& node, int /* version */)
279 {
280         /* use the IO's name for the name of the send */
281         XMLNodeList const & children = node.children ();
282
283         XMLNodeList::const_iterator i = children.begin();
284         while (i != children.end() && (*i)->name() != X_("Redirect")) {
285                 ++i;
286         }
287
288         if (i == children.end()) {
289                 return -1;
290         }
291
292         XMLNodeList const & grand_children = (*i)->children ();
293         XMLNodeList::const_iterator j = grand_children.begin ();
294         while (j != grand_children.end() && (*j)->name() != X_("IO")) {
295                 ++j;
296         }
297
298         if (j == grand_children.end()) {
299                 return -1;
300         }
301
302         XMLProperty const * prop = (*j)->property (X_("name"));
303         if (!prop) {
304                 return -1;
305         }
306
307         set_name (prop->value ());
308
309         return 0;
310 }
311
312 bool
313 Send::can_support_io_configuration (const ChanCount& in, ChanCount& out)
314 {
315         /* sends have no impact at all on the channel configuration of the
316            streams passing through the route. so, out == in.
317         */
318
319         out = in;
320         return true;
321 }
322
323 /** Caller must hold process lock */
324 bool
325 Send::configure_io (ChanCount in, ChanCount out)
326 {
327         if (!_amp->configure_io (in, out)) {
328                 return false;
329         }
330
331         if (!Processor::configure_io (in, out)) {
332                 return false;
333         }
334
335         if (!_meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()))) {
336                 return false;
337         }
338
339         if (_delayline && !_delayline->configure_io(in, out)) {
340                 cerr << "send delayline config failed\n";
341                 return false;
342         }
343
344         reset_panner ();
345
346         return true;
347 }
348
349 void
350 Send::panshell_changed ()
351 {
352         _meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()));
353 }
354
355 bool
356 Send::set_name (const string& new_name)
357 {
358         string unique_name;
359
360         if (_role == Delivery::Send) {
361                 char buf[32];
362
363                 /* rip any existing numeric part of the name, and append the bitslot
364                  */
365
366                 string::size_type last_letter = new_name.find_last_not_of ("0123456789");
367
368                 if (last_letter != string::npos) {
369                         unique_name = new_name.substr (0, last_letter + 1);
370                 } else {
371                         unique_name = new_name;
372                 }
373
374                 snprintf (buf, sizeof (buf), "%u", (_bitslot + 1));
375                 unique_name += buf;
376
377         } else {
378                 unique_name = new_name;
379         }
380
381         return Delivery::set_name (unique_name);
382 }
383
384 bool
385 Send::display_to_user () const
386 {
387         /* we ignore Deliver::_display_to_user */
388
389         if (_role == Listen) {
390                 /* don't make the monitor/control/listen send visible */
391                 return false;
392         }
393
394         return true;
395 }
396
397 string
398 Send::value_as_string (boost::shared_ptr<AutomationControl> ac) const
399 {
400         return _amp->value_as_string (ac);
401 }
402
403