DummyAudioBackend: port-engine, midi-buffers and the rest of it
authorRobin Gareus <robin@gareus.org>
Mon, 28 Apr 2014 21:00:40 +0000 (23:00 +0200)
committerRobin Gareus <robin@gareus.org>
Mon, 28 Apr 2014 21:00:40 +0000 (23:00 +0200)
libs/backends/dummy/dummy_audiobackend.cc
libs/backends/dummy/dummy_audiobackend.h

index 8acc08e71c5b7c3045a0fe45ef1f0baaba5842f5..4d2cdc827f700acdb205d6fe7de8b92264cade5c 100644 (file)
@@ -18,6 +18,8 @@
  */
 
 #include <sys/time.h>
+#include <regex.h>
+
 #include "dummy_audiobackend.h"
 #include "pbd/error.h"
 #include "i18n.h"
@@ -25,6 +27,8 @@
 using namespace ARDOUR;
 
 static std::string s_instance_name;
+size_t DummyAudioBackend::_max_buffer_size = 8192;
+
 DummyAudioBackend::DummyAudioBackend (AudioEngine& e)
        : AudioBackend (e)
        , _running (false)
@@ -71,8 +75,15 @@ std::vector<float>
 DummyAudioBackend::available_sample_rates (const std::string&) const
 {
        std::vector<float> sr;
+       sr.push_back (8000.0);
+       sr.push_back (22050.0);
+       sr.push_back (24000.0);
        sr.push_back (44100.0);
        sr.push_back (48000.0);
+       sr.push_back (88200.0);
+       sr.push_back (96000.0);
+       sr.push_back (176400.0);
+       sr.push_back (192000.0);
        return sr;
 }
 
@@ -80,8 +91,18 @@ std::vector<uint32_t>
 DummyAudioBackend::available_buffer_sizes (const std::string&) const
 {
        std::vector<uint32_t> bs;
+       bs.push_back (4);
+       bs.push_back (8);
+       bs.push_back (16);
+       bs.push_back (32);
        bs.push_back (64);
+       bs.push_back (128);
+       bs.push_back (256);
+       bs.push_back (512);
        bs.push_back (1024);
+       bs.push_back (2048);
+       bs.push_back (4096);
+       bs.push_back (8192);
        return bs;
 }
 
@@ -127,7 +148,9 @@ DummyAudioBackend::set_sample_rate (float sr)
 int
 DummyAudioBackend::set_buffer_size (uint32_t bs)
 {
-       return -1;
+       if (bs <= 0 || bs >= _max_buffer_size) {
+               return -1;
+       }
        _audio_buffersize = bs;
        engine.buffer_size_change (bs);
        return 0;
@@ -141,27 +164,31 @@ DummyAudioBackend::set_interleaved (bool yn)
 }
 
 int
-DummyAudioBackend::set_input_channels (uint32_t)
+DummyAudioBackend::set_input_channels (uint32_t cc)
 {
-       return -1;
+       _n_inputs = cc;
+       return 0;
 }
 
 int
-DummyAudioBackend::set_output_channels (uint32_t)
+DummyAudioBackend::set_output_channels (uint32_t cc)
 {
-       return -1;
+       _n_outputs = cc;
+       return 0;
 }
 
 int
-DummyAudioBackend::set_systemic_input_latency (uint32_t)
+DummyAudioBackend::set_systemic_input_latency (uint32_t sl)
 {
-       return -1;
+       _systemic_input_latency = sl;
+       return 0;
 }
 
 int
-DummyAudioBackend::set_systemic_output_latency (uint32_t)
+DummyAudioBackend::set_systemic_output_latency (uint32_t sl)
 {
-       return -1;
+       _systemic_output_latency = sl;
+       return 0;
 }
 
 /* Retrieving parameters */
@@ -192,25 +219,25 @@ DummyAudioBackend::interleaved () const
 uint32_t
 DummyAudioBackend::input_channels () const
 {
-       return 0;
+       return _n_inputs;
 }
 
 uint32_t
 DummyAudioBackend::output_channels () const
 {
-       return 0;
+       return _n_outputs;
 }
 
 uint32_t
 DummyAudioBackend::systemic_input_latency () const
 {
-       return 0;
+       return _systemic_input_latency;
 }
 
 uint32_t
 DummyAudioBackend::systemic_output_latency () const
 {
-       return 0;
+       return _systemic_output_latency;
 }
 
 /* MIDI */
