Bluetooth Enabled Keyboard/Synthesizer

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

Fork of 4180_Final_Design_Project by John Pinion

Revision:
9:e4df1a31a098
Parent:
8:f6699fd30737
Child:
10:085c49fe2509
--- a/main.cpp	Thu Apr 28 22:16:40 2016 +0000
+++ b/main.cpp	Thu Apr 28 22:32:34 2016 +0000
@@ -28,10 +28,10 @@
 volatile int currentSustainVal = 3;  // values will range from 1-5, default to 3
 volatile int currentReleaseVal = 3;  // values will range from 1-5, default to 3
 int *currentLookupTable;            // pointer to the correct lookup table of values
-int *currentAttackTable;            // pointer to the correct attack coefficient table
-int *currentDecayTable;             // pointer to the correct decay coefficient table
-int *currentSustainTable;           // pointer to the correct sustain coefficient table
-int *currentReleaseTable;           // pointer to the correct release coefficient table
+double *currentAttackTable;            // pointer to the correct attack coefficient table
+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<double> sampleBuffer;        // vector to hold samples of generated waveform
 volatile int lookupTableIndex;      // index used to find values in the lookup table for the waveforms
 volatile int phaseAccumulator;      // stores phase accumulator which is used to index into the lookup table
@@ -156,15 +156,50 @@
 double sustainVals2[160];
 double sustainVals1[160];
 double releaseVals5[32] = {
-    };
+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] = {
-    };
+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] = {
-    };
+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] = {
-    };
+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] = {
-    };
+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};
     
 /* Values for the sine wave shaped waveform */
 int sineTable[256] = {
@@ -310,11 +345,15 @@
   phaseAccumulator = 0;
 }
 
+void clear_Buffer(void){
+    sampleBuffer.clear();
+}
+
 void set_Note_Freq(int frequency){
     accumulator_reset();
     noteFreq = frequency;
     set_Frequency_Tuner();
-    clear_buffer();
+    clear_Buffer();
 }
 
 void change_Wave(const WaveType currentWave) {
@@ -440,10 +479,6 @@
         sustainVals1[j] = .6;
     }
 }
-
-void clear_Buffer(void){
-    sampleBuffer.clear();
-}
     
 void buffer_Samples(void){
     for(int j=0;j<num_samples;j++){
@@ -477,7 +512,41 @@
     }     
 }
 
-void create_samples(int frequency, waveType currentWaveType)
+void generate_sineWave(int frequency)
+{
+    double t = 0;   
+    for(int i = 0; i < 256 ; i++)
+    {
+        sampleBuffer.push_back(((sin(2*(PI)*frequency*t)) + 1)/2);  // scaled to be a % of maximum output voltage (3.3V)
+        t = t + timeIncrement;                                       // increment t for calculation of next value in the waveform   
+    }
+}
+
+void generate_sawtoothWave(int frequency)
+{
+    double t = 0;
+    for(int i = 0; i<256 ; i++)
+    {
+        sampleBuffer.push_back((2*(t*frequency) - (.5 + (t*frequency)) + 1) / 2);
+        t = t + timeIncrement;                       // increment t for calculation of next value in the waveform   
+    }
+}
+
+void generate_squareWave(int frequency)
+{
+    double width = (1 / 2 * frequency);     //Width of a half period of the square wave
+    double t = 0;
+    for(int i = 0; i < 256; i++)
+    {
+        if(((int)(t / width) % 2 ) == 0)     // Even, write a 1 for the square wave
+        sampleBuffer.push_back(1.0);
+        else                                // Odd, write a 0 for the square wave
+        sampleBuffer.push_back(0.0);
+        t = t + timeIncrement;              // increment t for calculation of next value in the waveform
+    }
+}
+
+void create_samples(int frequency, WaveType currentWaveType)
 {
     switch(currentWaveType){
         case sine:
@@ -488,51 +557,19 @@
         case square:
             //Generate square wave values
             generate_squareWave(frequency);
-            apply_Envelope();
+            //apply_Envelope();
             break;
         case sawtooth:
             //Generate sawtooth wave values
             generate_sawtoothWave(frequency);
-            apply_Envelope();
+            //apply_Envelope();
             break;
         default:
         break;
     }
 }
 
