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.
26 #include "pbd/enumwriter.h"
27 #include "pbd/enum_convert.h"
29 #include "ardour/amp.h"
30 #include "ardour/audioengine.h"
31 #include "ardour/buffer_set.h"
32 #include "ardour/debug.h"
33 #include "ardour/delivery.h"
34 #include "ardour/io.h"
35 #include "ardour/mute_master.h"
36 #include "ardour/pannable.h"
37 #include "ardour/panner_shell.h"
38 #include "ardour/port.h"
39 #include "ardour/session.h"
44 DEFINE_ENUM_CONVERT(ARDOUR::Delivery::Role);
47 namespace ARDOUR { class Panner; }
51 using namespace ARDOUR;
53 PBD::Signal0<void> Delivery::PannersLegal;
54 bool Delivery::panners_legal = false;
56 /* deliver to an existing IO object */
58 Delivery::Delivery (Session& s, boost::shared_ptr<IO> io, boost::shared_ptr<Pannable> pannable,
59 boost::shared_ptr<MuteMaster> mm, const string& name, Role r)
60 : IOProcessor(s, boost::shared_ptr<IO>(), (role_requires_output_ports (r) ? io : boost::shared_ptr<IO>()), name)
62 , _output_buffers (new BufferSet())
63 , _current_gain (GAIN_COEFF_UNITY)
64 , _no_outs_cuz_we_no_monitor (false)
66 , _no_panner_reset (false)
70 if (r & (Delivery::Send|Delivery::Aux|Delivery::Foldback)) is_send = true;
71 _panshell = boost::shared_ptr<PannerShell>(new PannerShell (_name, _session, pannable, is_send));
74 _display_to_user = false;
77 _output->changed.connect_same_thread (*this, boost::bind (&Delivery::output_changed, this, _1, _2));
81 /* deliver to a new IO object */
83 Delivery::Delivery (Session& s, boost::shared_ptr<Pannable> pannable, boost::shared_ptr<MuteMaster> mm, const string& name, Role r)
84 : IOProcessor(s, false, (role_requires_output_ports (r) ? true : false), name, "", DataType::AUDIO, (r == Send))
86 , _output_buffers (new BufferSet())
87 , _current_gain (GAIN_COEFF_UNITY)
88 , _no_outs_cuz_we_no_monitor (false)
90 , _no_panner_reset (false)
94 if (r & (Delivery::Send|Delivery::Aux|Delivery::Foldback)) is_send = true;
95 _panshell = boost::shared_ptr<PannerShell>(new PannerShell (_name, _session, pannable, is_send));
98 _display_to_user = false;
101 _output->changed.connect_same_thread (*this, boost::bind (&Delivery::output_changed, this, _1, _2));
106 Delivery::~Delivery()
108 DEBUG_TRACE (DEBUG::Destruction, string_compose ("delivery %1 destructor\n", _name));
110 /* this object should vanish from any signal callback lists
111 that it is on before we get any further. The full qualification
112 of the method name is not necessary, but is here to make it
113 clear that this call is about signals, not data flow connections.
116 ScopedConnectionList::drop_connections ();
118 delete _output_buffers;
122 Delivery::display_name () const
126 return _("main outs");
139 Delivery::can_support_io_configuration (const ChanCount& in, ChanCount& out)
143 /* the out buffers will be set to point to the port output buffers
144 of our output object.
148 if (_output->n_ports() != ChanCount::ZERO) {
149 /* increase number of output ports if the processor chain requires it */
150 out = ChanCount::max (_output->n_ports(), in);
153 /* not configured yet - we will passthru */
158 fatal << "programming error: this should never be reached" << endmsg;
159 abort(); /*NOTREACHED*/
163 } else if (_role == Insert) {
165 /* the output buffers will be filled with data from the *input* ports
170 if (_input->n_ports() != ChanCount::ZERO) {
171 out = _input->n_ports();
174 /* not configured yet - we will passthru */
179 fatal << "programming error: this should never be reached" << endmsg;
180 abort(); /*NOTREACHED*/
184 fatal << "programming error: this should never be reached" << endmsg;
190 /** Caller must hold process lock */
192 Delivery::configure_io (ChanCount in, ChanCount out)
195 bool r = AudioEngine::instance()->process_lock().trylock();
196 assert (!r && "trylock inside Delivery::configure_io");
199 /* check configuration by comparison with our I/O port configuration, if appropriate.
200 see ::can_support_io_configuration() for comments
206 if (_output->n_ports() != out) {
207 if (_output->n_ports() != ChanCount::ZERO) {
208 _output->ensure_io (out, false, this);
210 /* I/O not yet configured */
215 } else if (_role == Insert) {
218 if (_input->n_ports() != in) {
219 if (_input->n_ports() != ChanCount::ZERO) {
220 fatal << _name << " programming error: configure_io called with " << in << " and " << out << " with " << _input->n_ports() << " input ports" << endmsg;
221 abort(); /*NOTREACHED*/
223 /* I/O not yet configured */
230 if (!Processor::configure_io (in, out)) {
240 Delivery::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double /*speed*/, pframes_t nframes, bool result_required)
244 PortSet& ports (_output->ports());
247 if (ports.num_ports () == 0) {
251 if (!_active && !_pending_active) {
252 _output->silence (nframes);
256 /* this setup is not just for our purposes, but for anything that comes after us in the
257 * processing pathway that wants to use this->output_buffers() for some reason.
260 // TODO delayline -- latency-compensation
261 output_buffers().get_backend_port_addresses (ports, nframes);
263 // this Delivery processor is not a derived type, and thus we assume
264 // we really can modify the buffers passed in (it is almost certainly
265 // the main output stage of a Route). Contrast with Send::run()
266 // which cannot do this.
268 tgain = target_gain ();
270 if (tgain != _current_gain) {
271 /* target gain has changed */
273 _current_gain = Amp::apply_gain (bufs, _session.nominal_sample_rate(), nframes, _current_gain, tgain);
275 } else if (tgain < GAIN_COEFF_SMALL) {
277 /* we were quiet last time, and we're still supposed to be quiet.
278 Silence the outputs, and make sure the buffers are quiet too,
281 _output->silence (nframes);
282 if (result_required) {
283 bufs.set_count (output_buffers().count ());
284 Amp::apply_simple_gain (bufs, nframes, GAIN_COEFF_ZERO);
288 } else if (tgain != GAIN_COEFF_UNITY) {
290 /* target gain has not changed, but is not unity */
291 Amp::apply_simple_gain (bufs, nframes, tgain);
296 if (fabs (_session.transport_speed()) > 1.5 && Config->get_quieten_at_speed ()) {
297 Amp::apply_simple_gain (bufs, nframes, speed_quietning, false);
302 if (_panshell && !_panshell->bypassed() && _panshell->panner()) {
304 // Use the panner to distribute audio to output port buffers
306 _panshell->run (bufs, output_buffers(), start_sample, end_sample, nframes);
308 // non-audio data will not have been delivered by the panner
310 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
311 if (*t != DataType::AUDIO && bufs.count().get(*t) > 0) {
312 _output->copy_to_outputs (bufs, *t, nframes, Port::port_offset());
318 /* Do a 1:1 copy of data to output ports
320 Audio is handled separately because we use 0 for the offset,
321 since the port offset is only used for timestamped events
325 if (bufs.count().n_audio() > 0) {
326 _output->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
329 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
330 if (*t != DataType::AUDIO && bufs.count().get(*t) > 0) {
331 _output->copy_to_outputs (bufs, *t, nframes, Port::port_offset());
336 if (result_required) {
337 /* "bufs" are internal, meaning they should never reflect
338 split-cycle offsets. So shift events back in time from where
339 they were for the external buffers associated with Ports.
342 const BufferSet& outs (output_buffers());
343 bufs.set_count (output_buffers().count ());
345 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
348 for (BufferSet::iterator b = bufs.begin (*t); b != bufs.end (*t); ++b) {
349 if (outs.count ().get (*t) <= n) {
352 b->read_from (outs.get_available (*t, n++), nframes, (*t == DataType::AUDIO ? 0 : -Port::port_offset()));
358 _active = _pending_active;
364 XMLNode& node (IOProcessor::state ());
367 node.set_property("type", "main-outs");
368 } else if (_role & Listen) {
369 node.set_property("type", "listen");
371 node.set_property("type", "delivery");
374 node.set_property("role", _role);
377 node.add_child_nocopy (_panshell->get_state ());
378 if (_panshell->pannable()) {
379 node.add_child_nocopy (_panshell->pannable()->get_state ());
387 Delivery::set_state (const XMLNode& node, int version)
389 if (IOProcessor::set_state (node, version)) {
393 if (node.get_property ("role", _role)) {
394 // std::cerr << this << ' ' << _name << " set role to " << enum_2_string (_role) << std::endl;
396 // std::cerr << this << ' ' << _name << " NO ROLE INFO\n";
399 XMLNode* pan_node = node.child (X_("PannerShell"));
401 if (pan_node && _panshell) {
402 _panshell->set_state (*pan_node, version);
407 XMLNode* pannnode = node.child (X_("Pannable"));
408 if (_panshell && _panshell->panner() && pannnode) {
409 _panshell->pannable()->set_state (*pannnode, version);
418 /* caller must hold process lock */
424 Delivery::pan_outs () const
427 return _output->n_ports().n_audio();
430 return _configured_output.n_audio();
434 Delivery::reset_panner ()
437 if (!_no_panner_reset) {
439 if (_panshell && _role != Insert && _role != Listen) {
440 _panshell->configure_io (ChanCount (DataType::AUDIO, pans_required()), ChanCount (DataType::AUDIO, pan_outs()));
445 panner_legal_c.disconnect ();
446 PannersLegal.connect_same_thread (panner_legal_c, boost::bind (&Delivery::panners_became_legal, this));
451 Delivery::panners_became_legal ()
453 if (_panshell && _role != Insert) {
454 _panshell->configure_io (ChanCount (DataType::AUDIO, pans_required()), ChanCount (DataType::AUDIO, pan_outs()));
457 panner_legal_c.disconnect ();
461 Delivery::defer_pan_reset ()
463 _no_panner_reset = true;
467 Delivery::allow_pan_reset ()
469 _no_panner_reset = false;
475 Delivery::disable_panners ()
477 panners_legal = false;
482 Delivery::reset_panners ()
484 panners_legal = true;
489 Delivery::flush_buffers (samplecnt_t nframes)
491 /* io_lock, not taken: function must be called from Session::process() calltree */
497 PortSet& ports (_output->ports());
499 for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
500 i->flush_buffers (nframes);
505 Delivery::non_realtime_transport_stop (samplepos_t now, bool flush)
507 Processor::non_realtime_transport_stop (now, flush);
510 _panshell->pannable()->non_realtime_transport_stop (now, flush);
514 PortSet& ports (_output->ports());
516 for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
517 i->transport_stopped ();
523 Delivery::realtime_locate ()
526 PortSet& ports (_output->ports());
528 for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
529 i->realtime_locate ();
535 Delivery::target_gain ()
537 /* if we've been requested to deactivate, our target gain is zero */
539 if (!_pending_active) {
540 return GAIN_COEFF_ZERO;
543 /* if we've been told not to output because its a monitoring situation and
544 we're not monitoring, then be quiet.
547 if (_no_outs_cuz_we_no_monitor) {
548 return GAIN_COEFF_ZERO;
551 MuteMaster::MutePoint mp = MuteMaster::Main; // stupid gcc uninit warning
555 mp = MuteMaster::Main;
558 mp = MuteMaster::Listen;
565 mp = MuteMaster::PreFader;
567 mp = MuteMaster::PostFader;
572 gain_t desired_gain = _mute_master->mute_gain_at (mp);
574 if (_role == Listen && _session.monitor_out() && !_session.listening()) {
576 /* nobody is soloed, and this delivery is a listen-send to the
577 * control/monitor/listen bus, we should be silent since
578 * it gets its signal from the master out.
581 desired_gain = GAIN_COEFF_ZERO;
588 Delivery::no_outs_cuz_we_no_monitor (bool yn)
590 _no_outs_cuz_we_no_monitor = yn;
594 Delivery::set_name (const std::string& name)
596 bool ret = IOProcessor::set_name (name);
598 if (ret && _panshell) {
599 ret = _panshell->set_name (name);
605 bool ignore_output_change = false;
608 Delivery::output_changed (IOChange change, void* /*src*/)
610 if (change.type & IOChange::ConfigurationChanged) {
612 _output_buffers->attach_buffers (_output->ports ());
616 boost::shared_ptr<Panner>
617 Delivery::panner () const
620 return _panshell->panner();
622 return boost::shared_ptr<Panner>();