简体   繁体   中英

How do you get midi events using python-rtmidi

The documentation for python-rtmidi is basically the worse that it is ever written. I'm trying to figure out how to get midi event data using that program. The data I want is something like this

[ 1]

I'm not even sure if python-rtmidi can get that data, that's how bad their documentation is that they do not even tell you what it is that their app does. But in any case I need that data and I need it to be in a python program. The only example that exists on the python-rtmidi webpage seems to only show how to output a midi-event to an external musical instrument. I want the opposite. I want the data from the instrument to show up on my computer. I can at least get the software to sense my external instrument. For example, when I use the following code

import logging
import sys
import time

from rtmidi.midiutil import open_midiport
from rtmidi.midiutil import open_midiinput

log = logging.getLogger('test_midiin_poll')

log = logging.getLogger('midiin_poll')
logging.basicConfig(level=logging.DEBUG)

# Prompts user for MIDI input port, unless a valid port number or name
# is given as the first argument on the command line.
# API backend defaults to ALSA on Linux.
port = sys.argv[1] if len(sys.argv) > 1 else None

try:
    midiin, port_name = open_midiport(port)
    midiin, port_name = open_midiinput(port)
except (EOFError, KeyboardInterrupt):
    sys.exit()

It can detect my external Alesis keyboard but I see no variable that resembles something like a key being hit on the keyboard. I also need to point out that I need python software which can record the events in real time. My first goal is to get the midi events then write a program that calculates whether or not the events are timed properly.


Update

This guy at least is sort of doing what I want to do. He can get the midi events but he doesn't say how he got them.

Mido - How to get midi data in realtime from different ports

Ok, I solved it.

import mido

instrument = mido.get_input_names()
inport = mido.open_input(instrument[0])
events = []
while 1:
    msg = inport.receive()
    events.append(msg)

This will create a list of events. And from there you can make calculations on the events.

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