Introduction


This guide will show how to take a freshly installed Raspberry Pi and have it operate as an OS-discoverable MIDI I/O device. It will also provide some examples of using various Python libraries to get MIDI data into and out of the programming environment.

UPDATE – Jun 9, 2022.: This guide has been updated to fix some issues with the latest Raspberry Pi OS version. Added new examples. Big thanks to Marting Woolley for his contribution.
UPDATE – Aug 14, 2022.: Added old guide link and some community projects using MIDI board.

What we need


Assembly


Use the nylon screws and standoffs to assembly the Raspberry Pi together with the MIDI Board, as shown on the image below:

First time setup


We tested all the examples in this document on a Pi 4B using Rasperry Pi OS, version April 2022). The first time, it is necessary to use a screen and keyboard to set the Pi up. Thereafter, use your method of choice to access the Pi’s OS. All steps are mandatory unless otherwise stated.

Installation


Update/Upgrade

Perform the update and upgrade as described here: https://www.raspberrypi.com/documentation/computers/os

Python Examples


The examples use the serial library to make it easier for everybody to test the board functionality.

MIDI in over Serial (example_1_midi_in.py)

This example shows how to:

  • Lists some types of MIDI messages received in hex such as note on/note off of the MIDI device connected.
import serial

ser = serial.Serial('/dev/ttyAMA0', baudrate=31250)

message = [0, 0, 0]
while True:
  i = 0
  while i < 3:
    data = ord(ser.read(1)) # read a byte
    if data >> 7 != 0:
      i = 0      # status byte!   this is the beginning of a midi message!
    message[i] = data
    i += 1
    if i == 2 and message[0] >> 4 == 12:  # program change: don't wait for a
      message[2] = 0                      # third byte: it has only 2 bytes
      i = 3

  messagetype = message[0] >> 4
  messagechannel = (message[0] & 15) + 1
  note = message[1] if len(message) > 1 else None
  velocity = message[2] if len(message) > 2 else None

  if messagetype == 9:    # Note on
    print('Note on : 0x' + ''.join('{:02x}'.format(x) for x in message) + ' note=' + str(note) + ' on channel ' + str(messagechannel))
  elif messagetype == 8:  # Note off
    print('Note off: 0x' + ''.join('{:02x}'.format(x) for x in message) + ' note=' + str(note) + ' on channel ' + str(messagechannel))
  elif messagetype == 12: # Program change
    print 'Program change'


 

MIDI out over Serial (example_1_midi_out.py)

This example shows how to:

  • Play note C3 over and over again on a connected MIDI device.
import serial
import time

ser = serial.Serial('/dev/ttyAMA0', baudrate=31250)

channel = 2 # this represents channel 3
note = 60
velocity = 100
note_off = 8
note_on = 9

msg_note_on  = bytearray([(note_on << 4) | channel, 60, 72])
msg_note_off = bytearray([(note_off << 4) | channel, 60, 72])

while True:
        print(str(hex(msg_note_on[0]))+' '+str(msg_note_on[1])+' '+str(msg_note_on[2]))
        ser.write(msg_note_on)
        time.sleep(1)
        ser.write(msg_note_off)
        time.sleep(1)

Community Projects


Check out some of the projects that community is sharing with all of us using the OSA MIDI Board or similar:

 

Old guide


If somebody finds usefull the old guide that was posted, please check it out HERE.

5 thoughts on “Setting-up Raspberry Pi for MIDI

  1. Brice says:

    Hello All, i fought for many months to make it work and i have finally been able to use it ! You need to change the uart config and switch the Bluetooth uart to somewhere else ! Follow the tuto here !

  2. Joseph Olano says:

    Hi Oriol, I am trying to revisit this the last time I was not successful, can you confirm where is the latest guide? Or please email it to me. Thank you.

  3. Joseph Olano says:

    Hi Oriol,

    I trust you are well and continue to be keeping safe.
    I have revisited this again after a hiatus.
    Could you point me to the latest install instructions?

    W/o updated instructions I can see, I went ahead and try again:
    Steps;
    1) Use an fresh RPI ( sudo apt full-upgrade done and rebooted)
    2) I place the OSA Midi Board onto the RPI
    3) Put power
    4) Immediately, the TX light turns on and never turns off ( same as my observation the first time I asked for your help 5) On OSA Midi Board connect MIDI out to MIDI IN of my groovebox
    6) Ensure I am setup to receive MIDI IN of my groovebox from Channel 3
    7) Run the latest example you have posted – “MIDI out over Serial (example_1_midi_out.py)”
    6) No sound coming from the python script, TX light forever ON.
    7) 🙁

    ( My unit is Raspberry Pi 3 Model B v1.2 )

    Can you help me in trouble shoot? Or could it be that I have a faulty Midi board?

    Thanks Again, Joseph

Leave a Reply

Your email address will not be published. Required fields are marked *