Bluetooth Enabled Keyboard/Synthesizer for mbed

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

Files at this revision

API Documentation at this revision

Comitter:
Jake867
Date:
Sun May 01 22:51:18 2016 +0000
Parent:
25:5a312725710a
Commit message:
Set

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun May 01 00:26:52 2016 +0000
+++ b/main.cpp	Sun May 01 22:51:18 2016 +0000
@@ -30,10 +30,15 @@
 short unsigned Analog_out_data[32]; // holds the samples that will be output to p18
 volatile int noteFreq;              // the current frequency of the note being played
 float noteDuration = 2.0f;          // default note duration will be 2 seconds
-float attackPercentage[5] = {};     // holds values for percentage of waveform that the attack should take up
-float decayPercentage[5] = {};      // holds values for percentage of waveform that the decay should take up
-float sustainPercentage[5] = {};    // holds values for the percentage of waveform that the sustain should take up
-float releasePercentage[5] = {};    // holds values for the percentage of waveform that the sutain should take up
+float attackPercentage[5] = {0.7, 0.05, 0.25, 0.05, 0.1};     // holds values for percentage of waveform that the attack should take up
+float decayPercentage[5] = {0.1, 0.05, 0.25, 0.05, 0.1};      // holds values for percentage of waveform that the decay should take up
+float sustainPercentage[5] = {0.1, 0.8, 0.25, 0.05, 0.3};    // holds values for the percentage of waveform that the sustain should take up
+float releasePercentage[5] = {0.1, 0.1, 0.25, 0.05, 0.1};    // holds values for the percentage of waveform that the sutain should take up
+float attackDuration;
+float decayDuration;
+float sustainDuration;
+float releaseDuration;
+float sustainAmplitude = 0.6;
 
 /* Coefficient Matrices Corresponding to Different Attack Values
 each matrix is comprised of 4 elements (32/8). The first matrix corresponds
@@ -77,7 +82,8 @@
         break;
         }
         uLCD.printf("Octave: %i\r\n",currentOctave);        // displays octave
-        uLCD.printf("Current ADSR Preset: %i\r\n",currentADSR);     // displays attack value
+        uLCD.printf("ADSR: %i\r\n",currentADSR);     // displays attack value
+        uLCD.printf("Duration: %f\r\n", noteDuration);
     }
 }
 
@@ -92,7 +98,7 @@
     for(int j = 0; j < 32; j++)
     {
         attackVals[j] = (float)j/32.0f;
-        decayVals[j] = 1.0f - (j*((1-sustainAmplitude)/32))
+        decayVals[j] = 1.0f - (j*((1-sustainAmplitude)/32));
         sustainVals[j] = (float)sustainAmplitude;
         releaseVals[j] = (float)sustainAmplitude - (((sustainAmplitude)/32)*j);
     }
@@ -161,17 +167,17 @@
         case sine:
             //Generate sine wave values
             generate_sineWave(frequency);
-            generate_durations(currentASDR);
+            generate_durations(currentADSR);
             break;
         case square:
             //Generate square wave values
             generate_squareWave(frequency);
-            generate_durations(currentASDR);
+            generate_durations(currentADSR);
             break;
         case sawtooth:
             //Generate sawtooth wave values
             generate_sawtoothWave(frequency);
-            generate_durations(currentASDR);
+            generate_durations(currentADSR);
             break;
         default:
         break;
@@ -205,12 +211,9 @@
 
 void write_to_SDCard(char note)
 {
-    int AttackBits, SustainBits, DecayBits, ReleaseBits, OctaveBits, NoteBits;
+    int ADSRBits, OctaveBits, NoteBits;
+    ADSRBits = currentADSR;
  
-    AttackBits = currentAttackVal;  // Holds the value of the attack parameter
-    DecayBits = currentDecayVal;    // Holds the value of the decay parameter
-    SustainBits = currentSustainVal;// Holds the value of the sustain parameter
-    ReleaseBits = currentReleaseVal;// Holds the value of the release parameter
     OctaveBits = currentOctave;
     switch(note){
         case 'C':                   // a C corresponds to a 3
@@ -239,7 +242,7 @@
             break;
         }
     int writeVal;
-    writeVal  = (AttackBits << 15) | (DecayBits << 12) | (SustainBits << 9) | (ReleaseBits << 6) | (OctaveBits << 3) | (NoteBits);
+    writeVal  = (ADSRBits << 6) | (OctaveBits << 3) | (NoteBits);
     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
@@ -254,8 +257,13 @@
     
     
     mkdir("/sd/noteRecords", 0777);             // make directory to hold the record of notes played
+    FILE *fp = fopen("/sd/noteRecords/note_record_01.txt", "w");        // 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
+    }
+    fprintf(fp,"");      // writes value to the text file in hexadecimal
+    fclose(fp);
 
-    initialize_sustainVals();                   // fill the lookup tables with the sustain values in them
     initialize_ADSRVals();                      // fill the lookup tables for ADSR percentages
     
     PC.baud(9600);                              // setup baud rate for PC serial connection
@@ -268,7 +276,10 @@
         if((keyPress == C_NOTE_KEY) && (readyFlag)){        // Play note C
             set_Note_Freq(noteArray[currentOctave-1][0]);   // set the note frequency to the proper value 
             create_samples(noteFreq, myWave);               // creates the samples that are going to be output to the waveform
-            mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, attackDuration, attackVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, decayDuration, decayVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, sustainDuration, sustainVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, releaseDuration, releaseVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
             write_to_SDCard('C');                           // writes to the SD card
             readyFlag = false;                              // set this flag to false so that the program will not try to process the key press more than once
             
@@ -276,7 +287,10 @@
         else if((keyPress == D_NOTE_KEY) && (readyFlag)){ // Play note D
             set_Note_Freq(noteArray[currentOctave-1][1]);
             create_samples(noteFreq, myWave);
-            mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data);
+            mySpeaker.PlayNote(noteFreq, attackDuration, attackVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, decayDuration, decayVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, sustainDuration, sustainVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, releaseDuration, releaseVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
             write_to_SDCard('D');
             readyFlag = false;
             
@@ -284,35 +298,50 @@
         else if((keyPress == E_NOTE_KEY) && (readyFlag)){ // Play note E
             set_Note_Freq(noteArray[currentOctave-1][2]);
             create_samples(noteFreq, myWave);
-            mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data);
+            mySpeaker.PlayNote(noteFreq, attackDuration, attackVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, decayDuration, decayVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, sustainDuration, sustainVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, releaseDuration, releaseVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
             write_to_SDCard('E');
             readyFlag = false;
         }
         else if((keyPress == F_NOTE_KEY) && (readyFlag)){ // Play note F
             set_Note_Freq(noteArray[currentOctave-1][3]);
             create_samples(noteFreq, myWave);
-            mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data);
+            mySpeaker.PlayNote(noteFreq, attackDuration, attackVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, decayDuration, decayVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, sustainDuration, sustainVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, releaseDuration, releaseVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
             write_to_SDCard('F');
             readyFlag = false;
         }
         else if((keyPress == G_NOTE_KEY) && (readyFlag)){ // Play note G
             set_Note_Freq(noteArray[currentOctave-1][4]);
             create_samples(noteFreq, myWave);
-            mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data);
+            mySpeaker.PlayNote(noteFreq, attackDuration, attackVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, decayDuration, decayVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, sustainDuration, sustainVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, releaseDuration, releaseVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
             write_to_SDCard('G');
             readyFlag = false;
         }
         else if((keyPress == A_NOTE_KEY) && (readyFlag)){ // Play note A
             set_Note_Freq(noteArray[currentOctave][5]);
             create_samples(noteFreq, myWave);
-            mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data);
+            mySpeaker.PlayNote(noteFreq, attackDuration, attackVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, decayDuration, decayVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, sustainDuration, sustainVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, releaseDuration, releaseVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
             write_to_SDCard('A');
             readyFlag = false;
         }
         else if((keyPress == B_NOTE_KEY) && (readyFlag)){ // Play note B
             set_Note_Freq(noteArray[currentOctave][6]);
             create_samples(noteFreq, myWave);
-            mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data);
+            mySpeaker.PlayNote(noteFreq, attackDuration, attackVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, decayDuration, decayVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, sustainDuration, sustainVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
+            mySpeaker.PlayNote(noteFreq, releaseDuration, releaseVals, Analog_out_data);                              // outputs the samples that are currently in the buffer to p18
             write_to_SDCard('B');
             readyFlag = false;
         }
@@ -346,7 +375,7 @@
         }
         else if((keyPress == RAISE_ADSR_KEY) && (readyFlag)){ // button R pressed
             // Raise Release Value
-            if(currentReleaseVal < 5){
+            if(currentADSR < 5){
             currentADSR++;
             }
             else
@@ -355,7 +384,7 @@
         }
         else if((keyPress == LOWER_ADSR_KEY) && (readyFlag)){ // button F pressed
             // Lower Release Value
-            if(currentReleaseVal > 1){
+            if(currentADSR > 1){
             currentADSR--;
             }
             else