Dan Allegre / 401_reverb

Dependencies:   mbed

Revision:
8:ef4dca8695d1
Parent:
7:c84086dce9ac
Child:
9:f42863126573
diff -r c84086dce9ac -r ef4dca8695d1 main.cpp
--- a/main.cpp	Sat Jul 30 21:34:28 2016 +0000
+++ b/main.cpp	Sat Jul 30 23:57:45 2016 +0000
@@ -15,7 +15,7 @@
 //Added a nice lowpass filter class.  Might want to put the delay in a class like this for reverb work.  Need to make an oscillator next.
 
 #define DELAYLEN        2900        //24,000 is about as far as you can go in the ram.
-#define SAMPLINGFREQ    44          //roughly the smapling frequency in KHz.  options are 22,29,44,88 for 22khz,29khz,etc..
+#define SAMPLINGFREQ    22          //roughly the smapling frequency in KHz.  options are 22,29,44,88 for 22khz,29khz,etc..
 
 #include "mbed.h"
 #include "dsp.h"
@@ -35,6 +35,8 @@
 uint16_t left_in_u = 0, meas = 0, measprev = 0;
 float left_in_f = 0, feedback = 0, feedback2 = 0, feedback3 = 0, offset = 10.0,
 meas2f = 0.5, damping = 0, damping2 = 0, damping3 = 0, modL = 0, modL2 = 0, modL3 = 0, modL4, modR = 0, modR2 = 0, modR3 = 0, modR4, 
+modLos = 0, modL2os = 0, modL3os = 0, modL4os = 0, modRos = 0, modR2os = 0, modR3os = 0, modR4os = 0,
+upperLim, lowerLim,
 left_out_f = 0, right_out_f = 0;
 float left_out_2 = 0, right_out_2 = 0, left_out_3 = 0, right_out_3 = 0, left_out_4 = 0, right_out_4 = 0, loopfb = 0, loopfb2 = 0;
 float audioFeedbackRight,audioFeedbackLeft;
@@ -72,60 +74,61 @@
     
     /////////////////////////////////////////dsp part//////////////////////////////////////////////////////////////////////
         
+    ///*    
     //process control signals
     feedback2 = feedbackFilter.process(feedback);
-    feedback3 = feedbackFilter.process(feedback2);
-    //update delay times with every audio sample if you're going to filter them.
-    damping2 = lengthFilter.process(damping);
-    damping3 = lengthFilter.process(damping2);
-    damping3 *= (5000);                       //make about 10k the max cutoff for damping
-    
-    
-    dampingFilter.setFc(damping3);
+    //maybe move this to the dac sampling interrupt.  seems like there was a reason it's here though.
+    //damping2 = lengthFilter.process(damping);
+    //damping3 = lengthFilter.process(damping2);
+    //damping3 *= (5000);                       //make about 10k the max cutoff for damping
+
+    //dampingFilter.setFc(damping3/(SAMPLINGFREQ*1000));
 
     //process audio.   probably calculate these coefficients elsewhere so you don't do the multiply all the time.
     //left delay mod   
-    modL = DELAYLEN*.74 + OscillatorL.process(.53,DELAYLEN/66,1);
-    modL2 = DELAYLEN*.43 + OscillatorL2.process(.03,DELAYLEN/65,1);
-    modL3 = DELAYLEN*.33 + OscillatorL3.process(.02,DELAYLEN/78,1);
-    modL3 = DELAYLEN*.37 + OscillatorL3.process(.023,DELAYLEN/78,0);
+    modL =  modLos +  OscillatorL.process(1);
+    modL2 = modL2os + OscillatorL2.process(1);
+    modL3 = modL3os + OscillatorL3.process(1);
+    modL4 = modL4os + OscillatorL3.process(0);
     //right delay mod
