Bluetooth Enabled Keyboard/Synthesizer for mbed

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

Revision:
20:f3e994db66be
Parent:
19:2f635d03467c
--- a/main.cpp	Fri Apr 29 23:26:49 2016 +0000
+++ b/main.cpp	Sat Apr 30 02:45:18 2016 +0000
@@ -4,6 +4,9 @@
 #include <vector>
 #include "uLCD_4DGL.h"
 #include "synthesizer.h"
+#include <iostream>
+#include <cstring>
+#include <iomanip>
 RawSerial Blue(p13,p14);
 RawSerial PC(USBTX,USBRX);
 DigitalOut myled(LED1);
@@ -32,7 +35,8 @@
 vector<float> sampleBuffer;        // vector to hold samples of generated waveform
 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
+double timeIncrement = (2.0/256.0);     // 2 seconds with 256 samples
+short unsigned Analog_Out_Data[32];
 
 /* Coefficient Matrices Corresponding to Different Attack Values
 each matrix is comprised of 32 elements (256/8). The first matrix corresponds
@@ -371,6 +375,9 @@
     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
+    
+    full_envelope = 
+    
     for(int i = 0; i < attack_range; i++)
     {
         sampleBuffer[i] = sampleBuffer[i] * currentAttackTable[i];
@@ -389,39 +396,25 @@
     }     
 }
 
-void generate_sineWave(int frequency)       // Generates samples for a sine wave of a given input frequency
-{
-    double t = 0;                           // Represents time, since we want each note to last 2 seconds and have 256 samples
-    for(int i = 0; i < 256 ; i++)
+void generateSquareWave(){
+    for(int i = 0; i < 32; i++)
     {
-        sampleBuffer.push_back(((sin(2*(PI)*frequency*(float)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   
+    if(i<16)
+        Analog_Out_Data[k] = 1 * 65536;
+    else
+        Analog_Out_Data[k] = 0;
     }
 }
 
-void generate_sawtoothWave(int frequency)   // Generates samples for a sawtooth wave of a given input frequency
-{
-    double t = 0;                           // Represents time, since we want each note to last 2 seconds and have 256 samples
-    for(int i = 0; i<256 ; i++)
+void generateSawtoothWave(){
+    float t = 0;
+    for(int i = 0; i < 32; i++)
     {
-        sampleBuffer.push_back((2*((float)t*frequency) - (.5 + (t*frequency)) + 1) / 2);
-        t = t + timeIncrement;              // increment t for calculation of next value in the waveform   
+        Analog_Out_Data[k] = t * 65536;
+        t += (1/32);
     }
 }
 
-void generate_squareWave(int frequency)     // Generates samples for a square wave of a given input frequency. Looks at whether we have seen an even or odd number of 'widths' to determine if wave should be high or low at given t
-{
-    double width = (1 / (2 * frequency));     // Width of a half period of the square wave
-    double t = 0;                           // Represents time, since we want a 2 second note with 256 samples
-    for(int i = 0; i < 256; i++)
-    {
-        if(((int)(t / width) % 2 ) == 0)     // Even, write a 1 for the square wave
-        sampleBuffer.push_back((float)1.0);
-        else                                // Odd, write a 0 for the square wave
-        sampleBuffer.push_back((float)0.0);
-        t = t + timeIncrement;              // increment t for calculation of next value in the waveform
-    }
-}
 
 /* Generates the waveforms that will be output to the AnalogOut pin after being altered by the ADSR coefficient matrices.
 The envelope is only applied to sine waves here because when applied to the other wave shapes, the sound does not sounds good.