Arduino MIDI library (FortySevenEffects) seems to be faulty

The 'Arduino MIDI library' seems to be corrupting MIDI messages which arrive in rapid succession.

I've:

1/ Installed the FortySevenEffects MIDI library V4.3.1.

2/ Loaded the example 'MIDI-sniffer.ino' into the development environment Arduino 1.8.9

3/ Installed the SparkFun MIDI shield onto a 4duino Uno Pro (Uno compatible)

4/ Programmed the Uno Pro with MIDI-sniffer

5/ Connected an M-Audio Radium 49 keyboard to the shield MIDI In

6/ Connected the shield MIDI Out to a generic MIDI-to-USB converter

7/ Set Pocket MIDI (a MIDI message analyser) on my PC to show the incoming MIDI messages from the Arduino

Thus the MIDI message path is (software in italics):

keyboard -> shield -> Uno -> MIDI-sniffer (displaying to serial monitor) -> shield -> MIDI USB converter -> Pocket MIDI (displaying to PC screen)

--> Please note that MIDI-sniffer example program can display messages in hexadecimal form directly from the serial port, or in text form via the MIDI library functions. It also invokes 'thru mode' so it copies incoming messages to the MIDI out connector. <--

When pressing individual keys on the keyboard:

a/ MIDI-sniffer shows the correct hexadecimal and text-form MIDI messages

b/ Pocket MIDI shows the correct hexadecimal messages

c/ Pocket MIDI plays the correct notes on my soundcard's synth.

So, all would seem to work correctly.

HOWEVER:

If I press-and-release two keys simultaneously:

a/ MIDI-sniffer shows the correct hexadecimal messages from the serial port (i.e. two Note On messages in succession), but shows random, incorrect text-form messages

b/ Pocket MIDI shows scrambled hexadecimal messages, and plays random notes

When pressing two keys simultaneously MIDI-sniffer reports various weird messages, including 'Stopping', 'PolyAT', 'PropChange', 'Clock', etc. Usually one key is correctly reported, and the other corrupted.

Pressing the same two keys with two fingers in rapid succession results in correct behaviour - to induce the fault I must use one finger to press the two keys.

It seems as if the MIDI library cannot handle MIDI messages received in rapid succession and corrupts the second one if it arrives too close to the first.

QUESTIONS

Has anyone else noticed this problem? Could it be due to a lack of processing power on the UNO? Is there anything else that could be causing the problem?

All comments, advice and jeering gladly received! :slight_smile:

Regards,
Steve

I suspect that the problem is the use of the SoftwareSerial library. It is a hack to bitbang a UART interface on normal GPIO pins. It makes heavy use of interrupts, and timing is critical. When the timing is not met, received data is corrupted.

If you need two UARTs (one for MIDI and one for communication with the PC), the Arduino UNO is not the right board for the job.
You could look into boards that support USB natively, like the Arduino Leonardo, or Teensy. They also have support for MIDI over USB.
If you don't care about USB, the Arduino Mega has more hardware UARTs.

Pieter

Thank you, Pieter - that's much appreciated.

The reason for setting it up this way is that MIDI-sniffer.ino uses the hardware serial port for reporting the MIDI messages back to the host PC and the software uart for handling the MIDI messages. This is, in fact, as written by the author of the MIDI library, so I guess he was happy with it. Maybe he didn't stress test it enough.

Even so, I think you are on to the problem. I'll see what other Arduino boards I've got in stock and try some alternatives.

Thanks again for the steer.

Steve

Pieter, just to let you know (and any who follow on in the future) - you were completely right: I solved the problem by ditching SoftwareSerial and running the sketch on an Arduino MEGA, using a second hardware serial port.

Also, I made a mistake - the sketch 'MIDI-sniffer' is actually from Byron Jacquot of Sparkfun Electronics.

Just in case another novice needs further explanation, the Sparkfun MIDI shield connects to pins 0 and 1 on the Arduino by default, which is no use for the MIDI-sniffer sketch because it needs those pins to talk to the serial monitor. By changing two straps you can make the shield use pins 8 and 9 instead, in combination with SoftwareSerial on the Arduino, which you set to use those two pins. As described above, SoftwareSerial won't work with a high MIDI message load.

Instead, you have to use a MEGA and one of the other hardware serial ports which it provides. Lift pins 8 and 9 from the connector on the MIDI shield, so they don't make a connection with the Arduino. Wire them to pins 19 and 18 respectively on the MEGA.

In MIDI-sniffer.ino, replace

MIDI_CREATE_INSTANCE(SoftwareSerial, SoftSerial, MIDI);

with

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);

Then go through the sketch and replace each instance of 'SoftSerial' with 'Serial1'. MIDI-sniffer will then work correctly.

My thanks again to Pieter for nailing the problem so quickly. :slight_smile: