Bluetooth Enabled Keyboard/Synthesizer for mbed

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

Committer:
jmpin
Date:
Fri Apr 29 22:28:48 2016 +0000
Revision:
12:d60a9d0052a7
Parent:
11:c87f55a3b9e0
Child:
13:25d53936d385
Commented code, removed all stuff pertaining to lookup table implementation.;

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){
Jake867 11:c87f55a3b9e0 225 uLCD.locate(0,0);
jmpin 12:d60a9d0052a7 226 switch(myWave){
jmpin 12:d60a9d0052a7 227 case sine:
jmpin 12:d60a9d0052a7 228 uLCD.printf("Shape: Sine\r\n"); // if wave type is sine wave, display sine
jmpin 12:d60a9d0052a7 229 break;
jmpin 12:d60a9d0052a7 230 case square:
jmpin 12:d60a9d0052a7 231 uLCD.printf("Shape: Square\r\n"); // if wave type is square wave, display square
jmpin 12:d60a9d0052a7 232 break;
jmpin 12:d60a9d0052a7 233 case sawtooth:
jmpin 12:d60a9d0052a7 234 uLCD.printf("Shape: Sawtooth\r\n"); // if wave type is sawtooth wave, display sawtooth
jmpin 12:d60a9d0052a7 235 break;
jmpin 12:d60a9d0052a7 236 default:
jmpin 12:d60a9d0052a7 237 break;
jmpin 12:d60a9d0052a7 238 }
jmpin 12:d60a9d0052a7 239 uLCD.printf("Octave: %i\r\n",currentOctave); // displays octave
jmpin 12:d60a9d0052a7 240 uLCD.printf("Attack: %i\r\n",currentAttackVal); // displays attack value
jmpin 12:d60a9d0052a7 241 uLCD.printf("Decay: %i\r\n",currentDecayVal); // displays decay value
jmpin 12:d60a9d0052a7 242 uLCD.printf("Sustain: %i\r\n",currentSustainVal); // displays sustain value
jmpin 12:d60a9d0052a7 243 uLCD.printf("Release: %i\r\n",currentReleaseVal); // displays release value
Jake867 11:c87f55a3b9e0 244 }
jmpin 12:d60a9d0052a7 245 }
jmpin 6:68c6a50e1437 246
jmpin 12:d60a9d0052a7 247 void clear_Buffer(void){ // clears buffer that holds samples
jmpin 9:e4df1a31a098 248 sampleBuffer.clear();
jmpin 9:e4df1a31a098 249 }
jmpin 9:e4df1a31a098 250
jmpin 12:d60a9d0052a7 251 void set_Note_Freq(int frequency){ // updates the frequency of the note being played
jmpin 6:68c6a50e1437 252 noteFreq = frequency;
jmpin 9:e4df1a31a098 253 clear_Buffer();
jmpin 6:68c6a50e1437 254 }
jmpin 6:68c6a50e1437 255
jmpin 12:d60a9d0052a7 256 void change_Attack_Table(int attackVal) // change which table of coefficients to use for altering the attack portion of the waveform
jmpin 6:68c6a50e1437 257 {
jmpin 6:68c6a50e1437 258 switch(attackVal){
jmpin 6:68c6a50e1437 259 case 5:
jmpin 6:68c6a50e1437 260 currentAttackTable = attackVals5;
jmpin 6:68c6a50e1437 261 break;
jmpin 6:68c6a50e1437 262 case 4:
jmpin 6:68c6a50e1437 263 currentAttackTable = attackVals4;
jmpin 6:68c6a50e1437 264 break;
jmpin 6:68c6a50e1437 265 case 3:
jmpin 6:68c6a50e1437 266 currentAttackTable = attackVals3;
jmpin 6:68c6a50e1437 267 break;
jmpin 6:68c6a50e1437 268 case 2:
jmpin 6:68c6a50e1437 269 currentAttackTable = attackVals2;
jmpin 6:68c6a50e1437 270 break;
jmpin 6:68c6a50e1437 271 case 1:
jmpin 6:68c6a50e1437 272 currentAttackTable = attackVals1;
jmpin 6:68c6a50e1437 273 break;
jmpin 6:68c6a50e1437 274 default:
jmpin 6:68c6a50e1437 275 break;
jmpin 6:68c6a50e1437 276 }
jmpin 6:68c6a50e1437 277 }
jmpin 5:afd67e985df0 278
jmpin 12:d60a9d0052a7 279 void change_Decay_Table(int decayVal) // change which table of coefficients to use for altering the decay portion of the waveform
jmpin 6:68c6a50e1437 280 {
jmpin 6:68c6a50e1437 281 switch(decayVal){
jmpin 6:68c6a50e1437 282 case 5:
jmpin 6:68c6a50e1437 283 currentDecayTable = decayVals5;
jmpin 6:68c6a50e1437 284 break;
jmpin 6:68c6a50e1437 285 case 4:
jmpin 6:68c6a50e1437 286 currentDecayTable = decayVals4;
jmpin 6:68c6a50e1437 287 break;
jmpin 6:68c6a50e1437 288 case 3:
jmpin 6:68c6a50e1437 289 currentDecayTable = decayVals3;
jmpin 6:68c6a50e1437 290 break;
jmpin 6:68c6a50e1437 291 case 2:
jmpin 6:68c6a50e1437 292 currentDecayTable = decayVals2;
jmpin 6:68c6a50e1437 293 break;
jmpin 6:68c6a50e1437 294 case 1:
jmpin 6:68c6a50e1437 295 currentDecayTable = decayVals1;
jmpin 6:68c6a50e1437 296 break;
jmpin 6:68c6a50e1437 297 default:
jmpin 6:68c6a50e1437 298 break;
jmpin 6:68c6a50e1437 299 }
jmpin 6:68c6a50e1437 300 }
jmpin 5:afd67e985df0 301
jmpin 12:d60a9d0052a7 302 void change_Sustain_Table(int sustainVal) // change which table of coefficients to use for altering the sustain portion of the waveform
jmpin 6:68c6a50e1437 303 {
jmpin 6:68c6a50e1437 304 switch(sustainVal){
jmpin 6:68c6a50e1437 305 case 5:
jmpin 6:68c6a50e1437 306 currentSustainTable = sustainVals5;
jmpin 6:68c6a50e1437 307 break;
jmpin 6:68c6a50e1437 308 case 4:
jmpin 6:68c6a50e1437 309 currentSustainTable = sustainVals4;
jmpin 6:68c6a50e1437 310 break;
jmpin 6:68c6a50e1437 311 case 3:
jmpin 6:68c6a50e1437 312 currentSustainTable = sustainVals3;
jmpin 6:68c6a50e1437 313 break;
jmpin 6:68c6a50e1437 314 case 2:
jmpin 6:68c6a50e1437 315 currentSustainTable = sustainVals2;
jmpin 6:68c6a50e1437 316 break;
jmpin 6:68c6a50e1437 317 case 1:
jmpin 6:68c6a50e1437 318 currentSustainTable = sustainVals1;
jmpin 6:68c6a50e1437 319 break;
jmpin 6:68c6a50e1437 320 default:
jmpin 6:68c6a50e1437 321 break;
jmpin 6:68c6a50e1437 322 }
jmpin 6:68c6a50e1437 323 }
jmpin 6:68c6a50e1437 324
jmpin 12:d60a9d0052a7 325 void change_Release_Table(int releaseVal) // change which table of coefficients to use for altering the release portion of the waveform
jmpin 6:68c6a50e1437 326 {
jmpin 6:68c6a50e1437 327 switch(releaseVal){
jmpin 6:68c6a50e1437 328 case 5:
jmpin 6:68c6a50e1437 329 currentReleaseTable = releaseVals5;
jmpin 6:68c6a50e1437 330 break;
jmpin 6:68c6a50e1437 331 case 4:
jmpin 6:68c6a50e1437 332 currentReleaseTable = releaseVals4;
jmpin 6:68c6a50e1437 333 break;
jmpin 6:68c6a50e1437 334 case 3:
jmpin 6:68c6a50e1437 335 currentReleaseTable = releaseVals3;
jmpin 6:68c6a50e1437 336 break;
jmpin 6:68c6a50e1437 337 case 2:
jmpin 6:68c6a50e1437 338 currentReleaseTable = releaseVals2;
jmpin 6:68c6a50e1437 339 break;
jmpin 6:68c6a50e1437 340 case 1:
jmpin 6:68c6a50e1437 341 currentReleaseTable = releaseVals1;
jmpin 6:68c6a50e1437 342 break;
jmpin 6:68c6a50e1437 343 default:
jmpin 6:68c6a50e1437 344 break;
jmpin 6:68c6a50e1437 345 }
jmpin 6:68c6a50e1437 346 }
jmpin 12:d60a9d0052a7 347
jmpin 12:d60a9d0052a7 348 /* Having different sustain values for the amplitude of the wave would make the math neccesary to generate the other
jmpin 12:d60a9d0052a7 349 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 350 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 351 */
jmpin 12:d60a9d0052a7 352
jmpin 12:d60a9d0052a7 353 void initialize_sustainVals()
jmpin 8:f6699fd30737 354 {
jmpin 8:f6699fd30737 355 for(int j = 0; j < 160; j++)
jmpin 8:f6699fd30737 356 {
jmpin 8:f6699fd30737 357 sustainVals5[j] = .6;
jmpin 8:f6699fd30737 358 sustainVals4[j] = .6;
jmpin 8:f6699fd30737 359 sustainVals3[j] = .6;
jmpin 8:f6699fd30737 360 sustainVals2[j] = .6;
jmpin 8:f6699fd30737 361 sustainVals1[j] = .6;
jmpin 8:f6699fd30737 362 }
jmpin 8:f6699fd30737 363 }
jmpin 12:d60a9d0052a7 364 /* 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 365 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 366 are applied to samples 65 - 224, and the release coefficients are appplied to samples 225-256.
jmpin 12:d60a9d0052a7 367 */
jmpin 6:68c6a50e1437 368
jmpin 12:d60a9d0052a7 369
jmpin 12:d60a9d0052a7 370 void apply_Envelope(void){
jmpin 6:68c6a50e1437 371 int attack_range, decay_range, sustain_range, release_range;
jmpin 6:68c6a50e1437 372 attack_range = sampleBuffer.size() * (1/8); // The attack portion of the waveform will take (1/8) of the note's duration
jmpin 6:68c6a50e1437 373 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 374 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 375 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 376 for(int i = 0; i < attack_range; i++)
jmpin 6:68c6a50e1437 377 {
jmpin 6:68c6a50e1437 378 sampleBuffer[i] = sampleBuffer[i] * currentAttackTable[i];
jmpin 6:68c6a50e1437 379 }
jmpin 6:68c6a50e1437 380 for(int k = attack_range; k < decay_range; k++)
jmpin 6:68c6a50e1437 381 {
jmpin 6:68c6a50e1437 382 sampleBuffer[k] = sampleBuffer[k] * currentDecayTable[k-attack_range];
jmpin 6:68c6a50e1437 383 }
jmpin 6:68c6a50e1437 384 for(int m = decay_range; m < sustain_range; m++)
jmpin 6:68c6a50e1437 385 {
jmpin 6:68c6a50e1437 386 sampleBuffer[m] = sampleBuffer[m] * currentSustainTable[m-decay_range];
jmpin 6:68c6a50e1437 387 }
jmpin 6:68c6a50e1437 388 for(int n = sustain_range; n < release_range; n++)
jmpin 6:68c6a50e1437 389 {
jmpin 6:68c6a50e1437 390 sampleBuffer[n] = sampleBuffer[n] * currentReleaseTable[n-sustain_range];
jmpin 6:68c6a50e1437 391 }
jmpin 6:68c6a50e1437 392 }
jmpin 5:afd67e985df0 393
jmpin 12:d60a9d0052a7 394 void generate_sineWave(int frequency) // Generates samples for a sine wave of a given input frequency
jmpin 9:e4df1a31a098 395 {
jmpin 12:d60a9d0052a7 396 double t = 0; // Represents time, since we want each note to last 2 seconds and have 256 samples
jmpin 9:e4df1a31a098 397 for(int i = 0; i < 256 ; i++)
jmpin 9:e4df1a31a098 398 {
jmpin 9:e4df1a31a098 399 sampleBuffer.push_back(((sin(2*(PI)*frequency*t)) + 1)/2); // scaled to be a % of maximum output voltage (3.3V)
jmpin 9:e4df1a31a098 400 t = t + timeIncrement; // increment t for calculation of next value in the waveform
jmpin 9:e4df1a31a098 401 }
jmpin 9:e4df1a31a098 402 }
jmpin 9:e4df1a31a098 403
jmpin 12:d60a9d0052a7 404 void generate_sawtoothWave(int frequency) // Generates samples for a sawtooth wave of a given input frequency
jmpin 9:e4df1a31a098 405 {
jmpin 12:d60a9d0052a7 406 double t = 0; // Represents time, since we want each note to last 2 seconds and have 256 samples
jmpin 9:e4df1a31a098 407 for(int i = 0; i<256 ; i++)
jmpin 9:e4df1a31a098 408 {
jmpin 9:e4df1a31a098 409 sampleBuffer.push_back((2*(t*frequency) - (.5 + (t*frequency)) + 1) / 2);
jmpin 12:d60a9d0052a7 410 t = t + timeIncrement; // increment t for calculation of next value in the waveform
jmpin 9:e4df1a31a098 411 }
jmpin 9:e4df1a31a098 412 }
jmpin 9:e4df1a31a098 413
jmpin 12:d60a9d0052a7 414 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 415 {
jmpin 12:d60a9d0052a7 416 double width = (1 / 2 * frequency); // Width of a half period of the square wave
jmpin 12:d60a9d0052a7 417 double t = 0; // Represents time, since we want a 2 second note with 256 samples
jmpin 9:e4df1a31a098 418 for(int i = 0; i < 256; i++)
jmpin 9:e4df1a31a098 419 {
jmpin 9:e4df1a31a098 420 if(((int)(t / width) % 2 ) == 0) // Even, write a 1 for the square wave
jmpin 9:e4df1a31a098 421 sampleBuffer.push_back(1.0);
jmpin 9:e4df1a31a098 422 else // Odd, write a 0 for the square wave
jmpin 9:e4df1a31a098 423 sampleBuffer.push_back(0.0);
jmpin 9:e4df1a31a098 424 t = t + timeIncrement; // increment t for calculation of next value in the waveform
jmpin 9:e4df1a31a098 425 }
jmpin 9:e4df1a31a098 426 }
jmpin 9:e4df1a31a098 427
jmpin 12:d60a9d0052a7 428 /* Generates the waveforms that will be output to the AnalogOut pin after being altered by the ADSR coefficient matrices.
jmpin 12:d60a9d0052a7 429 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 430 @param: frequency - the frequency of the waveform to be generated
jmpin 12:d60a9d0052a7 431 @param: currentWaveType - the shape of the wave that needs to be generated
jmpin 12:d60a9d0052a7 432 */
jmpin 12:d60a9d0052a7 433
jmpin 12:d60a9d0052a7 434
jmpin 9:e4df1a31a098 435 void create_samples(int frequency, WaveType currentWaveType)
jmpin 8:f6699fd30737 436 {
jmpin 8:f6699fd30737 437 switch(currentWaveType){
jmpin 8:f6699fd30737 438 case sine:
jmpin 8:f6699fd30737 439 //Generate sine wave values
jmpin 8:f6699fd30737 440 generate_sineWave(frequency);
jmpin 8:f6699fd30737 441 apply_Envelope();
jmpin 8:f6699fd30737 442 break;
jmpin 8:f6699fd30737 443 case square:
jmpin 8:f6699fd30737 444 //Generate square wave values
jmpin 8:f6699fd30737 445 generate_squareWave(frequency);
jmpin 9:e4df1a31a098 446 //apply_Envelope();
jmpin 8:f6699fd30737 447 break;
jmpin 8:f6699fd30737 448 case sawtooth:
jmpin 8:f6699fd30737 449 //Generate sawtooth wave values
jmpin 8:f6699fd30737 450 generate_sawtoothWave(frequency);
jmpin 9:e4df1a31a098 451 //apply_Envelope();
jmpin 8:f6699fd30737 452 break;
jmpin 8:f6699fd30737 453 default:
jmpin 8:f6699fd30737 454 break;
jmpin 8:f6699fd30737 455 }
jmpin 8:f6699fd30737 456 }
jmpin 5:afd67e985df0 457
jmpin 5:afd67e985df0 458
jmpin 12:d60a9d0052a7 459 /* Outputs the samples that are currently in the buffer one at a time. There is a period of time
jmpin 12:d60a9d0052a7 460 where the program waits so that the 256 samples fill up the entire 2 seconds. The buffer is cleared
jmpin 12:d60a9d0052a7 461 after the output is finished so that next time the buffer will be ready for new samples.
jmpin 12:d60a9d0052a7 462 */
jmpin 8:f6699fd30737 463 void output_samples()
jmpin 8:f6699fd30737 464 {
jmpin 8:f6699fd30737 465 for( int sample = 0; sample < 256; sample++)
jmpin 8:f6699fd30737 466 {
jmpin 8:f6699fd30737 467 synthPin = sampleBuffer[sample];
jmpin 8:f6699fd30737 468 Thread::wait(timeIncrement * 1000);
jmpin 8:f6699fd30737 469 }
jmpin 12:d60a9d0052a7 470 clear_Buffer();
jmpin 9:e4df1a31a098 471 }
jmpin 8:f6699fd30737 472
jmpin 8:f6699fd30737 473
jmpin 8:f6699fd30737 474
jmpin 5:afd67e985df0 475
jmpin 0:48311ffdfa96 476 //Interrupt routine to parse message with one new character per serial RX interrupt
jmpin 0:48311ffdfa96 477 void parse_message()
jmpin 0:48311ffdfa96 478 {
jmpin 12:d60a9d0052a7 479 //PC.printf("Parse_message was called");
Jake867 11:c87f55a3b9e0 480 while(Blue.readable())
jmpin 10:085c49fe2509 481 {
Jake867 11:c87f55a3b9e0 482 keyPress = Blue.getc();
jmpin 12:d60a9d0052a7 483 //PC.putc(keyPress);
Jake867 11:c87f55a3b9e0 484 readyFlag = true;
jmpin 12:d60a9d0052a7 485 //PC.printf("\n\r Value of readyFlag is: %i",readyFlag);
jmpin 12:d60a9d0052a7 486 //PC.printf("Value of keyPress is: %c\n\r",keyPress);
jmpin 12:d60a9d0052a7 487 //wait(1);
jmpin 10:085c49fe2509 488 }
jmpin 0:48311ffdfa96 489 }
jmpin 3:3aba1d783730 490
jmpin 3:3aba1d783730 491
jmpin 3:3aba1d783730 492 /*
jmpin 3:3aba1d783730 493 This function writes which note was just played to a text file on the SDCard.
jmpin 3:3aba1d783730 494 The note played will be encoded in hexadecimal, as well as the octave, Attack Value,
jmpin 3:3aba1d783730 495 Delay Value, Sustain Value, and Release Value. The format of the bits will be as follows:
jmpin 3:3aba1d783730 496 | 3 bits | 3 bits | 3 bits | 3 bits | 3 bits | 3 bits |
jmpin 6:68c6a50e1437 497 | Attack | Decay | Susttain | Release | Octave | Note |
jmpin 3:3aba1d783730 498 For the 3 bits representing note, A will correspond to 1, B to 2, and so on.
jmpin 3:3aba1d783730 499 For example, if the lower 3 bits corresponding to note are 001, then the note is an A.
jmpin 3:3aba1d783730 500
jmpin 3:3aba1d783730 501 @param: The note that is being played/recorded into the text file
jmpin 3:3aba1d783730 502 */
jmpin 3:3aba1d783730 503
jmpin 3:3aba1d783730 504 void write_to_SDCard(char note)
jmpin 3:3aba1d783730 505 {
jmpin 6:68c6a50e1437 506 int AttackBits, SustainBits, DecayBits, ReleaseBits, OctaveBits, NoteBits;
jmpin 2:f06ba516b1ad 507
jmpin 12:d60a9d0052a7 508 AttackBits = currentAttackVal; // Holds the value of the attack parameter
jmpin 12:d60a9d0052a7 509 DecayBits = currentDecayVal; // Holds the value of the decay parameter
jmpin 12:d60a9d0052a7 510 SustainBits = currentSustainVal;// Holds the value of the sustain parameter
jmpin 12:d60a9d0052a7 511 ReleaseBits = currentReleaseVal;// Holds the value of the release parameter
jmpin 3:3aba1d783730 512 OctaveBits = currentOctave;
jmpin 3:3aba1d783730 513 switch(note){
jmpin 12:d60a9d0052a7 514 case 'C': // a C corresponds to a 3
jmpin 3:3aba1d783730 515 NoteBits = 3;
jmpin 3:3aba1d783730 516 break;
jmpin 3:3aba1d783730 517 case 'D':
jmpin 12:d60a9d0052a7 518 NoteBits = 4; // a D corresponds to a 4
jmpin 3:3aba1d783730 519 break;
jmpin 3:3aba1d783730 520 case 'E':
jmpin 12:d60a9d0052a7 521 NoteBits = 5; // an E corresponds to a 5
jmpin 3:3aba1d783730 522 break;
jmpin 3:3aba1d783730 523 case 'F':
jmpin 12:d60a9d0052a7 524 NoteBits = 6; // an F corresponds to a 6
jmpin 3:3aba1d783730 525 break;
jmpin 3:3aba1d783730 526 case 'G':
jmpin 12:d60a9d0052a7 527 NoteBits = 7; // a G corresponds to a 7
jmpin 3:3aba1d783730 528 break;
jmpin 3:3aba1d783730 529 case 'A':
jmpin 12:d60a9d0052a7 530 NoteBits = 1; // an A corresponds to a 1
jmpin 3:3aba1d783730 531 break;
jmpin 3:3aba1d783730 532 case 'B':
jmpin 12:d60a9d0052a7 533 NoteBits = 2; // a B corresponds to a 2
jmpin 3:3aba1d783730 534 break;
jmpin 3:3aba1d783730 535 default:
jmpin 3:3aba1d783730 536 NoteBits = 0;
jmpin 3:3aba1d783730 537 break;
jmpin 3:3aba1d783730 538 }
jmpin 3:3aba1d783730 539 int writeVal;
jmpin 6:68c6a50e1437 540 writeVal = (AttackBits << 15) | (DecayBits << 12) | (SustainBits << 9) | (ReleaseBits << 6) | (OctaveBits << 3) | (NoteBits);
jmpin 3:3aba1d783730 541
jmpin 12:d60a9d0052a7 542 FILE *fp = fopen("/sd/noteRecords/note_record_01.txt", "w"); // creates handle for file we want to write to
jmpin 3:3aba1d783730 543 if(fp == NULL) {
jmpin 12:d60a9d0052a7 544 error("Could not open file for write\n"); // if this is not a valid name, tell user there is an error
jmpin 3:3aba1d783730 545 }
jmpin 3:3aba1d783730 546 fprintf(fp,"%X\r\n",writeVal); // writes value to the text file in hexadecimal
jmpin 3:3aba1d783730 547 fclose(fp);
jmpin 3:3aba1d783730 548 }
jmpin 3:3aba1d783730 549
jmpin 0:48311ffdfa96 550 int main()
jmpin 0:48311ffdfa96 551 {
jmpin 12:d60a9d0052a7 552 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 553
jmpin 12:d60a9d0052a7 554
jmpin 12:d60a9d0052a7 555 mkdir("/sd/noteRecords", 0777); // make directory to hold the record of notes played
jmpin 3:3aba1d783730 556
jmpin 12:d60a9d0052a7 557 initialize_sustainVals(); // fill the lookup tables with the sustain values in them
Jake867 11:c87f55a3b9e0 558
jmpin 12:d60a9d0052a7 559 PC.baud(9600); // setup baud rate for PC serial connection
jmpin 12:d60a9d0052a7 560 Blue.baud(9600); // setup baud rate for bluetooth serial connection
jmpin 3:3aba1d783730 561
jmpin 12:d60a9d0052a7 562
jmpin 12:d60a9d0052a7 563 Blue.attach(&parse_message,Serial::RxIrq); //attach interrupt function for each new Bluetooth serial character
jmpin 0:48311ffdfa96 564 while(1) {
jmpin 0:48311ffdfa96 565 //check for a new button message ready
jmpin 12:d60a9d0052a7 566 if((keyPress == C_NOTE_KEY) && (readyFlag)){ // button Z pressed
jmpin 12:d60a9d0052a7 567 set_Note_Freq(noteArray[currentOctave-1][0]); // set the note frequency to the proper value
jmpin 9:e4df1a31a098 568 create_samples(noteFreq, myWave);
jmpin 3:3aba1d783730 569 write_to_SDCard('C');
Jake867 11:c87f55a3b9e0 570 readyFlag = false;
jmpin 0:48311ffdfa96 571 // Play note that corresponds to Z
Jake867 11:c87f55a3b9e0 572 }
jmpin 3:3aba1d783730 573 else if((keyPress == D_NOTE_KEY) && (readyFlag)){ // button X pressed
jmpin 6:68c6a50e1437 574 set_Note_Freq(noteArray[currentOctave-1][1]);
jmpin 9:e4df1a31a098 575 create_samples(noteFreq, myWave);
jmpin 3:3aba1d783730 576 write_to_SDCard('D');
jmpin 4:406f59c6a1a6 577 readyFlag = false;
jmpin 0:48311ffdfa96 578 // Play note that corresponds to X
Jake867 11:c87f55a3b9e0 579 }
jmpin 3:3aba1d783730 580 else if((keyPress == E_NOTE_KEY) && (readyFlag)){ // button C pressed
jmpin 6:68c6a50e1437 581 set_Note_Freq(noteArray[currentOctave-1][2]);
jmpin 9:e4df1a31a098 582 create_samples(noteFreq, myWave);
jmpin 0:48311ffdfa96 583 // Play note that corresponds to C
jmpin 3:3aba1d783730 584 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 585 write_to_SDCard('E');
jmpin 4:406f59c6a1a6 586 readyFlag = false;
Jake867 11:c87f55a3b9e0 587 }
jmpin 3:3aba1d783730 588 else if((keyPress == F_NOTE_KEY) && (readyFlag)){ // button V pressed
jmpin 6:68c6a50e1437 589 set_Note_Freq(noteArray[currentOctave-1][3]);
jmpin 9:e4df1a31a098 590 create_samples(noteFreq, myWave);
jmpin 0:48311ffdfa96 591 // Play note that corresponds to V
jmpin 3:3aba1d783730 592 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 593 write_to_SDCard('F');
jmpin 4:406f59c6a1a6 594 readyFlag = false;
Jake867 11:c87f55a3b9e0 595 }
jmpin 3:3aba1d783730 596 else if((keyPress == G_NOTE_KEY) && (readyFlag)){ // button B pressed
jmpin 6:68c6a50e1437 597 set_Note_Freq(noteArray[currentOctave-1][4]);
jmpin 9:e4df1a31a098 598 create_samples(noteFreq, myWave);
jmpin 0:48311ffdfa96 599 // Play note that corresponds to B
jmpin 3:3aba1d783730 600 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 601 write_to_SDCard('G');
jmpin 4:406f59c6a1a6 602 readyFlag = false;
Jake867 11:c87f55a3b9e0 603 }
jmpin 3:3aba1d783730 604 else if((keyPress == A_NOTE_KEY) && (readyFlag)){ // button N pressed
jmpin 6:68c6a50e1437 605 set_Note_Freq(noteArray[currentOctave][5]);
jmpin 9:e4df1a31a098 606 create_samples(noteFreq, myWave);
jmpin 0:48311ffdfa96 607 // Play note that corresponds to N
jmpin 3:3aba1d783730 608 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 609 write_to_SDCard('A');
jmpin 4:406f59c6a1a6 610 readyFlag = false;
Jake867 11:c87f55a3b9e0 611 }
jmpin 3:3aba1d783730 612 else if((keyPress == B_NOTE_KEY) && (readyFlag)){ // button M pressed
jmpin 6:68c6a50e1437 613 set_Note_Freq(noteArray[currentOctave][6]);
jmpin 9:e4df1a31a098 614 create_samples(noteFreq, myWave);
jmpin 0:48311ffdfa96 615 // Play note that corresponds to M
jmpin 3:3aba1d783730 616 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 617 write_to_SDCard('B');
jmpin 4:406f59c6a1a6 618 readyFlag = false;
Jake867 11:c87f55a3b9e0 619 }
jmpin 2:f06ba516b1ad 620 else if((keyPress == RAISE_OCTAVE_KEY) && (readyFlag)){ // button O pressed
jmpin 0:48311ffdfa96 621 // Raise an octave
jmpin 6:68c6a50e1437 622 if(currentOctave < 7)
jmpin 2:f06ba516b1ad 623 currentOctave++;
jmpin 4:406f59c6a1a6 624 else
jmpin 4:406f59c6a1a6 625 printf("Cannot raise octave above 7.\r\n");
Jake867 11:c87f55a3b9e0 626 readyFlag = false;
Jake867 11:c87f55a3b9e0 627 }
jmpin 2:f06ba516b1ad 628 else if((keyPress == LOWER_OCTAVE_KEY) && (readyFlag)){ // button L pressed
jmpin 2:f06ba516b1ad 629 // Lower an octave
jmpin 4:406f59c6a1a6 630 if(currentOctave > 1)
jmpin 2:f06ba516b1ad 631 currentOctave--;
jmpin 4:406f59c6a1a6 632 else
jmpin 4:406f59c6a1a6 633 printf("Cannot lower octave below 1.\r\n");
Jake867 11:c87f55a3b9e0 634 readyFlag = false;
Jake867 11:c87f55a3b9e0 635 }
jmpin 2:f06ba516b1ad 636 else if((keyPress == RAISE_ATTACK_KEY) && (readyFlag)){ // button Q pressed
jmpin 0:48311ffdfa96 637 // Raise Attack Value
jmpin 6:68c6a50e1437 638 if(currentAttackVal < 5){
jmpin 2:f06ba516b1ad 639 currentAttackVal++;
jmpin 6:68c6a50e1437 640 change_Attack_Table(currentAttackVal);
jmpin 6:68c6a50e1437 641 }
jmpin 4:406f59c6a1a6 642 else
jmpin 4:406f59c6a1a6 643 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 644 readyFlag = false;
Jake867 11:c87f55a3b9e0 645 }
jmpin 2:f06ba516b1ad 646 else if((keyPress == LOWER_ATTACK_KEY) && (readyFlag)){ // button A pressed
jmpin 0:48311ffdfa96 647 // Lower Attack Value
jmpin 6:68c6a50e1437 648 if(currentAttackVal > 1){
jmpin 2:f06ba516b1ad 649 currentAttackVal--;
jmpin 6:68c6a50e1437 650 change_Attack_Table(currentAttackVal);
jmpin 6:68c6a50e1437 651 }
jmpin 4:406f59c6a1a6 652 else
jmpin 6:68c6a50e1437 653 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 654 readyFlag = false;
Jake867 11:c87f55a3b9e0 655 }
jmpin 2:f06ba516b1ad 656 else if((keyPress == RAISE_DELAY_KEY) && (readyFlag)){ // button W pressed
jmpin 0:48311ffdfa96 657 // Raise Delay Value
jmpin 6:68c6a50e1437 658 if(currentDecayVal < 5){
jmpin 6:68c6a50e1437 659 currentDecayVal++;
jmpin 6:68c6a50e1437 660 change_Decay_Table(currentDecayVal);
jmpin 6:68c6a50e1437 661 }
jmpin 4:406f59c6a1a6 662 else
jmpin 4:406f59c6a1a6 663 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 664 readyFlag = false;
Jake867 11:c87f55a3b9e0 665 }
jmpin 2:f06ba516b1ad 666 else if((keyPress == LOWER_DELAY_KEY) && (readyFlag)){ // button S pressed
jmpin 0:48311ffdfa96 667 // Lower Delay Value
jmpin 6:68c6a50e1437 668 if(currentDecayVal > 1){
jmpin 6:68c6a50e1437 669 currentDecayVal--;
jmpin 6:68c6a50e1437 670 change_Decay_Table(currentDecayVal);
jmpin 6:68c6a50e1437 671 }
jmpin 4:406f59c6a1a6 672 else
jmpin 6:68c6a50e1437 673 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 674 readyFlag = false;
Jake867 11:c87f55a3b9e0 675 }
jmpin 2:f06ba516b1ad 676 else if((keyPress == RAISE_SUSTAIN_KEY) && (readyFlag)){ // button E pressed
jmpin 0:48311ffdfa96 677 // Raise Sustain Value
jmpin 6:68c6a50e1437 678 if(currentSustainVal < 5){
jmpin 2:f06ba516b1ad 679 currentSustainVal++;
jmpin 6:68c6a50e1437 680 change_Sustain_Table(currentSustainVal);
jmpin 6:68c6a50e1437 681 }
jmpin 4:406f59c6a1a6 682 else
jmpin 4:406f59c6a1a6 683 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 684 readyFlag = false;
Jake867 11:c87f55a3b9e0 685 }
jmpin 2:f06ba516b1ad 686 else if((keyPress == LOWER_SUSTAIN_KEY) && (readyFlag)){ // button D pressed
jmpin 0:48311ffdfa96 687 // Lower Sustain Value
jmpin 6:68c6a50e1437 688 if(currentSustainVal > 1){
jmpin 2:f06ba516b1ad 689 currentSustainVal--;
jmpin 6:68c6a50e1437 690 change_Sustain_Table(currentSustainVal);
jmpin 6:68c6a50e1437 691 }
jmpin 4:406f59c6a1a6 692 else
jmpin 6:68c6a50e1437 693 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 694 readyFlag = false;
Jake867 11:c87f55a3b9e0 695 }
jmpin 2:f06ba516b1ad 696 else if((keyPress == RAISE_RELEASE_KEY) && (readyFlag)){ // button R pressed
jmpin 0:48311ffdfa96 697 // Raise Release Value
jmpin 6:68c6a50e1437 698 if(currentReleaseVal < 5){
jmpin 2:f06ba516b1ad 699 currentReleaseVal++;
jmpin 6:68c6a50e1437 700 change_Release_Table(currentReleaseVal);
jmpin 6:68c6a50e1437 701 }
jmpin 4:406f59c6a1a6 702 else
jmpin 4:406f59c6a1a6 703 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 704 readyFlag = false;
Jake867 11:c87f55a3b9e0 705 }
jmpin 2:f06ba516b1ad 706 else if((keyPress == LOWER_RELEASE_KEY) && (readyFlag)){ // button F pressed
jmpin 0:48311ffdfa96 707 // Lower Release Value
jmpin 6:68c6a50e1437 708 if(currentReleaseVal > 1){
jmpin 2:f06ba516b1ad 709 currentReleaseVal--;
jmpin 6:68c6a50e1437 710 change_Release_Table(currentReleaseVal);
jmpin 6:68c6a50e1437 711 }
jmpin 4:406f59c6a1a6 712 else
jmpin 6:68c6a50e1437 713 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 714 readyFlag = false;
Jake867 11:c87f55a3b9e0 715 }
jmpin 2:f06ba516b1ad 716 else if((keyPress == CHANGE_WAVESHAPE_UP) && (readyFlag)){ // button T pressed
jmpin 2:f06ba516b1ad 717 // Change waveform shape to next waveform type
jmpin 2:f06ba516b1ad 718 switch(myWave){
jmpin 2:f06ba516b1ad 719 case sine:
jmpin 2:f06ba516b1ad 720 myWave = square;
Jake867 11:c87f55a3b9e0 721 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 722 break;
jmpin 2:f06ba516b1ad 723 case square:
jmpin 2:f06ba516b1ad 724 myWave = sawtooth;
Jake867 11:c87f55a3b9e0 725 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 726 break;
jmpin 2:f06ba516b1ad 727 case sawtooth:
jmpin 2:f06ba516b1ad 728 myWave = sine;
Jake867 11:c87f55a3b9e0 729 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 730 break;
jmpin 2:f06ba516b1ad 731 default:
jmpin 2:f06ba516b1ad 732 break;
jmpin 2:f06ba516b1ad 733 }
Jake867 11:c87f55a3b9e0 734 readyFlag = false;
Jake867 11:c87f55a3b9e0 735 }
jmpin 2:f06ba516b1ad 736 else if((keyPress == CHANGE_WAVESHAPE_DOWN) && (readyFlag)){ // button G pressed
jmpin 2:f06ba516b1ad 737 // Change waveform shape to previous waveform type
jmpin 2:f06ba516b1ad 738 switch(myWave){
jmpin 2:f06ba516b1ad 739 case sine:
jmpin 2:f06ba516b1ad 740 myWave = sawtooth;
Jake867 11:c87f55a3b9e0 741 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 742 break;
jmpin 2:f06ba516b1ad 743 case square:
jmpin 2:f06ba516b1ad 744 myWave = sine;
Jake867 11:c87f55a3b9e0 745 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 746 break;
jmpin 2:f06ba516b1ad 747 case sawtooth:
jmpin 2:f06ba516b1ad 748 myWave = square;
Jake867 11:c87f55a3b9e0 749 //change_Wave(myWave);
jmpin 2:f06ba516b1ad 750 break;
jmpin 2:f06ba516b1ad 751 default:
jmpin 2:f06ba516b1ad 752 break;
jmpin 2:f06ba516b1ad 753 }
Jake867 11:c87f55a3b9e0 754 readyFlag = false;
Jake867 11:c87f55a3b9e0 755
Jake867 11:c87f55a3b9e0 756 }
jmpin 2:f06ba516b1ad 757
jmpin 0:48311ffdfa96 758 //do other tasks in main - interrupts will process button message characters
Jake867 11:c87f55a3b9e0 759 //myled = 1;
Jake867 11:c87f55a3b9e0 760 // wait(2);
Jake867 11:c87f55a3b9e0 761 // myled = 0;
Jake867 11:c87f55a3b9e0 762 // wait(2);
jmpin 10:085c49fe2509 763 }
jmpin 0:48311ffdfa96 764 }