[英]Pygame.midi: How to detect simultaneous inputs from a synthesizer?
I'm using a synthesizer, to detect input of notes with pygame.midi我正在使用合成器来检测带有 pygame.midi 的音符输入
import pygame.midi
def readInput(input_device):
while True:
if input_device.poll():
event = input_device.read(1)[0]
data = event[0]
timestamp = event[1]
note_number = data[1]
velocity = data[2]
print(data[2])
if data[1] == 36 and 40 and 43 and 46: #not working
print("chord = Cmaj7")
else:
print(data[2])
if __name__ == '__main__':
pygame.midi.init()
my_input = pygame.midi.Input(1)
readInput(my_input)
notes are working, but how i can detect the simultaneous inputs 36 + 40 + 43+ 46, knowing the input 36=c, 40=e and 43=g anyway to do this?笔记正在工作,但我如何检测同时输入 36 + 40 + 43+ 46,知道输入 36=c、40=e 和 43=g 无论如何要做到这一点?
Okay, you need to understand how midi works.好的,您需要了解 midi 的工作原理。 MIDI event is triggered(input_device.poll() is True) when there is a change of state of any synth keys, eg key was pressed or released.当任何合成器键的 state 发生变化时触发 MIDI 事件(input_device.poll() 为 True),例如按下或释放键。 When this happens, your data variable contains list with [state, note, velocity, something(I couldn't identify it)].发生这种情况时,您的数据变量包含带有 [状态、注释、速度、某些东西(我无法识别)] 的列表。 Also, there are 15 channels.此外,还有 15 个频道。 I found out that key press calls state 128+channel_number and key release calls event with state 144+channel_number.我发现按键调用 state 128+channel_number 和按键释放调用事件 state 144+channel_number。 You have to keep track of actually pressed notes by yourself.您必须自己跟踪实际按下的音符。 Here's sample code for what you're trying to do:这是您要执行的操作的示例代码:
pressed = []
def readInput(input_device, channel):
while True:
if input_device.poll():
event = input_device.read(1)[0]
data = event[0]
# [state, note, velocity, something(always 0 for me)]
timestamp = event[1]
if data[0] == 128 + channel: # note off on channel data[0]-128
if data[1] in pressed:
pressed.remove(data[1])
if data[0] == 144 + channel: # note on on channel data[0]-144
if not data[1] in pressed:
pressed.append(data[1])
if all(el in pressed for el in [36, 40, 43, 46]):
print("chord = Cmaj7")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.