3

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.

1 Answers1

4

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)
  • 1
    Can you show me how you got the midi events in the first place? More precisely, how did you get the `msg1` object from your external instrument? – bobsmith76 May 15 '20 at 08:44
  • 1
    You need to create a port object first, which will get the messages sent via midi port. First you call `mido.get_input_names()` so you can see the active input and `mido.get_output_names()` for the output. Then you create the object with `msg1=mido.open_input('DEVICENAME')` and an out with `mido.open_output('DEVICEOUT')` –  May 16 '20 at 17:50