2 Copyright (C) 2009 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 "pbd/error.h"
21 #include "pbd/failed_constructor.h"
22 #include "pbd/types_convert.h"
24 #include "ardour/amp.h"
25 #include "ardour/audio_buffer.h"
26 #include "ardour/internal_return.h"
27 #include "ardour/internal_send.h"
28 #include "ardour/meter.h"
29 #include "ardour/panner_shell.h"
30 #include "ardour/route.h"
31 #include "ardour/session.h"
32 #include "ardour/audioengine.h"
36 namespace ARDOUR { class MuteMaster; class Pannable; }
39 using namespace ARDOUR;
42 PBD::Signal1<void, pframes_t> InternalSend::CycleStart;
44 InternalSend::InternalSend (Session& s,
45 boost::shared_ptr<Pannable> p,
46 boost::shared_ptr<MuteMaster> mm,
47 boost::shared_ptr<Route> sendfrom,
48 boost::shared_ptr<Route> sendto,
51 : Send (s, p, mm, role, ignore_bitslot)
52 , _send_from (sendfrom)
53 , _allow_feedback (false)
56 if (use_target (sendto)) {
57 throw failed_constructor();
63 _send_from->DropReferences.connect_same_thread (source_connection, boost::bind (&InternalSend::send_from_going_away, this));
64 CycleStart.connect_same_thread (*this, boost::bind (&InternalSend::cycle_start, this, _1));
67 InternalSend::~InternalSend ()
70 _send_to->remove_send_from_internal_return (this);
75 InternalSend::init_gain ()
77 if (_role == Listen) {
78 /* send to monitor bus is always at unity */
79 _gain_control->set_value (GAIN_COEFF_UNITY, PBD::Controllable::NoGroup);
81 /* aux sends start at -inf dB */
82 _gain_control->set_value (GAIN_COEFF_ZERO, PBD::Controllable::NoGroup);
87 InternalSend::use_target (boost::shared_ptr<Route> sendto)
90 _send_to->remove_send_from_internal_return (this);
95 _send_to->add_send_to_internal_return (this);
97 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
98 mixbufs.set_count (_send_to->internal_return()->input_streams());
102 set_name (sendto->name());
103 _send_to_id = _send_to->id();
105 target_connections.drop_connections ();
107 _send_to->DropReferences.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_going_away, this));
108 _send_to->PropertyChanged.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_property_changed, this, _1));
109 _send_to->io_changed.connect_same_thread (target_connections, boost::bind (&InternalSend::target_io_changed, this));
115 InternalSend::target_io_changed ()
118 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
119 mixbufs.set_count (_send_to->internal_return()->input_streams());
124 InternalSend::send_from_going_away ()
130 InternalSend::send_to_going_away ()
132 target_connections.drop_connections ();
138 InternalSend::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, double speed, pframes_t nframes, bool)
140 if ((!_active && !_pending_active) || !_send_to) {
145 // we have to copy the input, because we may alter the buffers with the amp
146 // in-place, which a send must never do.
148 if (_panshell && !_panshell->bypassed() && role() != Listen) {
149 if (mixbufs.count ().n_audio () > 0) {
150 _panshell->run (bufs, mixbufs, start_frame, end_frame, nframes);
153 /* non-audio data will not have been copied by the panner, do it now
154 * if there are more buffers available than send buffers, ignore them,
155 * if there are less, copy the last as IO::copy_to_output does. */
157 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
158 if (*t != DataType::AUDIO) {
159 BufferSet::iterator o = mixbufs.begin(*t);
160 BufferSet::iterator i = bufs.begin(*t);
162 while (i != bufs.end(*t) && o != mixbufs.end(*t)) {
163 o->read_from (*i, nframes);
167 while (o != mixbufs.end(*t)) {
168 o->silence(nframes, 0);
174 if (role() == Listen) {
175 /* We're going to the monitor bus, so discard MIDI data */
177 uint32_t const bufs_audio = bufs.count().get (DataType::AUDIO);
178 uint32_t const mixbufs_audio = mixbufs.count().get (DataType::AUDIO);
180 /* monitor-section has same number of channels as master-bus (on creation).
182 * There is no clear answer what should happen when trying to PFL or AFL
183 * a track that has more channels (bufs_audio from source-track is
184 * larger than mixbufs).
186 * There are two options:
187 * 1: discard additional channels (current)
189 * 2: require the monitor-section to have at least as many channels
190 * as the largest count of any route
192 //assert (mixbufs.available().get (DataType::AUDIO) >= bufs_audio);
194 /* Copy bufs into mixbufs, going round bufs more than once if necessary
195 to ensure that every mixbuf gets some data.
200 for (i = 0; i < mixbufs_audio && j < bufs_audio; ++i) {
201 mixbufs.get_audio(i).read_from (bufs.get_audio(j), nframes);
204 if (j == bufs_audio) {
208 /* in case or MIDI track with 0 audio channels */
209 for (; i < mixbufs_audio; ++i) {
210 mixbufs.get_audio(i).silence (nframes);
214 assert (mixbufs.available() >= bufs.count());
215 mixbufs.read_from (bufs, nframes);
221 gain_t tgain = target_gain ();
223 if (tgain != _current_gain) {
225 /* target gain has changed */
227 _current_gain = Amp::apply_gain (mixbufs, _session.nominal_frame_rate(), nframes, _current_gain, tgain);
229 } else if (tgain == GAIN_COEFF_ZERO) {
231 /* we were quiet last time, and we're still supposed to be quiet.
235 Amp::apply_simple_gain (mixbufs, nframes, GAIN_COEFF_ZERO);
238 } else if (tgain != GAIN_COEFF_UNITY) {
240 /* target gain has not changed, but is not zero or unity */
241 Amp::apply_simple_gain (mixbufs, nframes, tgain);
244 _amp->set_gain_automation_buffer (_session.send_gain_automation_buffer ());
245 _amp->setup_gain_automation (start_frame, end_frame, nframes);
246 _amp->run (mixbufs, start_frame, end_frame, speed, nframes, true);
248 _delayline->run (mixbufs, start_frame, end_frame, speed, nframes, true);
250 /* consider metering */
253 if (_amp->gain_control()->get_value() == GAIN_COEFF_ZERO) {
256 _meter->run (mixbufs, start_frame, end_frame, speed, nframes, true);
260 /* target will pick up our output when it is ready */
263 _active = _pending_active;
267 InternalSend::set_block_size (pframes_t nframes)
270 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), nframes);
277 InternalSend::set_allow_feedback (bool yn)
279 _allow_feedback = yn;
280 _send_from->processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
284 InternalSend::feeds (boost::shared_ptr<Route> other) const
286 if (_role == Listen || !_allow_feedback) {
287 return _send_to == other;
293 InternalSend::state (bool full)
295 XMLNode& node (Send::state (full));
297 /* this replaces any existing "type" property */
299 node.set_property ("type", "intsend");
302 node.set_property ("target", _send_to->id());
304 node.set_property ("allow-feedback", _allow_feedback);
310 InternalSend::get_state()
316 InternalSend::set_state (const XMLNode& node, int version)
320 Send::set_state (node, version);
322 if (node.get_property ("target", _send_to_id)) {
324 /* if we're loading a session, the target route may not have been
325 create yet. make sure we defer till we are sure that it should
329 if (!IO::connecting_legal) {
330 IO::ConnectingLegal.connect_same_thread (connect_c, boost::bind (&InternalSend::connect_when_legal, this));
332 connect_when_legal ();
336 node.get_property (X_("allow-feedback"), _allow_feedback);
342 InternalSend::connect_when_legal ()
344 connect_c.disconnect ();
346 if (_send_to_id == "0") {
347 /* it vanished before we could connect */
351 boost::shared_ptr<Route> sendto;
353 if ((sendto = _session.route_by_id (_send_to_id)) == 0) {
354 error << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endmsg;
355 cerr << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endl;
359 return use_target (sendto);
363 InternalSend::can_support_io_configuration (const ChanCount& in, ChanCount& out)
370 InternalSend::pan_outs () const
372 /* the number of targets for our panner is determined by what we are
373 sending to, if anything.
377 return _send_to->internal_return()->input_streams().n_audio();
380 return 1; /* zero is more accurate, but 1 is probably safer as a way to
386 InternalSend::configure_io (ChanCount in, ChanCount out)
388 bool ret = Send::configure_io (in, out);
389 set_block_size (_session.engine().samples_per_cycle());
394 InternalSend::set_name (const string& str)
396 /* rules for external sends don't apply to us */
397 return IOProcessor::set_name (str);
401 InternalSend::display_name () const
404 return string_compose (X_("%1"), _name);
411 InternalSend::visible () const
421 InternalSend::send_to_property_changed (const PropertyChange& what_changed)
423 if (what_changed.contains (Properties::name)) {
424 set_name (_send_to->name ());
429 InternalSend::set_can_pan (bool yn)
432 _panshell->set_bypassed (!yn);
437 InternalSend::cycle_start (pframes_t /*nframes*/)
439 for (BufferSet::audio_iterator b = mixbufs.audio_begin(); b != mixbufs.audio_end(); ++b) {