Austin Suszek / Mbed 2 deprecated MIDISynthesizer

Dependencies:   mbed USBDevice PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Karplus Strong MIDI synthesizer.
00002  *
00003  * @author Austin Suszek
00004  * @author Nick Delfino
00005  */
00006 
00007 #include "Audio/AudioEngine.h"
00008 #include "Constants.h"
00009 #include "mbed.h"
00010 #include "PinDetect.h"
00011 #include "USBMIDI.h"
00012 #include "LEDController.h"
00013 
00014 Serial pc(USBTX, USBRX);
00015 
00016 USBMIDI midi;
00017 AudioEngine audioEngine;
00018 LEDController ledController;
00019 
00020 // Initialize the LEDs on the controller.
00021 DigitalOut led1(LED1);
00022 DigitalOut led2(LED2);
00023 DigitalOut led3(LED3);
00024 DigitalOut led4(LED4);
00025 // Reuse the binary clock from the homework.
00026 extern "C" int my_leds(int value);
00027 
00028 // Buttons for the toggling of synths.
00029 PinDetect nextSynth(p27);
00030 PinDetect prevSynth(p28);
00031 
00032 void midiMessageReceived(MIDIMessage message);
00033 
00034 void nextSynthPressed();
00035 void prevSynthPressed();
00036 
00037 int main() { 
00038     // Attach the function to handle all incoming MIDI events.
00039     midi.attach(midiMessageReceived); 
00040     
00041     // Attach the synth toggling buttons.
00042     nextSynth.attach_asserted(&nextSynthPressed);
00043     prevSynth.attach_asserted(&prevSynthPressed);
00044     nextSynth.setSampleFrequency();
00045     prevSynth.setSampleFrequency();
00046     
00047     // Start the index counter at number 1.
00048     my_leds(1);
00049         
00050     while (1) {}
00051 }
00052 
00053 /*
00054  * The handler for all incoming MIDI messages.
00055  */
00056 void midiMessageReceived(MIDIMessage message) {
00057     #ifdef DEBUG
00058     pc.printf("Message Received\r\n");
00059     #endif
00060     
00061     int key;
00062     int velocity;
00063     
00064     switch (message.type()) {
00065         case MIDIMessage::NoteOnType:
00066             key = message.key();
00067             velocity = message.velocity();
00068             
00069             audioEngine.midiNoteOn(key, velocity);
00070             
00071             ledController.identifyKeyForLed(key, 1);
00072         
00073             #ifdef DEBUG
00074             pc.printf("\tType: Note On\r\n");
00075             pc.printf("\tNote: %d\tVelocity: %d\r\n", key, velocity);
00076             #endif
00077             break;
00078         case MIDIMessage::NoteOffType:
00079             key = message.key();
00080             velocity = message.velocity();
00081             
00082             audioEngine.midiNoteOff(key);
00083             
00084             ledController.identifyKeyForLed(key, -1);
00085         
00086             #ifdef DEBUG
00087             pc.printf("\tType: Note Off\r\n");
00088             pc.printf("\tNote: %d\r\n", key);
00089             #endif
00090             break;
00091         default:
00092             // no-op
00093             break;
00094     }
00095 }
00096 
00097 void nextSynthPressed() {
00098     #ifdef DEBUG
00099     pc.printf("Toggling to next synthesizer.r\n");
00100     #endif
00101    
00102     int nextIndex = audioEngine.nextSynth(1);
00103     my_leds(nextIndex + 1);
00104 }
00105 
00106 void prevSynthPressed() {
00107     #ifdef DEBUG
00108     pc.printf("Toggling to previous synthesizer.r\n");
00109     #endif
00110     
00111     int nextIndex = audioEngine.nextSynth(-1);
00112     my_leds(nextIndex + 1);
00113 }
00114