CSUSM FM Synth 2017 / Mbed 2 deprecated FMSynthCSUSM

Dependencies:   mbed

Fork of STM32FMSynth by Steven Clark

Revision:
7:b0cd74923bc6
Parent:
6:9f8c8c3c111d
Child:
8:13b0594510de
diff -r 9f8c8c3c111d -r b0cd74923bc6 main.cpp
--- a/main.cpp	Fri Dec 01 22:48:48 2017 +0000
+++ b/main.cpp	Wed Dec 06 22:33:04 2017 +0000
@@ -2,10 +2,10 @@
 #include "sintable.h"
 
 AnalogOut DAC0(PA_4);//Not labeled in the docs for the f401, but seems to be for all
-AnalogOut DAC1(PA_5);
+//AnalogOut DAC1(PA_5);
 
 AnalogIn ADC0(PA_0);
-AnalogIn ADC1(PA_1);
+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
@@ -22,13 +22,13 @@
 AnalogIn ADC15(PC_5);
 
 BusIn keyBank(PC_10, PC_11, PC_12, PC_13, PC_14, PC_15);
-BusOut keySelect(PB_0, PB_1, PB_2, PB_3, PB_4, PB_5, PB_6, PB_7, PB_8);
+BusOut bankSelect(PB_0, PB_1, PB_2, PB_3, PB_4, PB_5, PB_6, PB_7, PB_8);
 BusIn numerator(PA_8, PA_9, PA_10, PA_11);
 BusIn denominator(PA_12, PA_13, PA_14, PA_15);
 
 //Renaming ports
 #define inVol ADC0
-#define inModAmt ADC1
+//#define inModAmt ADC1
 #define inCarA ADC6
 #define inCarD ADC7
 #define inCarS ADC10
@@ -48,7 +48,8 @@
     428, 454, 481, 509, 540, 572, 606, 642, 680, 720, 763, 809, 857, 908, 962, 
     1019, 1080, 1144, 1212, 1284, 1360, 1441, 1527, 1618, 1714};
 const int attackLimit = (0x1 << 16) - 1;
-const int fixed2Pi = (int) ((2.0 * PI) * (0x1 << 16));
+#define U_PI 3.14159265358979f
+const int fixed2pi = (int) ((2.0 * U_PI) * (0x1 << 16));
 
 //non-constants
 //Most of these will be recalculated or reset on every input cycle of the main
@@ -83,20 +84,20 @@
     int sum = 0;
     switch (quadrant) {
         case 0:
-            sum += (4 - subindex) * sintable[index];
-            sum += subindex * sintable[index+1];
+            sum += (4 - subindex) * sinTable[index];
+            sum += subindex * sinTable[index+1];
             break;
         case 1:
-            sum += (4 - subindex) * sintable[1+4095-index];
-            sum += subindex * sintable[4095-index];
+            sum += (4 - subindex) * sinTable[1+4095-index];
+            sum += subindex * sinTable[4095-index];
             break;
         case 2:
-            sum -= (4 - subindex) * sintable[index];
-            sum -= subindex * sintable[index+1];        
+            sum -= (4 - subindex) * sinTable[index];
+            sum -= subindex * sinTable[index+1];        
             break;
         case 3:
-            sum -= (4 - subindex) * sintable[1+4095-index];
-            sum -= subindex * sintable[4095-index];
+            sum -= (4 - subindex) * sinTable[1+4095-index];
+            sum -= subindex * sinTable[4095-index];
             break;
     }
     sum = sum >> 2;
@@ -169,14 +170,14 @@
         }
         
         if(envelopeAmpsC[i] > 0){
-            modulatorPhases[i] += (Fmult * carrierIncrements[i]) >> 16;
+            modulatorPhases[i] += (FMmult * carrierIncrements[i]) >> 16;
             modAmpI = fastSin((((modulatorPhases[i] * envelopeAmpsM[i]) >> 16)
                 * modVol) >> 16);
             carrierPhases[i] += ((carrierIncrements[i] + modAmpI) * fixed2pi) >> 16;
-            carAmps += (fastSin(carrierPhases[i]) * envelopeAmpsC[i]) >> 16;
+            carAmpS += (fastSin(carrierPhases[i]) * envelopeAmpsC[i]) >> 16;
         }
     }
-    outMono.write_u16(((carAmps / numKeys) * Volume) >> 16);
+    outMono.write_u16(((carAmpS / numKeys) * Volume) >> 16);
 }
 
 
@@ -186,8 +187,8 @@
     
     while(true){
         ratNumer = 0xf & ~ numerator;
-        ratDenom = 0xf & ~ denom;
-        FMult = (ratNumer << 16) / ratDenom;
+        ratDenom = 0xf & ~ denominator;
+        FMmult = (ratNumer << 16) / ratDenom;
         
         Volume = (int)inVol.read_u16();
         modVol = (int)inModAmt.read_u16();
@@ -202,6 +203,26 @@
         modS = (int)inModS.read_u16();
         modR = 0xffff / ((int)inModR.read_u16());
         
-        
+        //THIS IS CORRECT (1 means the key is connected)
+        //                 B8    B7    B6    B5    B4    B3    B2    B1  B0
+        //               |----||----||----||----||----||----||----||----||
+        //0000000000000001111111111111111111111111111111111111111111111111
+        //
+        //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
+            bankSelect = (short)(~(1 << i)) & bankSelect.mask();
+    
+            int shiftOffset = 6 * i;
+            if(i > 0) 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 &= ~62; //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 |= ((~keyBank) & keyBank.mask()) << shiftOffset;
+        }
     }
 }