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