Abel Zatarain / Mbed 2 deprecated FMSynthCSUSM

Dependencies:   mbed

Revision:
9:86c0035f5321
Parent:
8:13b0594510de
Child:
10:59c829586a4f
--- a/main.cpp	Thu Dec 07 02:09:10 2017 +0000
+++ b/main.cpp	Thu Dec 07 19:18:51 2017 +0000
@@ -1,25 +1,25 @@
 #include "mbed.h"
 #include "sintable.h"
 
-AnalogOut DAC0(PA_4);//Not labeled in the docs for the f401, but seems to be for all
+AnalogOut outMono(PA_4);//Not labeled in the docs for the f401, but seems to be for all
 //AnalogOut DAC1(PA_5);
 
-AnalogIn ADC0(PA_0);
+AnalogIn inVol(PA_0);
 AnalogIn inModAmt(PA_1);
 //AnalogIn ADC2(PA_2);//these are the uart pins!!
 //AnalogIn ADC3(PA_3);//these are the uart pins!!
 //AnalogIn ADC4(PA_4);//we're using these for output
 //AnalogIn ADC5(PA_5);//we're using these for output
-AnalogIn ADC6(PA_6);
-AnalogIn ADC7(PA_7);
+AnalogIn inCarA(PA_6);
+AnalogIn inCarD(PA_7);
 //AnalogIn ADC8(PB_0);//lets leave the 2 we aren't using in a single port
 //AnalogIn ADC9(PB_1);//that way we know there's not ADCs on one of them
-AnalogIn ADC10(PC_0);
-AnalogIn ADC11(PC_1);
-AnalogIn ADC12(PC_2);
-AnalogIn ADC13(PC_3);
-AnalogIn ADC14(PC_4);
-AnalogIn ADC15(PC_5);
+AnalogIn inCarS(PC_0);
+AnalogIn inCarR(PC_1);
+AnalogIn inModA(PC_2);
+AnalogIn inModD(PC_3);
+AnalogIn inModS(PC_4);
+AnalogIn inModR(PC_5);
 
 //BusIn keyBank(PC_10, PC_11, PC_12, PC_13, PD_2, PH_1); old
 BusIn keyBank(PH_1, PD_2, PC_13, PC_12, PC_11, PC_10);
@@ -29,21 +29,9 @@
 
 Serial pc(USBTX, USBRX);
 
-//Renaming ports
-#define inVol ADC0
-//#define inModAmt ADC1
-#define inCarA ADC6
-#define inCarD ADC7
-#define inCarS ADC10
-#define inCarR ADC11
-#define inModA ADC12
-#define inModD ADC13
-#define inModS ADC14
-#define inModR ADC15
-#define outMono DAC0
+Ticker synthesisClock;
 
 #define numKeys 49
-#define PI M_PI
 
 //constants
 const int carrierIncrements[] = {107, 113, 120, 127, 135, 143, 151, 160, 170, 
@@ -52,7 +40,7 @@
     1019, 1080, 1144, 1212, 1284, 1360, 1441, 1527, 1618, 1714};
 const int attackLimit = (0x1 << 16) - 1;
 #define U_PI 3.14159265358979f
-const int fixed2pi = (int) ((2.0 * U_PI) * (0x1 << 16));
+const int fixed2pi = (2.0 * U_PI) * (0x1 << 16l);
 
 //non-constants
 //Most of these will be recalculated or reset on every input cycle of the main
@@ -62,7 +50,6 @@
 int modVol;
 int modAmpI;
 int carAmpS;
-//bool keysPressed[numKeys];
 int64_t keyboard;
 int carrierPhases[numKeys];
 int modulatorPhases[numKeys];
@@ -188,7 +175,33 @@
     int ratNumer;
     int ratDenom;
     
+    int64_t keytemp = 0;
+
+    FMmult = 1;
+    Volume = 0;
+    modVol = 0;
+    modAmpI = 0;
+    carAmpS = 0;
+    keyboard = 0ll;
+    for(int i = 0; i < numKeys; ++i){
+        carrierPhases[i] = 0;
+        modulatorPhases[i] = 0;
+        envelopeStatesC[i] = 0;
+        envelopeStatesM[i] = 0;
+        envelopeAmpsC[i] = 0;
+        envelopeAmpsM[i] = 0;
+    }
+    modA = 0;
+    modD = 0;
+    modS = 0;
+    modR = 0;
+    carA = 0;
+    carD = 0;
+    carS = 0;
+    carR = 0;
+    
     keyBank.mode(PullNone);
