Simple monophonic synthesizer. - USB MIDI function PWM出力を使用した簡単なモノフォニックシンセです。 USB MIDI Functionとして動作します。 {{http://www.youtube.com/watch?v=UWonXdsNGkE}} {{https://lh4.googleusercontent.com/-LHPkdbs5pdw/TxA07cSVUmI/AAAAAAAACVY/2Bia4nz1ptI/s476/mbedDeMonoSynth.png}}

Dependencies:   USBMIDI mbed

Revision:
0:9c32c9c8ead4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Dec 29 12:44:19 2011 +0000
@@ -0,0 +1,59 @@
+/*
+ * Simple monophonic synthesizer
+ * auther: alaif.
+ */
+#include "mbed.h"
+#include "USBMIDI.h"
+
+/* MIDI note no -> FREQ */
+#include "math.h"
+#define NOTE2FREQ(note) (440 * pow(2, ((note - 69) / 12.0f)))
+
+/* HARDWARE Config */
+PwmOut sp1(p21);
+DigitalOut led_ready(LED1);
+DigitalOut led_noteon(LED2);
+
+//NOTE ON event
+void note_on(int note, int vel)
+{
+    if (vel != 0) {
+        sp1.period(1.0f / NOTE2FREQ(note));         //Set freq
+        sp1.write(0.5f);                            //SQUARE WAVE ON
+        led_noteon = 1;
+    } else {
+        sp1.write(0.0f);                            //SQUARE WAVE OFF
+        led_noteon = 0;
+    }
+}
+
+//NOTE OFF event
+void note_off()
+{
+    sp1.write(0.0f);
+    led_noteon = 0;
+}
+
+//Recv midi message handler.
+void do_message(MIDIMessage msg)
+{
+    int key = msg.key();
+    switch (msg.type()) {
+        case MIDIMessage::NoteOnType:
+            note_on(key, msg.velocity());
+            break;
+        case MIDIMessage::NoteOffType:
+            note_off();
+            break;
+    }
+}
+
+/* Main */
+USBMIDI midi;
+int main() {
+    led_ready = 0;          
+    midi.attach(do_message);    // call back for messages received    
+    led_ready = 1;
+    
+    while (1) {}
+}