fix clicking when processors become active/inactive; reduce crazy 2.5sec delay for...
[ardour.git] / libs / ardour / internal_send.cc
index 60c0cde6afd6ba104be5e682c297112fc37d6d36..7c2c3842761aa83ed4eaa703448cd2539cbbf20c 100644 (file)
 
 */
 
-#include <algorithm>
+#include <iostream>
 
-#include <pbd/xml++.h>
+#include "pbd/error.h"
+#include "pbd/failed_constructor.h"
 
-#include <ardour/internal_send.h>
-#include <ardour/session.h>
-#include <ardour/port.h>
-#include <ardour/audio_port.h>
-#include <ardour/buffer_set.h>
-#include <ardour/meter.h>
-#include <ardour/panner.h>
+#include "ardour/amp.h"
+#include "ardour/internal_send.h"
+#include "ardour/meter.h"
+#include "ardour/route.h"
+#include "ardour/session.h"
 
 #include "i18n.h"
 
-using namespace ARDOUR;
 using namespace PBD;
+using namespace ARDOUR;
 
-InternalSend::InternalSend (Session& s, Placement p, boost::shared_ptr<IO> dst)
-       : IOProcessor (s, string_compose (_(">%1"), dst->name()), p, 
-                      -1, -1, -1, -1,
-                      DataType::AUDIO, false)
-       , destination (dst)
+InternalSend::InternalSend (Session& s, boost::shared_ptr<MuteMaster> mm, boost::shared_ptr<Route> sendto, Delivery::Role role)
+       : Send (s, mm, role)
+       , _send_to (sendto)
 {
-       _metering = false;
-
-       destination->input_changed.connect (mem_fun (*this, &InternalSend::destination_io_config_changed));
+       if ((target = _send_to->get_return_buffer ()) == 0) {
+               throw failed_constructor();
+       }
 
-       destination_io_config_changed (ConfigurationChanged, this);
+       set_name (sendto->name());
+       
+       _send_to->GoingAway.connect (mem_fun (*this, &InternalSend::send_to_going_away));
+}
 
-       ProcessorCreated (this); /* EMIT SIGNAL */
+InternalSend::InternalSend (Session& s, boost::shared_ptr<MuteMaster> mm, const XMLNode& node)
+       : Send (s, mm, node, Delivery::Aux /* will be reset in set_state() */)
+{
+       set_state (node);
 }
 
 InternalSend::~InternalSend ()
 {
-       GoingAway ();
+       if (_send_to) {
+               _send_to->release_return_buffer ();
+       }
+
+       connect_c.disconnect ();
 }
 
 void
