CSUSM FM Synth 2017 / Mbed 2 deprecated FMSynthCSUSM

Dependencies:   mbed

Fork of STM32FMSynth by Steven Clark

Revision:
22:0307adac8c35
Parent:
21:6dc8d091e878
Child:
23:7a9ff5230149
--- a/main.cpp	Fri Dec 15 03:41:39 2017 +0000
+++ b/main.cpp	Fri Dec 15 03:51:47 2017 +0000
@@ -164,26 +164,43 @@
                 }
             }
             
-            if(envelopeAmpsC[i] > 0){
-                modulatorPhases[i] += (carrierIncrements[i] * FMmult)>> 16;
-                int modulation = (fastSin(modulatorPhases[i]) * envelopeAmpsM[i])>>16;
-                modulation = (modulation * modVol) >> 16;
-                carrierPhases[i] += carrierIncrements[i] + modulation;
-                subsignal = (fastSin(carrierPhases[i]) * envelopeAmpsC[i])>>16;
-                wave += subsignal >> 3;
-            }
+        }
+        
+        //If this subsignal is not silent
+        if(envelopeAmpsC[i] > 0){
+            //calculate the new phase of the modulator
+            modulatorPhases[i] += (carrierIncrements[i] * FMmult)>> 16;
+                
+            //get the sine for that phase and scale it by the envelope
+            int modulation = (fastSin(modulatorPhases[i]) * envelopeAmpsM[i])>>16;
+             
+            //scale it again by the modulation amount
+            modulation = (modulation * modVol) >> 16;
+           
+            //calculate the new phase of the carrier, modualting frequency by the modulation
+            carrierPhases[i] += carrierIncrements[i] + modulation;
+                
+            //get the sine for that carrier phase and scale by the envelope
+            //additionally divide by 8 to allow 8 notes to play without saturating the DAC
+            subsignal = (fastSin(carrierPhases[i]) * envelopeAmpsC[i])>>19;
+                
+            //add the sample for this note into the overall sample
+            wave += subsignal;
         }
         
     }
-    //if(keyboard)
-    //    wave += fastSin(testTone);
-    
+
+    //Scale the complete sample by the volume    
     wave = wave * Volume >> 16;
-    //wave = wave << 3;
 
+    //clip the sample to within the limits of the DAC if neccessary
     wave = (wave > 32767) ? 32767 : wave;
     wave = (wave < -32768) ? - 32768 : wave;
+    
+    //Center the waveform within the range of the DAC
     wave += 32768;
+    
+    //output the sample
     outMono.write_u16(wave);
 }