CSUSM FM Synth 2017 / Mbed 2 deprecated FMSynthCSUSM

Dependencies:   mbed

Fork of STM32FMSynth by Steven Clark

Committer:
davolfman
Date:
Fri Dec 15 03:51:47 2017 +0000
Revision:
22:0307adac8c35
Parent:
21:6dc8d091e878
Child:
23:7a9ff5230149
Commented Synthesize() probably fixed release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davolfman 0:b2f7400596ce 1 #include "mbed.h"
davolfman 3:83ac767f3a63 2 #include "sintable.h"
davolfman 0:b2f7400596ce 3
davolfman 9:86c0035f5321 4 AnalogOut outMono(PA_4);//Not labeled in the docs for the f401, but seems to be for all
davolfman 7:b0cd74923bc6 5 //AnalogOut DAC1(PA_5);
davolfman 0:b2f7400596ce 6
davolfman 9:86c0035f5321 7 AnalogIn inVol(PA_0);
davolfman 7:b0cd74923bc6 8 AnalogIn inModAmt(PA_1);
davolfman 6:9f8c8c3c111d 9 //AnalogIn ADC2(PA_2);//these are the uart pins!!
davolfman 6:9f8c8c3c111d 10 //AnalogIn ADC3(PA_3);//these are the uart pins!!
davolfman 0:b2f7400596ce 11 //AnalogIn ADC4(PA_4);//we're using these for output
davolfman 0:b2f7400596ce 12 //AnalogIn ADC5(PA_5);//we're using these for output
davolfman 9:86c0035f5321 13 AnalogIn inCarA(PA_6);
davolfman 9:86c0035f5321 14 AnalogIn inCarD(PA_7);
davolfman 0:b2f7400596ce 15 //AnalogIn ADC8(PB_0);//lets leave the 2 we aren't using in a single port
davolfman 0:b2f7400596ce 16 //AnalogIn ADC9(PB_1);//that way we know there's not ADCs on one of them
davolfman 9:86c0035f5321 17 AnalogIn inCarS(PC_0);
davolfman 9:86c0035f5321 18 AnalogIn inCarR(PC_1);
davolfman 9:86c0035f5321 19 AnalogIn inModA(PC_2);
davolfman 9:86c0035f5321 20 AnalogIn inModD(PC_3);
davolfman 9:86c0035f5321 21 AnalogIn inModS(PC_4);
davolfman 9:86c0035f5321 22 AnalogIn inModR(PC_5);
davolfman 0:b2f7400596ce 23
lenzi002 8:13b0594510de 24 //BusIn keyBank(PC_10, PC_11, PC_12, PC_13, PD_2, PH_1); old
lenzi002 8:13b0594510de 25 BusIn keyBank(PH_1, PD_2, PC_13, PC_12, PC_11, PC_10);
davolfman 7:b0cd74923bc6 26 BusOut bankSelect(PB_0, PB_1, PB_2, PB_3, PB_4, PB_5, PB_6, PB_7, PB_8);
davolfman 2:5454dee210ed 27 BusIn numerator(PA_8, PA_9, PA_10, PA_11);
davolfman 2:5454dee210ed 28 BusIn denominator(PA_12, PA_13, PA_14, PA_15);
davolfman 2:5454dee210ed 29
davolfman 16:c2f912cdd919 30 //Serial pc(USBTX, USBRX);
lenzi002 8:13b0594510de 31
davolfman 20:7ecec5738790 32 Ticker synthesisClock;// this object sets up an ISR to execute every given fraction of a second.
davolfman 0:b2f7400596ce 33
davolfman 20:7ecec5738790 34 #define numKeys 49// our keyboard includes from two octaves above middle c to two octaves below it
davolfman 0:b2f7400596ce 35
davolfman 4:1d2a699c95c7 36 //constants
davolfman 20:7ecec5738790 37
davolfman 20:7ecec5738790 38 //These are the pitches of the notes of our keyboard in hz * int16_max / sampling rate(20khz)
davolfman 16:c2f912cdd919 39 const int carrierIncrements[] = {214, 227, 240, 254, 270, 286, 303, 321, 340,
davolfman 16:c2f912cdd919 40 360, 381, 404, 428, 454, 481, 509, 540, 572, 606, 642, 680, 720, 763, 809,
davolfman 16:c2f912cdd919 41 857, 908, 962, 1019, 1080, 1144, 1212, 1284, 1360, 1441, 1527, 1618, 1714,
davolfman 16:c2f912cdd919 42 1816, 1924, 2039, 2160, 2288, 2424, 2568, 2721, 2883, 3055, 3236, 3429};
davolfman 20:7ecec5738790 43
davolfman 20:7ecec5738790 44 //The maximum value of our envelopes is int16_max
davolfman 15:34ba7c2ef718 45 #define attackLimit 0xFFFF
davolfman 20:7ecec5738790 46
davolfman 20:7ecec5738790 47 //this gives us a value of pi to multiply things by
davolfman 10:59c829586a4f 48 #define U_PI 3.14159265358979
davolfman 20:7ecec5738790 49
davolfman 0:b2f7400596ce 50 //non-constants
davolfman 1:f3350a372732 51 //Most of these will be recalculated or reset on every input cycle of the main
davolfman 1:f3350a372732 52 // loop, as appropriate
davolfman 20:7ecec5738790 53
davolfman 20:7ecec5738790 54 int FMmult = 1;//The modulator pitch is FMmult * the base carrier pitch for that note
davolfman 20:7ecec5738790 55 int Volume = 0xffff;//the maximum volume to start, our number format is fixed 16 bits of fractional
davolfman 20:7ecec5738790 56 // inside a 32bit integer usually only in that fractional allowing for
davolfman 20:7ecec5738790 57 // integer multiplications and shifts to recenter instead of floating
davolfman 20:7ecec5738790 58 // -point arithmetic
davolfman 20:7ecec5738790 59 int modVol = 0x2000;//the amount of modulation to apply, most useful relatively low
davolfman 20:7ecec5738790 60 int64_t keyboard = 0;//our key state is stored as bit flags in the lower 49 bits of this
davolfman 20:7ecec5738790 61 int64_t modattack = 0x1ffffffffffff;//similar to keyboard, if the corrsponding bit is
davolfman 20:7ecec5738790 62 // zero the envelope for that bit is in decay or sustain instead of attack
davolfman 18:4992aa537387 63 int64_t carattack = 0x1ffffffffffff;
davolfman 20:7ecec5738790 64 int carrierPhases[numKeys];//store the phases of the notes in between samplings
davolfman 0:b2f7400596ce 65 int modulatorPhases[numKeys];
davolfman 20:7ecec5738790 66 int envelopeAmpsC[numKeys];//store the amplitudes of the envelopes in between samplings
davolfman 1:f3350a372732 67 int envelopeAmpsM[numKeys];
davolfman 1:f3350a372732 68
davolfman 20:7ecec5738790 69 //the envelope parameters for synthesis are read from these registers
davolfman 20:7ecec5738790 70 int modA = 0xffff;//modulator attack rate
davolfman 20:7ecec5738790 71 int modD = 0xffff;//modulator decay rate
davolfman 20:7ecec5738790 72 int modS = 0;//modulator sustain level
davolfman 20:7ecec5738790 73 int modR = 0xffff;//modulator release rate
davolfman 20:7ecec5738790 74 int carA = 0xffff;//carrier attack rate
davolfman 20:7ecec5738790 75 int carD = 0xffff;//carrier decay rate
davolfman 20:7ecec5738790 76 int carS = 0;//carrier sustain level
davolfman 20:7ecec5738790 77 int carR = 0xffff;//carrier release level
davolfman 1:f3350a372732 78
davolfman 1:f3350a372732 79 int fastSin(const int phase){
davolfman 5:ac5c4bd3ef4b 80 int index = (phase & 0x3ffc) >> 2;
davolfman 5:ac5c4bd3ef4b 81 int subindex = phase & 0x3;
davolfman 5:ac5c4bd3ef4b 82 int quadrant = (phase & 0xc000) >> 14;
davolfman 3:83ac767f3a63 83 int sum = 0;
davolfman 5:ac5c4bd3ef4b 84 switch (quadrant) {
davolfman 5:ac5c4bd3ef4b 85 case 0:
davolfman 7:b0cd74923bc6 86 sum += (4 - subindex) * sinTable[index];
davolfman 7:b0cd74923bc6 87 sum += subindex * sinTable[index+1];
davolfman 5:ac5c4bd3ef4b 88 break;
davolfman 5:ac5c4bd3ef4b 89 case 1:
davolfman 7:b0cd74923bc6 90 sum += (4 - subindex) * sinTable[1+4095-index];
davolfman 7:b0cd74923bc6 91 sum += subindex * sinTable[4095-index];
davolfman 5:ac5c4bd3ef4b 92 break;
davolfman 5:ac5c4bd3ef4b 93 case 2:
davolfman 7:b0cd74923bc6 94 sum -= (4 - subindex) * sinTable[index];
davolfman 7:b0cd74923bc6 95 sum -= subindex * sinTable[index+1];
davolfman 5:ac5c4bd3ef4b 96 break;
davolfman 5:ac5c4bd3ef4b 97 case 3:
davolfman 7:b0cd74923bc6 98 sum -= (4 - subindex) * sinTable[1+4095-index];
davolfman 7:b0cd74923bc6 99 sum -= subindex * sinTable[4095-index];
davolfman 5:ac5c4bd3ef4b 100 break;
davolfman 5:ac5c4bd3ef4b 101 }
davolfman 3:83ac767f3a63 102 sum = sum >> 2;
davolfman 3:83ac767f3a63 103
davolfman 3:83ac767f3a63 104 return sum;
davolfman 1:f3350a372732 105 }
davolfman 0:b2f7400596ce 106
davolfman 21:6dc8d091e878 107 ///@brief calculates one audio sample given the keyboard state and envelope paramaters and passes it to the DAC
davolfman 1:f3350a372732 108 void synthesize(){
davolfman 21:6dc8d091e878 109 int wave = 0;// holds the sample being constructed.
davolfman 21:6dc8d091e878 110 int subsignal;// the subsample for one note
davolfman 21:6dc8d091e878 111 int64_t keymask;// holds a mask of the current note for easy access to keyboard and attack registers
davolfman 21:6dc8d091e878 112
davolfman 21:6dc8d091e878 113 //for all keys
davolfman 15:34ba7c2ef718 114 for(int64_t i = 0; i < numKeys; ++i){
davolfman 21:6dc8d091e878 115 keymask = 1ll << i;//set the key mask
davolfman 15:34ba7c2ef718 116
davolfman 21:6dc8d091e878 117 if(!(keymask & keyboard)){//if the key is NOT pressed
davolfman 21:6dc8d091e878 118 carattack |= keymask;//allow attack the next time it is
davolfman 12:3a1e7fde5040 119 modattack |= keymask;
davolfman 15:34ba7c2ef718 120
davolfman 21:6dc8d091e878 121 //if envelope is still positive, decrement by decay rate
davolfman 15:34ba7c2ef718 122 if(envelopeAmpsC[i] > 0){
davolfman 15:34ba7c2ef718 123 envelopeAmpsC[i] -= carR;
davolfman 15:34ba7c2ef718 124 }
davolfman 15:34ba7c2ef718 125 if(envelopeAmpsM[i] > 0){
davolfman 18:4992aa537387 126 envelopeAmpsM[i] -= modR;
davolfman 15:34ba7c2ef718 127 }
davolfman 21:6dc8d091e878 128
davolfman 21:6dc8d091e878 129 }else{//if the key IS pressed
davolfman 21:6dc8d091e878 130
davolfman 21:6dc8d091e878 131 if(envelopeAmpsC[i] <= 0){//if this key was silent before,
davolfman 21:6dc8d091e878 132 carrierPhases[i] = 0;//reset the wave states
davolfman 21:6dc8d091e878 133 modulatorPhases[i] = 0;//this should prevent frequency drift from stopped FM
davolfman 18:4992aa537387 134 envelopeAmpsM[i] = 0;
davolfman 21:6dc8d091e878 135 envelopeAmpsC[i] = 1;//only do it once
davolfman 11:286386c0db40 136 }
davolfman 21:6dc8d091e878 137
davolfman 21:6dc8d091e878 138 //if carrier has not left attack phase
davolfman 15:34ba7c2ef718 139 if(keymask & carattack){
davolfman 21:6dc8d091e878 140 //add attack rate to envelope if not already maximised
davolfman 15:34ba7c2ef718 141 if(envelopeAmpsC[i] < attackLimit ){
davolfman 15:34ba7c2ef718 142 envelopeAmpsC[i] += carA;
davolfman 21:6dc8d091e878 143 }else{//otherwise clip to maximum and leave attack pahse
davolfman 15:34ba7c2ef718 144 envelopeAmpsC[i] = attackLimit;
davolfman 15:34ba7c2ef718 145 carattack &= ~keymask;
davolfman 15:34ba7c2ef718 146 }
davolfman 21:6dc8d091e878 147 }else{//if in decay/sustain
davolfman 21:6dc8d091e878 148 if(envelopeAmpsC[i] > carS){//subtract the decay rate if above sustain level
davolfman 15:34ba7c2ef718 149 envelopeAmpsC[i] -= carD;
davolfman 15:34ba7c2ef718 150 }
davolfman 15:34ba7c2ef718 151 }
davolfman 15:34ba7c2ef718 152
davolfman 21:6dc8d091e878 153 //do all that again for the modulator envelope
davolfman 15:34ba7c2ef718 154 if(keymask & modattack){
davolfman 15:34ba7c2ef718 155 if(envelopeAmpsM[i] < attackLimit){
davolfman 15:34ba7c2ef718 156 envelopeAmpsM[i] += modA;
davolfman 15:34ba7c2ef718 157 }else{
davolfman 15:34ba7c2ef718 158 envelopeAmpsM[i] = attackLimit;
davolfman 18:4992aa537387 159 modattack &= ~keymask;
davolfman 15:34ba7c2ef718 160 }
davolfman 15:34ba7c2ef718 161 }else{
davolfman 15:34ba7c2ef718 162 if(envelopeAmpsM[i] > modS){
davolfman 15:34ba7c2ef718 163 envelopeAmpsM[i] -= modD;
davolfman 15:34ba7c2ef718 164 }
davolfman 15:34ba7c2ef718 165 }
davolfman 15:34ba7c2ef718 166
davolfman 22:0307adac8c35 167 }
davolfman 22:0307adac8c35 168
davolfman 22:0307adac8c35 169 //If this subsignal is not silent
davolfman 22:0307adac8c35 170 if(envelopeAmpsC[i] > 0){
davolfman 22:0307adac8c35 171 //calculate the new phase of the modulator
davolfman 22:0307adac8c35 172 modulatorPhases[i] += (carrierIncrements[i] * FMmult)>> 16;
davolfman 22:0307adac8c35 173
davolfman 22:0307adac8c35 174 //get the sine for that phase and scale it by the envelope
davolfman 22:0307adac8c35 175 int modulation = (fastSin(modulatorPhases[i]) * envelopeAmpsM[i])>>16;
davolfman 22:0307adac8c35 176
davolfman 22:0307adac8c35 177 //scale it again by the modulation amount
davolfman 22:0307adac8c35 178 modulation = (modulation * modVol) >> 16;
davolfman 22:0307adac8c35 179
davolfman 22:0307adac8c35 180 //calculate the new phase of the carrier, modualting frequency by the modulation
davolfman 22:0307adac8c35 181 carrierPhases[i] += carrierIncrements[i] + modulation;
davolfman 22:0307adac8c35 182
davolfman 22:0307adac8c35 183 //get the sine for that carrier phase and scale by the envelope
davolfman 22:0307adac8c35 184 //additionally divide by 8 to allow 8 notes to play without saturating the DAC
davolfman 22:0307adac8c35 185 subsignal = (fastSin(carrierPhases[i]) * envelopeAmpsC[i])>>19;
davolfman 22:0307adac8c35 186
davolfman 22:0307adac8c35 187 //add the sample for this note into the overall sample
davolfman 22:0307adac8c35 188 wave += subsignal;
davolfman 12:3a1e7fde5040 189 }
davolfman 15:34ba7c2ef718 190
davolfman 15:34ba7c2ef718 191 }
davolfman 22:0307adac8c35 192
davolfman 22:0307adac8c35 193 //Scale the complete sample by the volume
davolfman 15:34ba7c2ef718 194 wave = wave * Volume >> 16;
davolfman 12:3a1e7fde5040 195
davolfman 22:0307adac8c35 196 //clip the sample to within the limits of the DAC if neccessary
davolfman 15:34ba7c2ef718 197 wave = (wave > 32767) ? 32767 : wave;
davolfman 15:34ba7c2ef718 198 wave = (wave < -32768) ? - 32768 : wave;
davolfman 22:0307adac8c35 199
davolfman 22:0307adac8c35 200 //Center the waveform within the range of the DAC
davolfman 15:34ba7c2ef718 201 wave += 32768;
davolfman 22:0307adac8c35 202
davolfman 22:0307adac8c35 203 //output the sample
davolfman 15:34ba7c2ef718 204 outMono.write_u16(wave);
davolfman 1:f3350a372732 205 }
davolfman 0:b2f7400596ce 206
davolfman 0:b2f7400596ce 207
davolfman 0:b2f7400596ce 208 int main() {
davolfman 2:5454dee210ed 209 int ratNumer;
davolfman 18:4992aa537387 210 int ratDenom;
davolfman 18:4992aa537387 211 int64_t keytemp;
davolfman 18:4992aa537387 212 int tempCarA, tempCarD, tempCarR, tempModA, tempModD, tempModR;
davolfman 9:86c0035f5321 213
davolfman 9:86c0035f5321 214 for(int i = 0; i < numKeys; ++i){
davolfman 9:86c0035f5321 215 carrierPhases[i] = 0;
davolfman 9:86c0035f5321 216 modulatorPhases[i] = 0;
davolfman 9:86c0035f5321 217 envelopeAmpsC[i] = 0;
davolfman 9:86c0035f5321 218 envelopeAmpsM[i] = 0;
davolfman 9:86c0035f5321 219 }
davolfman 9:86c0035f5321 220
lenzi002 8:13b0594510de 221 keyBank.mode(PullNone);
davolfman 20:7ecec5738790 222 synthesisClock.attach(synthesize, 0.00005);
lenzi002 8:13b0594510de 223
davolfman 2:5454dee210ed 224 while(true){
davolfman 2:5454dee210ed 225 ratNumer = 0xf & ~ numerator;
davolfman 7:b0cd74923bc6 226 ratDenom = 0xf & ~ denominator;
davolfman 7:b0cd74923bc6 227 FMmult = (ratNumer << 16) / ratDenom;
davolfman 2:5454dee210ed 228
davolfman 13:29705e6bf718 229 Volume = (int)inVol.read_u16();
davolfman 12:3a1e7fde5040 230
davolfman 2:5454dee210ed 231 modVol = (int)inModAmt.read_u16();
davolfman 2:5454dee210ed 232
davolfman 18:4992aa537387 233 tempCarA = inCarA.read_u16();
davolfman 18:4992aa537387 234 if(! tempCarA)
davolfman 9:86c0035f5321 235 carA = 0xffff;
davolfman 9:86c0035f5321 236 else
davolfman 18:4992aa537387 237 carA = 0xffff / tempCarA;
davolfman 9:86c0035f5321 238
davolfman 18:4992aa537387 239 tempCarD = inCarD.read_u16();
davolfman 18:4992aa537387 240 if(! tempCarD)
davolfman 9:86c0035f5321 241 carD = 0xffff;
davolfman 9:86c0035f5321 242 else
davolfman 18:4992aa537387 243 carD = 0xffff / tempCarD;
davolfman 9:86c0035f5321 244
davolfman 2:5454dee210ed 245 carS = (int)inCarS.read_u16();
davolfman 9:86c0035f5321 246
davolfman 18:4992aa537387 247 tempCarR = inCarR.read_u16();
davolfman 18:4992aa537387 248 if(! tempCarR)
davolfman 9:86c0035f5321 249 carR = 0xffff;
davolfman 9:86c0035f5321 250 else
davolfman 18:4992aa537387 251 carR = 0xffff / tempCarR;
davolfman 2:5454dee210ed 252
davolfman 18:4992aa537387 253 tempModA = inModA.read_u16();
davolfman 18:4992aa537387 254 if(! tempModA)
davolfman 9:86c0035f5321 255 modA = 0xffff;
davolfman 9:86c0035f5321 256 else
davolfman 18:4992aa537387 257 modA = 0xffff / tempModA;
davolfman 9:86c0035f5321 258
davolfman 18:4992aa537387 259 tempModD = inModD.read_u16();
davolfman 18:4992aa537387 260 if(! tempModD)
davolfman 9:86c0035f5321 261 modD = 0xffff;
davolfman 9:86c0035f5321 262 else
davolfman 18:4992aa537387 263 modD = 0xffff / tempModD;
davolfman 9:86c0035f5321 264
davolfman 2:5454dee210ed 265 modS = (int)inModS.read_u16();
davolfman 9:86c0035f5321 266
davolfman 18:4992aa537387 267 tempModR = inModR.read_u16();
davolfman 18:4992aa537387 268 if(! tempModR)
davolfman 9:86c0035f5321 269 modR = 0xffff;
davolfman 9:86c0035f5321 270 else
davolfman 18:4992aa537387 271 modR = 0xffff / tempModR;
davolfman 13:29705e6bf718 272
davolfman 9:86c0035f5321 273 keytemp = 0; //zero the keys before we start ORing on top of everything
davolfman 9:86c0035f5321 274 for(int i = 0; i < 9; ++i) {
davolfman 13:29705e6bf718 275 bankSelect = (~(1 << i)) & bankSelect.mask();
davolfman 19:72886f894dc5 276 wait_us(200);
lenzi002 8:13b0594510de 277 int shiftOffset = 6LL * i;
davolfman 13:29705e6bf718 278
davolfman 9:86c0035f5321 279 keytemp |= ((~(unsigned long long)keyBank) & (unsigned long long)keyBank.mask()) << (unsigned long long)shiftOffset;
davolfman 7:b0cd74923bc6 280 }
davolfman 9:86c0035f5321 281 keytemp >>= 5;
davolfman 9:86c0035f5321 282
davolfman 13:29705e6bf718 283 keyboard = keytemp;
davolfman 13:29705e6bf718 284
davolfman 20:7ecec5738790 285 //wait_ms(10);
davolfman 2:5454dee210ed 286 }
davolfman 0:b2f7400596ce 287 }