2 Copyright (C) 2009 Paul Davis
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2 of the License, or (at your option)
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "pbd/enumwriter.h"
23 #include "pbd/convert.h"
25 #include "ardour/delivery.h"
26 #include "ardour/audio_buffer.h"
27 #include "ardour/amp.h"
28 #include "ardour/buffer_set.h"
29 #include "ardour/configuration.h"
30 #include "ardour/io.h"
31 #include "ardour/meter.h"
32 #include "ardour/mute_master.h"
33 #include "ardour/panner.h"
34 #include "ardour/port.h"
35 #include "ardour/session.h"
41 using namespace ARDOUR;
43 sigc::signal<void,nframes_t> Delivery::CycleStart;
44 sigc::signal<int> Delivery::PannersLegal;
45 bool Delivery::panners_legal = false;
47 /* deliver to an existing IO object */
49 Delivery::Delivery (Session& s, boost::shared_ptr<IO> io, boost::shared_ptr<MuteMaster> mm, const string& name, Role r)
50 : IOProcessor(s, boost::shared_ptr<IO>(), (r == Listen ? boost::shared_ptr<IO>() : io), name)
52 , _output_buffers (new BufferSet())
55 , _no_outs_cuz_we_no_monitor (false)
57 , _solo_isolated (false)
61 _panner = boost::shared_ptr<Panner>(new Panner (_name, _session));
64 _output->changed.connect (mem_fun (*this, &Delivery::output_changed));
67 CycleStart.connect (mem_fun (*this, &Delivery::cycle_start));
70 /* deliver to a new IO object */
72 Delivery::Delivery (Session& s, boost::shared_ptr<MuteMaster> mm, const string& name, Role r)
73 : IOProcessor(s, false, (r == Listen ? false : true), name)
75 , _output_buffers (new BufferSet())
78 , _no_outs_cuz_we_no_monitor (false)
80 , _solo_isolated (false)
83 _panner = boost::shared_ptr<Panner>(new Panner (_name, _session));
86 _output->changed.connect (mem_fun (*this, &Delivery::output_changed));
89 CycleStart.connect (mem_fun (*this, &Delivery::cycle_start));
92 /* deliver to a new IO object, reconstruct from XML */
94 Delivery::Delivery (Session& s, boost::shared_ptr<MuteMaster> mm, const XMLNode& node)
95 : IOProcessor (s, false, true, "reset")
97 , _output_buffers (new BufferSet())
100 , _no_outs_cuz_we_no_monitor (false)
102 , _solo_isolated (false)
105 _panner = boost::shared_ptr<Panner>(new Panner (_name, _session));
107 if (set_state (node)) {
108 throw failed_constructor ();
112 _output->changed.connect (mem_fun (*this, &Delivery::output_changed));
115 CycleStart.connect (mem_fun (*this, &Delivery::cycle_start));
118 /* deliver to an existing IO object, reconstruct from XML */
120 Delivery::Delivery (Session& s, boost::shared_ptr<IO> out, boost::shared_ptr<MuteMaster> mm, const XMLNode& node)
121 : IOProcessor (s, boost::shared_ptr<IO>(), out, "reset")
123 , _output_buffers (new BufferSet())
124 , _current_gain (1.0)
126 , _no_outs_cuz_we_no_monitor (false)
128 , _solo_isolated (false)
131 _panner = boost::shared_ptr<Panner>(new Panner (_name, _session));
133 if (set_state (node)) {
134 throw failed_constructor ();
138 _output->changed.connect (mem_fun (*this, &Delivery::output_changed));
141 CycleStart.connect (mem_fun (*this, &Delivery::cycle_start));
145 Delivery::cycle_start (nframes_t nframes)
148 _no_outs_cuz_we_no_monitor = false;
152 Delivery::increment_output_offset (nframes_t n)
158 Delivery::visible () const
168 Delivery::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
175 Delivery::configure_io (ChanCount in, ChanCount out)
177 if (out != in) { // always 1:1
183 return Processor::configure_io (in, out);
187 Delivery::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
189 if (_output->n_ports ().get (_output->default_type()) == 0) {
193 /* this setup is not just for our purposes, but for anything that comes after us in the
194 processing pathway that wants to use this->output_buffers() for some reason.
197 PortSet& ports (_output->ports());
198 output_buffers().attach_buffers (ports, nframes, _output_offset);
200 // this Delivery processor is not a derived type, and thus we assume
201 // we really can modify the buffers passed in (it is almost certainly
202 // the main output stage of a Route). Contrast with Send::run()
203 // which cannot do this.
205 gain_t tgain = target_gain ();
207 if (tgain != _current_gain) {
209 /* target gain has changed */
211 Amp::apply_gain (bufs, nframes, _current_gain, tgain);
212 _current_gain = tgain;
214 } else if (tgain == 0.0) {
216 /* we were quiet last time, and we're still supposed to be quiet.
217 Silence the outputs, and make sure the buffers are quiet too,
220 _output->silence (nframes);
221 Amp::apply_simple_gain (bufs, nframes, 0.0);
225 } else if (tgain != 1.0) {
227 /* target gain has not changed, but is not unity */
228 Amp::apply_simple_gain (bufs, nframes, tgain);
231 // Attach output buffers to port buffers
233 if (_panner && _panner->npanners() && !_panner->bypassed()) {
235 // Use the panner to distribute audio to output port buffers
237 _panner->run (bufs, output_buffers(), start_frame, end_frame, nframes);
240 // Do a 1:1 copy of data to output ports
242 if (bufs.count().n_audio() > 0 && ports.count().n_audio () > 0) {
243 _output->copy_to_outputs (bufs, DataType::AUDIO, nframes, _output_offset);
246 if (bufs.count().n_midi() > 0 && ports.count().n_midi () > 0) {
247 _output->copy_to_outputs (bufs, DataType::MIDI, nframes, _output_offset);
254 Delivery::state (bool full_state)
256 XMLNode& node (IOProcessor::state (full_state));
259 node.add_property("type", "main-outs");
260 } else if (_role & Listen) {
261 node.add_property("type", "listen");
263 node.add_property("type", "delivery");
266 node.add_property("role", enum_2_string(_role));
267 node.add_child_nocopy (_panner->state (full_state));
273 Delivery::set_state (const XMLNode& node)
275 const XMLProperty* prop;
277 if (IOProcessor::set_state (node)) {
281 if ((prop = node.property ("role")) != 0) {
282 _role = Role (string_2_enum (prop->value(), _role));
283 // std::cerr << this << ' ' << _name << " set role to " << enum_2_string (_role) << std::endl;
285 // std::cerr << this << ' ' << _name << " NO ROLE INFO\n";
288 XMLNode* pan_node = node.child (X_("Panner"));
291 _panner->set_state (*pan_node);
300 Delivery::reset_panner ()
303 if (!no_panner_reset) {
308 ntargets = _output->n_ports().n_audio();
310 ntargets = _configured_output.n_audio();
313 _panner->reset (ntargets, pans_required());
316 panner_legal_c.disconnect ();
317 panner_legal_c = PannersLegal.connect (mem_fun (*this, &Delivery::panners_became_legal));
322 Delivery::panners_became_legal ()
327 ntargets = _output->n_ports().n_audio();
329 ntargets = _configured_output.n_audio();
332 _panner->reset (ntargets, pans_required());
333 _panner->load (); // automation
334 panner_legal_c.disconnect ();
339 Delivery::defer_pan_reset ()
341 no_panner_reset = true;
345 Delivery::allow_pan_reset ()
347 no_panner_reset = false;
353 Delivery::disable_panners (void)
355 panners_legal = false;
360 Delivery::reset_panners ()
362 panners_legal = true;
363 return PannersLegal ();
368 Delivery::start_pan_touch (uint32_t which)
370 if (which < _panner->npanners()) {
371 _panner->pan_control(which)->start_touch();
376 Delivery::end_pan_touch (uint32_t which)
378 if (which < _panner->npanners()) {
379 _panner->pan_control(which)->stop_touch();
385 Delivery::transport_stopped (sframes_t frame)
387 _panner->transport_stopped (frame);
391 Delivery::flush (nframes_t nframes)
393 /* io_lock, not taken: function must be called from Session::process() calltree */
395 PortSet& ports (_output->ports());
397 for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
398 (*i).flush_buffers (nframes, _output_offset);
403 Delivery::target_gain ()
405 /* if we've been told not to output because its a monitoring situation and
406 we're not monitoring, then be quiet.
409 if (_no_outs_cuz_we_no_monitor) {
410 std::cerr << this << " no outs cuz we no monitor\n";
420 MuteMaster::MutePoint mp;
424 mp = MuteMaster::Main;
427 mp = MuteMaster::Listen;
431 if (_placement == PreFader) {
432 mp = MuteMaster::PreFader;
434 mp = MuteMaster::PostFader;
439 if (_solo_isolated) {
441 /* ... but we are isolated from all that nonsense */
443 desired_gain = _mute_master->mute_gain_at (mp);
445 } else if (_session.soloing()) {
447 desired_gain = min (Config->get_solo_mute_gain(), _mute_master->mute_gain_at (mp));
450 desired_gain = _mute_master->mute_gain_at (mp);
458 Delivery::no_outs_cuz_we_no_monitor (bool yn)
460 _no_outs_cuz_we_no_monitor = yn;
464 Delivery::set_name (const std::string& name)
466 bool ret = IOProcessor::set_name (name);
469 ret = _panner->set_name (name);
476 Delivery::output_changed (IOChange change, void* src)
478 if (change & ARDOUR::ConfigurationChanged) {