2 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
4 * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2013-2017 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2018 Len Ovens <len@ovenwerks.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "pbd/error.h"
24 #include "pbd/failed_constructor.h"
25 #include "pbd/types_convert.h"
27 #include "ardour/amp.h"
28 #include "ardour/audio_buffer.h"
29 #include "ardour/delayline.h"
30 #include "ardour/internal_return.h"
31 #include "ardour/internal_send.h"
32 #include "ardour/meter.h"
33 #include "ardour/panner_shell.h"
34 #include "ardour/route.h"
35 #include "ardour/session.h"
36 #include "ardour/audioengine.h"
40 namespace ARDOUR { class MuteMaster; class Pannable; }
43 using namespace ARDOUR;
46 PBD::Signal1<void, pframes_t> InternalSend::CycleStart;
48 InternalSend::InternalSend (Session& s,
49 boost::shared_ptr<Pannable> p,
50 boost::shared_ptr<MuteMaster> mm,
51 boost::shared_ptr<Route> sendfrom,
52 boost::shared_ptr<Route> sendto,
55 : Send (s, p, mm, role, ignore_bitslot)
56 , _send_from (sendfrom)
57 , _allow_feedback (false)
60 if (use_target (sendto)) {
61 throw failed_constructor();
67 _send_from->DropReferences.connect_same_thread (source_connection, boost::bind (&InternalSend::send_from_going_away, this));
68 CycleStart.connect_same_thread (*this, boost::bind (&InternalSend::cycle_start, this, _1));
71 InternalSend::~InternalSend ()
74 _send_to->remove_send_from_internal_return (this);
79 InternalSend::init_gain ()
81 if (_role == Listen) {
82 /* send to monitor bus is always at unity */
83 _gain_control->set_value (GAIN_COEFF_UNITY, PBD::Controllable::NoGroup);
85 /* aux sends start at -inf dB */
86 _gain_control->set_value (GAIN_COEFF_ZERO, PBD::Controllable::NoGroup);
91 InternalSend::use_target (boost::shared_ptr<Route> sendto)
94 _send_to->remove_send_from_internal_return (this);
99 _send_to->add_send_to_internal_return (this);
101 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
102 mixbufs.set_count (_send_to->internal_return()->input_streams());
104 _meter->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()));
106 _send_delay->configure_io (ChanCount (DataType::AUDIO, pan_outs()), ChanCount (DataType::AUDIO, pan_outs()));
110 set_name (sendto->name());
111 _send_to_id = _send_to->id();
113 target_connections.drop_connections ();
115 _send_to->DropReferences.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_going_away, this));
116 _send_to->PropertyChanged.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_property_changed, this, _1));
117 _send_to->io_changed.connect_same_thread (target_connections, boost::bind (&InternalSend::target_io_changed, this));
123 InternalSend::target_io_changed ()
126 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
127 mixbufs.set_count (_send_to->internal_return()->input_streams());
132 InternalSend::send_from_going_away ()
138 InternalSend::send_to_going_away ()
140 target_connections.drop_connections ();
146 InternalSend::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool)
148 if ((!_active && !_pending_active) || !_send_to) {
153 // we have to copy the input, because we may alter the buffers with the amp
154 // in-place, which a send must never do.
156 if (_panshell && !_panshell->bypassed() && role() != Listen) {
157 if (mixbufs.count ().n_audio () > 0) {
158 _panshell->run (bufs, mixbufs, start_sample, end_sample, nframes);
161 /* non-audio data will not have been copied by the panner, do it now
162 * if there are more buffers available than send buffers, ignore them,
163 * if there are less, copy the last as IO::copy_to_output does. */
165 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
166 if (*t != DataType::AUDIO) {
167 BufferSet::iterator o = mixbufs.begin(*t);
168 BufferSet::iterator i = bufs.begin(*t);
170 while (i != bufs.end(*t) && o != mixbufs.end(*t)) {
171 o->read_from (*i, nframes);
175 while (o != mixbufs.end(*t)) {
176 o->silence(nframes, 0);
182 if (role() == Listen) {
183 /* We're going to the monitor bus, so discard MIDI data */
185 uint32_t const bufs_audio = bufs.count().get (DataType::AUDIO);
186 uint32_t const mixbufs_audio = mixbufs.count().get (DataType::AUDIO);
188 /* monitor-section has same number of channels as master-bus (on creation).
190 * There is no clear answer what should happen when trying to PFL or AFL
191 * a track that has more channels (bufs_audio from source-track is
192 * larger than mixbufs).
194 * There are two options:
195 * 1: discard additional channels (current)
197 * 2: require the monitor-section to have at least as many channels
198 * as the largest count of any route
200 //assert (mixbufs.available().get (DataType::AUDIO) >= bufs_audio);
202 /* Copy bufs into mixbufs, going round bufs more than once if necessary
203 to ensure that every mixbuf gets some data.
208 for (i = 0; i < mixbufs_audio && j < bufs_audio; ++i) {
209 mixbufs.get_audio(i).read_from (bufs.get_audio(j), nframes);
212 if (j == bufs_audio) {
216 /* in case or MIDI track with 0 audio channels */
217 for (; i < mixbufs_audio; ++i) {
218 mixbufs.get_audio(i).silence (nframes);
222 assert (mixbufs.available() >= bufs.count());
223 mixbufs.read_from (bufs, nframes);
229 gain_t tgain = target_gain ();
231 if (tgain != _current_gain) {
233 /* target gain has changed */
235 _current_gain = Amp::apply_gain (mixbufs, _session.nominal_sample_rate(), nframes, _current_gain, tgain);
237 } else if (tgain == GAIN_COEFF_ZERO) {
239 /* we were quiet last time, and we're still supposed to be quiet.
243 Amp::apply_simple_gain (mixbufs, nframes, GAIN_COEFF_ZERO);
246 } else if (tgain != GAIN_COEFF_UNITY) {
248 /* target gain has not changed, but is not zero or unity */
249 Amp::apply_simple_gain (mixbufs, nframes, tgain);
252 _amp->set_gain_automation_buffer (_session.send_gain_automation_buffer ());
253 _amp->setup_gain_automation (start_sample, end_sample, nframes);
254 _amp->run (mixbufs, start_sample, end_sample, speed, nframes, true);
256 _send_delay->run (mixbufs, start_sample, end_sample, speed, nframes, true);
258 /* consider metering */
261 if (_amp->gain_control()->get_value() == GAIN_COEFF_ZERO) {
264 _meter->run (mixbufs, start_sample, end_sample, speed, nframes, true);
268 _thru_delay->run (bufs, start_sample, end_sample, speed, nframes, true);
270 /* target will pick up our output when it is ready */
273 _active = _pending_active;
277 InternalSend::set_block_size (pframes_t nframes)
280 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), nframes);
287 InternalSend::set_allow_feedback (bool yn)
289 _allow_feedback = yn;
290 _send_from->processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
294 InternalSend::feeds (boost::shared_ptr<Route> other) const
296 if (_role == Listen || !_allow_feedback) {
297 return _send_to == other;
303 InternalSend::state ()
305 XMLNode& node (Send::state ());
307 /* this replaces any existing "type" property */
309 node.set_property ("type", "intsend");
312 node.set_property ("target", _send_to->id());
314 node.set_property ("allow-feedback", _allow_feedback);
320 InternalSend::set_state (const XMLNode& node, int version)
324 Send::set_state (node, version);
326 if (node.get_property ("target", _send_to_id)) {
328 /* if we're loading a session, the target route may not have been
329 create yet. make sure we defer till we are sure that it should
333 if (!IO::connecting_legal) {
334 IO::ConnectingLegal.connect_same_thread (connect_c, boost::bind (&InternalSend::connect_when_legal, this));
336 connect_when_legal ();
340 node.get_property (X_("allow-feedback"), _allow_feedback);
346 InternalSend::connect_when_legal ()
348 connect_c.disconnect ();
350 if (_send_to_id == "0") {
351 /* it vanished before we could connect */
355 boost::shared_ptr<Route> sendto;
357 if ((sendto = _session.route_by_id (_send_to_id)) == 0) {
358 error << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endmsg;
359 cerr << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endl;
363 return use_target (sendto);
367 InternalSend::can_support_io_configuration (const ChanCount& in, ChanCount& out)
374 InternalSend::pan_outs () const
376 /* the number of targets for our panner is determined by what we are
377 sending to, if anything.
381 return _send_to->internal_return()->input_streams().n_audio();
384 return 1; /* zero is more accurate, but 1 is probably safer as a way to
390 InternalSend::configure_io (ChanCount in, ChanCount out)
392 bool ret = Send::configure_io (in, out);
393 set_block_size (_session.engine().samples_per_cycle());
398 InternalSend::set_name (const string& str)
400 /* rules for external sends don't apply to us */
401 return IOProcessor::set_name (str);
405 InternalSend::display_name () const
408 return string_compose (X_("%1"), _name);
415 InternalSend::visible () const
425 InternalSend::send_to_property_changed (const PropertyChange& what_changed)
427 if (what_changed.contains (Properties::name)) {
428 set_name (_send_to->name ());
433 InternalSend::set_can_pan (bool yn)
436 _panshell->set_bypassed (!yn);
441 InternalSend::cycle_start (pframes_t /*nframes*/)
443 for (BufferSet::audio_iterator b = mixbufs.audio_begin(); b != mixbufs.audio_end(); ++b) {