Bluetooth Enabled Keyboard/Synthesizer for mbed

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

Revision:
22:9c80f7bcef86
Parent:
21:0df25c61c475
Child:
23:cf9d43d5a5b4
--- a/main.cpp	Sat Apr 30 20:44:20 2016 +0000
+++ b/main.cpp	Sat Apr 30 21:54:33 2016 +0000
@@ -30,185 +30,58 @@
 double *currentDecayTable;             // pointer to the correct decay coefficient table
 double *currentSustainTable;           // pointer to the correct sustain coefficient table
 double *currentReleaseTable;           // pointer to the correct release coefficient table
-//vector<float> sampleBuffer;        // vector to hold samples of generated waveform
 short unsigned Analog_out_data[32];
-//int num_samples = 256;              // number of samples 
 volatile int noteFreq;              // the current frequency of the note being played
-double timeIncrement = (2/256);     // 2 seconds with 256 samples
 
 /* Coefficient Matrices Corresponding to Different Attack Values
-each matrix is comprised of 32 elements (256/8). The first matrix corresponds
+each matrix is comprised of 4 elements (32/8). The first matrix corresponds
 to an attack value of 5.
 */
 
-double attackVals5[32] = {    //Approaches the maximum amplitude the quickest - corresponds to an attackValue of 5
-0, 0.275 , 0.55 , 0.7 ,
-0.8 , 0.85 , 0.9 , 0.91 ,
-0.92 , 0.93 , 0.939 , 0.948 ,
-0.956 , 0.963 , 0.969 , 0.974 ,
-0.978 , 0.982 , 0.986 , 0.989 ,
-0.991 , 0.992 , 0.993 , 0.994 ,
-0.995 , 0.996 , 0.997 , 0.998 ,
-0.9985 , 0.999 , 0.9995 , 1
-};
-double attackVals4[32] = {    //Corresponds to an attackValue of 4
-0 , 0.18 , 0.38 , 0.58 , 
-0.66 , 0.69 , 0.72 , 0.74 , 
-0.76 , 0.78 , 0.795 , 0.81 , 
-0.825 , 0.84 , 0.85 , 0.86 , 
-0.87 , 0.88 , 0.89 , 0.9 , 
-0.91 , 0.92 , 0.93 , 0.94 , 
-0.95 , 0.96 , 0.97 , 0.98 , 
-0.985 , 0.99 , 0.995 , 1
-};
-double attackVals3[32] = {    //Corresponds to an attackValue of 3
-0 , 0.09 , 0.18 , 0.27 , 
-0.35 , 0.43 , 0.5 , 0.57 , 
-0.61 , 0.65 , 0.68 , 0.71 , 
-0.74 , 0.76 , 0.78 , 0.8 , 
-0.82 , 0.84 , 0.86 , 0.88 , 
-0.895 , 0.91 , 0.925 , 0.94 , 
-0.95 , 0.96 , 0.97 , 0.98 , 
-0.985 , 0.99 , 0.995 , 1
-};
-double attackVals2[32] = {    //Corresponds to an attackValue of 2
-0 , 0.06 , 0.12 , 0.18 , 
-0.23 , 0.28 , 0.32 , 0.36 , 
-0.4 , 0.44 , 0.48 , 0.52 , 
-0.55 , 0.58 , 0.61 , 0.64 , 
-0.67 , 0.695 , 0.72 , 0.745 , 
-0.77 , 0.795 , 0.82 , 0.845 , 
-0.87 , 0.895 , 0.92 , 0.945 , 
-0.965 , 0.985 , 0.995 , 1
-};
-double attackVals1[32] = {    //Approaches the mamimum amplitude the slowest, in a linear fashion - corresponds to an attackValue of 1
-0 , 0.032258065 , 0.064516129 , 0.096774194 , 
-0.129032258 , 0.161290323 , 0.193548387 , 0.225806452 , 
-0.258064516 , 0.290322581 , 0.322580645 , 0.35483871 , 
-0.387096774 , 0.419354839 , 0.451612903 , 0.483870968 , 
-0.516129032 , 0.548387097 , 0.580645161 , 0.612903226 , 
-0.64516129 , 0.677419355 , 0.709677419 , 0.741935484 , 
-0.774193548 , 0.806451613 , 0.838709677 , 0.870967742 , 
-0.903225806 , 0.935483871 , 0.967741935 , 1
-};
+double attackVals5[4] = {0 , 0.9 , 0.98 , 1};  //Approaches the maximum amplitude the quickest - corresponds to an attackValue of 5
+
+double attackVals4[4] = {0 , 0.78 , 0.92 , 1};    //Corresponds to an attackValue of 4
+
+double attackVals3[4] = {0 , 0.63 , 0.84 , 1};    //Corresponds to an attackValue of 3
+
+double attackVals2[4] = {0 , 0.5 , 0.75 , 1};    //Corresponds to an attackValue of 2
+
+double attackVals1[4] = {0 , 0.33 , 0.66 , 1};    //Approaches the mamimum amplitude the slowest, in a linear fashion - corresponds to an attackValue of 1
+
 
 /* Coefficient Matrices Corresponding to Different Decay Values
-each matrix is comprised of 32 elements (256/8). The first matrix corresponds
+each matrix is comprised of 4 elements (32/8). The first matrix corresponds
 to a decay value of 5.
 */
 
