簡體   English   中英

Pygame.midi:如何檢測來自合成器的同時輸入?

[英]Pygame.midi: How to detect simultaneous inputs from a synthesizer?

我正在使用合成器來檢測帶有 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)

筆記正在工作,但我如何檢測同時輸入 36 + 40 + 43+ 46,知道輸入 36=c、40=e 和 43=g 無論如何要做到這一點?

好的,您需要了解 midi 的工作原理。 當任何合成器鍵的 state 發生變化時觸發 MIDI 事件(input_device.poll() 為 True),例如按下或釋放鍵。 發生這種情況時,您的數據變量包含帶有 [狀態、注釋、速度、某些東西(我無法識別)] 的列表。 此外,還有 15 個頻道。 我發現按鍵調用 state 128+channel_number 和按鍵釋放調用事件 state 144+channel_number。 您必須自己跟蹤實際按下的音符。 這是您要執行的操作的示例代碼:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM