Search code examples
pythonmido

Mido - How to get midi data in realtime from different ports


I have created 2 ports as input, to capture data from a keyboard and a midi surface controller (which has a bunch of sliders and knobs). Although I am not sure how to get data from both

for msg1 in input_hw:
    if not msg1.type == "clock":
        print(msg1)
    # Play the note if the note has been triggered
    if msg1.type == 'note_on' or msg1.type == 'note_off' and msg1.velocity > 0:
        out.send(msg1)

for msg in input_hw2:
    #avoid to print the clock message
    if not msg.type == "clock":
        print(msg)

The first For loop works, I get the midi note on and off when playing the keyboard, which is tied to the input_hw port, but the second loop never goes through.


Solution

  • Found a solution; you need to wrap the for loops in a while loop, adn use the iter_pending() function, which does allow mido to continue and not getting stuck waiting on the first loop.

    Probably there is a more elegant solution, but this is what I was able to find

    while True:
        for msg1 in input_hw.iter_pending():
            if not msg1.type == "clock":
                print(msg1)
            # Play the note if the note has been triggered
            if msg1.type == 'note_on' or msg1.type == 'note_off' and msg1.velocity > 0:
                out.send(msg1)
    
        for msg in input_hw2.iter_pending():
            #avoid to print the clock message
            if not msg.type == "clock":
                print(msg)