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