Bluetooth Enabled Keyboard/Synthesizer for mbed

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

Committer:
jmpin
Date:
Sat Apr 30 02:45:18 2016 +0000
Revision:
20:f3e994db66be
Parent:
19:2f635d03467c
Updated functions used to generate waveforms.

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