A MIDI piano synthesizer that implements the Karplus Strong physical modeling algorithm.

Dependencies:   mbed USBDevice PinDetect

Revision:
6:688698f814c0
Parent:
2:e21bd39bdf46
Child:
8:deaedb59243e
Child:
9:1e012f67470c
--- a/main.cpp	Sun Apr 10 22:06:25 2016 +0000
+++ b/main.cpp	Tue Apr 12 20:37:08 2016 +0000
@@ -4,18 +4,35 @@
  * @author Nick Delfino
  */
 
+#include "AudioEngine.h"
 #include "Constants.h"
 #include "mbed.h"
+#include "PinDetect.h"
 #include "USBMIDI.h"
 
 Serial pc(USBTX, USBRX);
+
 USBMIDI midi;
+AudioEngine audioEngine;
+
+// Buttons for the toggling of synths.
+PinDetect nextSynth(p27);
+PinDetect prevSynth(p28);
 
 void midiMessageReceived(MIDIMessage message);
 
+void nextSynthPressed();
+void prevSynthPressed();
+
 int main() { 
     // Attach the function to handle all incoming MIDI events.
     midi.attach(midiMessageReceived); 
+    
+    // Attach the synth toggling buttons.
+    nextSynth.attach_asserted(&nextSynthPressed);
+    prevSynth.attach_asserted(&prevSynthPressed);
+    nextSynth.setSampleFrequency();
+    prevSynth.setSampleFrequency();
         
     while (1) {}
 }
@@ -25,19 +42,33 @@
  */
 void midiMessageReceived(MIDIMessage message) {
     #ifdef DEBUG
-    printf("Message Received\r\n");
+    pc.printf("Message Received\r\n");
     #endif
     
+    int key;
+    int velocity;
+    
     switch (message.type()) {
         case MIDIMessage::NoteOnType:
+            key = message.key();
+            velocity = message.velocity();
+            
+            audioEngine.midiNoteOn(key, velocity);
+        
             #ifdef DEBUG
-            printf("\tType: Note On\r\n");
-            printf("\tNote: %d\r\n", message.key());
+            pc.printf("\tType: Note On\r\n");
+            pc.printf("\tNote: %d\tVelocity: %d\r\n", key, velocity);
             #endif
             break;
         case MIDIMessage::NoteOffType:
+            key = message.key();
+            velocity = message.velocity();
+            
+            audioEngine.midiNoteOff(key);
+        
             #ifdef DEBUG
-            printf("\tType: Note Off\r\n");
+            pc.printf("\tType: Note Off\r\n");
+            pc.printf("\tNote: %d\r\n", key);
             #endif
             break;
         default:
@@ -46,3 +77,19 @@
     }
 }
 
+void nextSynthPressed() {
+    #ifdef DEBUG
+    pc.printf("Toggling to next synthesizer.r\n");
+    #endif
+   
+    audioEngine.nextSynth(1);
+}
+
+void prevSynthPressed() {
+    #ifdef DEBUG
+    pc.printf("Toggling to previous synthesizer.r\n");
+    #endif
+    
+    audioEngine.nextSynth(-1);
+}
+