【问题标题】:Mido - How to get midi data in realtime from different portsMido - 如何从不同端口实时获取 midi 数据
【发布时间】:2020-02-12 06:40:55
【问题描述】:

我创建了 2 个端口作为输入,用于从键盘和 midi 表面控制器(具有一堆滑块和旋钮)捕获数据。虽然我不确定如何从两者中获取数据

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)

第一个 For 循环有效,我在弹奏键盘时打开和关闭了 midi 音符,它与input_hw 端口绑定,但第二个循环从未通过。

【问题讨论】:

    标签: python mido


    【解决方案1】:

    找到解决方案;您需要将 for 循环包装在 while 循环中,并使用 iter_pending() 函数,该函数确实允许 mido 继续并且不会在第一个循环上等待。

    可能有更优雅的解决方案,但这是我能够找到的

    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)
    

    【讨论】:

    • 你能告诉我你是如何获得midi事件的吗?更准确地说,您是如何从外部仪器获得msg1 对象的?
    • 你需要先创建一个端口对象,它会获取通过midi端口发送的消息。首先您调用mido.get_input_names(),这样您就可以看到活动输入和mido.get_output_names() 的输出。然后使用msg1=mido.open_input('DEVICENAME') 创建对象并使用mido.open_output('DEVICEOUT') 创建输出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 2019-12-22
    • 1970-01-01
    相关资源
    最近更新 更多