-double decayVals5[32] = {   //Approaches the sustain amplitude the quickest - corresponds to a decay value of 5
-1 , 0.8 , 0.75 , 0.71 , 
-0.68 , 0.66 , 0.65 , 0.64 , 
-0.635 , 0.63 , 0.625 , 0.62 , 
-0.615 , 0.61 , 0.605 , 0.6 , 
-0.6 , 0.6 , 0.6 , 0.6 , 
-0.6 , 0.6 , 0.6 , 0.6 , 
-0.6 , 0.6 , 0.6 , 0.6 , 
-0.6 , 0.6 , 0.6 , 0.6
-};
-double decayVals4[32] = {   // Decay value of 4
-1 , 0.93 , 0.86 , 0.8 , 
-0.75 , 0.71 , 0.69 , 0.68 , 
-0.67 , 0.66 , 0.655 , 0.65 , 
-0.645 , 0.64 , 0.635 , 0.63 , 
-0.625 , 0.62 , 0.615 , 0.61 , 
-0.605 , 0.6 , 0.6 , 0.6 , 
-0.6 , 0.6 , 0.6 , 0.6 , 
-0.6 , 0.6 , 0.6 , 0.6
-};
-double decayVals3[32] = {   // Decay value of 3
-1 , 0.96 , 0.92 , 0.88 , 
-0.85 , 0.82 , 0.79 , 0.76 , 
-0.74 , 0.72 , 0.705 , 0.69 , 
-0.68 , 0.67 , 0.665 , 0.66 , 
-0.655 , 0.65 , 0.645 , 0.64 , 
-0.635 , 0.63 , 0.625 , 0.62 , 
-0.615 , 0.61 , 0.605 , 0.6 , 
-0.6 , 0.6 , 0.6 , 0.6
-};
-double decayVals2[32] = {   // Decay value of 2
-1 , 0.98 , 0.96 , 0.94 , 
-0.92 , 0.9 , 0.88 , 0.86 , 
-0.84 , 0.82 , 0.8 , 0.79 , 
-0.78 , 0.77 , 0.76 , 0.75 , 
-0.74 , 0.73 , 0.72 , 0.71 , 
-0.7 , 0.69 , 0.68 , 0.67 , 
-0.66 , 0.65 , 0.64 , 0.63 , 
-0.62 , 0.61 , 0.6 , 0.6
-};
-double decayVals1[32] = {   // Decays the slowest, in a linear fashion - corresponds to a decay value of 1
-1 , 0.987096774 , 0.974193548 , 0.961290323 , 
-0.948387097 , 0.935483871 , 0.922580645 , 0.909677419 , 
-0.896774194 , 0.883870968 , 0.870967742 , 0.858064516 , 
-0.84516129 , 0.832258065 , 0.819354839 , 0.806451613 , 
-0.793548387 , 0.780645161 , 0.767741935 , 0.75483871 , 
-0.741935484 , 0.729032258 , 0.716129032 , 0.703225806 , 
-0.690322581 , 0.677419355 , 0.664516129 , 0.651612903 , 
-0.638709677 , 0.625806452 , 0.612903226 , 0.6
-};
+double decayVals5[4] = {0.67 , 0.63 , 0.62 , 0.6};  //Approaches the sustain amplitude the quickest - corresponds to a decay value of 5
+double decayVals4[4] = {0.73 , 0.675 , 0.63 , 0.60};   // Decay value of 4
+double decayVals3[4] = {0.77 , 0.7 , 0.63 , 0.60};   // Decay value of 3
+double decayVals2[4] = {0.84 , 0.73 , 0.65 , 0.6}; // Decay value of 2
+double decayVals1[4] = {0.9 , 0.8 , 0.7 , 0.6};   // Decays the slowest, in a linear fashion - corresponds to a decay value of 1
 
 /* Coefficient Matrices Corresponding to Different sustain values
-each matrix is comprised of 160 elements 5 * (256/8). The first matrix corresponds
+each matrix is comprised of 20 elements 5 * (32/8). The first matrix corresponds
 to a sustain value of 5. The matrices get initialized later in a for loop due to their size.
 */
 