@@ -309,6 +336,12 @@ DummyAudioBackend::dsp_load () const
 size_t
 DummyAudioBackend::raw_buffer_size (DataType t)
 {
+       switch (t) {
+               case DataType::AUDIO:
+                       return _max_buffer_size * sizeof(Sample);
+               case DataType::MIDI:
+                       return _max_buffer_size; // XXX not really limited
+       }
        return 0;
 }
 
@@ -435,130 +468,287 @@ DummyAudioBackend::port_name_size () const
 }
 
 int
-DummyAudioBackend::set_port_name (PortEngine::PortHandle, const std::string&)
+DummyAudioBackend::set_port_name (PortEngine::PortHandle port, const std::string& name)
 {
-       return -1;
+       if (!valid_port (port)) {
+               PBD::error << _("DummyBackend::set_port_name: Invalid Port(s)") << endmsg;
+               return -1;
+       }
+       return static_cast<DummyPort*>(port)->set_name (_instance_name + ":" + name);
 }
 
 std::string
-DummyAudioBackend::get_port_name (PortEngine::PortHandle) const
+DummyAudioBackend::get_port_name (PortEngine::PortHandle port) const
 {
-       return "port:XXX";
+       if (!valid_port (port)) {
+               PBD::error << _("DummyBackend::get_port_name: Invalid Port(s)") << endmsg;
+               return std::string ();
+       }
+       return static_cast<DummyPort*>(port)->name ();
 }
 
 PortEngine::PortHandle
-DummyAudioBackend::get_port_by_name (const std::string&) const
+DummyAudioBackend::get_port_by_name (const std::string& name) const
 {
-       PortEngine::PortHandle port_handle = 0;
-       return port_handle;
+       PortHandle port = (PortHandle) find_port (name);
+       return port;
 }
 
 int
 DummyAudioBackend::get_ports (
                const std::string& port_name_pattern,
                DataType type, PortFlags flags,
-               std::vector<std::string>&) const
+               std::vector<std::string>& port_names) const
 {
-       return 0;
+       int rv = 0;
+       regex_t port_regex;
+       bool use_regexp = false;
+       if (port_name_pattern.size () > 0) {
+               if (!regcomp (&port_regex, port_name_pattern.c_str (), REG_EXTENDED|REG_NOSUB)) {
+                       use_regexp = true;
+               }
+       }
+       for (size_t i = 0; i < _ports.size (); ++i) {
+               DummyPort* port = _ports[i];
+               if ((port->type () == type) && (port->flags () & flags)) {
+                       if (!use_regexp || !regexec (&port_regex, port->name ().c_str (), 0, NULL, 0)) {
+                               port_names.push_back (port->name ());
+                               ++rv;
+                       }
+               }
+       }
+       if (use_regexp) {
+               regfree (&port_regex);
+       }
+       return rv;
 }
 
 DataType
-DummyAudioBackend::port_data_type (PortEngine::PortHandle) const
+DummyAudioBackend::port_data_type (PortEngine::PortHandle port) const
 {
-       return DataType::AUDIO;
+       if (!valid_port (port)) {
+               return DataType::NIL;
+       }
+       return static_cast<DummyPort*>(port)->type ();
 }
 
 PortEngine::PortHandle
 DummyAudioBackend::register_port (
-               const std::string&,
-               ARDOUR::DataType,
-               ARDOUR::PortFlags)
-{
-       PortEngine::PortHandle port_handle = 0;
-       return port_handle;
+               const std::string& name,
+               ARDOUR::DataType type,
+               ARDOUR::PortFlags flags)
+{
+       if (name.size () == 0) { return 0; }
+       if (flags & IsPhysical) { return 0; }
+       if (find_port (_instance_name + ":" + name) != NULL) {
+               PBD::error << _("DummyBackend::register_port: Port already exists.") << endmsg;
+               return 0;
+       }
+
+       DummyPort* port = NULL;
+       switch (type) {
+               case DataType::AUDIO:
+                       port = new DummyAudioPort (_instance_name + ":" + name, flags);
+                       break;
+               case DataType::MIDI:
+                       port = new DummyMidiPort (_instance_name + ":" + name, flags);
+                       break;
+               default:
+                       PBD::error << _("DummyBackend::register_port: Invalid Data Type.") << endmsg;
+                       return 0;
+       }
+
+       _ports.push_back (port);
+
+       return port;
 }
 
 void
