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"
23 #include "ardour/amp.h"
24 #include "ardour/audio_buffer.h"
25 #include "ardour/internal_return.h"
26 #include "ardour/internal_send.h"
27 #include "ardour/meter.h"
28 #include "ardour/panner_shell.h"
29 #include "ardour/route.h"
30 #include "ardour/session.h"
31 #include "ardour/audioengine.h"
35 namespace ARDOUR { class MuteMaster; class Pannable; }
38 using namespace ARDOUR;
41 PBD::Signal1<void, pframes_t> InternalSend::CycleStart;
43 InternalSend::InternalSend (Session& s,
44 boost::shared_ptr<Pannable> p,
45 boost::shared_ptr<MuteMaster> mm,
46 boost::shared_ptr<Route> sendfrom,
47 boost::shared_ptr<Route> sendto,
50 : Send (s, p, mm, role, ignore_bitslot)
51 , _send_from (sendfrom)
54 if (use_target (sendto)) {
55 throw failed_constructor();
61 _send_from->DropReferences.connect_same_thread (source_connection, boost::bind (&InternalSend::send_from_going_away, this));
62 CycleStart.connect_same_thread (*this, boost::bind (&InternalSend::cycle_start, this, _1));
65 InternalSend::~InternalSend ()
68 _send_to->remove_send_from_internal_return (this);
73 InternalSend::init_gain ()
75 if (_role == Listen) {
76 /* send to monitor bus is always at unity */
77 _gain_control->set_value (GAIN_COEFF_UNITY, PBD::Controllable::NoGroup);
79 /* aux sends start at -inf dB */
80 _gain_control->set_value (GAIN_COEFF_ZERO, PBD::Controllable::NoGroup);
85 InternalSend::use_target (boost::shared_ptr<Route> sendto)
88 _send_to->remove_send_from_internal_return (this);
93 _send_to->add_send_to_internal_return (this);
95 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
96 mixbufs.set_count (_send_to->internal_return()->input_streams());
100 set_name (sendto->name());
101 _send_to_id = _send_to->id();
103 target_connections.drop_connections ();
105 _send_to->DropReferences.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_going_away, this));
106 _send_to->PropertyChanged.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_property_changed, this, _1));
107 _send_to->io_changed.connect_same_thread (target_connections, boost::bind (&InternalSend::target_io_changed, this));
113 InternalSend::target_io_changed ()
116 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
117 mixbufs.set_count (_send_to->internal_return()->input_streams());
122 InternalSend::send_from_going_away ()
128 InternalSend::send_to_going_away ()
130 target_connections.drop_connections ();
136 InternalSend::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
138 if ((!_active && !_pending_active) || !_send_to) {
143 // we have to copy the input, because we may alter the buffers with the amp
144 // in-place, which a send must never do.
146 if (_panshell && !_panshell->bypassed() && role() != Listen) {
147 _panshell->run (bufs, mixbufs, start_frame, end_frame, nframes);
149 if (role() == Listen) {
150 /* We're going to the monitor bus, so discard MIDI data */
152 uint32_t const bufs_audio = bufs.count().get (DataType::AUDIO);
153 uint32_t const mixbufs_audio = mixbufs.count().get (DataType::AUDIO);
155 /* monitor-section has same number of channels as master-bus (on creation).
157 * There is no clear answer what should happen when trying to PFL or AFL
158 * a track that has more channels (bufs_audio from source-track is
159 * larger than mixbufs).
161 * There are two options:
162 * 1: discard additional channels (current)
164 * 2: require the monitor-section to have at least as many channels
165 * as the largest count of any route
167 //assert (mixbufs.available().get (DataType::AUDIO) >= bufs_audio);
169 /* Copy bufs into mixbufs, going round bufs more than once if necessary
170 to ensure that every mixbuf gets some data.
174 for (uint32_t i = 0; i < mixbufs_audio; ++i) {
175 mixbufs.get_audio(i).read_from (bufs.get_audio(j), nframes);
178 if (j == bufs_audio) {
184 assert (mixbufs.available() >= bufs.count());
185 mixbufs.read_from (bufs, nframes);
191 gain_t tgain = target_gain ();
193 if (tgain != _current_gain) {
195 /* target gain has changed */
197 _current_gain = Amp::apply_gain (mixbufs, _session.nominal_frame_rate(), nframes, _current_gain, tgain);
199 } else if (tgain == GAIN_COEFF_ZERO) {
201 /* we were quiet last time, and we're still supposed to be quiet.
205 Amp::apply_simple_gain (mixbufs, nframes, GAIN_COEFF_ZERO);
208 } else if (tgain != GAIN_COEFF_UNITY) {
210 /* target gain has not changed, but is not zero or unity */
211 Amp::apply_simple_gain (mixbufs, nframes, tgain);
214 _amp->set_gain_automation_buffer (_session.send_gain_automation_buffer ());
215 _amp->setup_gain_automation (start_frame, end_frame, nframes);
216 _amp->run (mixbufs, start_frame, end_frame, nframes, true);
218 _delayline->run (mixbufs, start_frame, end_frame, nframes, true);
220 /* consider metering */
223 if (_amp->gain_control()->get_value() == GAIN_COEFF_ZERO) {
226 _meter->run (mixbufs, start_frame, end_frame, nframes, true);
230 /* target will pick up our output when it is ready */
233 _active = _pending_active;
237 InternalSend::set_block_size (pframes_t nframes)
240 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), nframes);
247 InternalSend::feeds (boost::shared_ptr<Route> other) const
249 return _send_to == other;
253 InternalSend::state (bool full)
255 XMLNode& node (Send::state (full));
257 /* this replaces any existing "type" property */
259 node.add_property ("type", "intsend");
262 node.add_property ("target", _send_to->id().to_s());
269 InternalSend::get_state()
275 InternalSend::set_state (const XMLNode& node, int version)
277 XMLProperty const * prop;
281 Send::set_state (node, version);
283 if ((prop = node.property ("target")) != 0) {
285 _send_to_id = prop->value();
287 /* if we're loading a session, the target route may not have been
288 create yet. make sure we defer till we are sure that it should
292 if (!IO::connecting_legal) {
293 IO::ConnectingLegal.connect_same_thread (connect_c, boost::bind (&InternalSend::connect_when_legal, this));
295 connect_when_legal ();
303 InternalSend::connect_when_legal ()
305 connect_c.disconnect ();
307 if (_send_to_id == "0") {
308 /* it vanished before we could connect */
312 boost::shared_ptr<Route> sendto;
314 if ((sendto = _session.route_by_id (_send_to_id)) == 0) {
315 error << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endmsg;
316 cerr << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endl;
320 return use_target (sendto);
324 InternalSend::can_support_io_configuration (const ChanCount& in, ChanCount& out)
331 InternalSend::pan_outs () const
333 /* the number of targets for our panner is determined by what we are
334 sending to, if anything.
338 return _send_to->internal_return()->input_streams().n_audio();
341 return 1; /* zero is more accurate, but 1 is probably safer as a way to
347 InternalSend::configure_io (ChanCount in, ChanCount out)
349 bool ret = Send::configure_io (in, out);
350 set_block_size (_session.engine().samples_per_cycle());
355 InternalSend::set_name (const string& str)
357 /* rules for external sends don't apply to us */
358 return IOProcessor::set_name (str);
362 InternalSend::display_name () const
365 return string_compose (X_("%1"), _name);
372 InternalSend::visible () const
382 InternalSend::send_to_property_changed (const PropertyChange& what_changed)
384 if (what_changed.contains (Properties::name)) {
385 set_name (_send_to->name ());
390 InternalSend::set_can_pan (bool yn)
393 _panshell->set_bypassed (!yn);
398 InternalSend::cycle_start (pframes_t /*nframes*/)
400 for (BufferSet::audio_iterator b = mixbufs.audio_begin(); b != mixbufs.audio_end(); ++b) {