首页 > 解决方案 > 如何使用 pygame.midi 发送“延音踏板”midi 信号?

问题描述

可以通过note_on()ornote_off()方法调用简单的 midi 信号,但我找不到使用pygame.midi. 有没有传统的方法可以做到这一点?

标签: pythonpygamemidi

解决方案


pygame.midi不幸的是,在(或大多数其他常用的 Python-MIDI 库)中没有实现延音踏板,因此从 Pygame 模块本地执行它是不可能的。

但是,您可以通过稍微重新构建代码来解决此问题。如果您可以使用特定的键(或事件)代替我假设的物理延音踏板(毕竟,大多数 MIDI 延音踏板都是简单的开关),您可以拉出类似于延音的东西。例如:

import pygame
from pygame.locals import *

# Midi init and setup, other code, etc...
# device_input = pygame.midi.Input(device_id)

sustain = False

# We will use the spacebar in place of a pedal in this case.

while 1:
    for event in pygame.event.get():
        # You can also use other events in place of KEYDOWN/KEYUP events.
        if event.type == KEYDOWN and event.key == K_SPACE:
            sustain = True
        elif event.type == KEYUP and event.key == K_SPACE:
            sustain = False
    # ...
    for i in device_input:
        if sustain:
            # Remove all MIDI key-up events here

    # Then play sounds or process midi input accordingly afterwards

推荐阅读