Ongoing work on latency compensation
[ardour.git] / libs / ardour / midi_port.cc
index 02a86bfb8f87b6822465aa32f2eda784bd9fc8bf..a728ce2a44acc1933cc35362d80ebd63d3e79bc8 100644 (file)
@@ -48,19 +48,24 @@ MidiPort::MidiPort (const std::string& name, PortFlags flags)
 
 MidiPort::~MidiPort()
 {
+       if (_shadow_port) {
+               AudioEngine::instance()->unregister_port (_shadow_port);
+               _shadow_port.reset ();
+       }
+
        delete _buffer;
 }
 
 void
 MidiPort::cycle_start (pframes_t nframes)
 {
-       framepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
+       samplepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
 
        Port::cycle_start (nframes);
 
        _buffer->clear ();
 
-       if (sends_output ()) {
+       if (sends_output () && _port_handle) {
                port_engine.midi_clear (port_engine.get_buffer (_port_handle, nframes));
        }
 
@@ -124,28 +129,33 @@ MidiPort::get_midi_buffer (pframes_t nframes)
 
                                pframes_t timestamp;
                                size_t size;
-                               uint8_t* buf;
+                               uint8_t const* buf;
 
                                port_engine.midi_event_get (timestamp, size, &buf, buffer, i);
 
                                if (buf[0] == 0xfe) {
                                        /* throw away active sensing */
                                        continue;
-                               } else if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) {
-                                       /* normalize note on with velocity 0 to proper note off */
-                                       buf[0] = 0x80 | (buf[0] & 0x0F);  /* note off */
-                                       buf[2] = 0x40;  /* default velocity */
                                }
 
                                /* check that the event is in the acceptable time range */
+                               if ((timestamp <  (_global_port_buffer_offset + _port_buffer_offset)) ||
+                                   (timestamp >= (_global_port_buffer_offset + _port_buffer_offset + nframes))) {
+                                       cerr << "Dropping incoming MIDI at time " << timestamp << "; offset="
+                                               << _global_port_buffer_offset << " limit="
+                                               << (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n";
+                                       continue;
+                               }
 
-                               if ((timestamp >= (_global_port_buffer_offset + _port_buffer_offset)) &&
-                                   (timestamp < (_global_port_buffer_offset + _port_buffer_offset + nframes))) {
-                                       _buffer->push_back (timestamp, size, buf);
+                               if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) {
+                                       /* normalize note on with velocity 0 to proper note off */
+                                       uint8_t ev[3];
+                                       ev[0] = 0x80 | (buf[0] & 0x0F);  /* note off */
+                                       ev[1] = buf[1];
+                                       ev[2] = 0x40;  /* default velocity */
+                                       _buffer->push_back (timestamp, size, ev);
                                } else {
-                                       cerr << "Dropping incoming MIDI at time " << timestamp << "; offset="
-                                            << _global_port_buffer_offset << " limit="
-                                            << (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n";
+                                       _buffer->push_back (timestamp, size, buf);
                                }
                        }
 
@@ -225,12 +235,12 @@ MidiPort::flush_buffers (pframes_t nframes)
 
                for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
 
-                       const Evoral::MIDIEvent<MidiBuffer::TimeType> ev (*i, false);
+                       const Evoral::Event<MidiBuffer::TimeType> ev (*i, false);
 
 
                        if (sends_output() && _trace_on) {
                                uint8_t const * const buf = ev.buffer();
-                               const framepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
+                               const samplepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
 
                                _self_parser.set_timestamp (now + ev.time());
 
@@ -242,12 +252,12 @@ MidiPort::flush_buffers (pframes_t nframes)
                        }
 
 
-                       // event times are in frames, relative to cycle start
+                       // event times are in samples, relative to cycle start
 
 #ifndef NDEBUG
                        if (DEBUG_ENABLED (DEBUG::MidiIO)) {
                                const Session* s = AudioEngine::instance()->session();
-                               const framepos_t now = (s ? s->transport_frame() : 0);
+                               const samplepos_t now = (s ? s->transport_sample() : 0);
                                DEBUG_STR_DECL(a);
                                DEBUG_STR_APPEND(a, string_compose ("MidiPort %8 %1 pop event    @ %2 (global %4, within %5 gpbo %6 pbo %7 sz %3 ", _buffer, ev.time(), ev.size(),
                                                                    now + ev.time(), nframes, _global_port_buffer_offset, _port_buffer_offset, name()));