Quick hack to get Arduino working with MIDI-rx.c, FIXME later...
[mirrors/Programs.git] / arduino / MIDI_synth / MIDI_synth.pde
diff --git a/arduino/MIDI_synth/MIDI_synth.pde b/arduino/MIDI_synth/MIDI_synth.pde
new file mode 100644 (file)
index 0000000..6585b00
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Simple monophonic MIDI synthesizer :-)
+ */
+
+#define MIDI_NOTE_OFF 128
+#define MIDI_NOTE_ON  144
+#define MIDI_CONTROL  176
+#define MIDI_PITCH_BEND 224
+
+#define sndout 13
+
+double base_a4=440; //set A4=440Hz
+double note_to_freq(double n) {
+  if( n>=0 && n<=119 ) {
+    return base_a4*pow(2,(n-57)/12);
+  } else {
+     return -1;
+  }
+}
+
+
+void setup() {
+  Serial.begin(19200);
+  pinMode(sndout, OUTPUT);
+}
+
+void loop() {
+  int dela=0;
+  
+  while(1) {
+    
+  if(Serial.available() >= 3) {
+    double command=0, channel=0, pitch=0;
+    command = Serial.read();
+    channel = Serial.read();
+    pitch = Serial.read();
+    if(command == MIDI_NOTE_ON && pitch > 0) dela=((1000000/2)/note_to_freq(pitch));
+    if(command == MIDI_NOTE_OFF || pitch == 0) dela = 0;
+    
+    Serial.println(note_to_freq(pitch), DEC);
+  }
+  
+  if(dela > 0) digitalWrite(sndout, HIGH); delayMicroseconds(dela);
+  digitalWrite(sndout, LOW);  delayMicroseconds(dela);
+  
+  }
+}
This page took 0.142425 seconds and 4 git commands to generate.