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