Bluetooth Enabled Keyboard/Synthesizer for mbed

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

Committer:
jmpin
Date:
Fri Apr 29 22:43:06 2016 +0000
Revision:
17:7dd298995c14
Parent:
16:e01a77428828
Child:
18:24f56dcc5a59
Added function that outputs samples to p18 to the main loop where key presses are processed.

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