Bluetooth Enabled Keyboard/Synthesizer for mbed

Dependencies:   mbed 4DGL-uLCD-SE SDFileSystem mbed-rtos

Committer:
jmpin
Date:
Fri Apr 29 22:37:02 2016 +0000
Revision:
15:8ff317cc5d2c
Parent:
14:40f57385c404
Child:
16:e01a77428828
Fixed uLCD thread so that text works properly.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmpin 0:48311ffdfa96 1 #include "mbed.h"
jmpin 3:3aba1d783730 2 #include "SDFileSystem.h"
jmpin 6:68c6a50e1437 3 #include "rtos.h"
jmpin 6:68c6a50e1437 4 #include <vector>
jmpin 6:68c6a50e1437 5 #include "uLCD_4DGL.h"
jmpin 3:3aba1d783730 6 #include "synthesizer.h"
Jake867 11:c87f55a3b9e0 7 RawSerial Blue(p13,p14);
Jake867 11:c87f55a3b9e0 8 RawSerial PC(USBTX,USBRX);
jmpin 0:48311ffdfa96 9 DigitalOut myled(LED1);
jmpin 0:48311ffdfa96 10 DigitalOut myled4(LED4);
jmpin 3:3aba1d783730 11
jmpin 3:3aba1d783730 12 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
jmpin 3:3aba1d783730 13
Jake867 11:c87f55a3b9e0 14 //Mutex mtx; //Mutex lock
jmpin 6:68c6a50e1437 15
Jake867 11:c87f55a3b9e0 16 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
jmpin 6:68c6a50e1437 17
jmpin 8:f6699fd30737 18 AnalogOut synthPin(p18); // p18 is the pin that will have the output voltages on it
jmpin 8:f6699fd30737 19
jmpin 6:68c6a50e1437 20
jmpin 0:48311ffdfa96 21 //global variables for main and interrupt routine
Jake867 11:c87f55a3b9e0 22 volatile bool readyFlag = false;
jmpin 0:48311ffdfa96 23 volatile char keyPress;
jmpin 3:3aba1d783730 24 WaveType myWave = sine; // default to sine wave
jmpin 3:3aba1d783730 25 volatile int currentOctave = 4; // default to 4 because thats where middle C is
jmpin 3:3aba1d783730 26 volatile int currentAttackVal = 3; // values will range from 1-5, default to 3
jmpin 6:68c6a50e1437 27 volatile int currentDecayVal = 3; // values will range from 1-5, default to 3
jmpin 3:3aba1d783730 28 volatile int currentSustainVal = 3; // values will range from 1-5, default to 3
jmpin 3:3aba1d783730 29 volatile int currentReleaseVal = 3; // values will range from 1-5, default to 3
jmpin 9:e4df1a31a098 30 double *currentAttackTable; // pointer to the correct attack coefficient table
jmpin 9:e4df1a31a098 31 double *currentDecayTable; // pointer to the correct decay coefficient table
jmpin 9:e4df1a31a098 32 double *currentSustainTable; // pointer to the correct sustain coefficient table
jmpin 9:e4df1a31a098 33 double *currentReleaseTable; // pointer to the correct release coefficient table
jmpin 6:68c6a50e1437 34 vector<double> sampleBuffer; // vector to hold samples of generated waveform
jmpin 6:68c6a50e1437 35 int num_samples = 256; // number of samples
jmpin 6:68c6a50e1437 36 volatile int noteFreq; // the current frequency of the note being played
jmpin 8:f6699fd30737 37 double timeIncrement = (2/256); // 2 seconds with 256 samples
jmpin 5:afd67e985df0 38
jmpin 5:afd67e985df0 39 /* Coefficient Matrices Corresponding to Different Attack Values
jmpin 5:afd67e985df0 40 each matrix is comprised of 32 elements (256/8). The first matrix corresponds
jmpin 12:d60a9d0052a7 41 to an attack value of 5.
jmpin 5:afd67e985df0 42 */
jmpin 5:afd67e985df0 43
jmpin 6:68c6a50e1437 44 double attackVals5[32] = { //Approaches the maximum amplitude the quickest - corresponds to an attackValue of 5
jmpin 5:afd67e985df0 45 0, 0.275 , 0.55 , 0.7 ,
jmpin 5:afd67e985df0 46 0.8 , 0.85 , 0.9 , 0.91 ,
jmpin 5:afd67e985df0 47 0.92 , 0.93 , 0.939 , 0.948 ,
jmpin 5:afd67e985df0 48 0.956 , 0.963 , 0.969 , 0.974 ,
jmpin 5:afd67e985df0 49 0.978 , 0.982 , 0.986 , 0.989 ,
jmpin 5:afd67e985df0 50 0.991 , 0.992 , 0.993 , 0.994 ,
jmpin 5:afd67e985df0 51 0.995 , 0.996 , 0.997 , 0.998 ,
jmpin 5:afd67e985df0 52 0.9985 , 0.999 , 0.9995 , 1
jmpin 5:afd67e985df0 53 };
jmpin 6:68c6a50e1437 54 double attackVals4[32] = { //Corresponds to an attackValue of 4
jmpin 5:afd67e985df0 55 0 , 0.18 , 0.38 , 0.58 ,
jmpin 5:afd67e985df0 56 0.66 , 0.69 , 0.72 , 0.74 ,
jmpin 5:afd67e985df0 57 0.76 , 0.78 , 0.795 , 0.81 ,
jmpin 5:afd67e985df0 58 0.825 , 0.84 , 0.85 , 0.86 ,
jmpin 5:afd67e985df0 59 0.87 , 0.88 , 0.89 , 0.9 ,
jmpin 5:afd67e985df0 60 0.91 , 0.92 , 0.93 , 0.94 ,
jmpin 5:afd67e985df0 61 0.95 , 0.96 , 0.97 , 0.98 ,
jmpin 5:afd67e985df0 62 0.985 , 0.99 , 0.995 , 1
jmpin 5:afd67e985df0 63 };
jmpin 6:68c6a50e1437 64 double attackVals3[32] = { //Corresponds to an attackValue of 3
jmpin 5:afd67e985df0 65 0 , 0.09 , 0.18 , 0.27 ,
jmpin 5:afd67e985df0 66 0.35 , 0.43 , 0.5 , 0.57 ,
jmpin 5:afd67e985df0 67 0.61 , 0.65 , 0.68 , 0.71 ,
jmpin 5:afd67e985df0 68 0.74 , 0.76 , 0.78 , 0.8 ,
jmpin 5:afd67e985df0 69 0.82 , 0.84 , 0.86 , 0.88 ,
jmpin 5:afd67e985df0 70 0.895 , 0.91 , 0.925 , 0.94 ,
jmpin 5:afd67e985df0 71 0.95 , 0.96 , 0.97 , 0.98 ,
jmpin 5:afd67e985df0 72 0.985 , 0.99 , 0.995 , 1
jmpin 5:afd67e985df0 73 };
jmpin 6:68c6a50e1437 74 double attackVals2[32] = { //Corresponds to an attackValue of 2
jmpin 5:afd67e985df0 75 0 , 0.06 , 0.12 , 0.18 ,
jmpin 5:afd67e985df0 76 0.23 , 0.28 , 0.32 , 0.36 ,
jmpin 5:afd67e985df0 77 0.4 , 0.44 , 0.48 , 0.52 ,
jmpin 5:afd67e985df0 78 0.55 , 0.58 , 0.61 , 0.64 ,
jmpin 5:afd67e985df0 79 0.67 , 0.695 , 0.72 , 0.745 ,
jmpin 5:afd67e985df0 80 0.77 , 0.795 , 0.82 , 0.845 ,
jmpin 5:afd67e985df0 81 0.87 , 0.895 , 0.92 , 0.945 ,
jmpin 5:afd67e985df0 82 0.965 , 0.985 , 0.995 , 1
jmpin 5:afd67e985df0 83 };
jmpin 6:68c6a50e1437 84 double attackVals1[32] = { //Approaches the mamimum amplitude the slowest, in a linear fashion - corresponds to an attackValue of 1
jmpin 5:afd67e985df0 85 0 , 0.032258065 , 0.064516129 , 0.096774194 ,
jmpin 5:afd67e985df0 86 0.129032258 , 0.161290323 , 0.193548387 , 0.225806452 ,
jmpin 5:afd67e985df0 87 0.258064516 , 0.290322581 , 0.322580645 , 0.35483871 ,
jmpin 5:afd67e985df0 88 0.387096774 , 0.419354839 , 0.451612903 , 0.483870968 ,
jmpin 5:afd67e985df0 89 0.516129032 , 0.548387097 , 0.580645161 , 0.612903226 ,
jmpin 5:afd67e985df0 90 0.64516129 , 0.677419355 , 0.709677419 , 0.741935484 ,
jmpin 5:afd67e985df0 91 0.774193548 , 0.806451613 , 0.838709677 , 0.870967742 ,
jmpin 5:afd67e985df0 92 0.903225806 , 0.935483871 , 0.967741935 , 1
jmpin 5:afd67e985df0 93 };
jmpin 5:afd67e985df0 94
jmpin 12:d60a9d0052a7 95 /* Coefficient Matrices Corresponding to Different Decay Values
jmpin 12:d60a9d0052a7 96 each matrix is comprised of 32 elements (256/8). The first matrix corresponds
jmpin 12:d60a9d0052a7 97 to a decay value of 5.
jmpin 12:d60a9d0052a7 98 */
jmpin 12:d60a9d0052a7 99
jmpin 7:d4c3260cb092 100 double decayVals5[32] = { //Approaches the sustain amplitude the quickest - corresponds to a decay value of 5
jmpin 7:d4c3260cb092 101 1 , 0.8 , 0.75 , 0.71 ,
jmpin 7:d4c3260cb092 102 0.68 , 0.66 , 0.65 , 0.64 ,
jmpin 7:d4c3260cb092 103 0.635 , 0.63 , 0.625 , 0.62 ,
jmpin 7:d4c3260cb092 104 0.615 , 0.61 , 0.605 , 0.6 ,
jmpin 7:d4c3260cb092 105 0.6 , 0.6 , 0.6 , 0.6 ,
jmpin 7:d4c3260cb092 106 0.6 , 0.6 , 0.6 , 0.6 ,
jmpin 7:d4c3260cb092 107 0.6 , 0.6 , 0.6 , 0.6 ,
jmpin 7:d4c3260cb092 108 0.6 , 0.6 , 0.6 , 0.6
jmpin 7:d4c3260cb092 109 };
jmpin 12:d60a9d0052a7 110 double decayVals4[32] = { // Decay value of 4
jmpin 7:d4c3260cb092 111 1 , 0.93 , 0.86 , 0.8 ,
jmpin 7:d4c3260cb092 112 0.75 , 0.71 , 0.69 , 0.68 ,
jmpin 7:d4c3260cb092 113 0.67 , 0.66 , 0.655 , 0.65 ,
jmpin 7:d4c3260cb092 114 0.645 , 0.64 , 0.635 , 0.63 ,
jmpin 7:d4c3260cb092 115 0.625 , 0.62 , 0.615 , 0.61 ,
jmpin 7:d4c3260cb092 116 0.605 , 0.6 , 0.6 , 0.6 ,
jmpin 7:d4c3260cb092 117 0.6 , 0.6 , 0.6 , 0.6 ,
jmpin 7:d4c3260cb092 118 0.6 , 0.6 , 0.6 , 0.6
jmpin 7:d4c3260cb092 119 };
jmpin 12:d60a9d0052a7 120 double decayVals3[32] = { // Decay value of 3
jmpin 7:d4c3260cb092 121 1 , 0.96 , 0.92 , 0.88 ,
jmpin 7:d4c3260cb092 122 0.85 , 0.82 , 0.79 , 0.76 ,
jmpin 7:d4c3260cb092 123 0.74 , 0.72 , 0.705 , 0.69 ,
jmpin 7:d4c3260cb092 124 0.68 , 0.67 , 0.665 , 0.66 ,
jmpin 7:d4c3260cb092 125 0.655 , 0.65 , 0.645 , 0.64 ,
jmpin 7:d4c3260cb092 126 0.635 , 0.63 , 0.625 , 0.62 ,
jmpin 7:d4c3260cb092 127 0.615 , 0.61 , 0.605 , 0.6 ,
jmpin 7:d4c3260cb092 128 0.6 , 0.6 , 0.6 , 0.6
jmpin 7:d4c3260cb092 129 };
jmpin 12:d60a9d0052a7 130 double decayVals2[32] = { // Decay value of 2
jmpin 7:d4c3260cb092 131 1 , 0.98 , 0.96 , 0.94 ,
jmpin 7:d4c3260cb092 132 0.92 , 0.9 , 0.88 , 0.86 ,
jmpin 7:d4c3260cb092 133 0.84 , 0.82 , 0.8 , 0.79 ,
jmpin 7:d4c3260cb092 134 0.78 , 0.77 , 0.76 , 0.75 ,
jmpin 7:d4c3260cb092 135 0.74 , 0.73 , 0.72 , 0.71 ,
jmpin 7:d4c3260cb092 136 0.7 , 0.69 , 0.68 , 0.67 ,
jmpin 7:d4c3260cb092 137 0.66 , 0.65 , 0.64 , 0.63 ,
jmpin 7:d4c3260cb092 138 0.62 , 0.61 , 0.6 , 0.6
jmpin 7:d4c3260cb092 139 };
jmpin 12:d60a9d0052a7 140 double decayVals1[32] = { // Decays the slowest, in a linear fashion - corresponds to a decay value of 1
jmpin 7:d4c3260cb092 141 1 , 0.987096774 , 0.974193548 , 0.961290323 ,
jmpin 7:d4c3260cb092 142 0.948387097 , 0.935483871 , 0.922580645 , 0.909677419 ,
jmpin 7:d4c3260cb092 143 0.896774194 , 0.883870968 , 0.870967742 , 0.858064516 ,
jmpin 7:d4c3260cb092 144 0.84516129 , 0.832258065 , 0.819354839 , 0.806451613 ,
jmpin 7:d4c3260cb092 145 0.793548387 , 0.780645161 , 0.767741935 , 0.75483871 ,
jmpin 7:d4c3260cb092 146 0.741935484 , 0.729032258 , 0.716129032 , 0.703225806 ,
jmpin 7:d4c3260cb092 147 0.690322581 , 0.677419355 , 0.664516129 , 0.651612903 ,
jmpin 7:d4c3260cb092 148 0.638709677 , 0.625806452 , 0.612903226 , 0.6
jmpin 7:d4c3260cb092 149 };
jmpin 8:f6699fd30737 150
jmpin 12:d60a9d0052a7 151 /* Coefficient Matrices Corresponding to Different sustain values
jmpin 12:d60a9d0052a7 152 each matrix is comprised of 160 elements 5 * (256/8). The first matrix corresponds
jmpin 12:d60a9d0052a7 153 to a sustain value of 5. The matrices get initialized later in a for loop due to their size.
jmpin 12:d60a9d0052a7 154 */
jmpin 12:d60a9d0052a7 155
jmpin 8:f6699fd30737 156 double sustainVals5[160];
jmpin 8:f6699fd30737 157 double sustainVals4[160];
jmpin 8:f6699fd30737 158 double sustainVals3[160];
jmpin 8:f6699fd30737 159 double sustainVals2[160];
jmpin 8:f6699fd30737 160 double sustainVals1[160];
jmpin 12:d60a9d0052a7 161
jmpin 12:d60a9d0052a7 162 /* Coefficient Matrices Corresponding to Different release values
jmpin 12:d60a9d0052a7 163 each matrix is comprised of 32 elements (256/8). The first matrix corresponds
jmpin 12:d60a9d0052a7 164 to a release value of 5.
jmpin 12:d60a9d0052a7 165 */
jmpin 12:d60a9d0052a7 166
jmpin 12:d60a9d0052a7 167 double releaseVals5[32] = { // Releases (goes to 0 amplitude) the quickest - corresponds to a release value of 5
jmpin 9:e4df1a31a098 168 0.6 , 0.3 , 0.15 , 0.1 ,
jmpin 9:e4df1a31a098 169 0.09 , 0.08 , 0.07 , 0.06 ,
jmpin 9:e4df1a31a098 170 0.05 , 0.045 , 0.04 , 0.035 ,
jmpin 9:e4df1a31a098 171 0.03 , 0.025 , 0.02 , 0.015 ,
jmpin 9:e4df1a31a098 172 0.01 , 0.0075 , 0.005 , 0.0025 ,
jmpin 9:e4df1a31a098 173 0 , 0 , 0 , 0 ,
jmpin 9:e4df1a31a098 174 0 , 0 , 0 , 0 ,
jmpin 9:e4df1a31a098 175 0 , 0 , 0 , 0};
jmpin 12:d60a9d0052a7 176 double releaseVals4[32] = { // Release value of 4
jmpin 9:e4df1a31a098 177 0.6 , 0.45 , 0.3 , 0.2 ,
jmpin 9:e4df1a31a098 178 0.17 , 0.16 , 0.15 , 0.14 ,
jmpin 9:e4df1a31a098 179 0.13 , 0.125 , 0.12 , 0.115 ,
jmpin 9:e4df1a31a098 180 0.11 , 0.105 , 0.1 , 0.095 ,
jmpin 9:e4df1a31a098 181 0.09 , 0.085 , 0.08 , 0.075 ,
jmpin 9:e4df1a31a098 182 0.07 , 0.065 , 0.06 , 0.055 ,
jmpin 9:e4df1a31a098 183 0.05 , 0.045 , 0.04 , 0.035 ,
jmpin 9:e4df1a31a098 184 0.03 , 0.02 , 0.01 , 0};
jmpin 12:d60a9d0052a7 185 double releaseVals3[32] = { // Release value of 3
jmpin 9:e4df1a31a098 186 0.6 , 0.5 , 0.43 , 0.37 ,
jmpin 9:e4df1a31a098 187 0.32 , 0.28 , 0.26 , 0.24 ,
jmpin 9:e4df1a31a098 188 0.22 , 0.2 , 0.18 , 0.17 ,
jmpin 9:e4df1a31a098 189 0.16 , 0.15 , 0.14 , 0.13 ,
jmpin 9:e4df1a31a098 190 0.12 , 0.11 , 0.1 , 0.09 ,
jmpin 9:e4df1a31a098 191 0.08 , 0.07 , 0.06 , 0.05 ,
jmpin 9:e4df1a31a098 192 0.04 , 0.035 , 0.03 , 0.025 ,
jmpin 9:e4df1a31a098 193 0.02 , 0.015 , 0.01 , 0};
jmpin 12:d60a9d0052a7 194 double releaseVals2[32] = { // Release value of 2
jmpin 9:e4df1a31a098 195 0.6 , 0.55 , 0.5 , 0.46 ,
jmpin 9:e4df1a31a098 196 0.43 , 0.4 , 0.37 , 0.34 ,
jmpin 9:e4df1a31a098 197 0.32 , 0.3 , 0.28 , 0.26 ,
jmpin 9:e4df1a31a098 198 0.24 , 0.22 , 0.2 , 0.18 ,
jmpin 9:e4df1a31a098 199 0.16 , 0.15 , 0.14 , 0.13 ,
jmpin 9:e4df1a31a098 200 0.12 , 0.11 , 0.1 , 0.09 ,
jmpin 9:e4df1a31a098 201 0.08 , 0.07 , 0.06 , 0.05 ,
jmpin 9:e4df1a31a098 202 0.04 , 0.03 , 0.015 , 0};
jmpin 12:d60a9d0052a7 203 double releaseVals1[32] = { // Release value of 1 - proceeds slowest, in a linear fashion
jmpin 9:e4df1a31a098 204 0.6 , 0.580645161 , 0.561290323 , 0.541935484 ,
jmpin 9:e4df1a31a098 205 0.522580645 , 0.503225806 , 0.483870968 , 0.464516129 ,
jmpin 9:e4df1a31a098 206 0.44516129 , 0.425806452 , 0.406451613 , 0.387096774 ,
jmpin 9:e4df1a31a098 207 0.367741935 , 0.348387097 , 0.329032258 , 0.309677419 ,
jmpin 9:e4df1a31a098 208 0.290322581 , 0.270967742 , 0.251612903 , 0.232258065 ,
jmpin 9:e4df1a31a098 209 0.212903226 , 0.193548387 , 0.174193548 , 0.15483871 ,
jmpin 9:e4df1a31a098 210 0.135483871 , 0.116129032 , 0.096774194 , 0.077419355 ,
jmpin 9:e4df1a31a098 211 0.058064516 , 0.038709677 , 0.019354839 , -1.38778E-16};
jmpin 5:afd67e985df0 212
jmpin 6:68c6a50e1437 213 int noteArray[7][7] = { // Array holding different note frequencies
jmpin 6:68c6a50e1437 214 C1 , D1 , E1 , F1 , G1 , A1 , B1 ,
jmpin 6:68c6a50e1437 215 C2 , D2 , E2 , F2 , G2 , A2 , B2,
jmpin 6:68c6a50e1437 216 C3 , D3 , E3 , F3 , G3 , A3 , B2 ,
jmpin 6:68c6a50e1437 217 C4 , D4 , E4 , F4 , G4 , A4 , B4 ,
jmpin 6:68c6a50e1437 218 C5 , D5 , E5 , F5 , G5 , A5 , B5 ,
jmpin 6:68c6a50e1437 219 C6 , D6 , E6 , F6 , G6 , A6 , B6 ,
jmpin 6:68c6a50e1437 220 C7 , D7 , E7 , F7 , G7 , A7 , B7
jmpin 6:68c6a50e1437 221 };
jmpin 5:afd67e985df0 222
jmpin 12:d60a9d0052a7 223 void uLCD_Display_Thread(void const *args){ // uLCD displays curernt waveform shape, current octave, and the values for the ADSR coefficients
Jake867 11:c87f55a3b9e0 224 while(1){
jmpin 14:40f57385c404 225 uLCD.cls();
Jake867 11:c87f55a3b9e0 226 uLCD.locate(0,0);
jmpin 12:d60a9d0052a7 227 switch(myWave){
jmpin 12:d60a9d0052a7 228 case sine:
jmpin 15:8ff317cc5d2c 229 uLCD.printf("Shape: Sine \r\n"); // if wave type is sine wave, display sine
jmpin 12:d60a9d0052a7 230 break;
jmpin 12:d60a9d0052a7 231 case square:
jmpin 15:8ff317cc5d2c 232 uLCD.printf("Shape: Square \r\n"); // if wave type is square wave, display square
jmpin 12:d60a9d0052a7 233 break;
jmpin 12:d60a9d0052a7 234 case sawtooth:
jmpin 12:d60a9d0052a7 235 uLCD.printf("Shape: Sawtooth\r\n"); // if wave type is sawtooth wave, display sawtooth
jmpin 12:d60a9d0052a7 236 break;
jmpin 12:d60a9d0052a7 237 default:
jmpin 12:d60a9d0052a7 238 break;
jmpin 12:d60a9d0052a7 239 }
jmpin 12:d60a9d0052a7 240 uLCD.printf("Octave: %i\r\n",currentOctave); // displays octave
jmpin 12:d60a9d0052a7 241 uLCD.printf("Attack: %i\r\n",currentAttackVal); // displays attack value
jmpin 12:d60a9d0052a7 242 uLCD.printf("Decay: %i\r\n",currentDecayVal); // displays decay value
jmpin 12:d60a9d0052a7 243 uLCD.printf("Sustain: %i\r\n",currentSustainVal); // displays sustain value
jmpin 12:d60a9d0052a7 244 uLCD.printf("Release: %i\r\n",currentReleaseVal); // displays release value
Jake867 11:c87f55a3b9e0 245 }
jmpin 12:d60a9d0052a7 246 }
jmpin 6:68c6a50e1437 247
jmpin 12:d60a9d0052a7 248 void clear_Buffer(void){ // clears buffer that holds samples
jmpin 9:e4df1a31a098 249 sampleBuffer.clear();
jmpin 9:e4df1a31a098 250 }
jmpin 9:e4df1a31a098 251
jmpin 12:d60a9d0052a7 252 void set_Note_Freq(int frequency){ // updates the frequency of the note being played
jmpin 6:68c6a50e1437 253 noteFreq = frequency;
jmpin 9:e4df1a31a098 254 clear_Buffer();
jmpin 6:68c6a50e1437 255 }
jmpin 6:68c6a50e1437 256
jmpin 12:d60a9d0052a7 257 void change_Attack_Table(int attackVal) // change which table of coefficients to use for altering the attack portion of the waveform
jmpin 6:68c6a50e1437 258 {
jmpin 6:68c6a50e1437 259 switch(attackVal){
jmpin 6:68c6a50e1437 260 case 5:
jmpin 6:68c6a50e1437 261 currentAttackTable = attackVals5;
jmpin 6:68c6a50e1437 262 break;
jmpin 6:68c6a50e1437 263 case 4:
jmpin 6:68c6a50e1437 264 currentAttackTable = attackVals4;
jmpin 6:68c6a50e1437 265 break;
jmpin 6:68c6a50e1437 266 case 3:
jmpin 6:68c6a50e1437 267 currentAttackTable = attackVals3;
jmpin 6:68c6a50e1437 268 break;
jmpin 6:68c6a50e1437 269 case 2:
jmpin 6:68c6a50e1437 270 currentAttackTable = attackVals2;
jmpin 6:68c6a50e1437 271 break;
jmpin 6:68c6a50e1437 272 case 1:
jmpin 6:68c6a50e1437 273 currentAttackTable = attackVals1;
jmpin 6:68c6a50e1437 274 break;
jmpin 6:68c6a50e1437 275 default:
jmpin 6:68c6a50e1437 276 break;
jmpin 6:68c6a50e1437 277 }
jmpin 6:68c6a50e1437 278 }
jmpin 5:afd67e985df0 279
jmpin 12:d60a9d0052a7 280 void change_Decay_Table(int decayVal) // change which table of coefficients to use for altering the decay portion of the waveform
jmpin 6:68c6a50e1437 281 {
jmpin 6:68c6a50e1437 282 switch(decayVal){
jmpin 6:68c6a50e1437 283 case 5:
jmpin 6:68c6a50e1437 284 currentDecayTable = decayVals5;
jmpin 6:68c6a50e1437 285 break;
jmpin 6:68c6a50e1437 286 case 4:
jmpin 6:68c6a50e1437 287 currentDecayTable = decayVals4;
jmpin 6:68c6a50e1437 288 break;
jmpin 6:68c6a50e1437 289 case 3:
jmpin 6:68c6a50e1437 290 currentDecayTable = decayVals3;
jmpin 6:68c6a50e1437 291 break;
jmpin 6:68c6a50e1437 292 case 2:
jmpin 6:68c6a50e1437 293 currentDecayTable = decayVals2;
jmpin 6:68c6a50e1437 294 break;
jmpin 6:68c6a50e1437 295 case 1:
jmpin 6:68c6a50e1437 296 currentDecayTable = decayVals1;
jmpin 6:68c6a50e1437 297 break;
jmpin 6:68c6a50e1437 298 default:
jmpin 6:68c6a50e1437 299 break;
jmpin 6:68c6a50e1437 300 }
jmpin 6:68c6a50e1437 301 }
jmpin 5:afd67e985df0 302
jmpin 12:d60a9d0052a7 303 void change_Sustain_Table(int sustainVal) // change which table of coefficients to use for altering the sustain portion of the waveform
jmpin 6:68c6a50e1437 304 {
jmpin 6:68c6a50e1437 305 switch(sustainVal){
jmpin 6:68c6a50e1437 306 case 5:
jmpin 6:68c6a50e1437 307 currentSustainTable = sustainVals5;
jmpin 6:68c6a50e1437 308 break;
jmpin 6:68c6a50e1437 309 case 4:
jmpin 6:68c6a50e1437 310 currentSustainTable = sustainVals4;
jmpin 6:68c6a50e1437 311 break;
jmpin 6:68c6a50e1437 312 case 3:
jmpin 6:68c6a50e1437 313 currentSustainTable = sustainVals3;
jmpin 6:68c6a50e1437 314 break;
jmpin 6:68c6a50e1437 315 case 2:
jmpin 6:68c6a50e1437 316 currentSustainTable = sustainVals2;
jmpin 6:68c6a50e1437 317 break;
jmpin 6:68c6a50e1437 318 case 1:
jmpin 6:68c6a50e1437 319 currentSustainTable = sustainVals1;
jmpin 6:68c6a50e1437 320 break;
jmpin 6:68c6a50e1437 321 default:
jmpin 6:68c6a50e1437 322 break;
jmpin 6:68c6a50e1437 323 }
jmpin 6:68c6a50e1437 324 }
jmpin 6:68c6a50e1437 325
jmpin 12:d60a9d0052a7 326 void change_Release_Table(int releaseVal) // change which table of coefficients to use for altering the release portion of the waveform
jmpin 6:68c6a50e1437 327 {
jmpin 6:68c6a50e1437 328 switch(releaseVal){
jmpin 6:68c6a50e1437 329 case 5:
jmpin 6:68c6a50e1437 330 currentReleaseTable = releaseVals5;
jmpin 6:68c6a50e1437 331 break;
jmpin 6:68c6a50e1437 332 case 4:
jmpin 6:68c6a50e1437 333 currentReleaseTable = releaseVals4;
jmpin 6:68c6a50e1437 334 break;
jmpin 6:68c6a50e1437 335 case 3:
jmpin 6:68c6a50e1437 336 currentReleaseTable = releaseVals3;
jmpin 6:68c6a50e1437 337 break;
jmpin 6:68c6a50e1437 338 case 2:
jmpin 6:68c6a50e1437 339 currentReleaseTable = releaseVals2;
jmpin 6:68c6a50e1437 340 break;
jmpin 6:68c6a50e1437 341 case 1:
jmpin 6:68c6a50e1437 342 currentReleaseTable = releaseVals1;
jmpin 6:68c6a50e1437 343 break;
jmpin 6:68c6a50e1437 344 default:
jmpin 6:68c6a50e1437 345 break;
jmpin 6:68c6a50e1437 346 }
jmpin 6:68c6a50e1437 347 }
jmpin 12:d60a9d0052a7 348
jmpin 12:d60a9d0052a7 349 /* Having different sustain values for the amplitude of the wave would make the math neccesary to generate the other
jmpin 12:d60a9d0052a7 350 coefficient matrices very complex, so only .6 is used, meaning a sustain value of 1-5 will all correspond to a sustain amplitude
jmpin 12:d60a9d0052a7 351 of .6. Since the sustain coefficient matrices are 160 elements long, they are all filled in a for loop with this function call.
jmpin 12:d60a9d0052a7 352 */
jmpin 12:d60a9d0052a7 353
jmpin 12:d60a9d0052a7 354 void initialize_sustainVals()
jmpin 8:f6699fd30737 355 {
jmpin 8:f6699fd30737 356 for(int j = 0; j < 160; j++)
jmpin 8:f6699fd30737 357 {
jmpin 8:f6699fd30737 358 sustainVals5[j] = .6;
jmpin 8:f6699fd30737 359 sustainVals4[j] = .6;
jmpin 8:f6699fd30737 360 sustainVals3[j] = .6;
jmpin 8:f6699fd30737 361 sustainVals2[j] = .6;
jmpin 8:f6699fd30737 362 sustainVals1[j] = .6;
jmpin 8:f6699fd30737 363 }
jmpin 8:f6699fd30737 364 }
jmpin 12:d60a9d0052a7 365 /* Applies the envelope to the waveform. Each set of coefficients is applied to a certain portion of the waveform to alter its shape.
jmpin 12:d60a9d0052a7 366 The attack coefficients are appplied to the first 32 samples, the decay coefficients are applied to samples 33-64, the sustain coefficients
jmpin 12:d60a9d0052a7 367 are applied to samples 65 - 224, and the release coefficients are appplied to samples 225-256.
jmpin 12:d60a9d0052a7 368 */
jmpin 6:68c6a50e1437 369
jmpin 12:d60a9d0052a7 370
jmpin 12:d60a9d0052a7 371 void apply_Envelope(void){
jmpin 6:68c6a50e1437 372 int attack_range, decay_range, sustain_range, release_range;
jmpin 6:68c6a50e1437 373 attack_range = sampleBuffer.size() * (1/8); // The attack portion of the waveform will take (1/8) of the note's duration
jmpin 6:68c6a50e1437 374 decay_range = attack_range + (sampleBuffer.size() * (1/8)); // The decay portion of the waveform will take (1/8) of the note's duration
jmpin 6:68c6a50e1437 375 sustain_range = sustain_range + (sampleBuffer.size() * (5/8)); // The sustain portion of the waveform will take (5/8) of the note's duration
jmpin 6:68c6a50e1437 376 release_range = release_range + (sampleBuffer.size() * (1/8)); // The release portion of the waveform will take (1/8) of the note's duration
jmpin 6:68c6a50e1437 377 for(int i = 0; i < attack_range; i++)
jmpin 6:68c6a50e1437 378 {
jmpin 6:68c6a50e1437 379 sampleBuffer[i] = sampleBuffer[i] * currentAttackTable[i];
jmpin 6:68c6a50e1437 380 }
jmpin 6:68c6a50e1437 381 for(int k = attack_range; k < decay_range; k++)
jmpin 6:68c6a50e1437 382 {
jmpin 6:68c6a50e1437 383 sampleBuffer[k] = sampleBuffer[k] * currentDecayTable[k-attack_range];
jmpin 6:68c6a50e1437 384 }
jmpin 6:68c6a50e1437 385 for(int m = decay_range; m < sustain_range; m++)
jmpin 6:68c6a50e1437 386 {
jmpin 6:68c6a50e1437 387 sampleBuffer[m] = sampleBuffer[m] * currentSustainTable[m-decay_range];
jmpin 6:68c6a50e1437 388 }
jmpin 6:68c6a50e1437 389 for(int n = sustain_range; n < release_range; n++)
jmpin 6:68c6a50e1437 390 {
jmpin 6:68c6a50e1437 391 sampleBuffer[n] = sampleBuffer[n] * currentReleaseTable[n-sustain_range];
jmpin 6:68c6a50e1437 392 }
jmpin 6:68c6a50e1437 393 }
jmpin 5:afd67e985df0 394
jmpin 12:d60a9d0052a7 395 void generate_sineWave(int frequency) // Generates samples for a sine wave of a given input frequency
jmpin 9:e4df1a31a098 396 {
jmpin 12:d60a9d0052a7 397 double t = 0; // Represents time, since we want each note to last 2 seconds and have 256 samples
jmpin 9:e4df1a31a098 398 for(int i = 0; i < 256 ; i++)
jmpin 9:e4df1a31a098 399 {
jmpin 9:e4df1a31a098 400 sampleBuffer.push_back(((sin(2*(PI)*frequency*t)) + 1)/2); // scaled to be a % of maximum output voltage (3.3V)
jmpin 9:e4df1a31a098 401 t = t + timeIncrement; // increment t for calculation of next value in the waveform
jmpin 9:e4df1a31a098 402 }
jmpin 9:e4df1a31a098 403 }
jmpin 9:e4df1a31a098 404
jmpin 12:d60a9d0052a7 405 void generate_sawtoothWave(int frequency) // Generates samples for a sawtooth wave of a given input frequency
jmpin 9:e4df1a31a098 406 {
jmpin 12:d60a9d0052a7 407 double t = 0; // Represents time, since we want each note to last 2 seconds and have 256 samples
jmpin 9:e4df1a31a098 408 for(int i = 0; i<256 ; i++)
jmpin 9:e4df1a31a098 409 {
jmpin 9:e4df1a31a098 410 sampleBuffer.push_back((2*(t*frequency) - (.5 + (t*frequency)) + 1) / 2);
jmpin 12:d60a9d0052a7 411 t = t + timeIncrement; // increment t for calculation of next value in the waveform
jmpin 9:e4df1a31a098 412 }
jmpin 9:e4df1a31a098 413 }
jmpin 9:e4df1a31a098 414
jmpin 12:d60a9d0052a7 415 void generate_squareWave(int frequency) // Generates samples for a square wave of a given input frequency. Looks at whether we have seen an even or odd number of 'widths' to determine if wave should be high or low at given t
jmpin 9:e4df1a31a098 416 {
jmpin 12:d60a9d0052a7 417 double width = (1 / 2 * frequency); // Width of a half period of the square wave
jmpin 12:d60a9d0052a7 418 double t = 0; // Represents time, since we want a 2 second note with 256 samples
jmpin 9:e4df1a31a098 419 for(int i = 0; i < 256; i++)
jmpin 9:e4df1a31a098 420 {
jmpin 9:e4df1a31a098 421 if(((int)(t / width) % 2 ) == 0) // Even, write a 1 for the square wave
jmpin 9:e4df1a31a098 422 sampleBuffer.push_back(1.0);
jmpin 9:e4df1a31a098 423 else // Odd, write a 0 for the square wave
jmpin 9:e4df1a31a098 424 sampleBuffer.push_back(0.0);
jmpin 9:e4df1a31a098 425 t = t + timeIncrement; // increment t for calculation of next value in the waveform
jmpin 9:e4df1a31a098 426 }
jmpin 9:e4df1a31a098 427 }
jmpin 9:e4df1a31a098 428
jmpin 12:d60a9d0052a7 429 /* Generates the waveforms that will be output to the AnalogOut pin after being altered by the ADSR coefficient matrices.
jmpin 12:d60a9d0052a7 430 The envelope is only applied to sine waves here because when applied to the other wave shapes, the sound does not sounds good.
jmpin 12:d60a9d0052a7 431 @param: frequency - the frequency of the waveform to be generated
jmpin 12:d60a9d0052a7 432 @param: currentWaveType - the shape of the wave that needs to be generated
jmpin 12:d60a9d0052a7 433 */
jmpin 12:d60a9d0052a7 434
jmpin 12:d60a9d0052a7 435
jmpin 9:e4df1a31a098 436 void create_samples(int frequency, WaveType currentWaveType)
jmpin 8:f6699fd30737 437 {
jmpin 8:f6699fd30737 438 switch(currentWaveType){
jmpin 8:f6699fd30737 439 case sine:
jmpin 8:f6699fd30737 440 //Generate sine wave values
jmpin 8:f6699fd30737 441 generate_sineWave(frequency);
jmpin 8:f6699fd30737 442 apply_Envelope();
jmpin 8:f6699fd30737 443 break;
jmpin 8:f6699fd30737 444 case square:
jmpin 8:f6699fd30737 445 //Generate square wave values
jmpin 8:f6699fd30737 446 generate_squareWave(frequency);
jmpin 9:e4df1a31a098 447 //apply_Envelope();
jmpin 8:f6699fd30737 448 break;
jmpin 8:f6699fd30737 449 case sawtooth:
jmpin 8:f6699fd30737 450 //Generate sawtooth wave values
jmpin 8:f6699fd30737 451 generate_sawtoothWave(frequency);
jmpin 9:e4df1a31a098 452 //apply_Envelope();
jmpin 8:f6699fd30737 453 break;
jmpin 8:f6699fd30737 454 default:
jmpin 8:f6699fd30737 455 break;
jmpin 8:f6699fd30737 456 }
jmpin 8:f6699fd30737 457 }
jmpin 5:afd67e985df0 458
jmpin 5:afd67e985df0 459
jmpin 12:d60a9d0052a7 460 /* Outputs the samples that are currently in the buffer one at a time. There is a period of time
jmpin 12:d60a9d0052a7 461 where the program waits so that the 256 samples fill up the entire 2 seconds. The buffer is cleared
jmpin 12:d60a9d0052a7 462 after the output is finished so that next time the buffer will be ready for new samples.
jmpin 12:d60a9d0052a7 463 */
jmpin 8:f6699fd30737 464 void output_samples()
jmpin 8:f6699fd30737 465 {
jmpin 8:f6699fd30737 466 for( int sample = 0; sample < 256; sample++)
jmpin 8:f6699fd30737 467 {
jmpin 8:f6699fd30737 468 synthPin = sampleBuffer[sample];
jmpin 8:f6699fd30737 469 Thread::wait(timeIncrement * 1000);
jmpin 8:f6699fd30737 470 }
jmpin 12:d60a9d0052a7 471 clear_Buffer();
jmpin 9:e4df1a31a098 472 }
jmpin 8:f6699fd30737 473
jmpin 8:f6699fd30737 474
jmpin 8:f6699fd30737 475
jmpin 5:afd67e985df0 476
jmpin 0:48311ffdfa96 477 //Interrupt routine to parse message with one new character per serial RX interrupt
jmpin 0:48311ffdfa96 478 void parse_message()
jmpin 0:48311ffdfa96 479 {
jmpin 12:d60a9d0052a7 480 //PC.printf("Parse_message was called");
Jake867 11:c87f55a3b9e0 481 while(Blue.readable())
jmpin 10:085c49fe2509 482 {
Jake867 11:c87f55a3b9e0 483 keyPress = Blue.getc();
jmpin 12:d60a9d0052a7 484 //PC.putc(keyPress);
Jake867 11:c87f55a3b9e0 485 readyFlag = true;
jmpin 12:d60a9d0052a7 486 //PC.printf("\n\r Value of readyFlag is: %i",readyFlag);
jmpin 12:d60a9d0052a7 487 //PC.printf("Value of keyPress is: %c\n\r",keyPress);
jmpin 12:d60a9d0052a7 488 //wait(1);
jmpin 10:085c49fe2509 489 }
jmpin 0:48311ffdfa96 490 }
jmpin 3:3aba1d783730 491
jmpin 3:3aba1d783730 492
jmpin 3:3aba1d783730 493 /*
jmpin 3:3aba1d783730 494 This function writes which note was just played to a text file on the SDCard.
jmpin 3:3aba1d783730 495 The note played will be encoded in hexadecimal, as well as the octave, Attack Value,
jmpin 3:3aba1d783730 496 Delay Value, Sustain Value, and Release Value. The format of the bits will be as follows:
jmpin 3:3aba1d783730 497 | 3 bits | 3 bits | 3 bits | 3 bits | 3 bits | 3 bits |
jmpin 6:68c6a50e1437 498 | Attack | Decay | Susttain | Release | Octave | Note |
jmpin 3:3aba1d783730 499 For the 3 bits representing note, A will correspond to 1, B to 2, and so on.
jmpin 3:3aba1d783730 500 For example, if the lower 3 bits corresponding to note are 001, then the note is an A.
jmpin 3:3aba1d783730 501
jmpin 3:3aba1d783730 502 @param: The note that is being played/recorded into the text file
jmpin 3:3aba1d783730 503 */
jmpin 3:3aba1d783730 504
jmpin 3:3aba1d783730 505 void write_to_SDCard(char note)
jmpin 3:3aba1d783730 506 {
jmpin 6:68c6a50e1437 507 int AttackBits, SustainBits, DecayBits, ReleaseBits, OctaveBits, NoteBits;
jmpin 2:f06ba516b1ad 508
jmpin 12:d60a9d0052a7 509 AttackBits = currentAttackVal; // Holds the value of the attack parameter
jmpin 12:d60a9d0052a7 510 DecayBits = currentDecayVal; // Holds the value of the decay parameter
jmpin 12:d60a9d0052a7 511 SustainBits = currentSustainVal;// Holds the value of the sustain parameter
jmpin 12:d60a9d0052a7 512 ReleaseBits = currentReleaseVal;// Holds the value of the release parameter
jmpin 3:3aba1d783730 513 OctaveBits = currentOctave;
jmpin 3:3aba1d783730 514 switch(note){
jmpin 12:d60a9d0052a7 515 case 'C': // a C corresponds to a 3
jmpin 3:3aba1d783730 516 NoteBits = 3;
jmpin 3:3aba1d783730 517 break;
jmpin 3:3aba1d783730 518 case 'D':
jmpin 12:d60a9d0052a7 519 NoteBits = 4; // a D corresponds to a 4
jmpin 3:3aba1d783730 520 break;
jmpin 3:3aba1d783730 521 case 'E':
jmpin 12:d60a9d0052a7 522 NoteBits = 5; // an E corresponds to a 5
jmpin 3:3aba1d783730 523 break;
jmpin 3:3aba1d783730 524 case 'F':
jmpin 12:d60a9d0052a7 525 NoteBits = 6; // an F corresponds to a 6
jmpin 3:3aba1d783730 526 break;
jmpin 3:3aba1d783730 527 case 'G':
jmpin 12:d60a9d0052a7 528 NoteBits = 7; // a G corresponds to a 7
jmpin 3:3aba1d783730 529 break;
jmpin 3:3aba1d783730 530 case 'A':
jmpin 12:d60a9d0052a7 531 NoteBits = 1; // an A corresponds to a 1
jmpin 3:3aba1d783730 532 break;
jmpin 3:3aba1d783730 533 case 'B':
jmpin 12:d60a9d0052a7 534 NoteBits = 2; // a B corresponds to a 2
jmpin 3:3aba1d783730 535 break;
jmpin 3:3aba1d783730 536 default:
jmpin 3:3aba1d783730 537 NoteBits = 0;
jmpin 3:3aba1d783730 538 break;
jmpin 3:3aba1d783730 539 }
jmpin 3:3aba1d783730 540 int writeVal;
jmpin 6:68c6a50e1437 541 writeVal = (AttackBits << 15) | (DecayBits << 12) | (SustainBits << 9) | (ReleaseBits << 6) | (OctaveBits << 3) | (NoteBits);
jmpin 3:3aba1d783730 542
jmpin 12:d60a9d0052a7 543 FILE *fp = fopen("/sd/noteRecords/note_record_01.txt", "w"); // creates handle for file we want to write to
jmpin 3:3aba1d783730 544 if(fp == NULL) {
jmpin 12:d60a9d0052a7 545 error("Could not open file for write\n"); // if this is not a valid name, tell user there is an error
jmpin 3:3aba1d783730 546 }
jmpin 3:3aba1d783730 547 fprintf(fp,"%X\r\n",writeVal); // writes value to the text file in hexadecimal
jmpin 3:3aba1d783730 548 fclose(fp);
jmpin 3:3aba1d783730 549 }
jmpin 3:3aba1d783730 550
jmpin 0:48311ffdfa96 551 int main()
jmpin 0:48311ffdfa96 552 {
jmpin 12:d60a9d0052a7 553 Thread thread1(uLCD_Display_Thread); // the thread that displays the current values of the parameters as well as the octave and wave shape
jmpin 10:085c49fe2509 554
jmpin 12:d60a9d0052a7 555
jmpin 12:d60a9d0052a7 556 mkdir("/sd/noteRecords", 0777); // make directory to hold the record of notes played
jmpin 3:3aba1d783730 557
jmpin 12:d60a9d0052a7 558 initialize_sustainVals(); // fill the lookup tables with the sustain values in them
Jake867 11:c87f55a3b9e0 559
jmpin 12:d60a9d0052a7 560 PC.baud(9600); // setup baud rate for PC serial connection
jmpin 12:d60a9d0052a7 561 Blue.baud(9600); // setup baud rate for bluetooth serial connection
jmpin 3:3aba1d783730 562
jmpin 12:d60a9d0052a7 563
jmpin 12:d60a9d0052a7 564 Blue.attach(&parse_message,Serial::RxIrq); //attach interrupt function for each new Bluetooth serial character
jmpin 0:48311ffdfa96 565 while(1) {
jmpin 0:48311ffdfa96 566 //check for a new button message ready
jmpin 13:25d53936d385 567 if((keyPress == C_NOTE_KEY) && (readyFlag)){ // Play note C
jmpin 12:d60a9d0052a7 568 set_Note_Freq(noteArray[currentOctave-1][0]); // set the note frequency to the proper value
jmpin 13:25d53936d385 569 create_samples(noteFreq, myWave); // creates the samples that are going to be output to the waveform
jmpin 13:25d53936d385 570 write_to_SDCard('C'); // writes to the SD card
jmpin 13:25d53936d385 571 readyFlag = false; // set this flag to false so that the program will not try to process the key press more than once
jmpin 13:25d53936d385 572
Jake867 11:c87f55a3b9e0 573 }
jmpin 13:25d53936d385 574 else if((keyPress == D_NOTE_KEY) && (readyFlag)){ // Play note D
jmpin 15:8ff317cc5d2c 575 set_Note_Freq(noteArray[currentOctave-1][1]);
jmpin 15:8ff317cc5d2c 576 create_samples(noteFreq, myWave);
jmpin 15:8ff317cc5d2c 577 write_to_SDCard('D');
jmpin 15:8ff317cc5d2c 578 readyFlag = false;
jmpin 13:25d53936d385 579
Jake867 11:c87f55a3b9e0 580 }
jmpin 13:25d53936d385 581 else if((keyPress == E_NOTE_KEY) && (readyFlag)){ // Play note E
jmpin 6:68c6a50e1437 582 set_Note_Freq(noteArray[currentOctave-1][2]);
jmpin 9:e4df1a31a098 583 create_samples(noteFreq, myWave);
jmpin 3:3aba1d783730 584 write_to_SDCard('E');
jmpin 4:406f59c6a1a6 585 readyFlag = false;
Jake867 11:c87f55a3b9e0 586 }
jmpin 13:25d53936d385 587 else if((keyPress == F_NOTE_KEY) && (readyFlag)){ // Play note F
jmpin 6:68c6a50e1437 588 set_Note_Freq(noteArray[currentOctave-1][3]);
jmpin 9:e4df1a31a098 589 create_samples(noteFreq, myWave);
jmpin 3:3aba1d783730 590 write_to_SDCard('F');
jmpin 4:406f59c6a1a6 591 readyFlag = false;
Jake867 11:c87f55a3b9e0 592 }
jmpin 13:25d53936d385 593 else if((keyPress == G_NOTE_KEY) && (readyFlag)){ // Play note G
jmpin 6:68c6a50e1437 594 set_Note_Freq(noteArray[currentOctave-1][4]);
jmpin 9:e4df1a31a098 595 create_samples(noteFreq, myWave);
jmpin 3:3aba1d783730 596 write_to_SDCard('G');
jmpin 4:406f59c6a1a6 597 readyFlag = false;
Jake867 11:c87f55a3b9e0 598 }
jmpin 13:25d53936d385 599 else if((keyPress == A_NOTE_KEY) && (readyFlag)){ // Play note A
jmpin 6:68c6a50e1437 600 set_Note_Freq(noteArray[currentOctave][5]);
jmpin 9:e4df1a31a098 601 create_samples(noteFreq, myWave);
jmpin 3:3aba1d783730 602 write_to_SDCard('A');
jmpin 4:406f59c6a1a6 603 readyFlag = false;
Jake867 11:c87f55a3b9e0 604 }
jmpin 15:8ff317cc5d2c 605 else if((keyPress == B_NOTE_KEY) && (readyFlag)){ // Play note B
jmpin 6:68c6a50e1437 606 set_Note_Freq(noteArray[currentOctave][6]);
jmpin 9:e4df1a31a098 607 create_samples(noteFreq, myWave);
jmpin 3:3aba1d783730 608 write_to_SDCard('B');
jmpin 4:406f59c6a1a6 609 readyFlag = false;
Jake867 11:c87f55a3b9e0 610 }
jmpin 2:f06ba516b1ad 611 else if((keyPress == RAISE_OCTAVE_KEY) && (readyFlag)){ // button O pressed
jmpin 0:48311ffdfa96 612 // Raise an octave
jmpin 6:68c6a50e1437 613 if(currentOctave < 7)
jmpin 2:f06ba516b1ad 614 currentOctave++;
jmpin 4:406f59c6a1a6 615 else
jmpin 4:406f59c6a1a6 616 printf("Cannot raise octave above 7.\r\n");
Jake867 11:c87f55a3b9e0 617 readyFlag = false;
Jake867 11:c87f55a3b9e0 618 }
jmpin 2:f06ba516b1ad 619 else if((keyPress == LOWER_OCTAVE_KEY) && (readyFlag)){ // button L pressed
jmpin 2:f06ba516b1ad 620 // Lower an octave
jmpin 4:406f59c6a1a6 621 if(currentOctave > 1)
jmpin 2:f06ba516b1ad 622 currentOctave--;
jmpin 4:406f59c6a1a6 623 else
jmpin 4:406f59c6a1a6 624 printf("Cannot lower octave below 1.\r\n");
Jake867 11:c87f55a3b9e0 625 readyFlag = false;
Jake867 11:c87f55a3b9e0 626 }
jmpin 2:f06ba516b1ad 627 else if((keyPress == RAISE_ATTACK_KEY) && (readyFlag)){ // button Q pressed
jmpin 0:48311ffdfa96 628 // Raise Attack Value
jmpin 6:68c6a50e1437 629 if(currentAttackVal < 5){
jmpin 2:f06ba516b1ad 630 currentAttackVal++;
jmpin 6:68c6a50e1437 631 change_Attack_Table(currentAttackVal);
jmpin 6:68c6a50e1437 632 }
jmpin 4:406f59c6a1a6 633 else
jmpin 4:406f59c6a1a6 634 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 635 readyFlag = false;
Jake867 11:c87f55a3b9e0 636 }
jmpin 2:f06ba516b1ad 637 else if((keyPress == LOWER_ATTACK_KEY) && (readyFlag)){ // button A pressed
jmpin 0:48311ffdfa96 638 // Lower Attack Value
jmpin 6:68c6a50e1437 639 if(currentAttackVal > 1){
jmpin 2:f06ba516b1ad 640 currentAttackVal--;
jmpin 6:68c6a50e1437 641 change_Attack_Table(currentAttackVal);
jmpin 6:68c6a50e1437 642 }
jmpin 4:406f59c6a1a6 643 else
jmpin 6:68c6a50e1437 644 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 645 readyFlag = false;
Jake867 11:c87f55a3b9e0 646 }
jmpin 2:f06ba516b1ad 647 else if((keyPress == RAISE_DELAY_KEY) && (readyFlag)){ // button W pressed
jmpin 0:48311ffdfa96 648 // Raise Delay Value
jmpin 6:68c6a50e1437 649 if(currentDecayVal < 5){
jmpin 6:68c6a50e1437 650 currentDecayVal++;
jmpin 6:68c6a50e1437 651 change_Decay_Table(currentDecayVal);
jmpin 6:68c6a50e1437 652 }
jmpin 4:406f59c6a1a6 653 else
jmpin 4:406f59c6a1a6 654 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 655 readyFlag = false;
Jake867 11:c87f55a3b9e0 656 }
jmpin 2:f06ba516b1ad 657 else if((keyPress == LOWER_DELAY_KEY) && (readyFlag)){ // button S pressed
jmpin 0:48311ffdfa96 658 // Lower Delay Value
jmpin 6:68c6a50e1437 659 if(currentDecayVal > 1){
jmpin 6:68c6a50e1437 660 currentDecayVal--;
jmpin 6:68c6a50e1437 661 change_Decay_Table(currentDecayVal);
jmpin 6:68c6a50e1437 662 }
jmpin 4:406f59c6a1a6 663 else
jmpin 6:68c6a50e1437 664 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 665 readyFlag = false;
Jake867 11:c87f55a3b9e0 666 }
jmpin 2:f06ba516b1ad 667 else if((keyPress == RAISE_SUSTAIN_KEY) && (readyFlag)){ // button E pressed
jmpin 0:48311ffdfa96 668 // Raise Sustain Value
jmpin 6:68c6a50e1437 669 if(currentSustainVal < 5){
jmpin 2:f06ba516b1ad 670 currentSustainVal++;
jmpin 6:68c6a50e1437 671 change_Sustain_Table(currentSustainVal);
jmpin 6:68c6a50e1437 672 }
jmpin 4:406f59c6a1a6 673 else
jmpin 4:406f59c6a1a6 674 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 675 readyFlag = false;
Jake867 11:c87f55a3b9e0 676 }
jmpin 2:f06ba516b1ad 677 else if((keyPress == LOWER_SUSTAIN_KEY) && (readyFlag)){ // button D pressed
jmpin 0:48311ffdfa96 678 // Lower Sustain Value
jmpin 6:68c6a50e1437 679 if(currentSustainVal > 1){
jmpin 2:f06ba516b1ad 680 currentSustainVal--;
jmpin 6:68c6a50e1437 681 change_Sustain_Table(currentSustainVal);
jmpin 6:68c6a50e1437 682 }
jmpin 4:406f59c6a1a6 683 else
jmpin 6:68c6a50e1437 684 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 685 readyFlag = false;
Jake867 11:c87f55a3b9e0 686 }
jmpin 2:f06ba516b1ad 687 else if((keyPress == RAISE_RELEASE_KEY) && (readyFlag)){ // button R pressed
jmpin 0:48311ffdfa96 688 // Raise Release Value
jmpin 6:68c6a50e1437 689 if(currentReleaseVal < 5){
jmpin 2:f06ba516b1ad 690 currentReleaseVal++;
jmpin 6:68c6a50e1437 691 change_Release_Table(currentReleaseVal);
jmpin 6:68c6a50e1437 692 }
jmpin 4:406f59c6a1a6 693 else
jmpin 4:406f59c6a1a6 694 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 695 readyFlag = false;
Jake867 11:c87f55a3b9e0 696 }
jmpin 2:f06ba516b1ad 697 else if((keyPress == LOWER_RELEASE_KEY) && (readyFlag)){ // button F pressed
jmpin 0:48311ffdfa96 698 // Lower Release Value
jmpin 6:68c6a50e1437 699 if(currentReleaseVal > 1){
jmpin 2:f06ba516b1ad 700 currentReleaseVal--;
jmpin 6:68c6a50e1437 701 change_Release_Table(currentReleaseVal);
jmpin 6:68c6a50e1437 702 }
jmpin 4:406f59c6a1a6 703 else
jmpin 6:68c6a50e1437 704 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 705 readyFlag = false;
Jake867 11:c87f55a3b9e0 706 }
jmpin 2:f06ba516b1ad 707 else if((keyPress == CHANGE_WAVESHAPE_UP) && (readyFlag)){ // button T pressed
jmpin 2:f06ba516b1ad 708 // Change waveform shape to next waveform type
jmpin 2:f06ba516b1ad 709 switch(myWave){
jmpin 2:f06ba516b1ad 710 case sine:
jmpin 2:f06ba516b1ad 711 myWave = square;
Jake867 11:c87f55a3b9e0 712 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 713 break;
jmpin 2:f06ba516b1ad 714 case square:
jmpin 2:f06ba516b1ad 715 myWave = sawtooth;
Jake867 11:c87f55a3b9e0 716 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 717 break;
jmpin 2:f06ba516b1ad 718 case sawtooth:
jmpin 2:f06ba516b1ad 719 myWave = sine;
Jake867 11:c87f55a3b9e0 720 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 721 break;
jmpin 2:f06ba516b1ad 722 default:
jmpin 2:f06ba516b1ad 723 break;
jmpin 2:f06ba516b1ad 724 }
Jake867 11:c87f55a3b9e0 725 readyFlag = false;
Jake867 11:c87f55a3b9e0 726 }
jmpin 2:f06ba516b1ad 727 else if((keyPress == CHANGE_WAVESHAPE_DOWN) && (readyFlag)){ // button G pressed
jmpin 2:f06ba516b1ad 728 // Change waveform shape to previous waveform type
jmpin 2:f06ba516b1ad 729 switch(myWave){
jmpin 2:f06ba516b1ad 730 case sine:
jmpin 2:f06ba516b1ad 731 myWave = sawtooth;
Jake867 11:c87f55a3b9e0 732 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 733 break;
jmpin 2:f06ba516b1ad 734 case square:
jmpin 2:f06ba516b1ad 735 myWave = sine;
Jake867 11:c87f55a3b9e0 736 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 737 break;
jmpin 2:f06ba516b1ad 738 case sawtooth:
jmpin 2:f06ba516b1ad 739 myWave = square;
Jake867 11:c87f55a3b9e0 740 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 741 break;
jmpin 2:f06ba516b1ad 742 default:
jmpin 2:f06ba516b1ad 743 break;
jmpin 2:f06ba516b1ad 744 }
Jake867 11:c87f55a3b9e0 745 readyFlag = false;
Jake867 11:c87f55a3b9e0 746
Jake867 11:c87f55a3b9e0 747 }
jmpin 2:f06ba516b1ad 748
jmpin 0:48311ffdfa96 749 //do other tasks in main - interrupts will process button message characters
Jake867 11:c87f55a3b9e0 750 //myled = 1;
Jake867 11:c87f55a3b9e0 751 // wait(2);
Jake867 11:c87f55a3b9e0 752 // myled = 0;
Jake867 11:c87f55a3b9e0 753 // wait(2);
jmpin 10:085c49fe2509 754 }
jmpin 0:48311ffdfa96 755 }