User avatar
Grumpy Mike
Posts: 914
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

MIDI - occasional quit without error message

Fri Dec 07, 2018 4:53 pm

I am simply reading the MIDI messages from a Korg nano keyboard. It works fine, but then after some random number of key presses the program simply quits with no error message. This is a example of the problem, striped down to the minimum.

Code: Select all

# #!/usr/bin/env python3
# MIDI crash minimum example

import rtmidi

midiIn = rtmidi.MidiIn()

def main(): # program will quit occasionally without an error message
   initMIDI()
   rx = 0
   while 1:
      try:
         m = midiIn.get_message()
      except:
         m = None
      if m != None :
          command, note, vel = m[0]
          rx +=1
          print(rx," - ",hex(command), hex(note), hex(vel))
     
def initMIDI(): # open first MIDI port seen
   available_ports = midiIn.get_ports()
   print("MIDI ports available:-")
   for i in range(0,len(available_ports)):
      print(i,available_ports[i])  
   if available_ports:
       midiIn.open_port(1)
   else:
       midiIn.open_virtual_port("My virtual output")    

# Main program logic:
if __name__ == '__main__':    
    main()
This can happen from the first press to something over a thousand presses. I have tried the error callback function of rtmidi and it simply quits without printing anything from the call back. I also tried without the try ... except without any results.

When I run the code again after a quit then sometimes there seems to be two MIDI messages in the buffer that pop out immediately, that is without pressing a key.

Is it me doing something wrong or is rtmidi a bit flakey? It was installed today so it should be up to date.

Return to “Python”