-DummyAudioBackend::unregister_port (PortEngine::PortHandle)
+DummyAudioBackend::unregister_port (PortEngine::PortHandle port_handle)
 {
+       if (!valid_port (port_handle)) {
+               PBD::error << _("DummyBackend::unregister_port: Invalid Port.") << endmsg;
+       }
+       DummyPort* port = (DummyPort*)port_handle;
+       std::vector<DummyPort*>::iterator i = std::find (_ports.begin (), _ports.end (), (DummyPort*)port_handle);
+       if (i == _ports.end ()) {
+               PBD::error << _("DummyBackend::unregister_port: Failed to find port") << endmsg;
+               return;
+       }
+       _ports.erase (i);
+       delete port;
 }
 
 int
 DummyAudioBackend::connect (const std::string& src, const std::string& dst)
 {
-       return -1;
+       DummyPort* src_port = find_port (src);
+       DummyPort* dst_port = find_port (dst);
+
+       if (!src_port) {
+               PBD::error << _("DummyBackend::connect: Invalid Source port:")
+                               << " (" << src <<")" << endmsg;
+               return -1;
+       }
+       if (!dst_port) {
+               PBD::error << _("DummyBackend::connect: Invalid Destination port:")
+                       << " (" << dst <<")" << endmsg;
+               return -1;
+       }
+       return src_port->connect (dst_port);
 }
 
 int
 DummyAudioBackend::disconnect (const std::string& src, const std::string& dst)
 {
-       return -1;
+       DummyPort* src_port = find_port (src);
+       DummyPort* dst_port = find_port (dst);
+
+       if (!src_port || !dst_port) {
+               PBD::error << _("DummyBackend::disconnect: Invalid Port(s)") << endmsg;
+               return -1;
+       }
+       return src_port->disconnect (dst_port);
 }
 
 int
-DummyAudioBackend::connect (PortEngine::PortHandle, const std::string&)
+DummyAudioBackend::connect (PortEngine::PortHandle src, const std::string& dst)
 {
-       return -1;
+       DummyPort* dst_port = find_port (dst);
+       if (!valid_port (src)) {
+               PBD::error << _("DummyBackend::connect: Invalid Source Port Handle") << endmsg;
+               return -1;
+       }
+       if (!dst_port) {
+               PBD::error << _("DummyBackend::connect: Invalid Destination Port")
+                       << " (" << dst << ")" << endmsg;
+               return -1;
+       }
+       return static_cast<DummyPort*>(src)->connect (dst_port);
 }
 
 int
-DummyAudioBackend::disconnect (PortEngine::PortHandle, const std::string&)
+DummyAudioBackend::disconnect (PortEngine::PortHandle src, const std::string& dst)
 {
-       return -1;
+       DummyPort* dst_port = find_port (dst);
+       if (!valid_port (src) || !dst_port) {
+               PBD::error << _("DummyBackend::disconnect: Invalid Port(s)") << endmsg;
+               return -1;
+       }
+       return static_cast<DummyPort*>(src)->disconnect (dst_port);
 }
 
 int
-DummyAudioBackend::disconnect_all (PortEngine::PortHandle)
+DummyAudioBackend::disconnect_all (PortEngine::PortHandle port)
 {
-       return -1;
+       if (!valid_port (port)) {
+               PBD::error << _("DummyBackend::disconnect_all: Invalid Port") << endmsg;
+               return -1;
+       }
+       static_cast<DummyPort*>(port)->disconnect_all ();
+       return 0;
 }
 
 bool