-InternalSend::destination_io_config_changed (IOChange c, void* src)
+InternalSend::send_to_going_away ()
 {
-       if (!(c & ConfigurationChanged)) {
+       target = 0;
+       _send_to.reset ();
+       _send_to_id = "0";
+}
+
+void
+InternalSend::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
+{
+       if ((!_active && !_pending_active) || !target || !_send_to) {
+               _meter->reset ();
                return;
        }
 
-       _io->disconnect_outputs (this);
-
-       _io->ensure_io (ChanCount::ZERO, destination->n_inputs(), false, this);
+       // we have to copy the input, because we may alter the buffers with the amp
+       // in-place, which a send must never do.
+       
+       BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
+       sendbufs.read_from (bufs, nframes);
+       assert(sendbufs.count() == bufs.count());
 
-       PortSet::const_iterator us (_io->outputs().begin());
-       PortSet::const_iterator them (destination->inputs().begin ());
+       /* gain control */
 
-       for (; us != _io->outputs().end() && them != destination->inputs().end(); ++us, ++them) {
-               (const_cast<Port*>(&(*us)))->connect (const_cast<Port*>(&(*them)));
-       }
-}
+       gain_t tgain = target_gain ();
+       
+       if (tgain != _current_gain) {
+               
+               /* target gain has changed */
 
-XMLNode&
-InternalSend::get_state(void)
-{
-       fatal << X_("InternalSend::get_state() called - should never happen") << endmsg;
-       /*NOTREACHED*/
-       return *(new XMLNode ("foo"));
-}
+               Amp::apply_gain (sendbufs, nframes, _current_gain, tgain);
+               _current_gain = tgain;
 
-int
-InternalSend::set_state(const XMLNode& node)
-{
-       fatal << X_("InternalSend::set_state() called - should never happen") << endmsg;
-       /*NOTREACHED*/
-       return 0;
-}
+       } else if (tgain == 0.0) {
 
-void
-InternalSend::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
-{
-       if (active()) {
+               /* we were quiet last time, and we're still supposed to be quiet.
+               */
 
-               // we have to copy the input, because IO::deliver_output may alter the buffers
-               // in-place, which a send must never do. otherwise its gain settings will
-               // affect the signal seen later in the parent Route.
+               _meter->reset ();
+               Amp::apply_simple_gain (sendbufs, nframes, 0.0);
+               goto out;
 
-               // BufferSet& sendbufs = _session.get_mix_buffers(bufs.count());
+       } else if (tgain != 1.0) {
 
-               // sendbufs.read_from (bufs, nframes);
-               // assert(sendbufs.count() == bufs.count());
+               /* target gain has not changed, but is not zero or unity */
+               Amp::apply_simple_gain (sendbufs, nframes, tgain);
+       }
 
-               _io->deliver_output (bufs, start_frame, end_frame, nframes, offset);
+       
+       // Can't automate gain for sends or returns yet because we need different buffers
+       // so that we don't overwrite the main automation data for the route amp
+       // _amp->setup_gain_automation (start_frame, end_frame, nframes);
 
-               if (_metering) {
-                       if (_io->effective_gain() == 0) {
-                               _io->peak_meter().reset();
-                       } else {
-                               _io->peak_meter().run_in_place(_io->output_buffers(), start_frame, end_frame, nframes, offset);
-                       }
-               }
+       _amp->run (sendbufs, start_frame, end_frame, nframes);
 
-       } else {
-               _io->silence (nframes, offset);
-               if (_metering) {
-                       _io->peak_meter().reset();
+       /* consider metering */
+       
+       if (_metering) {
+               if (_amp->gain_control()->get_value() == 0) {
+                       _meter->reset();
+               } else {
+                       _meter->run (sendbufs, start_frame, end_frame, nframes);
                }
        }
-}
 
-void
-InternalSend::set_metering (bool yn)
-{
-       _metering = yn;
+       /* deliver to target */
 
-       if (!_metering) {
-               /* XXX possible thread hazard here */
-               _io->peak_meter().reset();
-       }
+       target->merge_from (sendbufs, nframes);
+
+  out:
+       _active = _pending_active;
 }
 
 bool
-InternalSend::can_support_io_configuration (const ChanCount& in, ChanCount& out_is_ignored) const
+InternalSend::feeds (boost::shared_ptr<Route> other) const
 {
-       /* number of outputs is fixed (though mutable by changing the I/O configuration
-          of the destination)
-       */
+       return _send_to == other;
+}
 
-       cerr << "IS: testing I/O config in=" << in.n_audio() << " out=" << out_is_ignored.n_audio() << endl;
+XMLNode&
+InternalSend::state (bool full)
+{
+       XMLNode& node (Send::state (full));
 
-       if (in == _io->n_outputs()) {
-               return 1;
-       }
+       /* this replaces any existing property */
 
-       return -1;
+       node.add_property ("type", "intsend");
+
+       if (_send_to) {
+               node.add_property ("target", _send_to->id().to_s());
+       }
+       
+       return node;
 }
 
-bool
-InternalSend::configure_io (ChanCount in, ChanCount out)
+XMLNode&
+InternalSend::get_state()
 {
-       cerr << "Configure IS for in " << in.n_audio() << " out = " << out.n_audio() << endl;
-
-       /* we're transparent no matter what.  fight the power. */
+       return state (true);
+}
 
-       if (out != in) {
-               return false;
-       }
+int
+InternalSend::set_state (const XMLNode& node)
+{
+       const XMLProperty* prop;
 
-       _io->set_output_maximum (in);
-       _io->set_output_minimum (in);
-       _io->set_input_maximum (ChanCount::ZERO);
-       _io->set_input_minimum (ChanCount::ZERO);
+       Send::set_state (node);
 
-       out = _io->n_outputs();
+       if ((prop = node.property ("target")) != 0) {
 
-       Processor::configure_io(in, out);
+               _send_to_id = prop->value();
 
-       _io->reset_panner();
+               /* if we're loading a session, the target route may not have been
+                  create yet. make sure we defer till we are sure that it should
+                  exist.
+               */
 
-       return true;
+               if (!IO::connecting_legal) {
+                       connect_c = IO::ConnectingLegal.connect (mem_fun (*this, &InternalSend::connect_when_legal));
+               } else {
+                       connect_when_legal ();
+               }
+       }
+       
+       return 0;
 }
 
-ChanCount
-InternalSend::output_streams() const
+int
+InternalSend::connect_when_legal ()
 {
-       // this method reflects the idea that from the perspective of the Route's ProcessorList, 
-       // a send is just a passthrough. that doesn't match what the Send actually does with its 
-       // data, but since what it does is invisible to the Route, it appears to be a passthrough.
+       connect_c.disconnect ();
+
+       if (_send_to_id == "0") {
+               /* it vanished before we could connect */
+               return 0;
+       }
+
+       if ((_send_to = _session.route_by_id (_send_to_id)) == 0) {
+               error << X_("cannot find route to connect to") << endmsg;
+               return -1;
+       } 
        
-       return _io->n_outputs ();
+       if ((target = _send_to->get_return_buffer ()) == 0) {
+               error << X_("target for internal send has no return buffer") << endmsg;
+               return -1;
+       }
+
+       return 0;
 }
 
-ChanCount
-InternalSend::input_streams() const
+bool 
+InternalSend::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
 {
-       return _configured_input;
+       out = in;
+       return true;
 }
 
-
-void
-InternalSend::expect_inputs (const ChanCount& expected)
+bool
+InternalSend::set_name (const std::string& str)
 {
-       if (expected != expected_inputs) {
-               expected_inputs = expected;
-               _io->reset_panner ();
-       }
+       /* rules for external sends don't apply to us */
+       return IOProcessor::set_name (str);
 }
 
-void
-InternalSend::activate ()
+std::string
+InternalSend::display_name () const
 {
-       Processor::activate ();
+       if (_role == Aux) {
+               return string_compose (X_("aux-%1"), _name);
+       } else {
+               return _name;
+       }
 }
 
-void
-InternalSend::deactivate ()
+bool
+InternalSend::visible () const
 {
-       Processor::deactivate ();
+       if (_role == Aux) {
+               return true;
+       }
+
+       return false; 
 }