initial (incomplete) framework for DiskIOPoint manipulation
[ardour.git] / libs / ardour / async_midi_port.cc
index 3dfb5610d9958153363fb219b0b392d5f88737f7..f7147676b0b5286775e92546af9982be42e8d065 100644 (file)
@@ -46,6 +46,7 @@ AsyncMIDIPort::AsyncMIDIPort (string const & name, PortFlags flags)
        , MIDI::Port (name, MIDI::Port::Flags (0))
        , _currently_in_cycle (false)
        , _last_write_timestamp (0)
+       , _flush_at_cycle_start (false)
        , have_timer (false)
        , output_fifo (2048)
        , input_fifo (1024)
@@ -119,6 +120,9 @@ AsyncMIDIPort::cycle_start (MIDI::pframes_t nframes)
 
        if (ARDOUR::Port::sends_output()) {
                flush_output_fifo (nframes);
+               if (_flush_at_cycle_start) {
+                       flush_buffers (nframes);
+               }
        }
 
        /* copy incoming data from the port buffer into the input FIFO
@@ -139,19 +143,20 @@ AsyncMIDIPort::cycle_start (MIDI::pframes_t nframes)
                        if (!have_timer) {
                                when += (*b).time();
                        }
-                       input_fifo.write (when, (Evoral::EventType) 0, (*b).size(), (*b).buffer());
+                       input_fifo.write (when, Evoral::NO_EVENT, (*b).size(), (*b).buffer());
                }
 
                if (!mb.empty()) {
                        _xthread.wakeup ();
                }
+
        }
 }
 
 void
 AsyncMIDIPort::cycle_end (MIDI::pframes_t nframes)
 {
-       if (ARDOUR::Port::sends_output()) {
+       if (ARDOUR::Port::sends_output() && !_flush_at_cycle_start) {
                /* move any additional data from output FIFO into the port
                   buffer.
                */
@@ -168,7 +173,7 @@ AsyncMIDIPort::cycle_end (MIDI::pframes_t nframes)
  * Cannot be called from a processing thread.
  */
 void
-AsyncMIDIPort::drain (int check_interval_usecs)
+AsyncMIDIPort::drain (int check_interval_usecs, int total_usecs_to_wait)
 {
        RingBuffer< Evoral::Event<double> >::rw_vector vec = { { 0, 0 }, { 0, 0} };
 
@@ -183,12 +188,16 @@ AsyncMIDIPort::drain (int check_interval_usecs)
                return;
        }
 
-       while (1) {
+       microseconds_t now = get_microseconds ();
+       microseconds_t end = now + total_usecs_to_wait;
+
+       while (now < end) {
                output_fifo.get_write_vector (&vec);
                if (vec.len[0] + vec.len[1] >= output_fifo.bufsize() - 1) {
                        break;
                }
                Glib::usleep (check_interval_usecs);
+               now = get_microseconds();
        }
 }
 
@@ -232,14 +241,14 @@ AsyncMIDIPort::write (const MIDI::byte * msg, size_t msglen, MIDI::timestamp_t t
                           necessary.
                        */
                        if (!vec.buf[0]->owns_buffer()) {
-                                vec.buf[0]->set_buffer (0, 0, true);
-                        }
+                               vec.buf[0]->set_buffer (0, 0, true);
+                       }
                        vec.buf[0]->set (msg, msglen, timestamp);
                } else {
                        /* see comment in previous branch of if() statement */
                        if (!vec.buf[1]->owns_buffer()) {
-                                vec.buf[1]->set_buffer (0, 0, true);
-                        }
+                               vec.buf[1]->set_buffer (0, 0, true);
+                       }
                        vec.buf[1]->set (msg, msglen, timestamp);
                }