Abel Zatarain / Mbed 2 deprecated FMSynthCSUSM

Dependencies:   mbed

main.cpp

Committer:
davolfman
Date:
2017-12-01
Revision:
6:9f8c8c3c111d
Parent:
5:ac5c4bd3ef4b
Child:
7:b0cd74923bc6

File content as of revision 6:9f8c8c3c111d:

#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 DAC1(PA_5);

AnalogIn ADC0(PA_0);
AnalogIn ADC1(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 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);

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);
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 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

#define numKeys 49
#define PI M_PI

//constants
const int carrierIncrements[] = {107, 113, 120, 127, 135, 143, 151, 160, 170, 
    180, 190, 202, 214, 227, 240, 254, 270, 286, 303, 321, 340, 360, 381, 404, 
    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));

//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;
int modAmpI;
int carAmpS;
//bool keysPressed[numKeys];
int64_t keyboard;
int carrierPhases[numKeys];
int modulatorPhases[numKeys];
short envelopeStatesC[numKeys];
short envelopeStatesM[numKeys];
int envelopeAmpsC[numKeys];
int envelopeAmpsM[numKeys];

int modA;
int modD;
int modS;
int modR;
int carA;
int carD;
int carS;
int carR;

int fastSin(const int phase){
    int index = (phase & 0x3ffc) >> 2;
    int subindex = phase & 0x3;
    int quadrant = (phase & 0xc000) >> 14;
    int sum = 0;
    switch (quadrant) {
        case 0:
            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];
            break;
        case 2:
            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];
            break;
    }
    sum = sum >> 2;
    
    return sum;
}

void synthesize(){
    carAmpS = 0;
    for(int i = 0; i < numKeys; ++i){
        if(keyboard & (0x1 << i)){
            if(envelopeStatesC[i] < 2)
                envelopeStatesC[i] = 4;
            if(envelopeStatesM[i] < 2)
                envelopeStatesM[i] = 4;
            
            if(envelopeStatesC[i] == 4){
                envelopeAmpsC[i] += carA;
                if(envelopeAmpsC[i] >= attackLimit){
                    envelopeAmpsC[i] = attackLimit;
                    envelopeStatesC[i] = 3;
                }
            }
            
            if(envelopeStatesM[i] == 4){
                envelopeAmpsM[i] += modA;
                if(envelopeAmpsM[i] >= attackLimit){
                    envelopeAmpsM[i] = attackLimit;
                    envelopeStatesM[i] = 3;
                }
            }
            
            if(envelopeStatesC[i] == 3){
                envelopeAmpsC[i] += carD;
                if(envelopeAmpsC[i] <= carS){
                    envelopeAmpsC[i] = carS;
                    envelopeStatesC[i] = 2;
                }
            }

            if(envelopeStatesM[i] == 3){
                envelopeAmpsM[i] += modD;
                if(envelopeAmpsM[i] <= modS){
                    envelopeAmpsM[i] = modS;
                    envelopeStatesM[i] = 2;
                }
            }
        }else{
            if(envelopeStatesC[i] > 1)
                envelopeStatesC[i] = 1;
            if(envelopeStatesM[i] > 1)
                envelopeStatesM[i] = 1;
                
            if(envelopeStatesC[i] == 1){
                if(envelopeAmpsC[i] <= 0){
                    envelopeStatesC[i] = 0;
                    envelopeAmpsC[i] = 0;
                }else{
                    envelopeAmpsC[i] -= carR;
                }
            }
            if(envelopeStatesM[i] == 1){
                if(envelopeAmpsM[i] <= 0){
                    envelopeStatesM[i] = 0;
                    envelopeAmpsM[i] = 0;
                }else{
                    envelopeAmpsM[i] -= modR;
                }
            }            
        }
        
        if(envelopeAmpsC[i] > 0){
            modulatorPhases[i] += (Fmult * 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;
        }
    }
    outMono.write_u16(((carAmps / numKeys) * Volume) >> 16);
}


int main() {
    int ratNumer;
    int ratDenom;
    
    while(true){
        ratNumer = 0xf & ~ numerator;
        ratDenom = 0xf & ~ denom;
        FMult = (ratNumer << 16) / ratDenom;
        
        Volume = (int)inVol.read_u16();
        modVol = (int)inModAmt.read_u16();
        
        carA = 0xffff / ((int)inCarA.read_u16());
        carD = 0xffff / ((int)inCarD.read_u16());
        carS = (int)inCarS.read_u16();
        carR = 0xffff / ((int)inCarR.read_u16());
        
        modA = 0xffff / ((int)inModA.read_u16());
        modD = 0xffff / ((int)inModD.read_u16());
        modS = (int)inModS.read_u16();
        modR = 0xffff / ((int)inModR.read_u16());
        
        
    }
}