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

Dependencies:   mbed USBDevice PinDetect

Committer:
ndelfino
Date:
Mon Apr 25 03:23:43 2016 +0000
Revision:
21:8b5d753b6bf5
Parent:
20:bf675ba2c454
Child:
22:b800e1766647
Updated comments. Added more constants for code clarity about what some of the values are.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ndelfino 3:8f80e267a80d 1 #include "mbed.h"
ndelfino 8:deaedb59243e 2 #include "LEDController.h"
ndelfino 3:8f80e267a80d 3
ndelfino 21:8b5d753b6bf5 4 //Pins that output values to the brick LED
ndelfino 17:55e6132c54a8 5 DigitalOut brickled1(p5);
ndelfino 17:55e6132c54a8 6 DigitalOut brickled2(p6);
ndelfino 17:55e6132c54a8 7 DigitalOut brickled3(p7);
ndelfino 17:55e6132c54a8 8 DigitalOut brickled4(p8);
ndelfino 3:8f80e267a80d 9
ndelfino 21:8b5d753b6bf5 10 //Pins that output values to the tri-color LEDS for cLED1
ndelfino 17:55e6132c54a8 11 DigitalOut myled1_1(p9);
ndelfino 17:55e6132c54a8 12 DigitalOut myled1_2(p10);
ndelfino 17:55e6132c54a8 13 DigitalOut myled1_3(p11);
ndelfino 4:bb9f50305401 14
ndelfino 21:8b5d753b6bf5 15 //Pins that output values to the tri-color LEDS for LED2
ndelfino 17:55e6132c54a8 16 DigitalOut myled2_1(p12);
ndelfino 17:55e6132c54a8 17 DigitalOut myled2_2(p13);
ndelfino 17:55e6132c54a8 18 DigitalOut myled2_3(p14);
ndelfino 4:bb9f50305401 19
ndelfino 21:8b5d753b6bf5 20 //Pins that output values to the tri-color LEDS for LED3
ndelfino 17:55e6132c54a8 21 DigitalOut myled3_1(p15);
ndelfino 17:55e6132c54a8 22 DigitalOut myled3_2(p16);
ndelfino 17:55e6132c54a8 23 DigitalOut myled3_3(p17);
ndelfino 17:55e6132c54a8 24
ndelfino 21:8b5d753b6bf5 25 //Pins that output values to the tri-color LEDS for LED4
ndelfino 17:55e6132c54a8 26 DigitalOut myled4_1(p18);
ndelfino 17:55e6132c54a8 27 DigitalOut myled4_2(p19);
ndelfino 17:55e6132c54a8 28 DigitalOut myled4_3(p20);
ndelfino 3:8f80e267a80d 29
ndelfino 8:deaedb59243e 30 void identifyKeyForLed(int key, int type);
ndelfino 11:f65806ee5833 31 void chooseLedForKey(int colors[3], int type, int signature, int key);
ndelfino 11:f65806ee5833 32 void setLedToKey(int colors[3], int type, int signature, int led, int key);
ndelfino 4:bb9f50305401 33
ndelfino 21:8b5d753b6bf5 34 //These are the color assignments for the keys. In order, C, C#, D, D#, E,F, F#, G, G#, A, A#, B
ndelfino 17:55e6132c54a8 35 int colors[12][3] = {{1,0,1},{1,0,1},{1,0,0},{1,0,0},{0,1,0},{0,0,1},{0,0,1},{1,1,0},{1,1,0},{0,1,1},{0,1,1},{1,1,1}};
ndelfino 4:bb9f50305401 36
ndelfino 21:8b5d753b6bf5 37 //This holds the current key of the LED. If no key is in the LED then the value for that slot is -1.
ndelfino 11:f65806ee5833 38 int ledKeys[4] = {-1,-1,-1,-1};
ndelfino 5:a687cfa7be62 39
ndelfino 21:8b5d753b6bf5 40 const int SHARP = 1;
ndelfino 21:8b5d753b6bf5 41 const int NOT_SHARP = 0;
ndelfino 21:8b5d753b6bf5 42
ndelfino 21:8b5d753b6bf5 43 const int cLED1 = 1;
ndelfino 21:8b5d753b6bf5 44 const int cLED2 = 2;
ndelfino 21:8b5d753b6bf5 45 const int cLED3 = 3;
ndelfino 21:8b5d753b6bf5 46 const int cLED4 = 4;
ndelfino 17:55e6132c54a8 47
ndelfino 21:8b5d753b6bf5 48 const int cSharp = 1;
ndelfino 21:8b5d753b6bf5 49 const int dSharp = 3;
ndelfino 21:8b5d753b6bf5 50 const int fSharp = 6;
ndelfino 21:8b5d753b6bf5 51 const int gSharp = 8;
ndelfino 21:8b5d753b6bf5 52 const int aSharp = 10;
ndelfino 21:8b5d753b6bf5 53
ndelfino 21:8b5d753b6bf5 54 //Identifies the key.
ndelfino 21:8b5d753b6bf5 55 void LEDController::identifyKeyForLed(int key, int type){
ndelfino 21:8b5d753b6bf5 56
ndelfino 21:8b5d753b6bf5 57 //Performs modulus to determine key. This also determines color and signature.
ndelfino 21:8b5d753b6bf5 58 int determineColorSignature = key%12;
ndelfino 21:8b5d753b6bf5 59
ndelfino 21:8b5d753b6bf5 60 //If modulus operation is any of these values then it means the key is sharp.
ndelfino 21:8b5d753b6bf5 61 if(determineColorSignature == cSharp || determineColorSignature == dSharp || determineColorSignature == fSharp
ndelfino 21:8b5d753b6bf5 62 || determineColorSignature == gSharp || determineColorSignature == aSharp){
ndelfino 21:8b5d753b6bf5 63 chooseLedForKey(colors[determineColorSignature], type, SHARP, key);
ndelfino 17:55e6132c54a8 64 }else{
ndelfino 21:8b5d753b6bf5 65 chooseLedForKey(colors[determineColorSignature], type, NOT_SHARP, key);
ndelfino 17:55e6132c54a8 66 }
ndelfino 4:bb9f50305401 67 }
ndelfino 4:bb9f50305401 68
ndelfino 21:8b5d753b6bf5 69 //Chooses which LED needs to perform an action. Either turning on a light to the specific note
ndelfino 21:8b5d753b6bf5 70 //color or turning off the LED when the key is no longer active.
ndelfino 11:f65806ee5833 71 void LEDController::chooseLedForKey(int colors[3], int type, int signature, int key){
ndelfino 4:bb9f50305401 72
ndelfino 4:bb9f50305401 73 //Determines if the LED is not set and therefore can be used.
ndelfino 21:8b5d753b6bf5 74 //Or if it is in use by the same key being passed in this means the led needs to be turned off.
ndelfino 21:8b5d753b6bf5 75 if(ledKeys[0] == -1 || ledKeys[0] == key){
ndelfino 21:8b5d753b6bf5 76 setLedToKey(colors, type, signature, cLED1, key);
ndelfino 21:8b5d753b6bf5 77 }else if(ledKeys[1] == -1 || ledKeys[1] == key){
ndelfino 21:8b5d753b6bf5 78 setLedToKey(colors, type, signature, cLED2, key);
ndelfino 21:8b5d753b6bf5 79 }else if(ledKeys[2] == -1 || ledKeys[2] == key){
ndelfino 21:8b5d753b6bf5 80 setLedToKey(colors, type, signature, cLED3, key);
ndelfino 21:8b5d753b6bf5 81 }else if(ledKeys[3] == -1 || ledKeys[3] == key){
ndelfino 21:8b5d753b6bf5 82 setLedToKey(colors, type, signature, cLED4, key);
asuszek 20:bf675ba2c454 83 }
ndelfino 4:bb9f50305401 84 }
ndelfino 4:bb9f50305401 85
ndelfino 21:8b5d753b6bf5 86 //This sets the LED to the key or clears the previously set LED of that specific key if the type is off.
ndelfino 11:f65806ee5833 87 void LEDController::setLedToKey(int colors[3], int type, int signature, int led, int key){
ndelfino 21:8b5d753b6bf5 88 if(led == cLED1){
ndelfino 8:deaedb59243e 89 if(type == 1){
ndelfino 11:f65806ee5833 90 ledKeys[0] = key;
ndelfino 11:f65806ee5833 91 myled1_1 = colors[0];
ndelfino 11:f65806ee5833 92 myled1_2 = colors[1];
ndelfino 11:f65806ee5833 93 myled1_3 = colors[2];
ndelfino 21:8b5d753b6bf5 94 if(signature == SHARP){
ndelfino 17:55e6132c54a8 95 brickled1 = 1;
ndelfino 17:55e6132c54a8 96 }
ndelfino 8:deaedb59243e 97 }else{
ndelfino 11:f65806ee5833 98 ledKeys[0] = -1;
ndelfino 8:deaedb59243e 99 myled1_1 = 0;
ndelfino 8:deaedb59243e 100 myled1_2 = 0;
ndelfino 8:deaedb59243e 101 myled1_3 = 0;
ndelfino 17:55e6132c54a8 102 brickled1 = 0;
ndelfino 8:deaedb59243e 103 }
ndelfino 21:8b5d753b6bf5 104 }else if(led == cLED2){
ndelfino 8:deaedb59243e 105 if(type == 1){
ndelfino 11:f65806ee5833 106 ledKeys[1] = key;
ndelfino 11:f65806ee5833 107 myled2_1 = colors[0];
ndelfino 11:f65806ee5833 108 myled2_2 = colors[1];
ndelfino 11:f65806ee5833 109 myled2_3 = colors[2];
ndelfino 21:8b5d753b6bf5 110 if(signature == SHARP){
ndelfino 17:55e6132c54a8 111 brickled2 = 1;
ndelfino 17:55e6132c54a8 112 }
ndelfino 4:bb9f50305401 113 }else{
ndelfino 11:f65806ee5833 114 ledKeys[1] = -1;
ndelfino 8:deaedb59243e 115 myled2_1 = 0;
ndelfino 8:deaedb59243e 116 myled2_2 = 0;
ndelfino 8:deaedb59243e 117 myled2_3 = 0;
ndelfino 17:55e6132c54a8 118 brickled2 = 0;
ndelfino 4:bb9f50305401 119 }
ndelfino 21:8b5d753b6bf5 120 }else if(led == cLED3){
ndelfino 8:deaedb59243e 121 if(type == 1){
ndelfino 11:f65806ee5833 122 ledKeys[2] = key;
ndelfino 11:f65806ee5833 123 myled3_1 = colors[0];
ndelfino 11:f65806ee5833 124 myled3_2 = colors[1];
ndelfino 11:f65806ee5833 125 myled3_3 = colors[2];
ndelfino 21:8b5d753b6bf5 126 if(signature == SHARP){
ndelfino 17:55e6132c54a8 127 brickled3 = 1;
ndelfino 17:55e6132c54a8 128 }
ndelfino 8:deaedb59243e 129 }else{
ndelfino 11:f65806ee5833 130 ledKeys[2] = -1;
ndelfino 8:deaedb59243e 131 myled3_1 = 0;
ndelfino 8:deaedb59243e 132 myled3_2 = 0;
ndelfino 8:deaedb59243e 133 myled3_3 = 0;
ndelfino 17:55e6132c54a8 134 brickled3 = 0;
ndelfino 8:deaedb59243e 135 }
ndelfino 21:8b5d753b6bf5 136 }else if(led == cLED4){
ndelfino 8:deaedb59243e 137 if(type == 1){
ndelfino 11:f65806ee5833 138 ledKeys[3] = key;
ndelfino 11:f65806ee5833 139 myled4_1 = colors[0];;
ndelfino 11:f65806ee5833 140 myled4_2 = colors[1];
ndelfino 11:f65806ee5833 141 myled4_3 = colors[2];
ndelfino 21:8b5d753b6bf5 142 if(signature == SHARP){
ndelfino 17:55e6132c54a8 143 brickled4 = 1;
ndelfino 17:55e6132c54a8 144 }
ndelfino 8:deaedb59243e 145 }else{
ndelfino 11:f65806ee5833 146 ledKeys[3] = -1;
ndelfino 8:deaedb59243e 147 myled4_1 = 0;
ndelfino 8:deaedb59243e 148 myled4_2 = 0;
ndelfino 8:deaedb59243e 149 myled4_3 = 0;
ndelfino 17:55e6132c54a8 150 brickled4 = 0;
ndelfino 8:deaedb59243e 151 }
ndelfino 8:deaedb59243e 152 }
ndelfino 8:deaedb59243e 153
ndelfino 4:bb9f50305401 154 }