-DummyAudioBackend::connected (PortEngine::PortHandle, bool process_callback_safe)
+DummyAudioBackend::connected (PortEngine::PortHandle port, bool /* process_callback_safe*/)
 {
-       return false;
+       if (!valid_port (port)) {
+               PBD::error << _("DummyBackend::disconnect_all: Invalid Port") << endmsg;
+               return false;
+       }
+       return static_cast<DummyPort*>(port)->is_connected ();
 }
 
 bool
-DummyAudioBackend::connected_to (PortEngine::PortHandle, const std::string&, bool process_callback_safe)
+DummyAudioBackend::connected_to (PortEngine::PortHandle src, const std::string& dst, bool /*process_callback_safe*/)
 {
-       return false;
+       DummyPort* dst_port = find_port (dst);
+       if (!valid_port (src) || !dst_port) {
+               PBD::error << _("DummyBackend::connected_to: Invalid Port") << endmsg;
+               return false;
+       }
+       return static_cast<DummyPort*>(src)->is_connected (dst_port);
 }
 
 bool
-DummyAudioBackend::physically_connected (PortEngine::PortHandle, bool process_callback_safe)
+DummyAudioBackend::physically_connected (PortEngine::PortHandle port, bool /*process_callback_safe*/)
 {
-       return false;
+       if (!valid_port (port)) {
+               PBD::error << _("DummyBackend::physically_connected: Invalid Port") << endmsg;
+               return false;
+       }
+       return static_cast<DummyPort*>(port)->is_physically_connected ();
 }
 
 int
-DummyAudioBackend::get_connections (PortEngine::PortHandle, std::vector<std::string>&, bool process_callback_safe)
+DummyAudioBackend::get_connections (PortEngine::PortHandle port, std::vector<std::string>& names, bool /*process_callback_safe*/)
 {
-       return false;
+       if (!valid_port (port)) {
+               PBD::error << _("DummyBackend::get_connections: Invalid Port") << endmsg;
+               return -1;
+       }
+
+       assert (0 == names.size ());
+
+       const std::vector<DummyPort*>& connected_ports = static_cast<DummyPort*>(port)->get_connections ();
+
+       for (std::vector<DummyPort*>::const_iterator i = connected_ports.begin (); i != connected_ports.end (); ++i) {
+               names.push_back ((*i)->name ());
+       }
+
+       return (int)names.size ();
 }
 
 /* MIDI */
 int
-DummyAudioBackend::midi_event_get (pframes_t& timestamp, size_t& size, uint8_t** buf, void* port_buffer, uint32_t event_index)
-{
-       return -1;
+DummyAudioBackend::midi_event_get (
+               pframes_t& timestamp,
+               size_t& size, uint8_t** buf, void* port_buffer,
+               uint32_t event_index)
+{
+       assert (buf && port_buffer);
+       DummyMidiBuffer& source = * (DummyMidiBuffer*)port_buffer;
+       if (event_index >= source.size ()) {
+               return -1;
+       }
+       DummyMidiEvent * const event = source[event_index].get ();
+
+       timestamp = event->timestamp ();
+       size = event->size ();
+       *buf = event->data ();
+       return 0;
 }
 
 int
-DummyAudioBackend::midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size)
-{
-       return -1;
+DummyAudioBackend::midi_event_put (
+               void* port_buffer,
+               pframes_t timestamp,
+               const uint8_t* buffer, size_t size)
+{
+       assert (buffer && port_buffer);
+       DummyMidiBuffer& dst = * (DummyMidiBuffer*)port_buffer;
+       if (dst.size () && (pframes_t)dst.back ()->timestamp () > timestamp) {
+               fprintf (stderr, "DummyMidiBuffer: it's too late for this event.\n");
+               return -1;
+       }
+       dst.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (timestamp, buffer, size)));
+       return 0;
 }
 
 uint32_t
 DummyAudioBackend::get_midi_event_count (void* port_buffer)
 {
-       return -1;
+       assert (port_buffer);
+       return static_cast<DummyMidiBuffer*>(port_buffer)->size ();
 }
 
 void
 DummyAudioBackend::midi_clear (void* port_buffer)
 {
+       assert (port_buffer);
+       DummyMidiBuffer * buf = static_cast<DummyMidiBuffer*>(port_buffer);
+       assert (buf);
+       buf->clear ();
 }
 
 /* Monitoring */
