enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / libs / ardour / port_manager.cc
index 1a08849791cbd027cf07b7267cdc67a8873223a6..ec8e3024eafd668fc9027ead4b44c6f0eba4e795 100644 (file)
 
 */
 
-
+#include "pbd/convert.h"
+#include "pbd/error.h"
+
+#include "ardour/async_midi_port.h"
+#include "ardour/audio_backend.h"
+#include "ardour/audio_port.h"
+#include "ardour/debug.h"
+#include "ardour/midi_port.h"
+#include "ardour/midiport_manager.h"
 #include "ardour/port_manager.h"
+#include "ardour/profile.h"
+#include "ardour/session.h"
+
+#include "pbd/i18n.h"
 
 using namespace ARDOUR;
+using namespace PBD;
+using std::string;
+using std::vector;
 
 PortManager::PortManager ()
-       , ports (new Ports)
+       : ports (new Ports)
+       , _port_remove_in_progress (false)
 {
 }
 
 void
-AudioEngine::remove_all_ports ()
+PortManager::remove_all_ports ()
 {
        /* make sure that JACK callbacks that will be invoked as we cleanup
         * ports know that they have nothing to do.
         */
 
-       port_remove_in_progress = true;
+       _port_remove_in_progress = true;
 
        /* process lock MUST be held by caller
        */
@@ -49,33 +65,32 @@ AudioEngine::remove_all_ports ()
 
        ports.flush ();
 
-       port_remove_in_progress = false;
+       _port_remove_in_progress = false;
 }
 
 
 string
-AudioEngine::make_port_name_relative (const string& portname) const
+PortManager::make_port_name_relative (const string& portname) const
 {
-       string::size_type len;
-       string::size_type n;
+       if (!_backend) {
+               return portname;
+       }
 
-       len = portname.length();
+       string::size_type colon = portname.find (':');
 
-       for (n = 0; n < len; ++n) {
-               if (portname[n] == ':') {
-                       break;
-               }
+       if (colon == string::npos) {
+               return portname;
        }
 
-       if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
-               return portname.substr (n+1);
+       if (portname.substr (0, colon) == _backend->my_name()) {
+               return portname.substr (colon+1);
        }
 
        return portname;
 }
 
 string
-AudioEngine::make_port_name_non_relative (const string& portname) const
+PortManager::make_port_name_non_relative (const string& portname) const
 {
        string str;
 
@@ -83,144 +98,112 @@ AudioEngine::make_port_name_non_relative (const string& portname) const
                return portname;
        }
 
-       str  = jack_client_name;
+       str  = _backend->my_name();
        str += ':';
        str += portname;
 
        return str;
 }
 
-bool
-AudioEngine::port_is_mine (const string& portname) const
+std::string
+PortManager::get_pretty_name_by_name(const std::string& portname) const
 {
-       if (portname.find_first_of (':') != string::npos) {
-               if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
-                        return false;
-                }
-        }
-        return true;
+       PortEngine::PortHandle ph = _backend->get_port_by_name (portname);
+       if (ph) {
+               std::string value;
+               std::string type;
+               if (0 == _backend->get_port_property (ph,
+                                       "http://jackaudio.org/metadata/pretty-name",
+                                       value, type))
+               {
+                       return value;
+               }
+       }
+       return "";
 }
 
 bool
