This code read a MIDI IN (DIN 5) keyboard and send it right to the MIDI OUT ... eventually I will do some changes to the Midi velocity data. The issue I have is that the response of the code generates data error unless I add some delay(nn) ; but this create another issue of latency. How can I make the code works more effectively.
Welcome, this should be a fun protect for you. I and many others cannot or will not read your code as posted. Please use code tags. They are explained in the forum guidelines.
Thanks @PieterP for the details.
Good point on specific MIDI message... I will take note and considered how to read ONLY Midi Note ON and Note Off messages. Will check the MIDI parsers links provided. Thanks
This reads only one byte, it doesn't read the whole message. So with only one byte the code that follows is a nonsense, because it is acting like it has read the whole message and not just a byte.
You have to build up the message one byte at a time, waiting if necessary for each byte to arrive and not just assuming all the bytes of a MIDI message will arrive at once.
The loop function runs much faster than the note come in.
Hi @Grumpy_Mike , thanks a lot for the reply. Actually, the serial.read() capture the full MIDI message. My code is somehow based on this short code that works perfectly. It reads the MIDI data and pass it thru to the MIDI OUT.
What I did is to parse the MIDI message into its parts (which I now start to understand that MIDI messages are not all the same length or same parts....)
This is the original code:
int midiMsg = 0 ;
void setup() {
// put your setup code here, to run once:
// Initialize the serial port for MIDI
Serial.begin(32500) ;
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
midiMsg = Serial.read() ;
Serial.write(midiMsg) ;
}
}
I see you sending ~33 characters every second with no regard for there actually being new MIDI traffic to pass on. Just sending the same thing until a new character is arrived.
So logically it makes no sense.
Also int type on the UNO is 16 bits, so those 16 bit shifts are off to nowhere. Just nonsense.
It amounts to your latest posted example, except notice that the example sends each received character out only once.
One character comes in, you send the same one out. No characters, nothing comes out.
And those are not examined or modified. You might as well have used a wire.
If you want to get a MIDI message and modify it and send it along, that is what you'll have to do. Either take in an entire valid MIDI message, then change it and send it out, or get clever if it is something that needs as little delay in doing that as possible. You could be sending out characters forming the new MIDI message whilst still receiving the rest of the message going through.
As pointed out, the MIDI messages will need several characters to be read() and examined.
@alto777 , critical points to take into consideration , thanks. On the last code I post ,it reads the FULL MIDI message , as you point out, into a 16 bit variable. I don't understand , then , how it is working so well. It reads the complete MIDI message (3 bytes of 8 bits , thats 24 bits.....) and send it to the MIDI OUT , but it work. No data is lost. ... how?
gets one character when available, and sends that one character on to the output. Without examining it, without criticizing it, without caring what it is.
If these are any kind of messages, they are formed from multiple characters, and survive because you just pass every single one along as it arrives, one character in, one character out.
Because you are using the serial port, I cannot suggest you print that "message" midiMsg from inside the if statement - but I guarantee that it is a single character each time.
No it doesn't.
Any appearance of it works is just a fluke.
Transferring an input byte to an output byte might appear like it works, but you are deluding yourself.
Still, what do I know? I have only written and got published, a book on using MIDI on the Arduino.