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, boost::shared_ptr<Pannable> p, boost::shared_ptr<MuteMaster> mm, boost::shared_ptr<Route> sendto, Delivery::Role role)
44 : Send (s, p, mm, role)
47 if (use_target (sendto)) {
48 throw failed_constructor();
54 CycleStart.connect_same_thread (*this, boost::bind (&InternalSend::cycle_start, this, _1));
57 InternalSend::~InternalSend ()
60 _send_to->remove_send_from_internal_return (this);
65 InternalSend::init_gain ()
67 if (_role == Listen) {
68 /* send to monitor bus is always at unity */
69 _amp->set_gain (1.0, this);
71 /* aux sends start at -inf dB */
72 _amp->set_gain (0, this);
77 InternalSend::use_target (boost::shared_ptr<Route> sendto)
80 _send_to->remove_send_from_internal_return (this);
85 _send_to->add_send_to_internal_return (this);
87 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
88 mixbufs.set_count (_send_to->internal_return()->input_streams());
92 set_name (sendto->name());
93 _send_to_id = _send_to->id();
95 target_connections.drop_connections ();
97 _send_to->DropReferences.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_going_away, this));
98 _send_to->PropertyChanged.connect_same_thread (target_connections, boost::bind (&InternalSend::send_to_property_changed, this, _1));
99 _send_to->io_changed.connect_same_thread (target_connections, boost::bind (&InternalSend::target_io_changed, this));
105 InternalSend::target_io_changed ()
108 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), _session.get_block_size());
109 mixbufs.set_count (_send_to->internal_return()->input_streams());
114 InternalSend::send_to_going_away ()
116 target_connections.drop_connections ();
122 InternalSend::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
124 if ((!_active && !_pending_active) || !_send_to) {
129 // we have to copy the input, because we may alter the buffers with the amp
130 // in-place, which a send must never do.
132 if (_panshell && !_panshell->bypassed()) {
133 _panshell->run (bufs, mixbufs, start_frame, end_frame, nframes);
135 if (role() == Listen) {
136 /* We're going to the monitor bus, so discard MIDI data */
138 uint32_t const bufs_audio = bufs.count().get (DataType::AUDIO);
139 uint32_t const mixbufs_audio = mixbufs.count().get (DataType::AUDIO);
141 /* monitor-section has same number of channels as master-bus (on creation).
143 * There is no clear answer what should happen when trying to PFL or AFL
144 * a track that has more channels (bufs_audio from source-track is
145 * larger than mixbufs).
147 * There are two options:
148 * 1: discard additional channels (current)
150 * 2: require the monitor-section to have at least as many channels
151 * as the largest count of any route
153 //assert (mixbufs.available().get (DataType::AUDIO) >= bufs_audio);
155 /* Copy bufs into mixbufs, going round bufs more than once if necessary
156 to ensure that every mixbuf gets some data.
160 for (uint32_t i = 0; i < mixbufs_audio; ++i) {
161 mixbufs.get_audio(i).read_from (bufs.get_audio(j), nframes);
164 if (j == bufs_audio) {
170 assert (mixbufs.available() >= bufs.count());
171 mixbufs.read_from (bufs, nframes);
177 gain_t tgain = target_gain ();
179 if (tgain != _current_gain) {
181 /* target gain has changed */
183 Amp::apply_gain (mixbufs, nframes, _current_gain, tgain);
184 _current_gain = tgain;
186 } else if (tgain == 0.0) {
188 /* we were quiet last time, and we're still supposed to be quiet.
192 Amp::apply_simple_gain (mixbufs, nframes, 0.0);
195 } else if (tgain != 1.0) {
197 /* target gain has not changed, but is not zero or unity */
198 Amp::apply_simple_gain (mixbufs, nframes, tgain);
201 _amp->set_gain_automation_buffer (_session.send_gain_automation_buffer ());
202 _amp->setup_gain_automation (start_frame, end_frame, nframes);
203 _amp->run (mixbufs, start_frame, end_frame, nframes, true);
205 /* consider metering */
208 if (_amp->gain_control()->get_value() == 0) {
211 _meter->run (mixbufs, start_frame, end_frame, nframes, true);
215 /* target will pick up our output when it is ready */
218 _active = _pending_active;
222 InternalSend::set_block_size (pframes_t nframes)
225 mixbufs.ensure_buffers (_send_to->internal_return()->input_streams(), nframes);
232 InternalSend::feeds (boost::shared_ptr<Route> other) const
234 return _send_to == other;
238 InternalSend::state (bool full)
240 XMLNode& node (Send::state (full));
242 /* this replaces any existing "type" property */
244 node.add_property ("type", "intsend");
247 node.add_property ("target", _send_to->id().to_s());
254 InternalSend::get_state()
260 InternalSend::set_state (const XMLNode& node, int version)
262 const XMLProperty* prop;
266 Send::set_state (node, version);
268 if ((prop = node.property ("target")) != 0) {
270 _send_to_id = prop->value();
272 /* if we're loading a session, the target route may not have been
273 create yet. make sure we defer till we are sure that it should
277 if (!IO::connecting_legal) {
278 IO::ConnectingLegal.connect_same_thread (connect_c, boost::bind (&InternalSend::connect_when_legal, this));
280 connect_when_legal ();
288 InternalSend::connect_when_legal ()
290 connect_c.disconnect ();
292 if (_send_to_id == "0") {
293 /* it vanished before we could connect */
297 boost::shared_ptr<Route> sendto;
299 if ((sendto = _session.route_by_id (_send_to_id)) == 0) {
300 error << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endmsg;
301 cerr << string_compose (_("%1 - cannot find any track/bus with the ID %2 to connect to"), display_name(), _send_to_id) << endl;
305 return use_target (sendto);
309 InternalSend::can_support_io_configuration (const ChanCount& in, ChanCount& out)
316 InternalSend::pan_outs () const
318 /* the number of targets for our panner is determined by what we are
319 sending to, if anything.
323 return _send_to->internal_return()->input_streams().n_audio();
326 return 1; /* zero is more accurate, but 1 is probably safer as a way to
332 InternalSend::configure_io (ChanCount in, ChanCount out)
334 bool ret = Send::configure_io (in, out);
335 set_block_size (_session.engine().samples_per_cycle());
340 InternalSend::set_name (const string& str)
342 /* rules for external sends don't apply to us */
343 return IOProcessor::set_name (str);
347 InternalSend::display_name () const
350 return string_compose (X_("%1"), _name);
357 InternalSend::visible () const
367 InternalSend::send_to_property_changed (const PropertyChange& what_changed)
369 if (what_changed.contains (Properties::name)) {
370 set_name (_send_to->name ());
375 InternalSend::set_can_pan (bool yn)
378 _panshell->set_bypassed (!yn);
383 InternalSend::cycle_start (pframes_t /*nframes*/)
385 for (BufferSet::audio_iterator b = mixbufs.audio_begin(); b != mixbufs.audio_end(); ++b) {