Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of STM32FMSynth by
Diff: main.cpp
- Revision:
- 20:7ecec5738790
- Parent:
- 19:72886f894dc5
- Child:
- 21:6dc8d091e878
--- a/main.cpp Wed Dec 13 20:38:24 2017 +0000
+++ b/main.cpp Fri Dec 15 03:29:07 2017 +0000
@@ -29,41 +29,52 @@
//Serial pc(USBTX, USBRX);
-Ticker synthesisClock;
+Ticker synthesisClock;// this object sets up an ISR to execute every given fraction of a second.
-#define numKeys 49
+#define numKeys 49// our keyboard includes from two octaves above middle c to two octaves below it
//constants
+
+//These are the pitches of the notes of our keyboard in hz * int16_max / sampling rate(20khz)
const int carrierIncrements[] = {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,
1816, 1924, 2039, 2160, 2288, 2424, 2568, 2721, 2883, 3055, 3236, 3429};
+
+//The maximum value of our envelopes is int16_max
#define attackLimit 0xFFFF
+
+//this gives us a value of pi to multiply things by
#define U_PI 3.14159265358979
+
//non-constants
//Most of these will be recalculated or reset on every input cycle of the main
// loop, as appropriate
-int FMmult = 1;
-int Volume = 0xffff;
-int modVol = 0x2000;
-int64_t keyboard = 0;
-int64_t modattack = 0x1ffffffffffff;
+
+int FMmult = 1;//The modulator pitch is FMmult * the base carrier pitch for that note
+int Volume = 0xffff;//the maximum volume to start, our number format is fixed 16 bits of fractional
+// inside a 32bit integer usually only in that fractional allowing for
+// integer multiplications and shifts to recenter instead of floating
+// -point arithmetic
+int modVol = 0x2000;//the amount of modulation to apply, most useful relatively low
+int64_t keyboard = 0;//our key state is stored as bit flags in the lower 49 bits of this
+int64_t modattack = 0x1ffffffffffff;//similar to keyboard, if the corrsponding bit is
+// zero the envelope for that bit is in decay or sustain instead of attack
int64_t carattack = 0x1ffffffffffff;
-int carrierPhases[numKeys];
+int carrierPhases[numKeys];//store the phases of the notes in between samplings
int modulatorPhases[numKeys];
-int envelopeAmpsC[numKeys];
+int envelopeAmpsC[numKeys];//store the amplitudes of the envelopes in between samplings
int envelopeAmpsM[numKeys];
-//int testTone = 0;
-
-int modA = 0xffff;
-int modD = 0xffff;
-int modS = 0;
-int modR = 0xffff;
-int carA = 0xffff;
-int carD = 0xffff;
-int carS = 0;
-int carR = 0xffff;
+//the envelope parameters for synthesis are read from these registers
+int modA = 0xffff;//modulator attack rate
+int modD = 0xffff;//modulator decay rate
+int modS = 0;//modulator sustain level
+int modR = 0xffff;//modulator release rate
+int carA = 0xffff;//carrier attack rate
+int carD = 0xffff;//carrier decay rate
+int carS = 0;//carrier sustain level
+int carR = 0xffff;//carrier release level
int fastSin(const int phase){
int index = (phase & 0x3ffc) >> 2;
@@ -184,9 +195,8 @@
}
keyBank.mode(PullNone);
- synthesisClock.attach(synthesize, 0.00005);//debug
+ synthesisClock.attach(synthesize, 0.00005);
- //pc.printf("done.\r\n");
while(true){
ratNumer = 0xf & ~ numerator;
ratDenom = 0xf & ~ denominator;
@@ -248,6 +258,6 @@
keyboard = keytemp;
- wait_ms(10);
+ //wait_ms(10);
}
}
