first part of MIDI cut/copy/paste ; fix for input/output_streams of an IOProcessor...
[ardour.git] / libs / ardour / io_processor.cc
index 0210f1d2d473831afec463e5b9dae93bbbc2146f..505d33a1b09b360fd2e55d42e42c233335f20da8 100644 (file)
@@ -36,6 +36,7 @@
 #include "ardour/port_insert.h"
 #include "ardour/plugin_insert.h"
 #include "ardour/io.h"
+#include "ardour/route.h"
 
 #include "i18n.h"
 
@@ -43,17 +44,47 @@ using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-IOProcessor::IOProcessor (Session& s, const string& name, Placement p,
-                         int input_min, int input_max,
-                         int output_min, int output_max, 
-                         DataType dtype)
-       : Processor(s, name, p)
-       , _io (new IO(s, name, input_min, input_max, output_min, output_max, dtype))
+/* create an IOProcessor that proxies to a new IO object */
+
+IOProcessor::IOProcessor (Session& s, bool with_input, bool with_output,
+                         const string& proc_name, const string io_name, DataType dtype)
+       : Processor(s, proc_name)
 {
-       _active = false;
-       _sort_key = 0;
-       _gui = 0;
-       _extra_xml = 0;
+       /* these are true in this constructor whether we actually create the associated
+          IO objects or not.
+       */
+
+       _own_input = true;
+       _own_output = true;
+
+       if (with_input) {
+               _input.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Input, dtype));
+       }
+
+       if (with_output) {
+               _output.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Output, dtype));
+       }
+}
+
+/* create an IOProcessor that proxies to an existing IO object */
+
+IOProcessor::IOProcessor (Session& s, boost::shared_ptr<IO> in, boost::shared_ptr<IO> out, 
+                         const string& proc_name, DataType /*dtype*/)
+       : Processor(s, proc_name)
+       , _input (in)
+       , _output (out)
+{
+       if (in) {
+               _own_input = false;
+       } else {
+               _own_input = true;
+       }
+
+       if (out) {
+               _own_output = false;
+       } else {
+               _own_output = true;
+       }
 }
 
 IOProcessor::~IOProcessor ()
@@ -61,12 +92,56 @@ IOProcessor::~IOProcessor ()
        notify_callbacks ();
 }
 
+void
+IOProcessor::set_input (boost::shared_ptr<IO> io)
+{
+       /* CALLER MUST HOLD PROCESS LOCK */
+
+       _input = io;
+       _own_input = false;
+}
+
+void
+IOProcessor::set_output (boost::shared_ptr<IO> io)
+{
+       /* CALLER MUST HOLD PROCESS LOCK */
+
+       _output = io;
+       _own_output = false;
+}
+
 XMLNode&
 IOProcessor::state (bool full_state)
 {
-       XMLNode& node = Processor::state(full_state);
+       XMLNode& node (Processor::state (full_state));
        
-       node.add_child_nocopy (_io->state (full_state));
+       if (_own_input) {
+               node.add_property ("own-input", "yes");
+               if (_input) {
+                       XMLNode& i (_input->state (full_state));
+                       // i.name() = X_("output");
+                       node.add_child_nocopy (i);
+               }
+       } else {
+               node.add_property ("own-input", "no");
+               if (_input) {
+                       node.add_property ("input", _input->name());
+               }
+       }
+       
+       if (_own_output) {
+               node.add_property ("own-output", "yes");
+               if (_output) {
+                       XMLNode& o (_output->state (full_state));
+                       // o.name() = X_("output");
+                       node.add_child_nocopy (o);
+               }
+       } else {
+               node.add_property ("own-output", "no");
+               if (_output) {
+                       node.add_property ("output", _output->name());
+               }
+       }
 
        return node;
 }
@@ -79,76 +154,114 @@ IOProcessor::set_state (const XMLNode& node)
 
        Processor::set_state(node);
 
