Quick hack to get Arduino working with MIDI-rx.c, FIXME later...
authorHarvie <tomas@mudrunka.cz>
Mon, 15 Nov 2010 01:04:55 +0000 (02:04 +0100)
committerHarvie <tomas@mudrunka.cz>
Mon, 15 Nov 2010 01:04:55 +0000 (02:04 +0100)
arduino/MIDI_synth/MIDI_synth.pde [new file with mode: 0644]
c/ghetto-sound-system/midi-rx.c

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);
+  
+  }
+}
index 7ee730e83a90c646609badb0e14b367e39670183..0ce32d75a7f05f2fae371729b9f1371aa18f24f1 100644 (file)
@@ -11,7 +11,8 @@
 #define MIDI_NOTE_ON   144
 #define MIDI_CONTROL   176
 #define MIDI_PITCH_BEND        224
-#define MIDI_FORMAT    "%d:%d:%d\n"
+#define MIDI_FORMAT    "%c%c%c"
+#define MIDI_FORMAT_ERR        "%d:%d:%d\n"
 
 snd_seq_t *open_seq();
 void midi_action(snd_seq_t *seq_handle);
@@ -55,6 +56,7 @@ void midi_action(snd_seq_t *seq_handle) {
         printf(MIDI_FORMAT, MIDI_NOTE_OFF, ev->data.control.channel, ev->data.note.note);
         break;
     }
+               fflush(stdout); fflush(stderr);
     snd_seq_free_event(ev);
   } while (snd_seq_event_input_pending(seq_handle, 0) > 0);
 }
This page took 0.16705 seconds and 4 git commands to generate.