-double sustainVals5[160]; 
-double sustainVals4[160];
-double sustainVals3[160];
-double sustainVals2[160];
-double sustainVals1[160];
+double sustainVals5[20]; 
+double sustainVals4[20];
+double sustainVals3[20];
+double sustainVals2[20];
+double sustainVals1[20];
 
 /* Coefficient Matrices Corresponding to Different release values
 each matrix is comprised of 32 elements (256/8). The first matrix corresponds
 to a release value of 5.
 */
 
-double releaseVals5[32] = {     // Releases (goes to 0 amplitude) the quickest - corresponds to a release value of 5
-0.6 , 0.3 , 0.15 , 0.1 , 
-0.09 , 0.08 , 0.07 , 0.06 , 
-0.05 , 0.045 , 0.04 , 0.035 , 
-0.03 , 0.025 , 0.02 , 0.015 , 
-0.01 , 0.0075 , 0.005 , 0.0025 , 
-0 , 0 , 0 , 0 , 
-0 , 0 , 0 , 0 , 
-0 , 0 , 0 , 0};
-double releaseVals4[32] = {     // Release value of 4
-0.6 , 0.45 , 0.3 , 0.2 , 
-0.17 , 0.16 , 0.15 , 0.14 , 
-0.13 , 0.125 , 0.12 , 0.115 , 
-0.11 , 0.105 , 0.1 , 0.095 , 
-0.09 , 0.085 , 0.08 , 0.075 , 
-0.07 , 0.065 , 0.06 , 0.055 , 
-0.05 , 0.045 , 0.04 , 0.035 , 
-0.03 , 0.02 , 0.01 , 0};
-double releaseVals3[32] = {     // Release value of 3
-0.6 , 0.5 , 0.43 , 0.37 , 
-0.32 , 0.28 , 0.26 , 0.24 , 
-0.22 , 0.2 , 0.18 , 0.17 , 
-0.16 , 0.15 , 0.14 , 0.13 , 
-0.12 , 0.11 , 0.1 , 0.09 , 
-0.08 , 0.07 , 0.06 , 0.05 , 
-0.04 , 0.035 , 0.03 , 0.025 , 
-0.02 , 0.015 , 0.01 , 0};
-double releaseVals2[32] = {     // Release value of 2
-0.6 , 0.55 , 0.5 , 0.46 , 
-0.43 , 0.4 , 0.37 , 0.34 , 
-0.32 , 0.3 , 0.28 , 0.26 , 
-0.24 , 0.22 , 0.2 , 0.18 , 
-0.16 , 0.15 , 0.14 , 0.13 , 
-0.12 , 0.11 , 0.1 , 0.09 , 
-0.08 , 0.07 , 0.06 , 0.05 , 
-0.04 , 0.03 , 0.015 , 0};
-double releaseVals1[32] = {     // Release value of 1 - proceeds slowest, in a linear fashion
-0.6 , 0.580645161 , 0.561290323 , 0.541935484 , 
-0.522580645 , 0.503225806 , 0.483870968 , 0.464516129 , 
-0.44516129 , 0.425806452 , 0.406451613 , 0.387096774 , 
-0.367741935 , 0.348387097 , 0.329032258 , 0.309677419 , 
-0.290322581 , 0.270967742 , 0.251612903 , 0.232258065 , 
-0.212903226 , 0.193548387 , 0.174193548 , 0.15483871 , 
-0.135483871 , 0.116129032 , 0.096774194 , 0.077419355 , 
-0.058064516 , 0.038709677 , 0.019354839 , -1.38778E-16};
+double releaseVals5[4] = {0.1 , 0.03 , 0.01 , 0};     // Releases (goes to 0 amplitude) the quickest - corresponds to a release value of 5
+double releaseVals4[4] = {0.2 , 0.1 , .04 , 0};     // Release value of 4
+double releaseVals3[4] = {0.33 , 0.16 , 0.08 , 0};     // Release value of 3
+double releaseVals2[4] = {0.4 , 0.23 , 0.1 , 0};     // Release value of 2
+double releaseVals1[4] = {0.45 , 0.30 , 0.15 , 0};     // Release value of 1 - proceeds slowest, in a linear fashion
+
 
 int noteArray[7][7] = { // Array holding different note frequencies
   C1 , D1 , E1 , F1 , G1 , A1 , B1 ,
@@ -244,13 +117,8 @@
     }
 }
 
