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
Revision 23:7a9ff5230149, committed 2017-12-15
- Comitter:
- davolfman
- Date:
- Fri Dec 15 04:50:56 2017 +0000
- Parent:
- 22:0307adac8c35
- Commit message:
- Integration of main comments and added FastSin comments.
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Dec 15 03:51:47 2017 +0000
+++ b/main.cpp Fri Dec 15 04:50:56 2017 +0000
@@ -76,12 +76,20 @@
int carS = 0;//carrier sustain level
int carR = 0xffff;//carrier release level
+///@brief Converts a phase of period 2^16 into a sine value
+///@phase The phase to calculate the sine of in phase/65536 * 2 pi radians
+///@return the sine as a signed 16 bit fractonal part inside a 32 bit int
int fastSin(const int phase){
+ //the middle 12 bits are used to index into a lookuptable of pre computed sines
int index = (phase & 0x3ffc) >> 2;
+
+ //we used linear interpolation given out bottom 2 bits to turn a 2^12 Look Up Table into a 2^14 LUT
int subindex = phase & 0x3;
+
+ //We use mirroring of a quarter wave of sine so we only have to store a quarter of the samples again
int quadrant = (phase & 0xc000) >> 14;
int sum = 0;
- switch (quadrant) {
+ switch (quadrant) {//perform the mirroring and add the memebers of the weighted average for interpolation
case 0:
sum += (4 - subindex) * sinTable[index];
sum += subindex * sinTable[index+1];
@@ -99,7 +107,8 @@
sum -= subindex * sinTable[4095-index];
break;
}
- sum = sum >> 2;
+ sum = sum >> 2;//divide the weighted sum of the neighborign samples by 4
+ //to get a weighted average
return sum;
}
@@ -211,77 +220,94 @@
int64_t keytemp;
int tempCarA, tempCarD, tempCarR, tempModA, tempModD, tempModR;
- for(int i = 0; i < numKeys; ++i){
+ for(int i = 0; i < numKeys; ++i){ //zero out values
carrierPhases[i] = 0;
modulatorPhases[i] = 0;
envelopeAmpsC[i] = 0;
envelopeAmpsM[i] = 0;
}
- keyBank.mode(PullNone);
- synthesisClock.attach(synthesize, 0.00005);
+ keyBank.mode(PullNone); // we're using external pullup resistors,
+ //and things weren't working this didn't fix it but better safe
+
+ synthesisClock.attach(synthesize, 0.00005); //this runs every 50 us or 20khz
while(true){
- ratNumer = 0xf & ~ numerator;
- ratDenom = 0xf & ~ denominator;
- FMmult = (ratNumer << 16) / ratDenom;
+ ratNumer = 0xf & ~ numerator; //read ratio numerator
+ ratDenom = 0xf & ~ denominator; //read ratio denominator
+ FMmult = (ratNumer << 16) / ratDenom; //FM multiplier = numerator / denominator
+
+ Volume = (int)inVol.read_u16(); //read volume
- Volume = (int)inVol.read_u16();
+ modVol = (int)inModAmt.read_u16(); //read modulation amount
- modVol = (int)inModAmt.read_u16();
-
+ //ensure we don't divide by zero on any of these
+
+ //read carrier attack
tempCarA = inCarA.read_u16();
if(! tempCarA)
carA = 0xffff;
else
carA = 0xffff / tempCarA;
+ //read carrier decay
tempCarD = inCarD.read_u16();
if(! tempCarD)
carD = 0xffff;
else
carD = 0xffff / tempCarD;
+ //read carrier sustain
carS = (int)inCarS.read_u16();
+ //read carrier release
tempCarR = inCarR.read_u16();
if(! tempCarR)
carR = 0xffff;
else
carR = 0xffff / tempCarR;
+ //read modulation attack
tempModA = inModA.read_u16();
if(! tempModA)
modA = 0xffff;
else
modA = 0xffff / tempModA;
+ //read modulation decay
tempModD = inModD.read_u16();
if(! tempModD)
modD = 0xffff;
else
modD = 0xffff / tempModD;
+ //read modulation sustain
modS = (int)inModS.read_u16();
+ //read modulation release
tempModR = inModR.read_u16();
if(! tempModR)
modR = 0xffff;
else
modR = 0xffff / tempModR;
- keytemp = 0; //zero the keys before we start ORing on top of everything
- for(int i = 0; i < 9; ++i) {
- bankSelect = (~(1 << i)) & bankSelect.mask();
- wait_us(200);
- int shiftOffset = 6LL * i;
+ keytemp = 0; //zero the keys before we start ORing on top of everything,
+ //keytemp is a buffer that waits until every key is read, then it pushes
+ //into the 'keyboard' variable
+
+ for(int i = 0; i < 9; ++i) {//for all 9 half-octaves of the keyboard
+ bankSelect = (~(1 << i)) & bankSelect.mask();//supply power (as ground) to that bank
+ wait_us(200); //delay to get to get full reading
+ int shiftOffset = 6LL * i; //read the keys in the bank
+ //assign the bank to its corresponding position in the overall keyboard
keytemp |= ((~(unsigned long long)keyBank) & (unsigned long long)keyBank.mask()) << (unsigned long long)shiftOffset;
}
- keytemp >>= 5;
+ keytemp >>= 5;//shift 5 over because bank 0 only has 1 key and it is the 6th key (...111111 111111 100000)
- keyboard = keytemp;
+ keyboard = keytemp;//push our read values once we are complete.
+ //This only takes 1 cycle, especially since we are using 'keyboard' in the ISR
- //wait_ms(10);
+ //wait_ms(10);//we've stopped bothering to wait here. it didn't make a difference
}
}
