Abel Zatarain / Mbed 2 deprecated FMSynthCSUSM

Dependencies:   mbed

Revision:
18:4992aa537387
Parent:
17:65437c94ab1b
Child:
19:72886f894dc5
--- a/main.cpp	Sun Dec 10 21:02:54 2017 +0000
+++ b/main.cpp	Mon Dec 11 20:30:36 2017 +0000
@@ -43,12 +43,12 @@
 //non-constants
 //Most of these will be recalculated or reset on every input cycle of the main
 //  loop, as appropriate
-int FMmult;
-int Volume;
-int modVol;
-int64_t keyboard;
-int64_t modattack;
-int64_t carattack;
+int FMmult = 1;
+int Volume = 0xffff;
+int modVol = 0x2000;
+int64_t keyboard = 0;
+int64_t modattack = 0x1ffffffffffff;
+int64_t carattack = 0x1ffffffffffff;
 int carrierPhases[numKeys];
 int modulatorPhases[numKeys];
 int envelopeAmpsC[numKeys];
@@ -56,14 +56,14 @@
 
 //int testTone = 0;
 
-int modA;
-int modD;
-int modS;
-int modR;
-int carA;
-int carD;
-int carS;
-int carR;
+int modA = 0xffff;
+int modD = 0xffff;
+int modS = 0;
+int modR = 0xffff;
+int carA = 0xffff;
+int carD = 0xffff;
+int carS = 0;
+int carR = 0xffff;
 
 int fastSin(const int phase){
     int index = (phase & 0x3ffc) >> 2;
@@ -101,7 +101,6 @@
     //testTone = testTone + carrierIncrements[25] & 0xffff;
     
     for(int64_t i = 0; i < numKeys; ++i){
-        subsignal = 0;
         keymask = 1ll << i;
                 
         if(!(keymask & keyboard)){
@@ -112,12 +111,13 @@
                 envelopeAmpsC[i] -= carR;
             }
             if(envelopeAmpsM[i] > 0){
-                envelopeAmpsM[i] -= carR;
+                envelopeAmpsM[i] -= modR;
             }
         }else{
             if(envelopeAmpsC[i] <= 0){
                 carrierPhases[i] = 0;
                 modulatorPhases[i] = 0;
+                envelopeAmpsM[i] = 0;
             }
             
             if(keymask & carattack){
@@ -138,7 +138,7 @@
                     envelopeAmpsM[i] += modA;
                 }else{
                     envelopeAmpsM[i] = attackLimit;
-                    carattack &= ~keymask;
+                    modattack &= ~keymask;
                 }
             }else{
                 if(envelopeAmpsM[i] > modS){
@@ -152,10 +152,10 @@
                 modulation = (modulation * modVol) >> 16;
                 carrierPhases[i] += carrierIncrements[i] + modulation;
                 subsignal = (fastSin(carrierPhases[i]) * envelopeAmpsC[i])>>16;
+                wave += subsignal >> 3;
             }
         }
         
-        wave += subsignal >> 3;
     }
     //if(keyboard)
     //    wave += fastSin(testTone);
@@ -172,31 +172,16 @@
 
 int main() {
     int ratNumer;
-    int ratDenom;
-    
-    int64_t keytemp = 0;
+    int ratDenom;    
+    int64_t keytemp;
+    int tempCarA, tempCarD, tempCarR, tempModA, tempModD, tempModR;
 
-    //pc.printf("Starting up...");
-    FMmult = 1;
-    Volume = 0;
-    modVol = 0;
-    keyboard = 0ll;
-    modattack = 0x1ffffffffffff;
-    carattack = 0x1ffffffffffff;
     for(int i = 0; i < numKeys; ++i){
         carrierPhases[i] = 0;
         modulatorPhases[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.00005);//debug
@@ -211,45 +196,50 @@
         
         modVol = (int)inModAmt.read_u16();
         
-        if(! inCarA.read_u16())
+        tempCarA = inCarA.read_u16();
+        if(! tempCarA)
             carA = 0xffff;
         else
-            carA = 0xffff / ((int)inCarA.read_u16());
+            carA = 0xffff / tempCarA;
         
-        if(! inCarD.read_u16())
+        tempCarD = inCarD.read_u16();
+        if(! tempCarD)
             carD = 0xffff;
         else
-            carD = 0xffff / ((int)inCarD.read_u16());
+            carD = 0xffff / tempCarD;
         
         carS = (int)inCarS.read_u16();
         
-        if(! inCarR.read_u16())
+        tempCarR = inCarR.read_u16();
+        if(! tempCarR)
             carR = 0xffff;
         else
-            carR = 0xffff / ((int)inCarR.read_u16());
+            carR = 0xffff / tempCarR;
         
-        if(! inModA.read_u16())
+        tempModA = inModA.read_u16();
+        if(! tempModA)
             modA = 0xffff;
         else
-            modA = 0xffff / ((int)inModA.read_u16());
+            modA = 0xffff / tempModA;
         
-        if(! inModD.read_u16())
+        tempModD = inModD.read_u16();
+        if(! tempModD)
             modD = 0xffff;
         else
-            modD = 0xffff / ((int)inModD.read_u16());
+            modD = 0xffff / tempModD;
         
         modS = (int)inModS.read_u16();
         
-        if(! inModR.read_u16())
+        tempModR = inModR.read_u16();
+        if(! tempModR)
             modR = 0xffff;
         else
-            modR = 0xffff / ((int)inModR.read_u16());
-
+            modR = 0xffff / tempModR;
         
         keytemp = 0; //zero the keys before we start ORing on top of everything
         for(int i = 0; i < 9; ++i) {
             bankSelect = (~(1 << i)) & bankSelect.mask();
-            wait_us(200);
+            wait_us(40);
             int shiftOffset = 6LL * i;
             
             keytemp |= ((~(unsigned long long)keyBank) & (unsigned long long)keyBank.mask()) << (unsigned long long)shiftOffset;
@@ -258,6 +248,6 @@
         
         keyboard = keytemp;
         
-        wait_ms(5);
+        wait_ms(10);
     }
 }