简体   繁体   English

如何通过 Pygame 从 MIDI 设备获取输入?

[英]How do I get inputs from a MIDI device through Pygame?

I'm somewhat new to both Python and Pygame, and trying to simply read MIDI controls live from a Windows computer.我对 Python 和 Pygame 都有些陌生,并试图简单地从 Windows 计算机实时读取 MIDI 控件。 I've looked into Pygame and have set up what I think to be most of the right syntaxes, but the shell seems to crash or at least reset at the line pygame.midi.Input(input_id) every time.我查看了 Pygame 并设置了我认为最正确的语法,但是 shell 似乎每次都崩溃或至少在pygame.midi.Input(input_id)行重置。

import pygame.midi
import time

pygame.init()
pygame.midi.init()
print("The default input device number is "  + str(pygame.midi.get_default_input_id()))
input_id = int(pygame.midi.get_default_input_id())

clock = pygame.time.Clock()
crashed = False

while (crashed == False):
    print("input:")
    pygame.midi.Input(input_id)
    print(str(pygame.midi.Input.read(10)))
    clock.tick(2)

I am also new to both Python and and Pygame.我也是 Python 和 Pygame 的新手。

I also tried for quite some time to get the midi input reading to work.我也尝试了相当长的一段时间来让 midi 输入读取工作。 In another forum I was pointed to an included sample file in the library.在另一个论坛中,有人指出我在库中包含一个示例文件。 You can see the answer here .你可以在这里看到答案。

Side note:边注:

On my computer there are several Midi devices active.在我的电脑上,有几个 Midi 设备处于活动状态。 So it maybe would be a good idea to not rely on the default input id.因此,不依赖默认输入 ID 可能是个好主意。 You can get a list of your Midi devices like this:您可以获得您的 Midi 设备列表,如下所示:

import pygame
from pygame.locals import *
from pygame import midi

def printMIDIDeviceList():
        for i in range(pygame.midi.get_count()):
            print(pygame.midi.get_device_info(i), i)

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

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