Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed USBDevice PinDetect
main.cpp
- Committer:
- ndelfino
- Date:
- 2016-04-13
- Revision:
- 8:deaedb59243e
- Parent:
- 6:688698f814c0
- Child:
- 10:b9e14412cc23
File content as of revision 8:deaedb59243e:
/* 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"
#include "LEDController.h"
Serial pc(USBTX, USBRX);
USBMIDI midi;
AudioEngine audioEngine;
LEDController ledController;
// 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);
ledController.identifyKeyForLed(key, 1);
#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);
ledController.identifyKeyForLed(key, -1);
#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);
}
