use C++ for PortManager::make_port_name_relative()
[ardour.git] / libs / ardour / async_midi_port.cc
index 22b5438fe084cf11de01543c9181b133166a5e72..6c5625e716e7f623631a12e7d1f1bd42e69e23b3 100644 (file)
@@ -1,6 +1,6 @@
 /*
     Copyright (C) 1998 Paul Barton-Davis
-    
+
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
@@ -19,6 +19,9 @@
 */
 
 #include <iostream>
+#include <vector>
+
+#include <glibmm/timer.h>
 
 #include "pbd/error.h"
 #include "pbd/stacktrace.h"
@@ -34,10 +37,6 @@ using namespace ARDOUR;
 using namespace std;
 using namespace PBD;
 
-namespace Evoral {
-       template class EventRingBuffer<timestamp_t>;
-}
-
 pthread_t AsyncMIDIPort::_process_thread;
 
 #define port_engine AudioEngine::instance()->port_engine()
@@ -47,9 +46,10 @@ AsyncMIDIPort::AsyncMIDIPort (string const & name, PortFlags flags)
        , MIDI::Port (name, MIDI::Port::Flags (0))
        , _currently_in_cycle (false)
        , _last_write_timestamp (0)
+       , have_timer (false)
        , output_fifo (512)
        , input_fifo (1024)
-       , xthread (true)
+       , _xthread (true)
 {
 }
 
@@ -58,7 +58,14 @@ AsyncMIDIPort::~AsyncMIDIPort ()
 }
 
 void
-AsyncMIDIPort::flush_output_fifo (pframes_t nframes)
+AsyncMIDIPort::set_timer (boost::function<MIDI::framecnt_t (void)>& f)
+{
+       timer = f;
+       have_timer = true;
+}
+
+void
+AsyncMIDIPort::flush_output_fifo (MIDI::pframes_t nframes)
 {
        RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0 } };
        size_t written;
@@ -66,15 +73,15 @@ AsyncMIDIPort::flush_output_fifo (pframes_t nframes)
        output_fifo.get_read_vector (&vec);
 
        MidiBuffer& mb (get_midi_buffer (nframes));
-                       
+
        if (vec.len[0]) {
                Evoral::Event<double>* evp = vec.buf[0];
-               
+
                for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
                        mb.push_back (evp->time(), evp->size(), evp->buffer());
                }
        }
-       
+
        if (vec.len[1]) {
                Evoral::Event<double>* evp = vec.buf[1];
 
@@ -82,14 +89,14 @@ AsyncMIDIPort::flush_output_fifo (pframes_t nframes)
                        mb.push_back (evp->time(), evp->size(), evp->buffer());
                }
        }
-       
+
        if ((written = vec.len[0] + vec.len[1]) != 0) {
                output_fifo.increment_read_idx (written);
        }
 }
 
 void
-AsyncMIDIPort::cycle_start (pframes_t nframes)
+AsyncMIDIPort::cycle_start (MIDI::pframes_t nframes)
 {
        _currently_in_cycle = true;
        MidiPort::cycle_start (nframes);
@@ -100,29 +107,37 @@ AsyncMIDIPort::cycle_start (pframes_t nframes)
 
        if (ARDOUR::Port::sends_output()) {
                flush_output_fifo (nframes);
-       } 
-       
+       }
+
        /* copy incoming data from the port buffer into the input FIFO
           and if necessary wakeup the reader
        */
 
        if (ARDOUR::Port::receives_input()) {
                MidiBuffer& mb (get_midi_buffer (nframes));
-               pframes_t when = AudioEngine::instance()->sample_time_at_cycle_start();
+               framecnt_t when;
+
+               if (have_timer) {
+                       when = timer ();
+               } else {
+                       when = AudioEngine::instance()->sample_time_at_cycle_start();
+               }
 
                for (MidiBuffer::iterator b = mb.begin(); b != mb.end(); ++b) {
+                       if (!have_timer) {
+                               when += (*b).time();
+                       }
                        input_fifo.write (when, (Evoral::EventType) 0, (*b).size(), (*b).buffer());
                }
-               
+
                if (!mb.empty()) {
-                       xthread.wakeup ();
+                       _xthread.wakeup ();
                }
        }
-
 }
 
 void
