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 CycleStart.connect_same_thread (*this, boost::bind (&InternalSend::cycle_start, this, _1));
64 InternalSend::~InternalSend ()
67 _send_to->remove_send_from_internal_return (this);
72 InternalSend::init_gain ()
74 if (_role == Listen) {
75 /* send to monitor bus is always at unity */
76 _amp->set_gain (1.0, this);
78 /* aux sends start at -inf dB */
79 _amp->set_gain (0, this);
84 InternalSend::use_target (boost::shared_ptr<Route> sendto)
87 _send_to->remove_send_from_internal_return (this);
92 _send_to->add_send_to_internal_return (this);
94 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
95 mixbufs.set_count (_send_to->internal_return()->input_streams());
99 set_name (sendto->name());
100 _send_to_id = _send_to->id();
102 target_connections.drop_connections ();
104 _send_to->DropReferences.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_going_away, this));
105 _send_to->PropertyChanged.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_property_changed, this, _1));
106 _send_to->io_changed.connect_same_thread (target_connections, boost::bind (&InternalSend::target_io_changed, this));
112 InternalSend::target_io_changed ()
115 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
116 mixbufs.set_count (_send_to->internal_return()->input_streams());
121 InternalSend::send_to_going_away ()
123 target_connections.drop_connections ();
129 InternalSend::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
131 if ((!_active && !_pending_active) || !_send_to) {
136 // we have to copy the input, because we may alter the buffers with the amp
137 // in-place, which a send must never do.
139 if (_panshell && !_panshell->bypassed() && role() != Listen) {
140 _panshell->run (bufs, mixbufs, start_frame, end_frame, nframes);
142 if (role() == Listen) {
143 /* We're going to the monitor bus, so discard MIDI data */
145 uint32_t const bufs_audio = bufs.count().get (DataType::AUDIO);
146 uint32_t const mixbufs_audio = mixbufs.count().get (DataType::AUDIO);
148 /* monitor-section has same number of channels as master-bus (on creation).
150 * There is no clear answer what should happen when trying to PFL or AFL
151 * a track that has more channels (bufs_audio from source-track is
152 * larger than mixbufs).
154 * There are two options:
155 * 1: discard additional channels (current)
157 * 2: require the monitor-section to have at least as many channels
158 * as the largest count of any route
160 //assert (mixbufs.available().get (DataType::AUDIO) >= bufs_audio);
162 /* Copy bufs into mixbufs, going round bufs more than once if necessary
163 to ensure that every mixbuf gets some data.
167 for (uint32_t i = 0; i < mixbufs_audio; ++i) {
168 mixbufs.get_audio(i).read_from (bufs.get_audio(j), nframes);
171 if (j == bufs_audio) {
177 assert (mixbufs.available() >= bufs.count());
178 mixbufs.read_from (bufs, nframes);
184 gain_t tgain = target_gain ();
186 if (tgain != _current_gain) {
188 /* target gain has changed */
190 Amp::apply_gain (mixbufs, nframes, _current_gain, tgain);
191 _current_gain = tgain;
193 } else if (tgain == 0.0) {
195 /* we were quiet last time, and we're still supposed to be quiet.
199 Amp::apply_simple_gain (mixbufs, nframes, 0.0);
202 } else if (tgain != 1.0) {
204 /* target gain has not changed, but is not zero or unity */
205 Amp::apply_simple_gain (mixbufs, nframes, tgain);
208 _amp->set_gain_automation_buffer (_session.send_gain_automation_buffer ());
209 _amp->setup_gain_automation (start_frame, end_frame, nframes);
210 _amp->run (mixbufs, start_frame, end_frame, nframes, true);
212 _delayline->run (mixbufs, start_frame, end_frame, nframes, true);
214 /* consider metering */
217 if (_amp->gain_control()->get_value() == 0) {
220 _meter->run (mixbufs, start_frame, end_frame, nframes, true);
224 /* target will pick up our output when it is ready */
227 _active = _pending_active;
231 InternalSend::set_block_size (pframes_t nframes)
234 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), nframes);
241 InternalSend::feeds (boost::shared_ptr<Route> other) const
243 return _send_to == other;
247 InternalSend::state (bool full)
249 XMLNode& node (Send::state (full));
251 /* this replaces any existing "type" property */
253 node.add_property ("type", "intsend");
256 node.add_property ("target", _send_to->id().to_s());
263 InternalSend::get_state()
269 InternalSend::set_state (const XMLNode& node, int version)
271 const XMLProperty* prop;
275 Send::set_state (node, version);
277 if ((prop = node.property ("target")) != 0) {
279 _send_to_id = prop->value();
281 /* if we're loading a session, the target route may not have been
282 create yet. make sure we defer till we are sure that it should
286 if (!IO::connecting_legal) {
287 IO::ConnectingLegal.connect_same_thread (connect_c, boost::bind (&InternalSend::connect_when_legal, this));
289 connect_when_legal ();
297 InternalSend::connect_when_legal ()
299 connect_c.disconnect ();
301 if (_send_to_id == "0") {
302 /* it vanished before we could connect */
306 boost::shared_ptr<Route> sendto;
308 if ((sendto = _session.route_by_id (_send_to_id)) == 0) {
309 error << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endmsg;
310 cerr << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endl;
314 return use_target (sendto);
318 InternalSend::can_support_io_configuration (const ChanCount& in, ChanCount& out)
325 InternalSend::pan_outs () const
327 /* the number of targets for our panner is determined by what we are
328 sending to, if anything.
332 return _send_to->internal_return()->input_streams().n_audio();
335 return 1; /* zero is more accurate, but 1 is probably safer as a way to
341 InternalSend::configure_io (ChanCount in, ChanCount out)
343 bool ret = Send::configure_io (in, out);
344 set_block_size (_session.engine().samples_per_cycle());
349 InternalSend::set_name (const string& str)
351 /* rules for external sends don't apply to us */
352 return IOProcessor::set_name (str);
356 InternalSend::display_name () const
359 return string_compose (X_("%1"), _name);
366 InternalSend::visible () const
376 InternalSend::send_to_property_changed (const PropertyChange& what_changed)
378 if (what_changed.contains (Properties::name)) {
379 set_name (_send_to->name ());
384 InternalSend::set_can_pan (bool yn)
387 _panshell->set_bypassed (!yn);
392 InternalSend::cycle_start (pframes_t /*nframes*/)
394 for (BufferSet::audio_iterator b = mixbufs.audio_begin(); b != mixbufs.audio_end(); ++b) {