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=38400)

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=38400)

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.

Leave a Reply

Your email address will not be published.