A MIDI piano synthesizer that implements the Karplus Strong physical modeling algorithm.
Dependencies: mbed USBDevice PinDetect
main.cpp@22:b800e1766647, 2016-04-25 (annotated)
- Committer:
- asuszek
- Date:
- Mon Apr 25 23:58:15 2016 +0000
- Revision:
- 22:b800e1766647
- Parent:
- 18:26d93c5b9bb6
Fixed LED bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
asuszek | 2:e21bd39bdf46 | 1 | /* Karplus Strong MIDI synthesizer. |
asuszek | 2:e21bd39bdf46 | 2 | * |
asuszek | 2:e21bd39bdf46 | 3 | * @author Austin Suszek |
asuszek | 2:e21bd39bdf46 | 4 | * @author Nick Delfino |
asuszek | 2:e21bd39bdf46 | 5 | */ |
asuszek | 0:cad8dafb22be | 6 | |
asuszek | 9:1e012f67470c | 7 | #include "Audio/AudioEngine.h" |
asuszek | 2:e21bd39bdf46 | 8 | #include "Constants.h" |
asuszek | 0:cad8dafb22be | 9 | #include "mbed.h" |
asuszek | 6:688698f814c0 | 10 | #include "PinDetect.h" |
asuszek | 0:cad8dafb22be | 11 | #include "USBMIDI.h" |
ndelfino | 8:deaedb59243e | 12 | #include "LEDController.h" |
asuszek | 0:cad8dafb22be | 13 | |
asuszek | 0:cad8dafb22be | 14 | Serial pc(USBTX, USBRX); |
asuszek | 6:688698f814c0 | 15 | |
asuszek | 0:cad8dafb22be | 16 | USBMIDI midi; |
asuszek | 6:688698f814c0 | 17 | AudioEngine audioEngine; |
ndelfino | 8:deaedb59243e | 18 | LEDController ledController; |
ndelfino | 8:deaedb59243e | 19 | |
asuszek | 18:26d93c5b9bb6 | 20 | // Initialize the LEDs on the controller. |
asuszek | 18:26d93c5b9bb6 | 21 | DigitalOut led1(LED1); |
asuszek | 18:26d93c5b9bb6 | 22 | DigitalOut led2(LED2); |
asuszek | 18:26d93c5b9bb6 | 23 | DigitalOut led3(LED3); |
asuszek | 18:26d93c5b9bb6 | 24 | DigitalOut led4(LED4); |
asuszek | 18:26d93c5b9bb6 | 25 | // Reuse the binary clock from the homework. |
asuszek | 18:26d93c5b9bb6 | 26 | extern "C" int my_leds(int value); |
asuszek | 6:688698f814c0 | 27 | |
asuszek | 6:688698f814c0 | 28 | // Buttons for the toggling of synths. |
asuszek | 6:688698f814c0 | 29 | PinDetect nextSynth(p27); |
asuszek | 6:688698f814c0 | 30 | PinDetect prevSynth(p28); |
asuszek | 0:cad8dafb22be | 31 | |
asuszek | 2:e21bd39bdf46 | 32 | void midiMessageReceived(MIDIMessage message); |
asuszek | 2:e21bd39bdf46 | 33 | |
asuszek | 6:688698f814c0 | 34 | void nextSynthPressed(); |
asuszek | 6:688698f814c0 | 35 | void prevSynthPressed(); |
asuszek | 6:688698f814c0 | 36 | |
asuszek | 2:e21bd39bdf46 | 37 | int main() { |
asuszek | 2:e21bd39bdf46 | 38 | // Attach the function to handle all incoming MIDI events. |
asuszek | 2:e21bd39bdf46 | 39 | midi.attach(midiMessageReceived); |
asuszek | 6:688698f814c0 | 40 | |
asuszek | 6:688698f814c0 | 41 | // Attach the synth toggling buttons. |
asuszek | 6:688698f814c0 | 42 | nextSynth.attach_asserted(&nextSynthPressed); |
asuszek | 6:688698f814c0 | 43 | prevSynth.attach_asserted(&prevSynthPressed); |
asuszek | 6:688698f814c0 | 44 | nextSynth.setSampleFrequency(); |
asuszek | 6:688698f814c0 | 45 | prevSynth.setSampleFrequency(); |
asuszek | 18:26d93c5b9bb6 | 46 | |
asuszek | 18:26d93c5b9bb6 | 47 | // Start the index counter at number 1. |
asuszek | 18:26d93c5b9bb6 | 48 | my_leds(1); |
asuszek | 2:e21bd39bdf46 | 49 | |
asuszek | 2:e21bd39bdf46 | 50 | while (1) {} |
asuszek | 2:e21bd39bdf46 | 51 | } |
asuszek | 2:e21bd39bdf46 | 52 | |
asuszek | 2:e21bd39bdf46 | 53 | /* |
asuszek | 2:e21bd39bdf46 | 54 | * The handler for all incoming MIDI messages. |
asuszek | 2:e21bd39bdf46 | 55 | */ |
asuszek | 0:cad8dafb22be | 56 | void midiMessageReceived(MIDIMessage message) { |
asuszek | 2:e21bd39bdf46 | 57 | #ifdef DEBUG |
asuszek | 6:688698f814c0 | 58 | pc.printf("Message Received\r\n"); |
asuszek | 2:e21bd39bdf46 | 59 | #endif |
asuszek | 0:cad8dafb22be | 60 | |
asuszek | 6:688698f814c0 | 61 | int key; |
asuszek | 6:688698f814c0 | 62 | int velocity; |
asuszek | 6:688698f814c0 | 63 | |
asuszek | 0:cad8dafb22be | 64 | switch (message.type()) { |
asuszek | 0:cad8dafb22be | 65 | case MIDIMessage::NoteOnType: |
asuszek | 6:688698f814c0 | 66 | key = message.key(); |
asuszek | 6:688698f814c0 | 67 | velocity = message.velocity(); |
asuszek | 6:688698f814c0 | 68 | |
asuszek | 6:688698f814c0 | 69 | audioEngine.midiNoteOn(key, velocity); |
ndelfino | 8:deaedb59243e | 70 | |
ndelfino | 8:deaedb59243e | 71 | ledController.identifyKeyForLed(key, 1); |
asuszek | 6:688698f814c0 | 72 | |
asuszek | 2:e21bd39bdf46 | 73 | #ifdef DEBUG |
asuszek | 6:688698f814c0 | 74 | pc.printf("\tType: Note On\r\n"); |
asuszek | 6:688698f814c0 | 75 | pc.printf("\tNote: %d\tVelocity: %d\r\n", key, velocity); |
asuszek | 2:e21bd39bdf46 | 76 | #endif |
asuszek | 0:cad8dafb22be | 77 | break; |
asuszek | 0:cad8dafb22be | 78 | case MIDIMessage::NoteOffType: |
asuszek | 6:688698f814c0 | 79 | key = message.key(); |
asuszek | 6:688698f814c0 | 80 | velocity = message.velocity(); |
asuszek | 6:688698f814c0 | 81 | |
asuszek | 6:688698f814c0 | 82 | audioEngine.midiNoteOff(key); |
ndelfino | 8:deaedb59243e | 83 | |
ndelfino | 8:deaedb59243e | 84 | ledController.identifyKeyForLed(key, -1); |
asuszek | 6:688698f814c0 | 85 | |
asuszek | 2:e21bd39bdf46 | 86 | #ifdef DEBUG |
asuszek | 6:688698f814c0 | 87 | pc.printf("\tType: Note Off\r\n"); |
asuszek | 6:688698f814c0 | 88 | pc.printf("\tNote: %d\r\n", key); |
asuszek | 2:e21bd39bdf46 | 89 | #endif |
asuszek | 0:cad8dafb22be | 90 | break; |
asuszek | 0:cad8dafb22be | 91 | default: |
asuszek | 2:e21bd39bdf46 | 92 | // no-op |
asuszek | 2:e21bd39bdf46 | 93 | break; |
asuszek | 0:cad8dafb22be | 94 | } |
asuszek | 0:cad8dafb22be | 95 | } |
asuszek | 0:cad8dafb22be | 96 | |
asuszek | 6:688698f814c0 | 97 | void nextSynthPressed() { |
asuszek | 6:688698f814c0 | 98 | #ifdef DEBUG |
asuszek | 6:688698f814c0 | 99 | pc.printf("Toggling to next synthesizer.r\n"); |
asuszek | 6:688698f814c0 | 100 | #endif |
asuszek | 6:688698f814c0 | 101 | |
asuszek | 18:26d93c5b9bb6 | 102 | int nextIndex = audioEngine.nextSynth(1); |
asuszek | 18:26d93c5b9bb6 | 103 | my_leds(nextIndex + 1); |
asuszek | 6:688698f814c0 | 104 | } |
asuszek | 6:688698f814c0 | 105 | |
asuszek | 6:688698f814c0 | 106 | void prevSynthPressed() { |
asuszek | 6:688698f814c0 | 107 | #ifdef DEBUG |
asuszek | 6:688698f814c0 | 108 | pc.printf("Toggling to previous synthesizer.r\n"); |
asuszek | 6:688698f814c0 | 109 | #endif |
asuszek | 6:688698f814c0 | 110 | |
asuszek | 18:26d93c5b9bb6 | 111 | int nextIndex = audioEngine.nextSynth(-1); |
asuszek | 18:26d93c5b9bb6 | 112 | my_leds(nextIndex + 1); |
asuszek | 6:688698f814c0 | 113 | } |
asuszek | 6:688698f814c0 | 114 |