add Track::use_default_new_playlist()
[ardour.git] / libs / ardour / midi_port.cc
index c490fb95709286bbe064e3d71e15ed18adfdf751..7a87c4c273f8e91dd16de8ddbbef1a53119f9feb 100644 (file)
@@ -49,7 +49,8 @@ MidiPort::MidiPort (const std::string& name, PortFlags flags)
 MidiPort::~MidiPort()
 {
        if (_shadow_port) {
-               _shadow_port->disconnect_all ();
+               AudioEngine::instance()->unregister_port (_shadow_port);
+               _shadow_port.reset ();
        }
 
        delete _buffer;
@@ -64,7 +65,7 @@ MidiPort::cycle_start (pframes_t nframes)
 
        _buffer->clear ();
 
-       if (sends_output ()) {
+       if (sends_output () && _port_handle) {
                port_engine.midi_clear (port_engine.get_buffer (_port_handle, nframes));
        }
 
@@ -128,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);
                                }
                        }
 
@@ -229,7 +235,7 @@ 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) {