pythonpackagemidipiano

Piano MIDI input detection via Python


I am trying to create a program that takes an input from my piano and have Python detect what note I just pressed as MIDI.

I don't require help with the code itself, but I can't seem to find any package that can detect this.

I have an electric piano, that can output it's data through USB-B into the USB-A on my PC.

I know the detection is possible, since software like FL Studio can also recognize it, so I think there should be a way to do this with Python too.

Anyone here who can help me?

I've tried searching up a package online, but every way I've tried to search has lead to mainly results about how to use your PC keyboard for a virtual piano. I guess that search is just so much more common than what I'm looking for.


Solution

  • I use Mido for a lot of Python MIDI handling. You'll probably need to also install python-rtmidi to handle the real-time processing bits, but then you should be good to go!

    Here's an example of what I just tested on my system to make sure it was up and running with the latest library updates:

    !pip install mido
    !pip install python-rtmidi
    

    Not sure exactly your setup, but you'll need to make sure those are installed in your environment before anything else. And then to read in real-time:

    import mido
    
    mido.get_output_names() # Provides the list of connected MIDI outputs
    
    inport = mido.open_input('KL Essential 49 mk3 MIDI') # Specify MIDI port
    
    with inport:  # Keeps looping until you close connection/program
        for msg in inport: 
            print(msg)
    

    When I'm using my MIDI controller, I'm getting outputs like this note_on channel=0 note=48 velocity=97 time=0 when pushing down on the key, and note_off channel=0 note=48 velocity=0 time=0 when releasing.

    Hope it helps and best of luck! ✨