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