Pygame读取MIDI输入 [英] Pygame read MIDI input

查看:118
本文介绍了Pygame读取MIDI输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我引用了 Pygame MIDI文档

I referenced the Pygame MIDI documentation and this code to try to get MIDI input to work. The MIDI Interface (Avid Eleven Rack) receives MIDI data from my MIDI controller just fine in my audio software (Pro Tools). Using Pygame, however, I can not seem to read any information at all.

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

class MidiInput():
    def __init__(self):
        # variables
        self.elevenRackInID = 2

        # init methods
        pygame.init()
        pygame.midi.init()
        self.midiInput = pygame.midi.Input(self.elevenRackInID, 100)

    def run(self):
        # print(pygame.midi.Input(3, 100))
        # for i in range(10):
            # print(pygame.midi.get_device_info(i), i)
        self.read = self.midiInput.read(100)
        # self.convert = pygame.midi.midis2events(self.read, self.elevenRackInID)
        print(self.read)

test = MidiInput()
while True:
   test.run()

打印到控制台上的唯一东西是空的方括号:

The only thing printed to the console are empty square brackets:

[]

其他信息

我再次检查:输入ID是正确的,实际上它是输入.

Additional Info

I just checked again: the input ID is the right one and it is in fact an input.

"self.midiInput.poll()";返回False.因此,根据Pygame文档,没有任何数据输入.

"self.midiInput.poll()" returns False. So according to the Pygame documentation there is no data coming in.

您可以在下面查看数据,民意调查和设备信息:

You can see the data, poll and device info below:

data: [] || poll: False || device info: (b'MMSystem', b'Eleven Rack', 1, 0, 1)

根据Pygame我所有MIDI设备的列表(带有索引):

A list of all my MIDI devices according to Pygame (with indexes):

(b'MMSystem', b'Microsoft MIDI Mapper', 0, 1, 0) 0
(b'MMSystem', b'External', 1, 0, 0) 1
(b'MMSystem', b'Eleven Rack', 1, 0, 1) 2
(b'MMSystem', b'Maschine Mikro MK2 In', 1, 0, 0) 3
(b'MMSystem', b'Microsoft GS Wavetable Synth', 0, 1, 0) 4
(b'MMSystem', b'External', 0, 1, 0) 5
(b'MMSystem', b'Eleven Rack', 0, 1, 0) 6
(b'MMSystem', b'Maschine Mikro MK2 Out', 0, 1, 0) 7

任何帮助或建议,我们将不胜感激!

Any help or suggestions are greatly appreciated!

推荐答案

我在另一个论坛上得到了答案.事实证明,这里有一个示例文件,显示了如何使代码正常工作.因此,如果有人偶然发现了这个问题,那么这里是示例代码的有用部分:

I got an answer in another forum. It turns out that there is an example file which shows how to get the code to work. So if someone else stumbles over this problem here is the useful part of example code:

import sys
import os

import pygame as pg
import pygame.midi


def print_device_info():
    pygame.midi.init()
    _print_device_info()
    pygame.midi.quit()


def _print_device_info():
    for i in range(pygame.midi.get_count()):
        r = pygame.midi.get_device_info(i)
        (interf, name, input, output, opened) = r

        in_out = ""
        if input:
            in_out = "(input)"
        if output:
            in_out = "(output)"

        print(
            "%2i: interface :%s:, name :%s:, opened :%s:  %s"
            % (i, interf, name, opened, in_out)
        )


def input_main(device_id=None):
    pg.init()
    pg.fastevent.init()
    event_get = pg.fastevent.get
    event_post = pg.fastevent.post

    pygame.midi.init()

    _print_device_info()

    if device_id is None:
        input_id = pygame.midi.get_default_input_id()
    else:
        input_id = device_id

    print("using input_id :%s:" % input_id)
    i = pygame.midi.Input(input_id)

    pg.display.set_mode((1, 1))

    going = True
    while going:
        events = event_get()
        for e in events:
            if e.type in [pg.QUIT]:
                going = False
            if e.type in [pg.KEYDOWN]:
                going = False
            if e.type in [pygame.midi.MIDIIN]:
                print(e)

        if i.poll():
            midi_events = i.read(10)
            # convert them into pygame events.
            midi_evs = pygame.midi.midis2events(midi_events, i.device_id)

            for m_e in midi_evs:
                event_post(m_e)

    del i
    pygame.midi.quit()

您可以在以下目录中自己找到文件: C:\ Users \ myUser \ AppData \ Roaming \ Python \ Python37 \ site-packages \ pygame \ examples \ midi.py

You can find the file yourself in this directory: C:\Users\myUser\AppData\Roaming\Python\Python37\site-packages\pygame\examples\midi.py

用您的Win用户名替换'myUser'.另外,'Python37'可能会因您安装的Python版本而异.

Replace 'myUser' with your Win username. Also, 'Python37' can vary on the version of Python you have installed.

这篇关于Pygame读取MIDI输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