Generating a midi music stream with Python

using python package Mido to send midi information to midi player - an introduction

instructions originally sourced from this fine reddit post

Getting Started

Today, we will be installing Mido, a python midi package. We'll then open a new audio port (will show the ios instructions), and output our midi stream to that port. I'm using Ableton Live 10 Intro as my program to consume the midi stream, but any application that takes in external midi will do.

first off, if you don't have python install on your machine, you can get it here.
Then, install mido via pip install mido. Full mido documentation here.


Design

The final workflow will be as follows -

  1. Our midi player will be listening to our newly created midi port.
  2. Our python script will attach itself to our newly created midi port and send midi information to it.
  3. Our midi player will play the midi sent from our file.

The first thing we must do is create our midi port.


Creating a new midi port

we will be going over the steps for mac. Here are the official docs for windows.

  1. On Mac OS, navigate to Midi Audio Setup.
  2. double click on IAC Driver
  3. On the window that pops up, click "ports".
  4. There will probably already be a port named "Bus 1". Create a new port, or rename the current one. I named mine "pioneer."

IAC Driver ports


Point your midi player to your new port

This next step is different based on which midi player you use. The following screenshot is from Ableton Live 10 Intro.

Ableton - point your midi track at your IAC driver port


Write your python code

Below is some sample code that will play send your midi player a middle C, which is represented with the integer 60. Musical notes are represented with the values 1 through 127. When you run the following code, it will play once.

1import mido 2 3middleC = 60 4msg = mido.Message('note_on', note=middleC, velocity=64) 5outport = mido.open_output('IAC Driver pioneer') 6outport.send(msg) 7
python
<iframe width="80%" height="`00" src="https://clyp.it/wpdhjpc4/widget" frameborder="0"></iframe>

To demonstrate what I mean, I modified the above code to play continuously, pausing for .5 seconds every after every play.

1import mido 2from time import sleep 3 4middleC = 60 5while True: 6 msg = mido.Message('note_on', note=middleC, velocity=64) 7 outport = mido.open_output('IAC Driver pioneer') 8 outport.send(msg) 9 sleep(.5) 10

midi note looping


In conclusion

You can now send midi messages to your midi player from Python as it if were a midi controller!

We'll build cooler stuff with this in blogposts to come.

See you next time!