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