Fix crash on out of range MIDI events (though this shouldn't be possible at all....
[ardour.git] / libs / ardour / midi_port.cc
index 8fb66255d81350b54fdcf614edfb287cd142f725..fc48218efad747c4ad073f0202373c7d923201c5 100644 (file)
 using namespace ARDOUR;
 using namespace std;
 
-MidiPort::MidiPort (const std::string& name, Flags flags, bool publish, nframes_t bufsize)
+MidiPort::MidiPort (const std::string& name, Flags flags, bool external, nframes_t capacity)
        : Port (name, flags)
        , BaseMidiPort (name, flags)
        , PortFacade (name, flags)
 {
-       set_name (name);
+       _buffer = new MidiBuffer (capacity);
 
-       _buffer = new MidiBuffer (bufsize);
-
-       if (!publish) {
-               _ext_port = 0;
-       } else {
+       if (external) {
+               /* external ports use the same buffer for the jack port (_ext_port)
+                * and internal ports (this) */
                _ext_port = new JackMidiPort (name, flags, _buffer);
+               Port::set_name (_ext_port->name());
+       } else {
+               /* internal ports just have a single buffer, no jack port */
+               _ext_port = 0;
+               set_name (name);
        }
 
        reset ();
@@ -65,16 +68,18 @@ MidiPort::reset()
 void
 MidiPort::cycle_start (nframes_t nframes, nframes_t offset)
 {
-       /* caller must hold process lock */
-       
        if (_ext_port) {
                _ext_port->cycle_start (nframes, offset);
        }
-
+       
        if (_flags & IsInput) {
-
+                       
                if (_ext_port) {
-                       _buffer->read_from (dynamic_cast<BaseMidiPort*>(_ext_port)->get_midi_buffer(), nframes, offset);
+               
+                       BaseMidiPort* mprt = dynamic_cast<BaseMidiPort*>(_ext_port);
+                       assert(mprt);
+                       assert(&mprt->get_midi_buffer() == _buffer);
+
                        if (!_connections.empty()) {
                                (*_mixdown) (_connections, _buffer, nframes, offset, false);
                        }
@@ -89,7 +94,17 @@ MidiPort::cycle_start (nframes_t nframes, nframes_t offset)
                }
 
        } else {
-               
+                       
                _buffer->silence (nframes, offset);
        }
 }
+
+       
+void
+MidiPort::cycle_end (nframes_t nframes, nframes_t offset)
+{
+       if (_ext_port) {
+               _ext_port->cycle_end (nframes, offset);
+       }
+}
+