CSUSM FM Synth 2017 / Mbed 2 deprecated FMSynthCSUSM

Dependencies:   mbed

Fork of STM32FMSynth by Steven Clark

Revision:
21:6dc8d091e878
Parent:
20:7ecec5738790
Child:
22:0307adac8c35
--- a/main.cpp	Fri Dec 15 03:29:07 2017 +0000
+++ b/main.cpp	Fri Dec 15 03:41:39 2017 +0000
@@ -104,46 +104,53 @@
     return sum;
 }
 
+///@brief calculates one audio sample given the keyboard state and envelope paramaters and passes it to the DAC
 void synthesize(){
-    int wave = 0;
-    int subsignal;
-    int64_t keymask;
-    
-    //testTone = testTone + carrierIncrements[25] & 0xffff;
-    
+    int wave = 0;// holds the sample being constructed.
+    int subsignal;// the subsample for one note
+    int64_t keymask;// holds a mask of the current note for easy access to keyboard and attack registers
+        
+    //for all keys
     for(int64_t i = 0; i < numKeys; ++i){
-        keymask = 1ll << i;
+        keymask = 1ll << i;//set the key mask
                 
-        if(!(keymask & keyboard)){
-            carattack |= keymask;
+        if(!(keymask & keyboard)){//if the key is NOT pressed
+            carattack |= keymask;//allow attack the next time it is
             modattack |= keymask;
             
+            //if envelope is still positive, decrement by decay rate
             if(envelopeAmpsC[i] > 0){
                 envelopeAmpsC[i] -= carR;
             }
             if(envelopeAmpsM[i] > 0){
                 envelopeAmpsM[i] -= modR;
             }
-        }else{
-            if(envelopeAmpsC[i] <= 0){
-                carrierPhases[i] = 0;
-                modulatorPhases[i] = 0;
+            
+        }else{//if the key IS pressed
+        
+            if(envelopeAmpsC[i] <= 0){//if this key was silent before,
+                carrierPhases[i] = 0;//reset the wave states
+                modulatorPhases[i] = 0;//this should prevent frequency drift from stopped FM
                 envelopeAmpsM[i] = 0;
+                envelopeAmpsC[i] = 1;//only do it once
             }
-            
+
+            //if carrier has not left attack phase
             if(keymask & carattack){
+                //add attack rate to envelope if not already maximised
                 if(envelopeAmpsC[i] < attackLimit ){
                     envelopeAmpsC[i] += carA;
-                }else{
+                }else{//otherwise clip to maximum and leave attack pahse
                     envelopeAmpsC[i] = attackLimit;
                     carattack &= ~keymask;
                 }
-            }else{
-                if(envelopeAmpsC[i] > carS){
+            }else{//if in decay/sustain
+                if(envelopeAmpsC[i] > carS){//subtract the decay rate if above sustain level
                     envelopeAmpsC[i] -= carD;
                 }
             }
             
+            //do all that again for the modulator envelope
             if(keymask & modattack){
                 if(envelopeAmpsM[i] < attackLimit){
                     envelopeAmpsM[i] += modA;