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