
Bluetooth Enabled Keyboard/Synthesizer for mbed
Dependencies: mbed 4DGL-uLCD-SE SDFileSystem mbed-rtos
Diff: main.cpp
- Revision:
- 23:cf9d43d5a5b4
- Parent:
- 22:9c80f7bcef86
- Child:
- 24:3bd4e691ae59
--- a/main.cpp Sat Apr 30 21:54:33 2016 +0000 +++ b/main.cpp Sat Apr 30 23:01:52 2016 +0000 @@ -26,11 +26,12 @@ volatile int currentDecayVal = 3; // values will range from 1-5, default to 3 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 -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 +float currentAttackTable[4] = {0 , 0.63 , 0.84 , 1}; // pointer to the correct attack coefficient table +float currentDecayTable[4] = {0.77 , 0.7 , 0.63 , 0.60}; // pointer to the correct decay coefficient table +float currentSustainTable[20]= {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, 0.6, 0.6, 0.6}; // pointer to the correct sustain coefficient table +float currentReleaseTable[4] = {0.33 , 0.16 , 0.08 , 0}; // pointer to the correct release coefficient table short unsigned Analog_out_data[32]; +float coefficientMatrix[32]; // Array to hold all 32 coefficients for modulating the amplitude of the waveform volatile int noteFreq; // the current frequency of the note being played /* Coefficient Matrices Corresponding to Different Attack Values @@ -38,15 +39,15 @@ to an attack value of 5. */ -double attackVals5[4] = {0 , 0.9 , 0.98 , 1}; //Approaches the maximum amplitude the quickest - corresponds to an attackValue of 5 +float 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 +float 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 +float 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 +float 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 +float 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 @@ -54,33 +55,33 @@ to a decay value of 5. */ -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 +float decayVals5[4] = {0.67 , 0.63 , 0.62 , 0.6}; //Approaches the sustain amplitude the quickest - corresponds to a decay value of 5 +float decayVals4[4] = {0.73 , 0.675 , 0.63 , 0.60}; // Decay value of 4 +float decayVals3[4] = {0.77 , 0.7 , 0.63 , 0.60}; // Decay value of 3 +float decayVals2[4] = {0.84 , 0.73 , 0.65 , 0.6}; // Decay value of 2 +float 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 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[20]; -double sustainVals4[20]; -double sustainVals3[20]; -double sustainVals2[20]; -double sustainVals1[20]; +float sustainVals5[20]; +float sustainVals4[20]; +float sustainVals3[20]; +float sustainVals2[20]; +float 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[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 +float releaseVals5[4] = {0.1 , 0.03 , 0.01 , 0}; // Releases (goes to 0 amplitude) the quickest - corresponds to a release value of 5 +float releaseVals4[4] = {0.2 , 0.1 , .04 , 0}; // Release value of 4 +float releaseVals3[4] = {0.33 , 0.16 , 0.08 , 0}; // Release value of 3 +float releaseVals2[4] = {0.4 , 0.23 , 0.1 , 0}; // Release value of 2 +float 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 @@ -123,93 +124,86 @@ void change_Attack_Table(int attackVal) // change which table of coefficients to use for altering the attack portion of the waveform { - switch(attackVal){ - case 5: - currentAttackTable = attackVals5; - break; - case 4: - currentAttackTable = attackVals4; - break; - case 3: - currentAttackTable = attackVals3; - break; - case 2: - currentAttackTable = attackVals2; - break; - case 1: - currentAttackTable = attackVals1; - break; - default: - break; + for(int i = 0; i < 4; i++){ + if( attackVal == 5){ + currentAttackTable[i] = attackVals5[i]; + } + else if( attackVal == 4){ + currentAttackTable[i] = attackVals4[i]; + } + else if( attackVal == 3){ + currentAttackTable[i] = attackVals3[i]; + } + else if( attackVal == 2){ + currentAttackTable[i] = attackVals2[i]; + } + else if( attackVal == 1){ + currentAttackTable[i] = attackVals1[i]; + } } } void change_Decay_Table(int decayVal) // change which table of coefficients to use for altering the decay portion of the waveform { - switch(decayVal){ - case 5: - currentDecayTable = decayVals5; - break; - case 4: - currentDecayTable = decayVals4; - break; - case 3: - currentDecayTable = decayVals3; - break; - case 2: - currentDecayTable = decayVals2; - break; - case 1: - currentDecayTable = decayVals1; - break; - default: - break; + for(int i = 0; i < 4; i++){ + if( decayVal == 5){ + currentDecayTable[i] = decayVals5[i]; + } + else if( decayVal == 4){ + currentDecayTable[i] = decayVals4[i]; + } + else if( decayVal == 3){ + currentDecayTable[i] = decayVals3[i]; + } + else if( decayVal == 2){ + currentDecayTable[i] = decayVals2[i]; + } + else if( decayVal == 1){ + currentDecayTable[i] = decayVals1[i]; + } } } void change_Sustain_Table(int sustainVal) // change which table of coefficients to use for altering the sustain portion of the waveform { - switch(sustainVal){ - case 5: - currentSustainTable = sustainVals5; - break; - case 4: - currentSustainTable = sustainVals4; - break; - case 3: - currentSustainTable = sustainVals3; - break; - case 2: - currentSustainTable = sustainVals2; - break; - case 1: - currentSustainTable = sustainVals1; - break; - default: - break; + + for(int i = 0; i < 20; i++){ + if( sustainVal == 5){ + currentSustainTable[i] = sustainVals5[i]; + } + else if( sustainVal == 4){ + currentSustainTable[i] = sustainVals4[i]; + } + else if( sustainVal == 3){ + currentSustainTable[i] = sustainVals3[i]; + } + else if( sustainVal == 2){ + currentSustainTable[i] = sustainVals2[i]; + } + else if( sustainVal == 1){ + currentSustainTable[i] = sustainVals1[i]; + } } } void change_Release_Table(int releaseVal) // change which table of coefficients to use for altering the release portion of the waveform { - switch(releaseVal){ - case 5: - currentReleaseTable = releaseVals5; - break; - case 4: - currentReleaseTable = releaseVals4; - break; - case 3: - currentReleaseTable = releaseVals3; - break; - case 2: - currentReleaseTable = releaseVals2; - break; - case 1: - currentReleaseTable = releaseVals1; - break; - default: - break; + for(int i = 0; i < 4; i++){ + if( releaseVal == 5){ + currentReleaseTable[i] = releaseVals5[i]; + } + else if( releaseVal == 4){ + currentReleaseTable[i] = releaseVals4[i]; + } + else if( releaseVal == 3){ + currentReleaseTable[i] = releaseVals3[i]; + } + else if( releaseVal == 2){ + currentReleaseTable[i] = releaseVals2[i]; + } + else if( releaseVal == 1){ + currentReleaseTable[i] = releaseVals1[i]; + } } } @@ -236,17 +230,25 @@ void apply_Envelope(void){ - double coefficientMatrix[32]; // Array to hold all 32 coefficients for modulating the amplitude of the waveform for(int j = 0; j < 32 ; j++) { - if( j < 4 ) + 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]; + //PC.printf("%f\n\r", currentAttackTable[j]); + } + else if(( j < 8) && ( j > 3)){ + coefficientMatrix[j] = currentDecayTable[j-4]; + //PC.printf("%f\n\r", currentDecayTable[j-4]); + } + else if(( j < 28) && (j > 7)){ + coefficientMatrix[j] = currentSustainTable[j-8]; + //PC.printf("%f\n\r", currentSustainTable[j-8]); + } + else if(( j < 32) && (j > 27)){ + coefficientMatrix[j] = currentReleaseTable[j-28]; + //PC.printf("%f\n\r", currentReleaseTable[j-28]); + } + PC.printf("%f\n\r", coefficientMatrix[j]); } } @@ -293,17 +295,17 @@ case sine: //Generate sine wave values generate_sineWave(frequency); - //apply_Envelope(); + apply_Envelope(); break; 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; @@ -399,7 +401,7 @@ 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, 1.0, Analog_out_data); // outputs the samples that are currently in the buffer to p18 + mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, 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 @@ -407,7 +409,7 @@ 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, 1.0, Analog_out_data); + mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data); write_to_SDCard('D'); readyFlag = false; @@ -415,35 +417,35 @@ 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, 1.0, Analog_out_data); + mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data); 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, 1.0, Analog_out_data); + mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data); 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, 1.0, Analog_out_data); + mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data); 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, 1.0, Analog_out_data); + mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data); 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, 1.0, Analog_out_data); + mySpeaker.PlayNote(noteFreq, 2, coefficientMatrix, Analog_out_data); write_to_SDCard('B'); readyFlag = false; }