@@ -590,60 +780,111 @@ DummyAudioBackend::monitoring_input (PortEngine::PortHandle)
 /* Latency management */
 
 void
-DummyAudioBackend::set_latency_range (PortEngine::PortHandle, bool for_playback, LatencyRange)
+DummyAudioBackend::set_latency_range (PortEngine::PortHandle port, bool for_playback, LatencyRange latency_range)
 {
+       if (!valid_port (port)) {
+               PBD::error << _("DummyPort::set_latency_range (): invalid port.") << endmsg;
+       }
+       static_cast<DummyPort*>(port)->set_latency_range (latency_range, for_playback);
 }
 
 LatencyRange
-DummyAudioBackend::get_latency_range (PortEngine::PortHandle, bool for_playback)
-{
-       LatencyRange r;
-       r.min = 0;
-       r.max = 0;
-       return r;
+DummyAudioBackend::get_latency_range (PortEngine::PortHandle port, bool for_playback)
+{
+       if (!valid_port (port)) {
+               PBD::error << _("DummyPort::get_latency_range (): invalid port.") << endmsg;
+               LatencyRange r;
+               r.min = 0;
+               r.max = 0;
+               return r;
+       }
+       return static_cast<DummyPort*>(port)->latency_range (for_playback);
 }
 
 /* Discovering physical ports */
 
 bool
-DummyAudioBackend::port_is_physical (PortEngine::PortHandle) const
+DummyAudioBackend::port_is_physical (PortEngine::PortHandle port) const
 {
-       return false;
+       if (!valid_port (port)) {
+               PBD::error << _("DummyPort::port_is_physical (): invalid port.") << endmsg;
+               return false;
+       }
+       return static_cast<DummyPort*>(port)->is_physical ();
 }
 
 void
-DummyAudioBackend::get_physical_outputs (DataType type, std::vector<std::string>&)
+DummyAudioBackend::get_physical_outputs (DataType type, std::vector<std::string>& port_names)
 {
+       for (size_t i = 0; i < _ports.size (); ++i) {
+               DummyPort* port = _ports[i];
+               if ((port->type () == type) && port->is_output () && port->is_physical ()) {
+                       port_names.push_back (port->name ());
+               }
+       }
 }
 
 void
-DummyAudioBackend::get_physical_inputs (DataType type, std::vector<std::string>&)
+DummyAudioBackend::get_physical_inputs (DataType type, std::vector<std::string>& port_names)
 {
+       for (size_t i = 0; i < _ports.size (); ++i) {
+               DummyPort* port = _ports[i];
+               if ((port->type () == type) && port->is_input () && port->is_physical ()) {
+                       port_names.push_back (port->name ());
+               }
+       }
 }
 
 ChanCount
 DummyAudioBackend::n_physical_outputs () const
 {
+       int n_midi = 0;
+       int n_audio = 0;
+       for (size_t i = 0; i < _ports.size (); ++i) {
+               DummyPort* port = _ports[i];
+               if (port->is_output () && port->is_physical ()) {
+                       switch (port->type ()) {
+                               case DataType::AUDIO: ++n_audio; break;
+                               case DataType::MIDI: ++n_midi; break;
+                               default: break;
+                       }
+               }
+       }
        ChanCount cc;
-       cc.set (DataType::AUDIO, 0);
-       cc.set (DataType::MIDI, 0);
+       cc.set (DataType::AUDIO, n_audio);
+       cc.set (DataType::MIDI, n_midi);
        return cc;
 }
 
 ChanCount
 DummyAudioBackend::n_physical_inputs () const
 {
+       int n_midi = 0;
+       int n_audio = 0;
+       for (size_t i = 0; i < _ports.size (); ++i) {
+               DummyPort* port = _ports[i];
+               if (port->is_input () && port->is_physical ()) {
+                       switch (port->type ()) {
+                               case DataType::AUDIO: ++n_audio; break;
+                               case DataType::MIDI: ++n_midi; break;
+                               default: break;
+                       }
+               }
+       }
        ChanCount cc;
-       cc.set (DataType::AUDIO, 0);
-       cc.set (DataType::MIDI, 0);
+       cc.set (DataType::AUDIO, n_audio);
+       cc.set (DataType::MIDI, n_midi);
        return cc;
 }
 
 /* Getting access to the data buffer for a port */
 
 void*
-DummyAudioBackend::get_buffer (PortEngine::PortHandle, pframes_t)
+DummyAudioBackend::get_buffer (PortEngine::PortHandle port, pframes_t nframes)
 {
+       assert (port);
+       assert (valid_port (port));
+       return static_cast<DummyPort*>(port)->get_buffer (nframes);
 }
 
 /* Engine Process */
@@ -681,6 +922,7 @@ DummyAudioBackend::main_process_thread ()
        return 0;
 }
 