+    synthesisClock.attach(synthesize, 0.000025);
     
     while(true){
         ratNumer = 0xf & ~ numerator;
@@ -198,15 +211,39 @@
         Volume = (int)inVol.read_u16();
         modVol = (int)inModAmt.read_u16();
         
-        carA = 0xffff / ((int)inCarA.read_u16());
-        carD = 0xffff / ((int)inCarD.read_u16());
+        if(! inCarA.read_u16())
+            carA = 0xffff;
+        else
+            carA = 0xffff / ((int)inCarA.read_u16());
+        
+        if(! inCarD.read_u16())
+            carD = 0xffff;
+        else
+            carD = 0xffff / ((int)inCarD.read_u16());
+        
         carS = (int)inCarS.read_u16();
-        carR = 0xffff / ((int)inCarR.read_u16());
+        
+        if(! inCarR.read_u16())
+            carR = 0xffff;
+        else
+            carR = 0xffff / ((int)inCarR.read_u16());
         
-        modA = 0xffff / ((int)inModA.read_u16());
-        modD = 0xffff / ((int)inModD.read_u16());
+        if(! inModA.read_u16())
+            modA = 0xffff;
+        else
+            modA = 0xffff / ((int)inModA.read_u16());
+        
+        if(! inModD.read_u16())
+            modD = 0xffff;
+        else
+            modD = 0xffff / ((int)inModD.read_u16());
+        
         modS = (int)inModS.read_u16();
-        modR = 0xffff / ((int)inModR.read_u16());
+        
+        if(! inModR.read_u16())
+            modR = 0xffff;
+        else
+            modR = 0xffff / ((int)inModR.read_u16());
         
         //THIS IS CORRECT (1 means the key is connected)
         //                 B8    B7    B6    B5    B4    B3    B2    B1  B0
@@ -216,24 +253,20 @@
         //THIS IS NOT CORRECT (1 means the key is connected)
         //            B8    B7    B6    B5    B4    B3    B2    B1    B0
         //          |----||----||----||----||----||----||----||----||----|
-        //0000000000111111111111111111111111111111111111111111111111000001
-        //Thus, we need to subtract 5 from our shift for every bank > 0
-        //'keyboard' should be uint64_t and not int64_t b/c keyboard&=~62 will make it negative, which isn't a huge deal really
-        keyboard = 0; //zero the keys before we start ORing on top of everything
-        for(int i = 0; i < 9; ++i) { //removed <= 9 because there is not 10 banks
+        //0000000000111111111111111111111111111111111111111111111111100000
+        //Thus, we need to subtract 5 from our shift
+        keytemp = 0; //zero the keys before we start ORing on top of everything
+        for(int i = 0; i < 9; ++i) {
             bankSelect = (~(1LL << i)) & (unsigned long long)bankSelect.mask();
             wait_us(200);
             int shiftOffset = 6LL * i;
-            //shiftOffset -= 5; //bank 0 only has 1 key, so to prevent 5 missing keys and bits we subtract 5 for every bank > 0
-            //else keyboard &= ~31; //clear bits 1-5 (xx00000x) b/c we are ORing the next bank of keys on top of them & they may not be 0
+
     
-            keyboard |= ((~(unsigned long long)keyBank) & (unsigned long long)keyBank.mask()) << (unsigned long long)shiftOffset;
+            keytemp |= ((~(unsigned long long)keyBank) & (unsigned long long)keyBank.mask()) << (unsigned long long)shiftOffset;
         }
-        keyboard >>= 5;
-        /*for(int i = 63; i >= 0; i--) {
-            pc.printf("%d", !!(keyboard & (1LL << i)));
-        }
-        pc.printf("\r\n");
-        wait_ms(200);*/
+        keytemp >>= 5;
+        
+        keyboard = keytemp;
+        wait_ms(5);
     }
 }