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.
Connect the digital piano to the computer(MacOS) and turn both on.
Install the mido python package
pip install mido
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.
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)
https://github.com/mido/mido
https://mido.readthedocs.io/en/latest/midi_files.html