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

空扰寡人 提交于 2020-06-01 04:43:46

问题


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:


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)


来源:https://stackoverflow.com/questions/60182510/mido-how-to-get-midi-data-in-realtime-from-different-ports

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!