简体   繁体   中英

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

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.

I also tried for quite some time to get the midi input reading to work. 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. So it maybe would be a good idea to not rely on the default input id. You can get a list of your Midi devices like this:

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)

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