Fix various MIDI corruption bugs.
[ardour.git] / libs / midi++2 / jack_midiport.cc
index 311bbb98f634a9ccf41dbac91c64e7bf0bb5a112..0b49517212e00a55fb91a8e0a22b18221de14ecd 100644 (file)
@@ -95,24 +95,27 @@ JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
                                
        } else {
 
-               assert(_currently_in_cycle);
                assert(_jack_output_port);
                assert(timestamp < _nframes_this_cycle);
 
-               if (timestamp == 0) {
-                       timestamp = _last_write_timestamp;
-               } 
-
-               if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle), 
-                                          timestamp, msg, msglen) == 0) {
-                       ret = msglen;
-                       _last_write_timestamp = timestamp;
-
+               if (_currently_in_cycle) {
+                       if (timestamp == 0) {
+                               timestamp = _last_write_timestamp;
+                       } 
+
+                       if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle), 
+                                               timestamp, msg, msglen) == 0) {
+                               ret = msglen;
+                               _last_write_timestamp = timestamp;
+
+                       } else {
+                               ret = 0;
+                               cerr << "write of " << msglen << " failed, port holds "
+                                       << jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle))
+                                       << endl;
+                       }
                } else {
-                       ret = 0;
-                       cerr << "write of " << msglen << " failed, port holds "
-                            << jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle))
-                            << endl;
+                       cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
                }
        }