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

Dependencies:   mbed USBDevice PinDetect

main.cpp

Committer:
asuszek
Date:
2016-04-12
Revision:
6:688698f814c0
Parent:
2:e21bd39bdf46
Child:
8:deaedb59243e
Child:
9:1e012f67470c

File content as of revision 6:688698f814c0:

/* Karplus Strong MIDI synthesizer.
 *
 * @author Austin Suszek
 * @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) {}
}

/*
 * The handler for all incoming MIDI messages.
 */
void midiMessageReceived(MIDIMessage message) {
    #ifdef DEBUG
    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
            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
            pc.printf("\tType: Note Off\r\n");
            pc.printf("\tNote: %d\r\n", key);
            #endif
            break;
        default:
            // no-op
            break;
    }
}

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);
}