-AsyncMIDIPort::cycle_end (pframes_t nframes)
+AsyncMIDIPort::cycle_end (MIDI::pframes_t nframes)
 {
        if (ARDOUR::Port::sends_output()) {
                /* move any additional data from output FIFO into the port
@@ -145,6 +160,12 @@ AsyncMIDIPort::drain (int check_interval_usecs)
 {
        RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
 
+       if (!AudioEngine::instance()->running() || AudioEngine::instance()->session() == 0) {
+               /* no more process calls - it will never drain */
+               return;
+       }
+
+
        if (is_process_thread()) {
                error << "Process thread called MIDI::AsyncMIDIPort::drain() - this cannot work" << endmsg;
                return;
@@ -155,12 +176,12 @@ AsyncMIDIPort::drain (int check_interval_usecs)
                if (vec.len[0] + vec.len[1] >= output_fifo.bufsize() - 1) {
                        break;
                }
-               usleep (check_interval_usecs);
+               Glib::usleep (check_interval_usecs);
        }
 }
 
 int
-AsyncMIDIPort::write (const byte * msg, size_t msglen, timestamp_t timestamp)
+AsyncMIDIPort::write (const MIDI::byte * msg, size_t msglen, MIDI::timestamp_t timestamp)
 {
        int ret = 0;
 
@@ -173,7 +194,7 @@ AsyncMIDIPort::write (const byte * msg, size_t msglen, timestamp_t timestamp)
                /* this is the best estimate of "when" this MIDI data is being
                 * delivered
                 */
-               
+
                _parser->set_timestamp (AudioEngine::instance()->sample_time() + timestamp);
                for (size_t n = 0; n < msglen; ++n) {
                        _parser->scanner (msg[n]);
@@ -181,7 +202,7 @@ AsyncMIDIPort::write (const byte * msg, size_t msglen, timestamp_t timestamp)
 
                Glib::Threads::Mutex::Lock lm (output_fifo_lock);
                RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
-               
+
                output_fifo.get_write_vector (&vec);
 
                if (vec.len[0] + vec.len[1] < 1) {
@@ -202,7 +223,7 @@ AsyncMIDIPort::write (const byte * msg, size_t msglen, timestamp_t timestamp)
                }
 
                output_fifo.increment_write_idx (1);
-               
+
                ret = msglen;
 
        } else {
@@ -213,7 +234,7 @@ AsyncMIDIPort::write (const byte * msg, size_t msglen, timestamp_t timestamp)
                }
 
                if (timestamp >= _cycle_nframes) {
-                       std::cerr << "attempting to write MIDI event of " << msglen << " bytes at time "
+                       std::cerr << "attempting to write MIDI event of " << msglen << " MIDI::bytes at time "
                                  << timestamp << " of " << _cycle_nframes
                                  << " (this will not work - needs a code fix)"
                                  << std::endl;
@@ -228,11 +249,11 @@ AsyncMIDIPort::write (const byte * msg, size_t msglen, timestamp_t timestamp)
                if (_currently_in_cycle) {
 
                        MidiBuffer& mb (get_midi_buffer (_cycle_nframes));
-                       
+
                        if (timestamp == 0) {
                                timestamp = _last_write_timestamp;
-                       } 
-                       
+                       }
+
                        if (mb.push_back (timestamp, msglen, msg)) {
                                ret = msglen;
                                _last_write_timestamp = timestamp;
@@ -258,13 +279,13 @@ AsyncMIDIPort::read (MIDI::byte *, size_t)
        if (!ARDOUR::Port::receives_input()) {
                return 0;
        }
-       
+
        timestamp_t time;
        Evoral::EventType type;
        uint32_t size;
-       byte buffer[input_fifo.capacity()];
+       vector<MIDI::byte> buffer(input_fifo.capacity());
 
-       while (input_fifo.read (&time, &type, &size, buffer)) {
+       while (input_fifo.read (&time, &type, &size, &buffer[0])) {
                _parser->set_timestamp (time);
                for (uint32_t i = 0; i < size; ++i) {
                        _parser->scanner (buffer[i]);
@@ -275,7 +296,7 @@ AsyncMIDIPort::read (MIDI::byte *, size_t)
 }
 
 void
-AsyncMIDIPort::parse (framecnt_t)
+AsyncMIDIPort::parse (MIDI::framecnt_t)
 {
        MIDI::byte buf[1];