Hey. one question: how can i make arduino read and write MIDI messages directly from USB UART?
it is useless to have a midi library if you can't send and receive midi over USB. I mean it's quite important! for example if i want to control Ableton Live or a virtual instrument on the PC,
or if i want arduino to behave as a synthethiser (a midi SLAVE) and use the PC as a midi MASTER.
i know that 31250 bps, which is the midi baudrate , isn't ok for the RS-232 serial protocol (which is the one working on USB right?)
but if you google Serial to midi converter, it will show you a program written in processing (thus Java) which handles midi over usb. it uses a finite state machine to emulate a 31250 bauderate with an actual 57600 bps. the link is here.Serial_MIDI
can anyone please help about this?
for now i manage to receive midi from serial with the function MIDI.read() of your library (it turns on a led when this happens)
but it doesn't seem to reckognize the TYPE (status byte) as I use MIDI.getType() function
I'm using arduino and MIDI for my thesis project but i'm really stuck at this point.
here's my arduino code for now:
#include <MIDI.h>
#include <stdio.h>//printf
#include <math.h>//pow
//MIDI REVEALER FROM MIDIYOKE 1, CHANNEL 1
#define LED 2
#define LED_PWM 6
#define BUTTON 5
#define BUZ 9
int decay=500;
float midivec[128];
int buttonState = 0;
int a = 440; // tono fondamentale 440 hz...
void setup() {
pinMode(LED, OUTPUT);
pinMode(LED_PWM, OUTPUT);
pinMode(BUTTON, INPUT);
pinMode(BUZ, OUTPUT);
MIDI.begin(); // Launch MIDI with default options (input channel 1)
// with MIDI_CHANNEL_OMNI input channel is set to all
//p = 69 + 12 × log2 (f/(440 Hz)), dove con p si denota il pitch (0-127) ossia il valore della nota midi.
//--> f = 440*2^((p-69)/12)
for (int x = 0; x < 128; x++) //CALCULATE ALL MIDI FREQUENCIES (midi number (pitch) to frequency converter)
{
midivec[x] = a * pow(2, ((x - 69) / 12));
}
}
void loop() {
digitalWrite(LED,HIGH);
if (MIDI.read()) {
digitalWrite(LED,LOW);
if (MIDI.getType()==NoteOn)
{
playMidiNote(MIDI.getData1(), MIDI.getData2());//pitch, velocity
analogWrite(LED_PWM,MIDI.getData2());
delay(500);
}
if (MIDI.getType()==NoteOff)
{
playMidiNote(MIDI.getData1(), MIDI.getData2());//pitch, velocity
digitalWrite(LED_PWM,LOW);
delay(500);
} // Suona la frequenza corrispondente alla nota midi
delay(500);
}
}
//PLAYS A TONE (given the frequency and the intensity (velocity)) ON THE BUZZER
void playTone(int tone, int velocity) { //ho eliminato la durata INT duration
//for (long i = 0; i < duration * 1000L; i += tone * 2) {
analogWrite(BUZ, velocity*2); //128*2 = 256 --> PWM a 8 bit
delayMicroseconds(tone);
digitalWrite(BUZ, LOW);
delayMicroseconds(tone);
// }
}
//PLAYS A MIDI NOTE given the MIDI VALUE (or number, or pitch) and ITS VELOCITY
void playMidiNote(int midi_val, int velocity){
playTone(midivec[midi_val], velocity);
}
