I'm trying to write code on Python using the MIDO library to compare MIDI data from a .mid file with incoming MIDI data from notes played on my MIDI keyboard to generate a list of wrong notes played. I'm easily able to print a list of all note pitches from the .mid file but having some trouble doing the same from my keyboard.
I've attached the code I've written for the latter task. It works until I enter the name of my MIDI port - it accepts it and simply doesn't print anything no matter what notes I play. It also doesn't print 'Exiting' in a new line after I press Ctrl + C. I'm not sure what the error is here and I'm pretty new to the world of coding. Any ideas on what may be the issue?
def collect_note_events(port):
note_events = []
for msg in port:
if msg.type == 'note_on':
note_events.append([msg.note])
return note_events
print("Available MIDI ports: ")
for i, port_name in enumerate(mido.get_input_names()):
print(f"{i}: {port_name}")
selected_port = input("Enter the name of the MIDI port to use: ")
with mido.open_input(selected_port) as port:
print("Listening for MIDI input. Press Ctrl+C to exit.")
try:
while True:
note_events = collect_note_events(port)
if note_events:
print(note_events)
time.sleep(0.1)
except KeyboardInterrupt:
print("\nExiting...")