Search code examples
pythonpython-3.xmidi

Send MIDI data via port on MacOS to Digital Piano


I would like to send midi data from my computer (running MacOS) via a port to my digital piano. I want to be able to play a midi file, play certain notes for certain durations and potentially change instruments.

I have literally no idea on how to achieve this and cannot find any resources online. Please provide code with your solution as it would help greatly.

I am using USB-to-Host, to send the data --> not sure if this means anything.


Solution

  • Manually with Garageband:

    1. Connect the digital piano to the computer(MacOS) via USB and turn both on.
    2. Open the midi file with Garageband, which is usually available on MacOS devices.
    3. Then go to Gargageband->Preferences->'Audio/Midi'->'Output Device' dropdown
    4. If the Digital Piano is recognized by the computer, it should be listed in the dropdown. Select it.
    5. Click the 'play' icon on Garageband. The midi file should play with output comming from the digital piano.

    Programatically with Mido:

    1. Connect the digital piano to the computer(MacOS) and turn both on.

    2. Install the mido python package
      pip install mido

    3. Get the list of midi ports that mido recognizes:
      python -c "import mido; print(mido.get_output_names())"

      The output should be something like:
      ['Digital Keyboard', 'IAC Driver Bus 1']

      In this example 'Digital Keyboard' is your keyboard.
      Your actual keyboard probably has a different name.
      NOTE: The 'IAC Driver Bus 1' is a standard MacOS midi output port.

    4. Update the following play-midi.py script by changing 'Digital Keyboard' to the name of your keyboard which mido displayed in #3. Update the *.mid file to the name of your *.mid file.

    import mido
    
    # This will list the available ports 
    print(mido.get_output_names())
    
    # Open the midi link to your keyboard
    outport = mido.open_output('Digital Keyboard')
    
    # Open the mid file to be played
    mid = mido.MidiFile('my_midi_file.mid', clip=True)
    
    # Play the file out to your keyboard
    for msg in mid.play():
        outport.send(msg)
    
    1. Run the script. The sound should play out of your keyboard.

    Mido Reference resources:

    https://github.com/mido/mido
    https://mido.readthedocs.io/en/latest/midi_files.html