+
 /******************************************************************************/
 
 static boost::shared_ptr<DummyAudioBackend> _instance;
@@ -726,3 +968,208 @@ extern "C" ARDOURBACKEND_API ARDOUR::AudioBackendInfo* descriptor ()
 {
        return &_descriptor;
 }
+
+
+/******************************************************************************/
+DummyPort::DummyPort (const std::string& name, PortFlags flags)
+       : _name  (name)
+       , _flags (flags)
+{
+       _capture_latency_range.min = 0;
+       _capture_latency_range.max = 0;
+       _playback_latency_range.min = 0;
+       _playback_latency_range.max = 0;
+}
+
+DummyPort::~DummyPort () {
+       disconnect_all ();
+}
+
+
+int DummyPort::connect (DummyPort *port)
+{
+       if (!port) {
+               PBD::error << _("DummyPort::connect (): invalid (null) port") << endmsg;
+               return -1;
+       }
+
+       if (type () != port->type ()) {
+               PBD::error << _("DummyPort::connect (): wrong port-type") << endmsg;
+               return -1;
+       }
+
+       if (is_output () && port->is_output ()) {
+               PBD::error << _("DummyPort::connect (): cannot inter-connect output ports.") << endmsg;
+               return -1;
+       }
+
+       if (is_input () && port->is_input ()) {
+               PBD::error << _("DummyPort::connect (): cannot inter-connect input ports.") << endmsg;
+               return -1;
+       }
+
+       if (this == port) {
+               PBD::error << _("DummyPort::connect (): cannot self-connect ports.") << endmsg;
+               return -1;
+       }
+
+       if (is_connected (port)) {
+               PBD::error << _("DummyPort::connect (): ports are already connected:")
+                       << " (" << name () << ") -> (" << port->name () << ")"
+                       << endmsg;
+               return -1;
+       }
+
+       _connect (port, true);
+       return 0;
+}
+
+
+void DummyPort::_connect (DummyPort *port, bool callback)
+{
+       _connections.push_back (port);
+       if (callback) {
+               port->_connect (this, false);
+       }
+}
+
+int DummyPort::disconnect (DummyPort *port)
+{
+       if (!port) {
+               PBD::error << _("DummyPort::disconnect (): invalid (null) port") << endmsg;
+               return -1;
+       }
+
+       if (!is_connected (port)) {
+               PBD::error << _("DummyPort::disconnect (): ports are not connected:")
+                       << " (" << name () << ") -> (" << port->name () << ")"
+                       << endmsg;
+               return -1;
+       }
+       _disconnect (port, true);
+       return 0;
+}
+
+void DummyPort::_disconnect (DummyPort *port, bool callback)
+{
+       std::vector<DummyPort*>::iterator it = std::find (_connections.begin (), _connections.end (), port);
+
+       assert (it != _connections.end ());
+
+       _connections.erase (it);
+
+       if (callback) {
+               port->_disconnect (this, false);
+       }
+}
+
+
+void DummyPort::disconnect_all ()
+{
+       while (!_connections.empty ()) {
+               _connections.back ()->_disconnect (this, false);
+               _connections.pop_back ();
+       }
+}
+
+bool
+DummyPort::is_connected (const DummyPort *port) const
+{
+       return std::find (_connections.begin (), _connections.end (), port) != _connections.end ();
+}
+
+bool DummyPort::is_physically_connected () const
+{
+       for (std::vector<DummyPort*>::const_iterator it = _connections.begin (); it != _connections.end (); ++it) {
+               if ((*it)->is_physical ()) {
+                       return true;
+               }
+       }
+       return false;
+}
+
+/******************************************************************************/
+
+DummyAudioPort::DummyAudioPort (const std::string& name, PortFlags flags)
+       : DummyPort (name, flags)
+{
+       memset (_buffer, 0, sizeof (_buffer));
+}
+
+DummyAudioPort::~DummyAudioPort () { }
+
+void* DummyAudioPort::get_buffer (pframes_t n_samples)
+{
+       if (is_input ()) {
+               std::vector<DummyPort*>::const_iterator it = get_connections ().begin ();
+               if (it == get_connections ().end ()) {
+                       memset (_buffer, 0, n_samples * sizeof (Sample));
+               } else {
+                       DummyAudioPort const * source = static_cast<const DummyAudioPort*>(*it);
+                       assert (source && source->is_output ());
+                       memcpy (_buffer, source->const_buffer (), n_samples * sizeof (Sample));
+                       while (++it != get_connections ().end ()) {
+                               source = static_cast<const DummyAudioPort*>(*it);
+                               assert (source && source->is_output ());
+                               Sample* dst = buffer ();
+                               const Sample* src = source->const_buffer ();
+                               for (uint32_t s = 0; s < n_samples; ++s, ++dst, ++src) {
+                                       *dst += *src;
+                               }
+                       }
+               }
+       }
+       return _buffer;
+}
+
+
+DummyMidiPort::DummyMidiPort (const std::string& name, PortFlags flags)
+       : DummyPort (name, flags)
+{
+       _buffer.clear ();
+}
+
+DummyMidiPort::~DummyMidiPort () { }
+
+void* DummyMidiPort::get_buffer (pframes_t /* nframes */)
+{
+       if (is_input ()) {
+               _buffer.clear ();
+               for (std::vector<DummyPort*>::const_iterator i = get_connections ().begin ();
+                               i != get_connections ().end ();
+                               ++i) {
+                       const DummyMidiBuffer src = static_cast<const DummyMidiPort*>(*i)->const_buffer ();
+                       for (DummyMidiBuffer::const_iterator it = src.begin (); it != src.end (); ++it) {
+                               _buffer.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (**it)));
+                       }
+               }
+               std::sort (_buffer.begin (), _buffer.end ());
+       }
+       return &_buffer;
+}
+
+DummyMidiEvent::DummyMidiEvent (const pframes_t timestamp, const uint8_t* data, size_t size)
+       : _size (size)
+       , _timestamp (timestamp)
+       , _data (0)
+{
+       if (size > 0) {
+               _data = (uint8_t*) malloc (size);
+        memcpy (_data, data, size);
+       }
+}
+
+DummyMidiEvent::DummyMidiEvent (const DummyMidiEvent& other)
+       : _size (other.size ())
+       , _timestamp (other.timestamp ())
+       , _data (0)
+{
+       if (other.size () && other.const_data ()) {
+               _data = (uint8_t*) malloc (other.size ());
+               memcpy (_data, other.const_data (), other.size ());
+       }
+};
+
+DummyMidiEvent::~DummyMidiEvent () {
+       free (_data);
+};
index ad6035dccabadf9653c6cd70a34b36bc9bf99978..b31d4f1658a058602d67d9f3d659e99d8c646e3f 100644 (file)
 #include <stdint.h>
 #include <pthread.h>
 
