我试图使用Python向合成器发送基本的MIDI消息。
我知道PC合成器链接是可用的,因为Rosegarden应用程序可以配置为在设备上播放MIDI文件,当MIDI输出设置为'DigitalKBD 20:0‘端口时。
我已经发现了这个Python (米多)并安装了它。好消息是外部MIDI设备已被识别并可在端口列表中使用。不幸的是,简单的便笺测试不会在设备上触发任何声音。下面是我尝试过的代码:
使用PortMidi (这是MIDO的默认设置):
>>> import mido
>>> output = mido.open_output('DigitalKBD MIDI 1')
>>> output.send(mido.Message('note_on', note=60, velocity=64))使用RtMidi:
>>> import mido
>>> rtmidi = mido.Backend('mido.backends.rtmidi')
>>> output = rtmidi.open_output('DigitalKBD 20:0')
>>> output.send(mido.Message('note_on', note=60, velocity=64))在这两种情况下,都没有来自合成器的声音。
请告诉我如何修正代码(或设置),使仪器能够正确接收和解释消息吗?
发布于 2015-04-07 21:16:36
好吧,好吧,我让MIDI工作了,通过创建一个小脚本来回响键盘上播放的任何内容,并且有一定的延迟:
import mido
import time
from collections import deque
print mido.get_output_names() # To list the output ports
print mido.get_input_names() # To list the input ports
inport = mido.open_input('DigitalKBD MIDI 1')
outport = mido.open_output('DigitalKBD MIDI 1')
msglog = deque()
echo_delay = 2
while True:
while inport.pending():
msg = inport.receive()
if msg.type != "clock":
print msg
msglog.append({"msg": msg, "due": time.time() + echo_delay})
while len(msglog) > 0 and msglog[0]["due"] <= time.time():
outport.send(msglog.popleft()["msg"])这个脚本运行得很好,所以我有机会仔细地走回来,看看为什么我的最初测试失败了。结果,要接收输出消息,还必须打开输入端口。不知道原因,但这是最简单的代码,工作:
import mido
inport = mido.open_input('DigitalKBD MIDI 1')
outport = mido.open_output('DigitalKBD MIDI 1')
outport.send(mido.Message('note_on', note=72))更重要的是,如果python在运行上述代码后立即退出,则可能会发生MIDO未能发送消息,因此不会播放声音。给它一些时间来结束。
发布于 2019-12-14 21:39:55
您需要在将消息发送到输出后立即为您的调用添加一个睡眠。
我在1s的消息之后加了一个简单的睡眠,音调播放得很好。
https://stackoverflow.com/questions/27720647
复制相似问题