-       XMLNodeList nlist = node.children();
-       XMLNodeIterator niter;
+       if ((prop = node.property ("own-input")) != 0) {
+               _own_input = (prop->value() == "yes");
+       }
 
-       for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
-               if ((*niter)->name() == IO::state_node_name) {
-                       io_node = (*niter);
-                       break;
-               } else if ((*niter)->name() == "Redirect") {
-                       XMLNodeList rlist = (*niter)->children();
-                       XMLNodeIterator riter;
-
-                       for (riter = rlist.begin(); riter != rlist.end(); ++riter) {
-                               if ( (*riter)->name() == IO::state_node_name) {
-                                       warning << _("Found legacy IO in a redirect") << endmsg;
-                                       io_node = (*riter);
-                                       break;
-                               }
-                       }
-               }
+       if ((prop = node.property ("own-output")) != 0) {
+               _own_output = (prop->value() == "yes");
        }
 
-       if (io_node) {
-               _io->set_state(*io_node);
+       /* don't attempt to set state for a proxied IO that we don't own */
 
-               // legacy sessions: use IO name
-               if ((prop = node.property ("name")) == 0) {
-                       set_name(_io->name());
+       XMLNodeList nlist = node.children();
+       XMLNodeIterator niter;
+       
+       if (_own_input) {
+               for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
+                       if ((*niter)->name() == "input") {
+                               io_node = (*niter);
+                               break;
+                       }
+               }
+               
+               if (io_node) {
+                       _input->set_state(*io_node);
+                       
+                       // legacy sessions: use IO name
+                       if ((prop = node.property ("name")) == 0) {
+                               set_name (_input->name());
+                       }
+                       
+               } else {
+                       /* no input */
                }
 
-       } else {
-               error << _("XML node describing a redirect is missing an IO node") << endmsg;
-               return -1;
+       }
+       
+       if (_own_output) {
+               for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
+                       if ((*niter)->name() == "output") {
+                               io_node = (*niter);
+                               break;
+                       }
+               }
+               
+               if (io_node) {
+                       _output->set_state(*io_node);
+                       
+                       // legacy sessions: use IO name
+                       if ((prop = node.property ("name")) == 0) {
+                               set_name (_output->name());
+                       }
+               } else {
+                       /* no output */
+               }
        }
 
        return 0;
 }
 
 void
-IOProcessor::silence (nframes_t nframes, nframes_t offset)
+IOProcessor::silence (nframes_t nframes)
 {
-       _io->silence(nframes, offset);
+       if (_own_output && _output) {
+               _output->silence (nframes);
+       }
 }
 
 ChanCount
-IOProcessor::output_streams() const
+IOProcessor::natural_output_streams() const
 {
-       return _io->n_outputs();
+       return _output ? _output->n_ports() : ChanCount::ZERO;
 }
 
 ChanCount
-IOProcessor::input_streams () const
+IOProcessor::natural_input_streams () const
 {
-       return _io->n_inputs();
+       return _input ? _input->n_ports() : ChanCount::ZERO;
 }
 
-ChanCount
-IOProcessor::natural_output_streams() const
+bool
+IOProcessor::set_name (const std::string& name)
 {
-       return _io->n_outputs();
+       bool ret = SessionObject::set_name (name);
+
+       if (ret && _own_input && _input) {
+               ret = _input->set_name (name);
+       }
+
+       if (ret && _own_output && _output) {
+               ret = _output->set_name (name);
+       }
+
+       return ret;
 }
 
-ChanCount
-IOProcessor::natural_input_streams () const
+bool
+IOProcessor::feeds (boost::shared_ptr<Route> other) const
 {
-       return _io->n_inputs();
+       return _output && _output->connected_to (other->input());
 }
 
 void
-IOProcessor::automation_snapshot (nframes_t now, bool force)
+IOProcessor::disconnect ()
 {
-       _io->automation_snapshot(now, force);
-}
+       if (_input) {
+               _input->disconnect (this);
+       }
 
+       if (_output) {
+               _output->disconnect (this);
+       }
+}