简体   繁体   中英

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 . 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.

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. 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.

pygame.midi does not have a special function for that, so you have to send the raw bytes:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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