-AudioEngine::port_is_physical (const std::string& portname) const
+PortManager::port_is_mine (const string& portname) const
 {
-        GET_PRIVATE_JACK_POINTER_RET(_jack, false);
+       if (!_backend) {
+               return true;
+       }
 
-        jack_port_t *port = jack_port_by_name (_priv_jack, portname.c_str());
+       string self = _backend->my_name();
 
-        if (!port) {
-                return false;
+       if (portname.find_first_of (':') != string::npos) {
+               if (portname.substr (0, self.length ()) != self) {
+                        return false;
+                }
         }
 
-        return jack_port_flags (port) & JackPortIsPhysical;
+        return true;
 }
 
-ChanCount
-AudioEngine::n_physical (unsigned long flags) const
+bool
+PortManager::port_is_physical (const std::string& portname) const
 {
-       ChanCount c;
-
-       GET_PRIVATE_JACK_POINTER_RET (_jack, c);
-
-       const char ** ports = jack_get_ports (_priv_jack, NULL, NULL, JackPortIsPhysical | flags);
-       if (ports == 0) {
-               return c;
+       if (!_backend) {
+               return false;
        }
 
-       for (uint32_t i = 0; ports[i]; ++i) {
-               if (!strstr (ports[i], "Midi-Through")) {
-                       DataType t (jack_port_type (jack_port_by_name (_jack, ports[i])));
-                       c.set (t, c.get (t) + 1);
-               }
+       PortEngine::PortHandle ph = _backend->get_port_by_name (portname);
+       if (!ph) {
+               return false;
        }
 
-       free (ports);
-
-       return c;
-}
-
-ChanCount
-AudioEngine::n_physical_inputs () const
-{
-       return n_physical (JackPortIsInput);
+       return _backend->port_is_physical (ph);
 }
 
-ChanCount
-AudioEngine::n_physical_outputs () const
+void
+PortManager::get_physical_outputs (DataType type, std::vector<std::string>& s)
 {
-       return n_physical (JackPortIsOutput);
+       if (!_backend) {
+               s.clear ();
+               return;
+       }
+       _backend->get_physical_outputs (type, s);
 }
 
 void
-AudioEngine::get_physical (DataType type, unsigned long flags, vector<string>& phy)
+PortManager::get_physical_inputs (DataType type, std::vector<std::string>& s)
 {
-       GET_PRIVATE_JACK_POINTER (_jack);
-       const char ** ports;
-
-       if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical | flags)) == 0) {
+       if (!_backend) {
+               s.clear ();
                return;
        }
 
-       if (ports) {
-               for (uint32_t i = 0; ports[i]; ++i) {
-                        if (strstr (ports[i], "Midi-Through")) {
-                                continue;
-                        }
-                       phy.push_back (ports[i]);
-               }
-               free (ports);
-       }
+       _backend->get_physical_inputs (type, s);
 }
 
-/** Get physical ports for which JackPortIsOutput is set; ie those that correspond to
- *  a physical input connector.
- */
-void
-AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
+ChanCount
+PortManager::n_physical_outputs () const
 {
-       get_physical (type, JackPortIsOutput, ins);
-}
+       if (!_backend) {
+               return ChanCount::ZERO;
+       }
 
-/** Get physical ports for which JackPortIsInput is set; ie those that correspond to
- *  a physical output connector.
- */
-void
-AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
-{
-       get_physical (type, JackPortIsInput, outs);
+       return _backend->n_physical_outputs ();
 }
 
-
-bool
-AudioEngine::can_request_hardware_monitoring ()
+ChanCount
+PortManager::n_physical_inputs () const
 {
-       GET_PRIVATE_JACK_POINTER_RET (_jack,false);
-       const char ** ports;
-
-       if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
-               return false;
+       if (!_backend) {
+               return ChanCount::ZERO;
        }
-
-       free (ports);
-
-       return true;
+       return _backend->n_physical_inputs ();
 }
 
-
 /** @param name Full or short name of port
  *  @return Corresponding Port or 0.
  */
 
 boost::shared_ptr<Port>
-AudioEngine::get_port_by_name (const string& portname)
+PortManager::get_port_by_name (const string& portname)
 {
-       if (!_running) {
-               if (!_has_run) {
-                       fatal << _("get_port_by_name() called before engine was started") << endmsg;
-                       /*NOTREACHED*/
-               } else {
-                       boost::shared_ptr<Port> ();
-               }
+       if (!_backend) {
+               return boost::shared_ptr<Port>();
        }
 
         if (!port_is_mine (portname)) {
@@ -238,7 +221,7 @@ AudioEngine::get_port_by_name (const string& portname)
                   and cheap), and if so, rename the port (which will alter
                   the port map as a side effect).
                */
-               const std::string check = make_port_name_relative (jack_port_name (x->second->jack_port()));
+               const std::string check = make_port_name_relative (_backend->get_port_name (x->second->port_handle()));
                if (check != rel) {
                        x->second->set_name (check);
                }
@@ -249,12 +232,12 @@ AudioEngine::get_port_by_name (const string& portname)
 }
 
 void
-AudioEngine::port_renamed (const std::string& old_relative_name, const std::string& new_relative_name)
+PortManager::port_renamed (const std::string& old_relative_name, const std::string& new_relative_name)
 {
        RCUWriter<Ports> writer (ports);
        boost::shared_ptr<Ports> p = writer.get_copy();
        Ports::iterator x = p->find (old_relative_name);
-       
+
        if (x != p->end()) {
                boost::shared_ptr<Port> port = x->second;
                p->erase (x);
@@ -262,52 +245,74 @@ AudioEngine::port_renamed (const std::string& old_relative_name, const std::stri
        }
 }
 
-const char **
-AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
+int
+PortManager::get_ports (DataType type, PortList& pl)
 {
-       GET_PRIVATE_JACK_POINTER_RET (_jack,0);
-       if (!_running) {
-               if (!_has_run) {
-                       fatal << _("get_ports called before engine was started") << endmsg;
-                       /*NOTREACHED*/
-               } else {
-                       return 0;
+       boost::shared_ptr<Ports> plist = ports.reader();
+       for (Ports::iterator p = plist->begin(); p != plist->end(); ++p) {
+               if (p->second->type() == type) {
+                       pl.push_back (p->second);
                }
        }
-       return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
+       return pl.size();
+}
+
+int
+PortManager::get_ports (const string& port_name_pattern, DataType type, PortFlags flags, vector<string>& s)
+{
+       s.clear();
+
+       if (!_backend) {
+               return 0;
+       }
+
+       return _backend->get_ports (port_name_pattern, type, flags, s);
 }
 
 void
-AudioEngine::port_registration_failure (const std::string& portname)
+PortManager::port_registration_failure (const std::string& portname)
 {
-       GET_PRIVATE_JACK_POINTER (_jack);
-       string full_portname = jack_client_name;
+       if (!_backend) {
+               return;
+       }
+
+       string full_portname = _backend->my_name();
        full_portname += ':';
        full_portname += portname;
 
 
-       jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
+       PortEngine::PortHandle p = _backend->get_port_by_name (full_portname);
        string reason;
 
        if (p) {
                reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
        } else {
-               reason = string_compose (_("No more JACK ports are available. You will need to stop %1 and restart JACK with more ports if you need this many tracks."), PROGRAM_NAME);
+               reason = string_compose (_("No more ports are available. You will need to stop %1 and restart with more ports if you need this many tracks."), PROGRAM_NAME);
        }
 
        throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
 }
 
 boost::shared_ptr<Port>
-AudioEngine::register_port (DataType dtype, const string& portname, bool input)
+PortManager::register_port (DataType dtype, const string& portname, bool input, bool async)
 {
        boost::shared_ptr<Port> newport;
 
        try {
                if (dtype == DataType::AUDIO) {
-                       newport.reset (new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput)));
+                       DEBUG_TRACE (DEBUG::Ports, string_compose ("registering AUDIO port %1, input %2\n",
+                                                                  portname, input));
+                       newport.reset (new AudioPort (portname, (input ? IsInput : IsOutput)));
                } else if (dtype == DataType::MIDI) {
-                       newport.reset (new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput)));
+                       if (async) {
+                               DEBUG_TRACE (DEBUG::Ports, string_compose ("registering ASYNC MIDI port %1, input %2\n",
+                                                                          portname, input));
+                               newport.reset (new AsyncMIDIPort (portname, (input ? IsInput : IsOutput)));
+                       } else {
+                               DEBUG_TRACE (DEBUG::Ports, string_compose ("registering MIDI port %1, input %2\n",
+                                                                          portname, input));
+                               newport.reset (new MidiPort (portname, (input ? IsInput : IsOutput)));
+                       }
                } else {
                        throw PortRegistrationFailure("unable to create port (unknown type)");
                }
@@ -318,7 +323,6 @@ AudioEngine::register_port (DataType dtype, const string& portname, bool input)
 
                /* writer goes out of scope, forces update */
 
-               return newport;
        }
 
        catch (PortRegistrationFailure& err) {
@@ -329,32 +333,28 @@ AudioEngine::register_port (DataType dtype, const string& portname, bool input)
        } catch (...) {
                throw PortRegistrationFailure("unable to create port (unknown error)");
        }
+
+       DEBUG_TRACE (DEBUG::Ports, string_compose ("\t%2 port registration success, ports now = %1\n", ports.reader()->size(), this));
+       return newport;
 }
 
 boost::shared_ptr<Port>
-AudioEngine::register_input_port (DataType type, const string& portname)
+PortManager::register_input_port (DataType type, const string& portname, bool async)
 {
-       return register_port (type, portname, true);
+       return register_port (type, portname, true, async);
 }
 
 boost::shared_ptr<Port>
-AudioEngine::register_output_port (DataType type, const string& portname)
+PortManager::register_output_port (DataType type, const string& portname, bool async)
 {
-       return register_port (type, portname, false);
+       return register_port (type, portname, false, async);
 }
 
 int
-AudioEngine::unregister_port (boost::shared_ptr<Port> port)
+PortManager::unregister_port (boost::shared_ptr<Port> port)
 {
        /* caller must hold process lock */
 
-       if (!_running) {
-               /* probably happening when the engine has been halted by JACK,
-                  in which case, there is nothing we can do here.
-                  */
-               return 0;
-       }
-
        {
                RCUWriter<Ports> writer (ports);
                boost::shared_ptr<Ports> ps = writer.get_copy ();
@@ -375,33 +375,61 @@ AudioEngine::unregister_port (boost::shared_ptr<Port> port)
 bool
 PortManager::connected (const string& port_name)
 {
-       PortEngine::PortHandle handle = _impl->get_port_by_name (port_name);
+       if (!_backend) {
+               return false;
+       }
+
+       PortEngine::PortHandle handle = _backend->get_port_by_name (port_name);
+
+       if (!handle) {
+               return false;
+       }
+
+       return _backend->connected (handle);
+}
+
+bool
+PortManager::physically_connected (const string& port_name)
+{
+       if (!_backend) {
+               return false;
+       }
+
+       PortEngine::PortHandle handle = _backend->get_port_by_name (port_name);
 
        if (!handle) {
                return false;
        }
 
-       return _impl->connected (handle);
+       return _backend->physically_connected (handle);
 }
 
 int
-AudioEngine::connect (const string& source, const string& destination)
+PortManager::get_connections (const string& port_name, std::vector<std::string>& s)
 {
-       int ret;
+       if (!_backend) {
+               s.clear ();
+               return 0;
+       }
 
-       if (!_running) {
-               if (!_has_run) {
-                       fatal << _("connect called before engine was started") << endmsg;
-                       /*NOTREACHED*/
-               } else {
-                       return -1;
-               }
+       PortEngine::PortHandle handle = _backend->get_port_by_name (port_name);
+
+       if (!handle) {
+               s.clear ();
+               return 0;
        }
 
+       return _backend->get_connections (handle, s);
+}
+
+int
+PortManager::connect (const string& source, const string& destination)
+{
+       int ret;
+
        string s = make_port_name_non_relative (source);
        string d = make_port_name_non_relative (destination);
 
-
        boost::shared_ptr<Port> src = get_port_by_name (s);
        boost::shared_ptr<Port> dst = get_port_by_name (d);
 
@@ -410,8 +438,13 @@ AudioEngine::connect (const string& source, const string& destination)
        } else if (dst) {
                ret = dst->connect (s);
        } else {
-               /* neither port is known to us, and this API isn't intended for use as a general patch bay */
-               ret = -1;
+               /* neither port is known to us ...hand-off to the PortEngine
+                */
+               if (_backend) {
+                       ret = _backend->connect (s, d);
+               } else {
+                       ret = -1;
+               }
        }
 
        if (ret > 0) {
@@ -426,19 +459,10 @@ AudioEngine::connect (const string& source, const string& destination)
 }
 
 int
-AudioEngine::disconnect (const string& source, const string& destination)
+PortManager::disconnect (const string& source, const string& destination)
 {
        int ret;
 
-       if (!_running) {
-               if (!_has_run) {
-                       fatal << _("disconnect called before engine was started") << endmsg;
-                       /*NOTREACHED*/
-               } else {
-                       return -1;
-               }
-       }
-
        string s = make_port_name_non_relative (source);
        string d = make_port_name_non_relative (destination);
 
@@ -446,30 +470,24 @@ AudioEngine::disconnect (const string& source, const string& destination)
        boost::shared_ptr<Port> dst = get_port_by_name (d);
 
        if (src) {
-                       ret = src->disconnect (d);
+               ret = src->disconnect (d);
        } else if (dst) {
-                       ret = dst->disconnect (s);
+               ret = dst->disconnect (s);
        } else {
-               /* neither port is known to us, and this API isn't intended for use as a general patch bay */
-               ret = -1;
+               /* neither port is known to us ...hand-off to the PortEngine
+                */
+               if (_backend) {
+                       ret = _backend->disconnect (s, d);
+               } else {
+                       ret = -1;
+               }
        }
        return ret;
 }
 
 int
-AudioEngine::disconnect (boost::shared_ptr<Port> port)
+PortManager::disconnect (boost::shared_ptr<Port> port)
 {
-       GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
-
-       if (!_running) {
-               if (!_has_run) {
-                       fatal << _("disconnect called before engine was started") << endmsg;
-                       /*NOTREACHED*/
-               } else {
-                       return -1;
-               }
-       }
-
        return port->disconnect_all ();
 }
 
@@ -480,8 +498,12 @@ PortManager::reestablish_ports ()
 
        boost::shared_ptr<Ports> p = ports.reader ();
 
+       DEBUG_TRACE (DEBUG::Ports, string_compose ("reestablish %1 ports\n", p->size()));
+
        for (i = p->begin(); i != p->end(); ++i) {
                if (i->second->reestablish ()) {
+                       error << string_compose (_("Re-establising port %1 failed"), i->second->name()) << endmsg;
+                       std::cerr << string_compose (_("Re-establising port %1 failed"), i->second->name()) << std::endl;
                        break;
                }
        }
@@ -492,8 +514,6 @@ PortManager::reestablish_ports ()
                return -1;
        }
 
-       MIDI::Manager::instance()->reestablish ();
-
        return 0;
 }
 
@@ -502,13 +522,248 @@ PortManager::reconnect_ports ()
 {
        boost::shared_ptr<Ports> p = ports.reader ();
 
-       /* re-establish connections */
-       
-       for (i = p->begin(); i != p->end(); ++i) {
-               i->second->reconnect ();
+       if (!Profile->get_trx()) {
+               /* re-establish connections */
+
+               DEBUG_TRACE (DEBUG::Ports, string_compose ("reconnect %1 ports\n", p->size()));
+
+               for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+                       i->second->reconnect ();
+               }
+       }
+
+       return 0;
+}
+
+void
+PortManager::connect_callback (const string& a, const string& b, bool conn)
+{
+       boost::shared_ptr<Port> port_a;
+       boost::shared_ptr<Port> port_b;
+       Ports::iterator x;
+       boost::shared_ptr<Ports> pr = ports.reader ();
+
+       x = pr->find (make_port_name_relative (a));
+       if (x != pr->end()) {
+               port_a = x->second;
+       }
+
+       x = pr->find (make_port_name_relative (b));
+       if (x != pr->end()) {
+               port_b = x->second;
+       }
+
+       PortConnectedOrDisconnected (
+               port_a, a,
+               port_b, b,
+               conn
+               ); /* EMIT SIGNAL */
+}
+
+void
+PortManager::registration_callback ()
+{
+       if (!_port_remove_in_progress) {
+               PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
+       }
+}
+
+bool
+PortManager::can_request_input_monitoring () const
+{
+       if (!_backend) {
+               return false;
+       }
+
+       return _backend->can_monitor_input ();
+}
+
+void
+PortManager::request_input_monitoring (const string& name, bool yn) const
+{
+       if (!_backend) {
+               return;
+       }
+
+       PortEngine::PortHandle ph = _backend->get_port_by_name (name);
+
+       if (ph) {
+               _backend->request_input_monitoring (ph, yn);
+       }
+}
+
+void
+PortManager::ensure_input_monitoring (const string& name, bool yn) const
+{
+       if (!_backend) {
+               return;
+       }
+
+       PortEngine::PortHandle ph = _backend->get_port_by_name (name);
+
+       if (ph) {
+               _backend->ensure_input_monitoring (ph, yn);
+       }
+}
+
+uint32_t
+PortManager::port_name_size() const
+{
+       if (!_backend) {
+               return 0;
+       }
+
+       return _backend->port_name_size ();
+}
+
+string
+PortManager::my_name() const
+{
+       if (!_backend) {
+               return string();
        }
 
-       MIDI::Manager::instance()->reconnect ();
+       return _backend->my_name();
+}
+
+int
+PortManager::graph_order_callback ()
+{
+       if (!_port_remove_in_progress) {
+               GraphReordered(); /* EMIT SIGNAL */
+       }
 
        return 0;
 }
+
+void
+PortManager::cycle_start (pframes_t nframes)
+{
+       Port::set_global_port_buffer_offset (0);
+        Port::set_cycle_framecnt (nframes);
+
+       _cycle_ports = ports.reader ();
+
+       for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
+               p->second->cycle_start (nframes);
+       }
+}
+
+void
+PortManager::cycle_end (pframes_t nframes)
+{
+       for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
+               p->second->cycle_end (nframes);
+       }
+
+       for (Ports::iterator p = _cycle_ports->begin(); p != _cycle_ports->end(); ++p) {
+               p->second->flush_buffers (nframes);
+       }
+
+       _cycle_ports.reset ();
+
+       /* we are done */
+}
+
+void
+PortManager::silence (pframes_t nframes, Session *s)
+{
+       for (Ports::iterator i = _cycle_ports->begin(); i != _cycle_ports->end(); ++i) {
+               if (s && i->second == s->mtc_output_port ()) {
+                       continue;
+               }
+               if (s && i->second == s->midi_clock_output_port ()) {
+                       continue;
+               }
+               if (s && i->second == s->ltc_output_port ()) {
+                       continue;
+               }
+               if (i->second->sends_output()) {
+                       i->second->get_buffer(nframes).silence(nframes);
+               }
+       }
+}
+
+void
+PortManager::silence_outputs (pframes_t nframes)
+{
+       std::vector<std::string> port_names;
+       if (get_ports("", DataType::AUDIO, IsOutput, port_names)) {
+               for (std::vector<std::string>::iterator p = port_names.begin(); p != port_names.end(); ++p) {
+                       if (!port_is_mine(*p)) {
+                               continue;
+                       }
+                       PortEngine::PortHandle ph = _backend->get_port_by_name (*p);
+                       if (!ph) {
+                               continue;
+                       }
+                       void *buf = _backend->get_buffer(ph, nframes);
+                       if (!buf) {
+                               continue;
+                       }
+                       memset (buf, 0, sizeof(float) * nframes);
+               }
+       }
+
+       if (get_ports("", DataType::MIDI, IsOutput, port_names)) {
+               for (std::vector<std::string>::iterator p = port_names.begin(); p != port_names.end(); ++p) {
+                       if (!port_is_mine(*p)) {
+                               continue;
+                       }
+                       PortEngine::PortHandle ph = _backend->get_port_by_name (*p);
+                       if (!ph) {
+                               continue;
+                       }
+                       void *buf = _backend->get_buffer(ph, nframes);
+                       if (!buf) {
+                               continue;
+                       }
+                       _backend->midi_clear (buf);
+               }
+       }
+}
+
+void
+PortManager::check_monitoring ()
+{
+       for (Ports::iterator i = _cycle_ports->begin(); i != _cycle_ports->end(); ++i) {
+
+               bool x;
+
+               if (i->second->last_monitor() != (x = i->second->monitoring_input ())) {
+                       i->second->set_last_monitor (x);
+                       /* XXX I think this is dangerous, due to
+                          a likely mutex in the signal handlers ...
+                       */
+                       i->second->MonitorInputChanged (x); /* EMIT SIGNAL */
+               }
+       }
+}
+
+void
+PortManager::fade_out (gain_t base_gain, gain_t gain_step, pframes_t nframes)
+{
+       for (Ports::iterator i = _cycle_ports->begin(); i != _cycle_ports->end(); ++i) {
+
+               if (i->second->sends_output()) {
+
+                       boost::shared_ptr<AudioPort> ap = boost::dynamic_pointer_cast<AudioPort> (i->second);
+                       if (ap) {
+                               Sample* s = ap->engine_get_whole_audio_buffer ();
+                               gain_t g = base_gain;
+
+                               for (pframes_t n = 0; n < nframes; ++n) {
+                                       *s++ *= g;
+                                       g -= gain_step;
+                               }
+                       }
+               }
+       }
+}
+
+PortEngine&
+PortManager::port_engine()
+{
+       assert (_backend);
+       return *_backend;
+}