Bluetooth Enabled Keyboard/Synthesizer for mbed

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

Committer:
Jake867
Date:
Sat Apr 30 20:44:20 2016 +0000
Revision:
21:0df25c61c475
Parent:
19:2f635d03467c
Child:
22:9c80f7bcef86
Enabled playback for all waveform types and notes.

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 21:0df25c61c475 7 #include "Speaker.h"
Jake867 11:c87f55a3b9e0 8 RawSerial Blue(p13,p14);
Jake867 11:c87f55a3b9e0 9 RawSerial PC(USBTX,USBRX);
jmpin 0:48311ffdfa96 10 DigitalOut myled(LED1);
jmpin 0:48311ffdfa96 11 DigitalOut myled4(LED4);
jmpin 3:3aba1d783730 12
jmpin 3:3aba1d783730 13 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
jmpin 3:3aba1d783730 14
Jake867 11:c87f55a3b9e0 15 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
jmpin 6:68c6a50e1437 16
Jake867 21:0df25c61c475 17 Speaker mySpeaker(p18);; // p18 is the pin that will have the output voltages on it
jmpin 8:f6699fd30737 18
jmpin 6:68c6a50e1437 19
jmpin 0:48311ffdfa96 20 //global variables for main and interrupt routine
Jake867 11:c87f55a3b9e0 21 volatile bool readyFlag = false;
jmpin 0:48311ffdfa96 22 volatile char keyPress;
jmpin 3:3aba1d783730 23 WaveType myWave = sine; // default to sine wave
jmpin 3:3aba1d783730 24 volatile int currentOctave = 4; // default to 4 because thats where middle C is
jmpin 3:3aba1d783730 25 volatile int currentAttackVal = 3; // values will range from 1-5, default to 3
jmpin 6:68c6a50e1437 26 volatile int currentDecayVal = 3; // values will range from 1-5, default to 3
jmpin 3:3aba1d783730 27 volatile int currentSustainVal = 3; // values will range from 1-5, default to 3
jmpin 3:3aba1d783730 28 volatile int currentReleaseVal = 3; // values will range from 1-5, default to 3
jmpin 9:e4df1a31a098 29 double *currentAttackTable; // pointer to the correct attack coefficient table
jmpin 9:e4df1a31a098 30 double *currentDecayTable; // pointer to the correct decay coefficient table
jmpin 9:e4df1a31a098 31 double *currentSustainTable; // pointer to the correct sustain coefficient table
jmpin 9:e4df1a31a098 32 double *currentReleaseTable; // pointer to the correct release coefficient table
Jake867 21:0df25c61c475 33 //vector<float> sampleBuffer; // vector to hold samples of generated waveform
Jake867 21:0df25c61c475 34 short unsigned Analog_out_data[32];
Jake867 21:0df25c61c475 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 15:8ff317cc5d2c 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 15:8ff317cc5d2c 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
Jake867 21:0df25c61c475 247 //void clear_Buffer(void){ // clears buffer that holds samples
Jake867 21:0df25c61c475 248 // sampleBuffer.clear();
Jake867 21:0df25c61c475 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;
Jake867 21:0df25c61c475 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;
Jake867 21:0df25c61c475 372 //attack_range = sampleBuffer.size() * (1/8); // The attack portion of the waveform will take (1/8) of the note's duration
Jake867 21:0df25c61c475 373 //decay_range = attack_range + (sampleBuffer.size() * (1/8)); // The decay portion of the waveform will take (1/8) of the note's duration
Jake867 21:0df25c61c475 374 //sustain_range = sustain_range + (sampleBuffer.size() * (5/8)); // The sustain portion of the waveform will take (5/8) of the note's duration
Jake867 21:0df25c61c475 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 {
Jake867 21:0df25c61c475 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 {
Jake867 21:0df25c61c475 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 {
Jake867 21:0df25c61c475 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 {
Jake867 21:0df25c61c475 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 {
Jake867 21:0df25c61c475 396 for(int i = 0; i < 32 ; i++)
jmpin 9:e4df1a31a098 397 {
Jake867 21:0df25c61c475 398 Analog_out_data[i] = int (65536.0 * ((1.0 + sin((float(i)/32.0*6.28318530717959)))/2.0)); // scaled to be 16bit
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 {
Jake867 21:0df25c61c475 404 float t = 0; // Represents time
Jake867 21:0df25c61c475 405 for(int i = 0; i<32 ; i++)
jmpin 9:e4df1a31a098 406 {
Jake867 21:0df25c61c475 407 Analog_out_data[i] = int(t * 65536.0); //scaled to 16bit
Jake867 21:0df25c61c475 408 t+= 1.0/32.0; // 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 {
Jake867 21:0df25c61c475 414 for(int i = 0; i < 32; i++){
Jake867 21:0df25c61c475 415 if(i<16){
Jake867 21:0df25c61c475 416 Analog_out_data[i] = 65535; //scaled to 16bit
Jake867 21:0df25c61c475 417 }
Jake867 21:0df25c61c475 418 else{
Jake867 21:0df25c61c475 419 Analog_out_data[i] = 0; //scaled to 16bit
Jake867 21:0df25c61c475 420 }
jmpin 9:e4df1a31a098 421 }
jmpin 9:e4df1a31a098 422 }
jmpin 9:e4df1a31a098 423
jmpin 12:d60a9d0052a7 424 /* Generates the waveforms that will be output to the AnalogOut pin after being altered by the ADSR coefficient matrices.
jmpin 12:d60a9d0052a7 425 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 426 @param: frequency - the frequency of the waveform to be generated
jmpin 12:d60a9d0052a7 427 @param: currentWaveType - the shape of the wave that needs to be generated
jmpin 12:d60a9d0052a7 428 */
jmpin 12:d60a9d0052a7 429
jmpin 12:d60a9d0052a7 430
jmpin 9:e4df1a31a098 431 void create_samples(int frequency, WaveType currentWaveType)
jmpin 8:f6699fd30737 432 {
jmpin 8:f6699fd30737 433 switch(currentWaveType){
jmpin 8:f6699fd30737 434 case sine:
jmpin 8:f6699fd30737 435 //Generate sine wave values
jmpin 8:f6699fd30737 436 generate_sineWave(frequency);
Jake867 21:0df25c61c475 437 //apply_Envelope();
jmpin 8:f6699fd30737 438 break;
jmpin 8:f6699fd30737 439 case square:
jmpin 8:f6699fd30737 440 //Generate square wave values
jmpin 8:f6699fd30737 441 generate_squareWave(frequency);
jmpin 9:e4df1a31a098 442 //apply_Envelope();
jmpin 8:f6699fd30737 443 break;
jmpin 8:f6699fd30737 444 case sawtooth:
jmpin 8:f6699fd30737 445 //Generate sawtooth wave values
jmpin 8:f6699fd30737 446 generate_sawtoothWave(frequency);
jmpin 9:e4df1a31a098 447 //apply_Envelope();
jmpin 8:f6699fd30737 448 break;
jmpin 8:f6699fd30737 449 default:
jmpin 8:f6699fd30737 450 break;
jmpin 8:f6699fd30737 451 }
jmpin 8:f6699fd30737 452 }
jmpin 8:f6699fd30737 453
jmpin 5:afd67e985df0 454
jmpin 0:48311ffdfa96 455 //Interrupt routine to parse message with one new character per serial RX interrupt
jmpin 0:48311ffdfa96 456 void parse_message()
jmpin 0:48311ffdfa96 457 {
jmpin 12:d60a9d0052a7 458 //PC.printf("Parse_message was called");
Jake867 11:c87f55a3b9e0 459 while(Blue.readable())
jmpin 10:085c49fe2509 460 {
Jake867 11:c87f55a3b9e0 461 keyPress = Blue.getc();
Jake867 11:c87f55a3b9e0 462 readyFlag = true;
jmpin 10:085c49fe2509 463 }
jmpin 0:48311ffdfa96 464 }
jmpin 3:3aba1d783730 465
jmpin 3:3aba1d783730 466
jmpin 3:3aba1d783730 467 /*
jmpin 3:3aba1d783730 468 This function writes which note was just played to a text file on the SDCard.
jmpin 3:3aba1d783730 469 The note played will be encoded in hexadecimal, as well as the octave, Attack Value,
jmpin 3:3aba1d783730 470 Delay Value, Sustain Value, and Release Value. The format of the bits will be as follows:
jmpin 3:3aba1d783730 471 | 3 bits | 3 bits | 3 bits | 3 bits | 3 bits | 3 bits |
jmpin 6:68c6a50e1437 472 | Attack | Decay | Susttain | Release | Octave | Note |
jmpin 3:3aba1d783730 473 For the 3 bits representing note, A will correspond to 1, B to 2, and so on.
jmpin 3:3aba1d783730 474 For example, if the lower 3 bits corresponding to note are 001, then the note is an A.
jmpin 3:3aba1d783730 475
jmpin 3:3aba1d783730 476 @param: The note that is being played/recorded into the text file
jmpin 3:3aba1d783730 477 */
jmpin 3:3aba1d783730 478
jmpin 3:3aba1d783730 479 void write_to_SDCard(char note)
jmpin 3:3aba1d783730 480 {
jmpin 6:68c6a50e1437 481 int AttackBits, SustainBits, DecayBits, ReleaseBits, OctaveBits, NoteBits;
jmpin 2:f06ba516b1ad 482
jmpin 12:d60a9d0052a7 483 AttackBits = currentAttackVal; // Holds the value of the attack parameter
jmpin 12:d60a9d0052a7 484 DecayBits = currentDecayVal; // Holds the value of the decay parameter
jmpin 12:d60a9d0052a7 485 SustainBits = currentSustainVal;// Holds the value of the sustain parameter
jmpin 12:d60a9d0052a7 486 ReleaseBits = currentReleaseVal;// Holds the value of the release parameter
jmpin 3:3aba1d783730 487 OctaveBits = currentOctave;
jmpin 3:3aba1d783730 488 switch(note){
jmpin 12:d60a9d0052a7 489 case 'C': // a C corresponds to a 3
jmpin 3:3aba1d783730 490 NoteBits = 3;
jmpin 3:3aba1d783730 491 break;
jmpin 3:3aba1d783730 492 case 'D':
jmpin 12:d60a9d0052a7 493 NoteBits = 4; // a D corresponds to a 4
jmpin 3:3aba1d783730 494 break;
jmpin 3:3aba1d783730 495 case 'E':
jmpin 12:d60a9d0052a7 496 NoteBits = 5; // an E corresponds to a 5
jmpin 3:3aba1d783730 497 break;
jmpin 3:3aba1d783730 498 case 'F':
jmpin 12:d60a9d0052a7 499 NoteBits = 6; // an F corresponds to a 6
jmpin 3:3aba1d783730 500 break;
jmpin 3:3aba1d783730 501 case 'G':
jmpin 12:d60a9d0052a7 502 NoteBits = 7; // a G corresponds to a 7
jmpin 3:3aba1d783730 503 break;
jmpin 3:3aba1d783730 504 case 'A':
jmpin 12:d60a9d0052a7 505 NoteBits = 1; // an A corresponds to a 1
jmpin 3:3aba1d783730 506 break;
jmpin 3:3aba1d783730 507 case 'B':
jmpin 12:d60a9d0052a7 508 NoteBits = 2; // a B corresponds to a 2
jmpin 3:3aba1d783730 509 break;
jmpin 3:3aba1d783730 510 default:
jmpin 3:3aba1d783730 511 NoteBits = 0;
jmpin 3:3aba1d783730 512 break;
jmpin 3:3aba1d783730 513 }
jmpin 3:3aba1d783730 514 int writeVal;
jmpin 6:68c6a50e1437 515 writeVal = (AttackBits << 15) | (DecayBits << 12) | (SustainBits << 9) | (ReleaseBits << 6) | (OctaveBits << 3) | (NoteBits);
jmpin 3:3aba1d783730 516
jmpin 12:d60a9d0052a7 517 FILE *fp = fopen("/sd/noteRecords/note_record_01.txt", "w"); // creates handle for file we want to write to
jmpin 3:3aba1d783730 518 if(fp == NULL) {
jmpin 12:d60a9d0052a7 519 error("Could not open file for write\n"); // if this is not a valid name, tell user there is an error
jmpin 3:3aba1d783730 520 }
jmpin 3:3aba1d783730 521 fprintf(fp,"%X\r\n",writeVal); // writes value to the text file in hexadecimal
jmpin 3:3aba1d783730 522 fclose(fp);
jmpin 3:3aba1d783730 523 }
jmpin 3:3aba1d783730 524
jmpin 0:48311ffdfa96 525 int main()
jmpin 0:48311ffdfa96 526 {
jmpin 12:d60a9d0052a7 527 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 528
jmpin 12:d60a9d0052a7 529
jmpin 12:d60a9d0052a7 530 mkdir("/sd/noteRecords", 0777); // make directory to hold the record of notes played
jmpin 3:3aba1d783730 531
jmpin 12:d60a9d0052a7 532 initialize_sustainVals(); // fill the lookup tables with the sustain values in them
Jake867 11:c87f55a3b9e0 533
jmpin 12:d60a9d0052a7 534 PC.baud(9600); // setup baud rate for PC serial connection
jmpin 12:d60a9d0052a7 535 Blue.baud(9600); // setup baud rate for bluetooth serial connection
jmpin 3:3aba1d783730 536
jmpin 12:d60a9d0052a7 537
jmpin 12:d60a9d0052a7 538 Blue.attach(&parse_message,Serial::RxIrq); //attach interrupt function for each new Bluetooth serial character
jmpin 0:48311ffdfa96 539 while(1) {
jmpin 0:48311ffdfa96 540 //check for a new button message ready
jmpin 13:25d53936d385 541 if((keyPress == C_NOTE_KEY) && (readyFlag)){ // Play note C
jmpin 12:d60a9d0052a7 542 set_Note_Freq(noteArray[currentOctave-1][0]); // set the note frequency to the proper value
jmpin 13:25d53936d385 543 create_samples(noteFreq, myWave); // creates the samples that are going to be output to the waveform
Jake867 21:0df25c61c475 544 mySpeaker.PlayNote(noteFreq, 2, 1.0, Analog_out_data); // outputs the samples that are currently in the buffer to p18
jmpin 13:25d53936d385 545 write_to_SDCard('C'); // writes to the SD card
jmpin 13:25d53936d385 546 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 547
Jake867 11:c87f55a3b9e0 548 }
jmpin 13:25d53936d385 549 else if((keyPress == D_NOTE_KEY) && (readyFlag)){ // Play note D
jmpin 15:8ff317cc5d2c 550 set_Note_Freq(noteArray[currentOctave-1][1]);
jmpin 15:8ff317cc5d2c 551 create_samples(noteFreq, myWave);
Jake867 21:0df25c61c475 552 mySpeaker.PlayNote(noteFreq, 2, 1.0, Analog_out_data);
jmpin 15:8ff317cc5d2c 553 write_to_SDCard('D');
jmpin 15:8ff317cc5d2c 554 readyFlag = false;
jmpin 13:25d53936d385 555
Jake867 11:c87f55a3b9e0 556 }
jmpin 13:25d53936d385 557 else if((keyPress == E_NOTE_KEY) && (readyFlag)){ // Play note E
jmpin 6:68c6a50e1437 558 set_Note_Freq(noteArray[currentOctave-1][2]);
jmpin 9:e4df1a31a098 559 create_samples(noteFreq, myWave);
Jake867 21:0df25c61c475 560 mySpeaker.PlayNote(noteFreq, 2, 1.0, Analog_out_data);
jmpin 3:3aba1d783730 561 write_to_SDCard('E');
jmpin 4:406f59c6a1a6 562 readyFlag = false;
Jake867 11:c87f55a3b9e0 563 }
jmpin 13:25d53936d385 564 else if((keyPress == F_NOTE_KEY) && (readyFlag)){ // Play note F
jmpin 6:68c6a50e1437 565 set_Note_Freq(noteArray[currentOctave-1][3]);
jmpin 9:e4df1a31a098 566 create_samples(noteFreq, myWave);
Jake867 21:0df25c61c475 567 mySpeaker.PlayNote(noteFreq, 2, 1.0, Analog_out_data);
jmpin 3:3aba1d783730 568 write_to_SDCard('F');
jmpin 4:406f59c6a1a6 569 readyFlag = false;
Jake867 11:c87f55a3b9e0 570 }
jmpin 13:25d53936d385 571 else if((keyPress == G_NOTE_KEY) && (readyFlag)){ // Play note G
jmpin 6:68c6a50e1437 572 set_Note_Freq(noteArray[currentOctave-1][4]);
jmpin 9:e4df1a31a098 573 create_samples(noteFreq, myWave);
Jake867 21:0df25c61c475 574 mySpeaker.PlayNote(noteFreq, 2, 1.0, Analog_out_data);
jmpin 3:3aba1d783730 575 write_to_SDCard('G');
jmpin 4:406f59c6a1a6 576 readyFlag = false;
Jake867 11:c87f55a3b9e0 577 }
jmpin 13:25d53936d385 578 else if((keyPress == A_NOTE_KEY) && (readyFlag)){ // Play note A
jmpin 6:68c6a50e1437 579 set_Note_Freq(noteArray[currentOctave][5]);
jmpin 9:e4df1a31a098 580 create_samples(noteFreq, myWave);
Jake867 21:0df25c61c475 581 mySpeaker.PlayNote(noteFreq, 2, 1.0, Analog_out_data);
jmpin 3:3aba1d783730 582 write_to_SDCard('A');
jmpin 4:406f59c6a1a6 583 readyFlag = false;
Jake867 11:c87f55a3b9e0 584 }
jmpin 15:8ff317cc5d2c 585 else if((keyPress == B_NOTE_KEY) && (readyFlag)){ // Play note B
jmpin 6:68c6a50e1437 586 set_Note_Freq(noteArray[currentOctave][6]);
jmpin 9:e4df1a31a098 587 create_samples(noteFreq, myWave);
Jake867 21:0df25c61c475 588 mySpeaker.PlayNote(noteFreq, 2, 1.0, Analog_out_data);
jmpin 3:3aba1d783730 589 write_to_SDCard('B');
jmpin 4:406f59c6a1a6 590 readyFlag = false;
Jake867 11:c87f55a3b9e0 591 }
jmpin 2:f06ba516b1ad 592 else if((keyPress == RAISE_OCTAVE_KEY) && (readyFlag)){ // button O pressed
jmpin 0:48311ffdfa96 593 // Raise an octave
jmpin 6:68c6a50e1437 594 if(currentOctave < 7)
jmpin 2:f06ba516b1ad 595 currentOctave++;
jmpin 4:406f59c6a1a6 596 else
jmpin 4:406f59c6a1a6 597 printf("Cannot raise octave above 7.\r\n");
Jake867 11:c87f55a3b9e0 598 readyFlag = false;
Jake867 11:c87f55a3b9e0 599 }
jmpin 2:f06ba516b1ad 600 else if((keyPress == LOWER_OCTAVE_KEY) && (readyFlag)){ // button L pressed
jmpin 2:f06ba516b1ad 601 // Lower an octave
jmpin 4:406f59c6a1a6 602 if(currentOctave > 1)
jmpin 2:f06ba516b1ad 603 currentOctave--;
jmpin 4:406f59c6a1a6 604 else
jmpin 4:406f59c6a1a6 605 printf("Cannot lower octave below 1.\r\n");
Jake867 11:c87f55a3b9e0 606 readyFlag = false;
Jake867 11:c87f55a3b9e0 607 }
jmpin 2:f06ba516b1ad 608 else if((keyPress == RAISE_ATTACK_KEY) && (readyFlag)){ // button Q pressed
jmpin 0:48311ffdfa96 609 // Raise Attack Value
jmpin 6:68c6a50e1437 610 if(currentAttackVal < 5){
jmpin 2:f06ba516b1ad 611 currentAttackVal++;
jmpin 6:68c6a50e1437 612 change_Attack_Table(currentAttackVal);
jmpin 6:68c6a50e1437 613 }
jmpin 4:406f59c6a1a6 614 else
jmpin 4:406f59c6a1a6 615 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 616 readyFlag = false;
Jake867 11:c87f55a3b9e0 617 }
jmpin 2:f06ba516b1ad 618 else if((keyPress == LOWER_ATTACK_KEY) && (readyFlag)){ // button A pressed
jmpin 0:48311ffdfa96 619 // Lower Attack Value
jmpin 6:68c6a50e1437 620 if(currentAttackVal > 1){
jmpin 2:f06ba516b1ad 621 currentAttackVal--;
jmpin 6:68c6a50e1437 622 change_Attack_Table(currentAttackVal);
jmpin 6:68c6a50e1437 623 }
jmpin 4:406f59c6a1a6 624 else
jmpin 6:68c6a50e1437 625 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 626 readyFlag = false;
Jake867 11:c87f55a3b9e0 627 }
jmpin 2:f06ba516b1ad 628 else if((keyPress == RAISE_DELAY_KEY) && (readyFlag)){ // button W pressed
jmpin 0:48311ffdfa96 629 // Raise Delay Value
jmpin 6:68c6a50e1437 630 if(currentDecayVal < 5){
jmpin 6:68c6a50e1437 631 currentDecayVal++;
jmpin 6:68c6a50e1437 632 change_Decay_Table(currentDecayVal);
jmpin 6:68c6a50e1437 633 }
jmpin 4:406f59c6a1a6 634 else
jmpin 4:406f59c6a1a6 635 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 636 readyFlag = false;
Jake867 11:c87f55a3b9e0 637 }
jmpin 2:f06ba516b1ad 638 else if((keyPress == LOWER_DELAY_KEY) && (readyFlag)){ // button S pressed
jmpin 0:48311ffdfa96 639 // Lower Delay Value
jmpin 6:68c6a50e1437 640 if(currentDecayVal > 1){
jmpin 6:68c6a50e1437 641 currentDecayVal--;
jmpin 6:68c6a50e1437 642 change_Decay_Table(currentDecayVal);
jmpin 6:68c6a50e1437 643 }
jmpin 4:406f59c6a1a6 644 else
jmpin 6:68c6a50e1437 645 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 646 readyFlag = false;
Jake867 11:c87f55a3b9e0 647 }
jmpin 2:f06ba516b1ad 648 else if((keyPress == RAISE_SUSTAIN_KEY) && (readyFlag)){ // button E pressed
jmpin 0:48311ffdfa96 649 // Raise Sustain Value
jmpin 6:68c6a50e1437 650 if(currentSustainVal < 5){
jmpin 2:f06ba516b1ad 651 currentSustainVal++;
jmpin 6:68c6a50e1437 652 change_Sustain_Table(currentSustainVal);
jmpin 6:68c6a50e1437 653 }
jmpin 4:406f59c6a1a6 654 else
jmpin 4:406f59c6a1a6 655 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 656 readyFlag = false;
Jake867 11:c87f55a3b9e0 657 }
jmpin 2:f06ba516b1ad 658 else if((keyPress == LOWER_SUSTAIN_KEY) && (readyFlag)){ // button D pressed
jmpin 0:48311ffdfa96 659 // Lower Sustain Value
jmpin 6:68c6a50e1437 660 if(currentSustainVal > 1){
jmpin 2:f06ba516b1ad 661 currentSustainVal--;
jmpin 6:68c6a50e1437 662 change_Sustain_Table(currentSustainVal);
jmpin 6:68c6a50e1437 663 }
jmpin 4:406f59c6a1a6 664 else
jmpin 6:68c6a50e1437 665 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 666 readyFlag = false;
Jake867 11:c87f55a3b9e0 667 }
jmpin 2:f06ba516b1ad 668 else if((keyPress == RAISE_RELEASE_KEY) && (readyFlag)){ // button R pressed
jmpin 0:48311ffdfa96 669 // Raise Release Value
jmpin 6:68c6a50e1437 670 if(currentReleaseVal < 5){
jmpin 2:f06ba516b1ad 671 currentReleaseVal++;
jmpin 6:68c6a50e1437 672 change_Release_Table(currentReleaseVal);
jmpin 6:68c6a50e1437 673 }
jmpin 4:406f59c6a1a6 674 else
jmpin 4:406f59c6a1a6 675 printf("Cannot raise value above 5.\r\n");
Jake867 11:c87f55a3b9e0 676 readyFlag = false;
Jake867 11:c87f55a3b9e0 677 }
jmpin 2:f06ba516b1ad 678 else if((keyPress == LOWER_RELEASE_KEY) && (readyFlag)){ // button F pressed
jmpin 0:48311ffdfa96 679 // Lower Release Value
jmpin 6:68c6a50e1437 680 if(currentReleaseVal > 1){
jmpin 2:f06ba516b1ad 681 currentReleaseVal--;
jmpin 6:68c6a50e1437 682 change_Release_Table(currentReleaseVal);
jmpin 6:68c6a50e1437 683 }
jmpin 4:406f59c6a1a6 684 else
jmpin 6:68c6a50e1437 685 printf("Cannot lower value below 1.\r\n");
Jake867 11:c87f55a3b9e0 686 readyFlag = false;
Jake867 11:c87f55a3b9e0 687 }
jmpin 2:f06ba516b1ad 688 else if((keyPress == CHANGE_WAVESHAPE_UP) && (readyFlag)){ // button T pressed
jmpin 2:f06ba516b1ad 689 // Change waveform shape to next waveform type
jmpin 2:f06ba516b1ad 690 switch(myWave){
jmpin 2:f06ba516b1ad 691 case sine:
jmpin 2:f06ba516b1ad 692 myWave = square;
jmpin 2:f06ba516b1ad 693 break;
jmpin 2:f06ba516b1ad 694 case square:
jmpin 2:f06ba516b1ad 695 myWave = sawtooth;
jmpin 2:f06ba516b1ad 696 break;
jmpin 2:f06ba516b1ad 697 case sawtooth:
jmpin 2:f06ba516b1ad 698 myWave = sine;
jmpin 2:f06ba516b1ad 699 break;
jmpin 2:f06ba516b1ad 700 default:
jmpin 2:f06ba516b1ad 701 break;
jmpin 2:f06ba516b1ad 702 }
Jake867 11:c87f55a3b9e0 703 readyFlag = false;
Jake867 11:c87f55a3b9e0 704 }
jmpin 2:f06ba516b1ad 705 else if((keyPress == CHANGE_WAVESHAPE_DOWN) && (readyFlag)){ // button G pressed
jmpin 2:f06ba516b1ad 706 // Change waveform shape to previous waveform type
jmpin 2:f06ba516b1ad 707 switch(myWave){
jmpin 2:f06ba516b1ad 708 case sine:
jmpin 2:f06ba516b1ad 709 myWave = sawtooth;
jmpin 2:f06ba516b1ad 710 break;
jmpin 2:f06ba516b1ad 711 case square:
jmpin 2:f06ba516b1ad 712 myWave = sine;
jmpin 2:f06ba516b1ad 713 break;
jmpin 2:f06ba516b1ad 714 case sawtooth:
jmpin 2:f06ba516b1ad 715 myWave = square;
jmpin 2:f06ba516b1ad 716 break;
jmpin 2:f06ba516b1ad 717 default:
jmpin 2:f06ba516b1ad 718 break;
jmpin 2:f06ba516b1ad 719 }
Jake867 11:c87f55a3b9e0 720 readyFlag = false;
Jake867 11:c87f55a3b9e0 721
Jake867 11:c87f55a3b9e0 722 }
jmpin 10:085c49fe2509 723 }
jmpin 0:48311ffdfa96 724 }