Lettura del flusso MIDI di uno strumento

# mido_test.py
#
# Questo programma dimostra l'utilizzo della libreria MIDO 
# per la gestione dei dispositivi MIDI.
# In particolare, questo programma cattura e stampa il flusso midi
# prodotto da una tastiera elettronica.
# Dopo aver selezionato il dispositivo tra quelli disponibili,
# viene effettuato un ciclo di lettura continua sino all'interruzione
# tramite tasto CTRL-C.
#
# by Andrea Bianchini (2021)
#


import mido

o=mido.get_output_names()

for i in range(len(o)):
    print(str(i)+"-"+o[i])

print()
k=int(input("Seleziona il numero corrispondente al dispositivo di interesse:"))
print()

port = mido.open_input(o[k])

try:
    while True:
        for msg in port.iter_pending():
            if  msg.type!="clock":
                print(msg)
except KeyboardInterrupt:
    print("Interrupted by user")

port.close()

Esempio:

0-Midi Through:Midi Through Port-0 14:0
1-Digital Keyboard:Digital Keyboard MIDI 1 20:0
2-Midi Through:Midi Through Port-0 14:0
3-Digital Keyboard:Digital Keyboard MIDI 1 20:0

Seleziona il numero corrispondente al dispositivo di interesse:1

start time=0
control_change channel=3 control=120 value=0 time=0
control_change channel=7 control=120 value=0 time=0
control_change channel=5 control=120 value=0 time=0
control_change channel=3 control=7 value=122 time=0
note_on channel=3 note=67 velocity=40 time=0
note_on channel=3 note=48 velocity=39 time=0
note_on channel=3 note=69 velocity=48 time=0
note_off channel=3 note=67 velocity=0 time=0
note_on channel=3 note=59 velocity=45 time=0
note_off channel=3 note=59 velocity=0 time=0
note_on channel=3 note=55 velocity=42 time=0
note_off channel=3 note=69 velocity=0 time=0
note_off channel=3 note=48 velocity=0 time=0
note_on channel=3 note=67 velocity=33 time=0
note_off channel=3 note=67 velocity=0 time=0
note_off channel=3 note=55 velocity=0 time=0
note_on channel=3 note=65 velocity=34 time=0
note_on channel=3 note=53 velocity=39 time=0
note_on channel=3 note=63 velocity=35 time=0
note_off channel=3 note=65 velocity=0 time=0
note_on channel=3 note=52 velocity=50 time=0
note_off channel=3 note=53 velocity=0 time=0
note_off channel=3 note=63 velocity=0 time=0
note_on channel=3 note=62 velocity=42 time=0
note_off channel=3 note=52 velocity=0 time=0
note_off channel=3 note=62 velocity=0 time=0
note_on channel=3 note=53 velocity=41 time=0
note_on channel=3 note=64 velocity=49 time=0