Quick hack to get Arduino working with MIDI-rx.c, FIXME later...
[mirrors/Programs.git] / arduino / MIDI_synth / MIDI_synth.pde
1 /*
2 * Simple monophonic MIDI synthesizer :-)
3 */
4
5 #define MIDI_NOTE_OFF 128
6 #define MIDI_NOTE_ON 144
7 #define MIDI_CONTROL 176
8 #define MIDI_PITCH_BEND 224
9
10 #define sndout 13
11
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);
16 } else {
17 return -1;
18 }
19 }
20
21
22 void setup() {
23 Serial.begin(19200);
24 pinMode(sndout, OUTPUT);
25 }
26
27 void loop() {
28 int dela=0;
29
30 while(1) {
31
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;
39
40 Serial.println(note_to_freq(pitch), DEC);
41 }
42
43 if(dela > 0) digitalWrite(sndout, HIGH); delayMicroseconds(dela);
44 digitalWrite(sndout, LOW); delayMicroseconds(dela);
45
46 }
47
48 }
This page took 0.393141 seconds and 4 git commands to generate.