-    modR = DELAYLEN*.75 + OscillatorR.process(.51,DELAYLEN/69,0);
-    modR2 = DELAYLEN*.49 + OscillatorR2.process(.03,DELAYLEN/67,0);
-    modR3 = DELAYLEN*.33 + OscillatorR3.process(.02,DELAYLEN/78,1);
-    modR4 = DELAYLEN*.37 + OscillatorR4.process(.023,DELAYLEN/78,1);
-    if(modL > DELAYLEN*.9)
-        modL = DELAYLEN*.9;
-    if(modL < DELAYLEN*.05)
-        modL = DELAYLEN*.05;
-    if(modL2 > DELAYLEN*.9)
-        modL2 = DELAYLEN*.9;
-    if(modL2 < DELAYLEN*.05)
-        modL2 = DELAYLEN*.05;
-    if(modL3 > DELAYLEN*.9)
-        modL3 = DELAYLEN*.9;
-    if(modL3 < DELAYLEN*.05)
-        modL3 = DELAYLEN*.05;
-    if(modL4 > DELAYLEN*.9)
-        modL4 = DELAYLEN*.9;
-    if(modL4 < DELAYLEN*.05)
-        modL4 = DELAYLEN*.05;
-    if(modR > DELAYLEN*.9)
-        modR = DELAYLEN*.9;
-    if(modR < DELAYLEN*.05)
-        modR = DELAYLEN*.05;
-    if(modR2 > DELAYLEN*.9)
-        modR2 = DELAYLEN*.9;
-    if(modR2 < DELAYLEN*.05)
-        modR2 = DELAYLEN*.05;
-    if(modR3 > DELAYLEN*.9)
-        modR3 = DELAYLEN*.9;
-    if(modR3 < DELAYLEN*.05)
-        modR3 = DELAYLEN*.05;
-    if(modR4 > DELAYLEN*.9)
-        modR4 = DELAYLEN*.9;
-    if(modR4 < DELAYLEN*.05)
-        modR4 = DELAYLEN*.05;
+    modR =  modRos +  OscillatorR.process(0);
+    modR2 = modR2os + OscillatorR2.process(0);
+    modR3 = modR3os + OscillatorR3.process(1);
+    modR4 = modR4os + OscillatorR4.process(1);
+    
+    if(modL > upperLim)
+        modL = upperLim;
+    if(modL < lowerLim)
+        modL = lowerLim;
+    if(modL2 > upperLim)
+        modL2 = upperLim;
+    if(modL2 < lowerLim)
+        modL2 = lowerLim;
+    if(modL3 > upperLim)
+        modL3 = upperLim;
+    if(modL3 < lowerLim)
+        modL3 = lowerLim;
+    if(modL4 > upperLim)
+        modL4 = upperLim;
+    if(modL4 < lowerLim)
+        modL4 = lowerLim;
+    if(modR > upperLim)
+        modR = upperLim;
+    if(modR < lowerLim)
+        modR = lowerLim;
+    if(modR2 > upperLim)
+        modR2 = upperLim;
+    if(modR2 < lowerLim)
+        modR2 = lowerLim;
+    if(modR3 > upperLim)
+        modR3 = upperLim;
+    if(modR3 < lowerLim)
+        modR3 = lowerLim;
+    if(modR4 > upperLim)
+        modR4 = upperLim;
+    if(modR4 < lowerLim)
+        modR4 = lowerLim;
+
     left_out_2 = delayLeft2.process(satAdd(left_in_f,loopfb),.3,modL);
     left_out_3 = delayLeft3.process(left_out_2,.4,modL2);
     left_out_4 = delayLeft4.process(left_out_3,.4,modL3);
@@ -134,17 +137,17 @@
     right_out_3 = delayRight3.process(right_out_2,.4,modR2);
     right_out_4 = delayRight4.process(right_out_3,.4,modR3);
     right_out_f = delayRight.process(right_out_4,.4,modR4);
-    //loopfb = dampingFilter.process(left_out_f);
-    //loopfb2 = dampingFilter.process(right_out_f);
 
-    //loopfb *= feedback3;
-    //loopfb2 *= feedback3;
-    loopfb2 = left_out_f*feedback3;                     //swizzle the feedbacks left to right
-    loopfb = right_out_f*feedback3;
+    loopfb2 = left_out_f*feedback2;                     //swizzle the feedbacks left to right
+    loopfb = right_out_f*feedback2;      
+    
+    //apply damping
     //loopfb = dampingFilter.process(loopfb);
     //loopfb2 = dampingFilter.process(loopfb2);
-    //left_out_u = satAdd(left_in_f,left_out_f*damping3/200);
-    //right_out_u = satAdd(left_in_f,right_out_f*damping3/200);
+    //*/
+    //left_out_f = left_in_f;
+    //right_out_f = left_in_f;
+    
     left_out_u = left_out_f;
     right_out_u = right_out_f;
 
@@ -185,6 +188,27 @@
     feedbackFilter.setFc(feedbackCutoff);
     double lengthCutoff = 3.0/(SAMPLINGFREQ*1000);
     lengthFilter.setFc(lengthCutoff);
+    
+    //initize oscillators and mod offsets
+    modLos = DELAYLEN*.74;
+    modL2os = DELAYLEN*.43;
+    modL3os = DELAYLEN*.33; 
+    modL4os = DELAYLEN*.37;
+    OscillatorL.setF(.53,DELAYLEN/66);
+    OscillatorL2.setF(.03,DELAYLEN/65);
+    OscillatorL3.setF(.02,DELAYLEN/78);
+    OscillatorL4.setF(.023,DELAYLEN/78);
+    //right delay mod
+    modRos = DELAYLEN*.75;
+    modR2os = DELAYLEN*.49;
+    modR3os = DELAYLEN*.33;
+    modR4os = DELAYLEN*.37;
+    OscillatorR.setF(.51,DELAYLEN/69);
+    OscillatorR2.setF(.03,DELAYLEN/67);
+    OscillatorR3.setF(.02,DELAYLEN/78);
+    OscillatorR4.setF(.023,DELAYLEN/78);
+    upperLim = .9*DELAYLEN;
+    lowerLim = .05*DELAYLEN;
 
     //set up I2S
     spi.frequency(8e6*2);                         //8MHz spi.  This is fast enough for 88KHz 16 bit sampling.