MIDI Out on Raspberry Pi using Python

1.2k views Asked by At

I'm using buttons in a scan matrix to output MIDI notes to a MIDI-USB converter connected to MIDI-OX.

For this, I'm using Python to figure out when a button is pressed or released and which button it was. This part of the code works fine.

However, I'm getting a lot of inconsistency with the commands received by MIDI-OX. For communications, I have the UART0_TXD pin of the Pi connected directly to pin 2 of the 5 pole DIN connector used for MIDI and PySerial sending the data. As far as I can see, my code should be generating correct commands and sending them over serial correctly so I'm not sure where to go here for troubleshooting.

The code sending the command is as follows:

message=chr(0x90) + chr(0x3D) + chr(0x40)
port.write(message)

This results in the following output in MIDI-OX:

TIMESTAMP IN PORT STATUS DATA1 DATA2 CHAN NOTE EVENT       
007B971E   1  --     90    68    27    1  G# 8 Note On     
007B971E   1  --     FF    --    --   --  ---  System Reset

It can be seen above that MIDI-OX is recieving 68 for data 2 and is outputting the correct note (G#) for this number. If however I change the code to read:

message=chr(0x90) + chr(0x3C) + chr(0x40)
port.write(message)

MIDI-OX output is:

TIMESTAMP IN PORT STATUS DATA1 DATA2 CHAN NOTE EVENT               
007FAC19   1  --     81    00    42    2  C  0 Note Off              
007FAC3A   1  --     FF    --    --   --  ---  System Reset          

Showing that it is receiving completely different data.

Any ideas on where to go from here would be greatly appreciated.

1

There are 1 answers

0
user660195 On

Just starting on a similar project, I am referring to your comment:

Unfortunately, all the data MIDI-OX recieved was wrong and I can't see a pattern. I'm suspecting that I'm doing something wrong with PySerial but I don't know what.

As far as I understood the MIDI baud rate setting is not so simple for a Raspi, see https://zuzebox.wordpress.com/2014/06/26/raspberry-pi-minipiio-midi-board/

Maybe your setup is slightly off synchronization, therefore produces only "almost" correct, but somewhat erratic MIDI data, and needs baud tweaking to get things correct.