+#include <boost/shared_ptr.hpp>
+
 #include "ardour/types.h"
 #include "ardour/audio_backend.h"
 
 namespace ARDOUR {
 
+class DummyMidiEvent {
+       public:
+               DummyMidiEvent(const pframes_t timestamp, const uint8_t* data, size_t size);
+               DummyMidiEvent(const DummyMidiEvent& other);
+               ~DummyMidiEvent();
+               size_t size () const { return _size; };
+               pframes_t timestamp () const { return _timestamp; };
+               const unsigned char* const_data () const { return _data; };
+               unsigned char* data () { return _data; };
+               bool operator< (const DummyMidiEvent &other) const { return timestamp () < other.timestamp (); };
+       private:
+               size_t _size;
+               pframes_t _timestamp;
+               uint8_t *_data;
+};
+
+typedef std::vector<boost::shared_ptr<DummyMidiEvent> > DummyMidiBuffer;
+
+class DummyPort {
+       protected:
+               DummyPort (const std::string&, PortFlags);
+       public:
+               virtual ~DummyPort ();
+
+               const std::string& name () const { return _name; }
+               PortFlags flags () const { return _flags; }
+
+               int set_name (const std::string &name) { _name = name; return 0; }
+
+               virtual DataType type () const = 0;
+
+               bool is_input ()     const { return flags () & IsInput; }
+               bool is_output ()    const { return flags () & IsOutput; }
+               bool is_physical ()  const { return flags () & IsPhysical; }
+               bool is_terminal ()  const { return flags () & IsTerminal; }
+               bool is_connected () const { return _connections.size () != 0; }
+               bool is_connected (const DummyPort *port) const;
+               bool is_physically_connected () const;
+
+               const std::vector<DummyPort *>& get_connections () const { return _connections; }
+
+               int connect (DummyPort *port);
+               int disconnect (DummyPort *port);
+               void disconnect_all ();
+
+               virtual void* get_buffer (pframes_t nframes) = 0;
+
+               const LatencyRange& latency_range (bool for_playback) const
+               {
+                       return for_playback ? _playback_latency_range : _capture_latency_range;
+               }
+
+               void set_latency_range (const LatencyRange &latency_range, bool for_playback)
+               {
+                       if (for_playback)
+                       {
+                               _playback_latency_range = latency_range;
+                       }
+                       else
+                       {
+                               _capture_latency_range = latency_range;
+                       }
+               }
+
+       private:
+               std::string _name;
+               const PortFlags _flags;
+               LatencyRange _capture_latency_range;
+               LatencyRange _playback_latency_range;
+               std::vector<DummyPort*> _connections;
+
+               void _connect (DummyPort* , bool);
+               void _disconnect (DummyPort* , bool);
+
+}; // class DummyPort
+
+class DummyAudioPort : public DummyPort {
+       public:
+               DummyAudioPort (const std::string&, PortFlags);
+               ~DummyAudioPort ();
+
+               DataType type () const { return DataType::AUDIO; };
+
+               Sample* buffer () { return _buffer; }
+               const Sample* const_buffer () const { return _buffer; }
+               void* get_buffer (pframes_t nframes);
+
+       private:
+               Sample _buffer[8192];
+}; // class DummyAudioPort
+
+class DummyMidiPort : public DummyPort {
+       public:
+               DummyMidiPort (const std::string&, PortFlags);
+               ~DummyMidiPort ();
+
+               DataType type () const { return DataType::MIDI; };
+
+               void* get_buffer (pframes_t nframes);
+               const DummyMidiBuffer const_buffer () const { return _buffer; }
+
+       private:
+               DummyMidiBuffer _buffer;
+}; // class DummyMidiPort
+
 class DummyAudioBackend : public AudioBackend {
        public:
                DummyAudioBackend (AudioEngine& e);
@@ -170,6 +277,7 @@ class DummyAudioBackend : public AudioBackend {
                float  _samplerate;
                size_t _audio_buffersize;
                float  _dsp_load;
+               static size_t _max_buffer_size;
 
                uint32_t _n_inputs;
                uint32_t _n_outputs;
@@ -193,6 +301,22 @@ class DummyAudioBackend : public AudioBackend {
                        ThreadData (DummyAudioBackend* e, boost::function<void ()> fp, size_t stacksz)
                                : engine (e) , f (fp) , stacksize (stacksz) {}
                };
+
+               /* port engine */
+               std::vector<DummyPort *> _ports;
+
+               bool valid_port (PortHandle port) const {
+                       return std::find (_ports.begin (), _ports.end (), (DummyPort*)port) != _ports.end ();
+               }
+               DummyPort * find_port(const std::string& port_name) const {
+                       for (std::vector<DummyPort*>::const_iterator it = _ports.begin (); it != _ports.end (); ++it) {
+                               if ((*it)->name () == port_name) {
+                                       return *it;
+                               }
+                       }
+                       return NULL;
+               }
+
 }; // class DummyAudioBackend
 
 } // namespace