2 * Simple monophonic MIDI synthesizer :-)
5 #define MIDI_NOTE_OFF 128
6 #define MIDI_NOTE_ON 144
7 #define MIDI_CONTROL 176
8 #define MIDI_PITCH_BEND 224
12 double base_a4=440; //set A4=440Hz
13 double note_to_freq(double n) {
14 if( n>=0 && n<=119 ) {
15 return base_a4*pow(2,(n-57)/12);
24 pinMode(sndout, OUTPUT);
32 if(Serial.available() >= 3) {
33 double command=0, channel=0, pitch=0;
34 command = Serial.read();
35 channel = Serial.read();
36 pitch = Serial.read();
37 if(command == MIDI_NOTE_ON && pitch > 0) dela=((1000000/2)/note_to_freq(pitch));
38 if(command == MIDI_NOTE_OFF || pitch == 0) dela = 0;
40 Serial.println(note_to_freq(pitch), DEC);
43 if(dela > 0) digitalWrite(sndout, HIGH); delayMicroseconds(dela);
44 digitalWrite(sndout, LOW); delayMicroseconds(dela);