-void generate_sineWave(int frequency)
-{
-    double t = 0;   
-    for(i = 0; i < 256 ; i++)
-    {
-        sampleBuffer.push_back(((sin(2*(PI)*frequency*t)) + 1)/2);  // scaled to be a % of maximum output voltage (3.3V)
-        t = t + timeIncrement;                                       // increment t for calculation of next value in the waveform   
-    }
-}
 
-void generate_sawtoothWave(int frequency)
-{
-    double t = 0;
-    for(i = 0; i<256 ; i++)
-    {
-        sampleBuffer.push_back((2*(t*frequency) - (.5 + (t*frequency)) + 1) / 2);
-        t = t + timeIncrement;                       // increment t for calculation of next value in the waveform   
-    }
-}
-
-void generate_squareWave(int frequency)
-{
-    double width = (1 / 2 * frequency);     //Width of a half period of the square wave
-    double t = 0;
-    for(i = 0; i < 256; i++)
-    {
-        if((int)(t / width) % 2 ) == 0)     // Even, write a 1 for the square wave
-        sampleBuffer.push_back(1.0);
-        else                                // Odd, write a 0 for the square wave
-        sampleBuffer.push_back(0.0);
-        t = t + timeIncrement;              // increment t for calculation of next value in the waveform
-    }
-}
 
 void output_samples()
 {
@@ -541,6 +578,7 @@
         synthPin = sampleBuffer[sample];
         Thread::wait(timeIncrement * 1000);
     }
+}
         
     
     
@@ -628,19 +666,21 @@
         //check for a new button message ready
         if((keyPress == C_NOTE_KEY) && (readyFlag)){ // button Z pressed
             set_Note_Freq(noteArray[currentOctave-1][0]);
-            create_samples(note_freq, myWave);
+            create_samples(noteFreq, myWave);
             write_to_SDCard('C');
             readyFlag = false;
             // Play note that corresponds to Z
             }
         else if((keyPress == D_NOTE_KEY) && (readyFlag)){ // button X pressed
                 set_Note_Freq(noteArray[currentOctave-1][1]);
+                create_samples(noteFreq, myWave);
                 write_to_SDCard('D');
                 readyFlag = false;
             // Play note that corresponds to X
             }
         else if((keyPress == E_NOTE_KEY) && (readyFlag)){ // button C pressed
             set_Note_Freq(noteArray[currentOctave-1][2]);
+            create_samples(noteFreq, myWave);
             // Play note that corresponds to C
             // Make note of which note was played in file on SD Card
             write_to_SDCard('E');
@@ -648,6 +688,7 @@
             }
         else if((keyPress == F_NOTE_KEY) && (readyFlag)){ // button V pressed
             set_Note_Freq(noteArray[currentOctave-1][3]);
+            create_samples(noteFreq, myWave);
             // Play note that corresponds to V
             // Make note of which note was played in file on SD Card
             write_to_SDCard('F');
@@ -655,6 +696,7 @@
             }
         else if((keyPress == G_NOTE_KEY) && (readyFlag)){ // button B pressed
             set_Note_Freq(noteArray[currentOctave-1][4]);
+            create_samples(noteFreq, myWave);
             // Play note that corresponds to B
             // Make note of which note was played in file on SD Card
             write_to_SDCard('G');
@@ -662,7 +704,7 @@
             }
         else if((keyPress == A_NOTE_KEY) && (readyFlag)){ // button N pressed
             set_Note_Freq(noteArray[currentOctave][5]);
-            buffer_Samples();
+            create_samples(noteFreq, myWave);
             // Play note that corresponds to N
             // Make note of which note was played in file on SD Card
             write_to_SDCard('A');
@@ -670,6 +712,7 @@
             }
         else if((keyPress == B_NOTE_KEY) && (readyFlag)){ // button M pressed
             set_Note_Freq(noteArray[currentOctave][6]);
+            create_samples(noteFreq, myWave);
             // Play note that corresponds to M
             // Make note of which note was played in file on SD Card
             write_to_SDCard('B');