简体   繁体   English

如何使用pygame.midi发送“延音踏板” MIDI信号?

[英]How do I send the 'sustain pedal' midi signal using pygame.midi?

Simple midi signals can be invoked by the note_on() or note_off() methods, but I couldn't find a way to send the 'sustain pedal' midi signal using pygame.midi . 简单的midi信号可以通过note_on()note_off()方法调用,但是我找不到使用pygame.midi发送“延音踏板” midi信号的方法。 Is there any conventional way to do that? 有什么常规方法可以做到吗?

Unfortunately, there is no implementation of the sustain pedal in pygame.midi (or most other commonly used Python-MIDI libraries), so doing it natively from the Pygame module is out of the question. 不幸的是,在pygame.midi (或大多数其他常用的Python-MIDI库)中没有实现延音踏板,因此从Pygame模块本地进行是不可能的。

However, you may be able to work around this by re-structuring your code a little. 但是,您可以通过稍微重新组织代码来解决此问题。 If you can use a specific key (or an event) in place of what I assume to be a physical sustain pedal (after all, most MIDI sustain pedals are simple switches ), you can pull off something similar to a sustain. 如果您可以使用特定的键(或事件)来代替我假定的物理延音踏板(毕竟,大多数MIDI延音踏板都是简单的开关 ),则可以拉出类似于延音的东西。 For example: 例如:

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

The specification defines the sustain pedal as controller 64 , so you have to send a control change message. 该规范将延音踏板定义为控制器64 ,因此您必须发送控制更改消息。

pygame.midi does not have a special function for that, so you have to send the raw bytes: pygame.midi没有特殊的功能,因此您必须发送原始字节:

write_short(0xb0 + channel, 64, 127 if pressed else 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM