possible fix for race between diskstream buffer overwrite and channel setup
[ardour2.git] / libs / ardour / send.cc
blob41dd3e8f860212e5c0b570d4f6a844b2b93a0ceb
1 /*
2 Copyright (C) 2000 Paul Davis
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.
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.
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.
20 #include <iostream>
21 #include <algorithm>
23 #include "pbd/xml++.h"
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"
35 #include "i18n.h"
37 using namespace ARDOUR;
38 using namespace PBD;
39 using namespace std;
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)
45 _amp.reset (new Amp (_session, _mute_master));
46 _meter.reset (new PeakMeter (_session));
49 Send::~Send ()
51 _session.unmark_send_id (_bitslot);
54 void
55 Send::activate ()
57 _amp->activate ();
58 _meter->activate ();
60 Processor::activate ();
63 void
64 Send::deactivate ()
66 _amp->deactivate ();
67 _meter->deactivate ();
68 _meter->reset ();
70 Processor::deactivate ();
73 void
74 Send::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes, bool)
76 if (_output->n_ports() == ChanCount::ZERO) {
77 _meter->reset ();
78 _active = _pending_active;
79 return;
82 if (!_active && !_pending_active) {
83 _meter->reset ();
84 _output->silence (nframes);
85 _active = _pending_active;
86 return;
89 // we have to copy the input, because deliver_output() may alter the buffers
90 // in-place, which a send must never do.
92 BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
93 sendbufs.read_from (bufs, nframes);
94 assert(sendbufs.count() == bufs.count());
96 /* gain control */
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);
103 /* deliver to outputs */
105 Delivery::run (sendbufs, start_frame, end_frame, nframes, true);
107 /* consider metering */
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);
117 /* _active was set to _pending_active by Delivery::run() */
120 XMLNode&
121 Send::get_state(void)
123 return state (true);
126 XMLNode&
127 Send::state (bool full)
129 XMLNode& node = Delivery::state(full);
130 char buf[32];
132 node.add_property ("type", "send");
133 snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
134 node.add_property ("bitslot", buf);
136 return node;
140 Send::set_state (const XMLNode& node, int version)
142 XMLNodeList nlist = node.children();
143 XMLNodeIterator niter;
144 const XMLProperty* prop;
146 Delivery::set_state (node, version);
148 /* don't try to reset bitslot if its already set: this can cause
149 issues with the session's accounting of send ID's
152 if ((prop = node.property ("bitslot")) == 0) {
153 _bitslot = _session.next_send_id();
154 } else {
155 _session.unmark_send_id (_bitslot);
156 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
157 _session.mark_send_id (_bitslot);
160 /* XXX need to load automation state & data for amp */
162 return 0;
165 bool
166 Send::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
168 /* sends have no impact at all on the channel configuration of the
169 streams passing through the route. so, out == in.
172 out = in;
173 return true;
176 bool
177 Send::configure_io (ChanCount in, ChanCount out)
179 if (!_amp->configure_io (in, out) || !_meter->configure_io (in, out)) {
180 return false;
183 if (!Processor::configure_io (in, out)) {
184 return false;
187 reset_panner ();
189 return true;
192 /** Set up the XML description of a send so that its name is unique.
193 * @param state XML send state.
194 * @param session Session.
196 void
197 Send::make_unique (XMLNode &state, Session &session)
199 uint32_t const bitslot = session.next_send_id() + 1;
201 char buf[32];
202 snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
203 state.property("bitslot")->set_value (buf);
205 string const name = string_compose (_("send %1"), bitslot);
207 state.property("name")->set_value (name);
209 XMLNode* io = state.child ("IO");
211 if (io) {
212 io->property("name")->set_value (name);
216 bool
217 Send::set_name (const string& new_name)
219 string unique_name;
221 if (_role == Delivery::Send) {
222 char buf[32];
224 /* rip any existing numeric part of the name, and append the bitslot
227 string::size_type last_letter = new_name.find_last_not_of ("0123456789");
229 if (last_letter != string::npos) {
230 unique_name = new_name.substr (0, last_letter + 1);
231 } else {
232 unique_name = new_name;
235 snprintf (buf, sizeof (buf), "%u", (_bitslot + 1));
236 unique_name += buf;
238 } else {
239 unique_name = new_name;
242 return Delivery::set_name (unique_name);
245 bool
246 Send::display_to_user () const
248 /* we ignore Deliver::_display_to_user */
250 if (_role == Listen) {
251 /* don't make the monitor/control/listen send visible */
252 return false;
255 return true;