-//void clear_Buffer(void){        // clears buffer that holds samples
-//    sampleBuffer.clear();
-//}
-
 void set_Note_Freq(int frequency){      // updates the frequency of the note being played
     noteFreq = frequency;
-    //clear_Buffer();
 }
 
 void change_Attack_Table(int attackVal)     // change which table of coefficients to use for altering the attack portion of the waveform
@@ -347,12 +215,12 @@
 
 /* Having different sustain values for the amplitude of the wave would make the math neccesary to generate the other
 coefficient matrices very complex, so only .6 is used, meaning a sustain value of 1-5 will all correspond to a sustain amplitude
-of .6. Since the sustain coefficient matrices are 160 elements long, they are all filled in a for loop with this function call.
+of .6. Since the sustain coefficient matrices are 20 elements long, they are all filled in a for loop with this function call.
 */
 
 void initialize_sustainVals()   
 {
-    for(int j = 0; j < 160; j++)
+    for(int j = 0; j < 20; j++)
     {
         sustainVals5[j] = .6;
         sustainVals4[j] = .6;
@@ -362,33 +230,24 @@
     }
 }
 /* Applies the envelope to the waveform. Each set of coefficients is applied to a certain portion of the waveform to alter its shape.
-The attack coefficients are appplied to the first 32 samples, the decay coefficients are applied to samples 33-64, the sustain coefficients
-are applied to samples 65 - 224, and the release coefficients are appplied to samples 225-256.
+The attack coefficients are appplied to the first 4 samples, the decay coefficients are applied to samples 5-8, the sustain coefficients
+are applied to samples 9 - 28, and the release coefficients are appplied to samples 29-32.
 */
 
 
 void apply_Envelope(void){                                          
-    int attack_range, decay_range, sustain_range, release_range;
-    //attack_range = sampleBuffer.size() * (1/8);                     // The attack portion of the waveform will take (1/8) of the note's duration
-    //decay_range = attack_range + (sampleBuffer.size() * (1/8));     // The decay portion of the waveform will take (1/8) of the note's duration
-    //sustain_range = sustain_range + (sampleBuffer.size() * (5/8));  // The sustain portion of the waveform will take (5/8) of the note's duration
-    //release_range = release_range + (sampleBuffer.size() * (1/8));  // The release portion of the waveform will take (1/8) of the note's duration
-    for(int i = 0; i < attack_range; i++)
-    {
-        //sampleBuffer[i] = sampleBuffer[i] * currentAttackTable[i];
-    }
-    for(int k = attack_range; k < decay_range; k++)
+    double coefficientMatrix[32];       // Array to hold all 32 coefficients for modulating the amplitude of the waveform
+    for(int j = 0; j < 32 ; j++)
     {
-        //sampleBuffer[k] = sampleBuffer[k] * currentDecayTable[k-attack_range];
+        if( j < 4 )
+            coefficientMatrix[j] = currentAttackTable[j];
+        else if(( j < 8) && ( j > 3))
+            coefficientMatrix[j] = currentDecayTable[j];
+        else if(( j < 28) && (j > 7))
+            coefficientMatrix[j] = currentSustainTable[j];
+        else if(( j < 32) && (j > 27))
+            coefficientMatrix[j] = currentReleaseTable[j];
     }
-    for(int m = decay_range; m < sustain_range; m++)
-    {
-        //sampleBuffer[m] = sampleBuffer[m] * currentSustainTable[m-decay_range];
-    }
-    for(int n = sustain_range; n < release_range; n++)
-    {
-        //sampleBuffer[n] = sampleBuffer[n] * currentReleaseTable[n-sustain_range];
-    }     
 }
 
 void generate_sineWave(int frequency)       // Generates samples for a sine wave of a given input frequency
@@ -513,8 +372,7 @@
         }
     int writeVal;
     writeVal  = (AttackBits << 15) | (DecayBits << 12) | (SustainBits << 9) | (ReleaseBits << 6) | (OctaveBits << 3) | (NoteBits);
-    
-    FILE *fp = fopen("/sd/noteRecords/note_record_01.txt", "w");        // creates handle for file we want to write to
+    FILE *fp = fopen("/sd/noteRecords/note_record_01.txt", "a");        // creates handle for file we want to write to
     if(fp == NULL) {
         error("Could not open file for write\n");                       // if this is not a valid name, tell user there is an error
     }