shithub: leaf

Download patch

ref: 5386dfb1fece3a9bf3aa36f474a6b863239e701d
parent: 20bce9c7a2c74e53b8859a6e896bd7b27d801b7e
author: Jeffrey Snyder <jeffsnyder@jeffreys-mbp.mynetworksettings.com>
date: Tue Jul 5 19:38:23 EDT 2022

fixed tCompressor problems - it now calculates slope properly from a ratio in the whole number format

--- a/leaf/Src/leaf-distortion.c
+++ b/leaf/Src/leaf-distortion.c
@@ -389,7 +389,7 @@
     // Antialiasing error threshold
     w->AAthresh = 10e-10; //10
 
-	w->LambertThresh = 10e-10; //12  //was 8
+	w->LambertThresh = 10e-12; //12  //was 8
 
 
     w->w = 0.0f;
@@ -427,64 +427,60 @@
 	mwf->w = ln;
     
     // Haley's method (Sec. 4.2 of the paper)
-    for(int i=0; i<3000; i+=1) { //1000
+    for(int i=0; i<6000; i+=1) { //1000
         
     	mwf->expw = exp(mwf->w);
-    	/*
+    	
         if (isinf(mwf->expw) || isnan(mwf->expw))
         {
         	mwf->expw = 10e-5;
-        	LEAF_error();
+
         }
-        */
+        
     	mwf->p = mwf->w*mwf->expw - x;
-    	/*
+    	
         if (isinf(mwf->p) || isnan(mwf->p))
         {
         	mwf->p = 10e-5;
-        	LEAF_error();
+
         }
-        */
+        
     	mwf->r = (mwf->w+1.0)*mwf->expw;
-    	/*
+    	
         if (isinf(mwf->r) || isnan(mwf->r))
         {
         	mwf->r = 10e-5;
-        	LEAF_error();
+
         }
-        */
+        
     	mwf->tempsDenom = (2.0*(mwf->w+1.0));
-    	/*
+    	
         if ((mwf->tempsDenom == 0.0) || isinf(mwf->tempsDenom) || isnan(mwf->tempsDenom))
         {
         	mwf->tempsDenom = 10e-5;
-        	LEAF_error();
         }
-        */
+        
         mwf->s = (mwf->w+2.0)/mwf->tempsDenom;
-        /*
+        
         if (isnan(mwf->s) || isinf(mwf->s))
         {
         	mwf->s = 10e-5;
-        	LEAF_error();
         }
-        */
+        
         mwf->tempErrDenom = (mwf->r-(mwf->p*mwf->s));
-         /*
+         
         if ((mwf->tempErrDenom == 0.0) || isinf(mwf->tempErrDenom) || isnan(mwf->tempErrDenom))
         {
         	mwf->tempErrDenom = 10e-5;
-        	LEAF_error();
         }
-        */
+        
         mwf->myerr = (mwf->p/mwf->tempErrDenom);
-         /*
+         
         if (isnan(mwf->myerr) || isinf(mwf->myerr))
         {
         	mwf->myerr = 10e-5;
-        	LEAF_error();
         }
-        */
+        
 
         if ((fabs(mwf->myerr))<mwf->LambertThresh) {
 
@@ -513,14 +509,23 @@
     
     // Compute Antiderivative
     w->l = (in > 0.0) - (in < 0.0);
-    w->u = w->d*exp(w->l*w->b*in);
-    /*
+    double tempbin = w->b*in;
+    if (tempbin > 600.0)
+    {
+        tempbin = 600.0;
+    }
+    else if (tempbin < -600.0)
+    {
+        tempbin = -600.0;
+    }
+    w->u = w->d*exp(w->l*tempbin);
+    
     if ((w->u == 0.0) || isinf(w->u) || isnan(w->u))
     {
     	w->u = 10e-5;
-    	LEAF_error();
+
     }
-    */
+    
 
     w->Ln = tLockhartWavefolderLambert(wf,w->u,w->Ln1);
     /*
@@ -545,15 +550,24 @@
         
         // Compute Averaged Wavefolder Output
     	double xn = 0.5*(in+w->xn1);
-    	w->u = w->d*exp(w->l*w->b*xn);
-        /*
+        double tempbxn = xn * w->b;
+        if (tempbxn > 600.0)
+        {
+            tempbxn = 600.0;
+        }
+        else if (tempbxn < -600.0)
+        {
+            tempbxn = -600.0;
+        }
+    	w->u = w->d*exp(w->l*tempbxn);
+        
     	if ((w->u == 0.0) || isinf(w->u) || isnan(w->u))
         {
         	w->u = 10e-5;
-        	LEAF_error();
 
+
         }
-        */
+        
     	w->Ln = tLockhartWavefolderLambert(wf,w->u,w->Ln1);
     	/*
         if ((w->Ln == 0.0) || isinf(w->Ln) || isnan(w->Ln))
@@ -570,14 +584,13 @@
 
         // Apply AA Form
     	w->tempOutDenom = (in-w->xn1);
-    	/*
+    	
     	if ((w->tempOutDenom == 0.0) || isinf(w->tempOutDenom))
     	{
     		w->tempOutDenom = 10e-5;
-    		LEAF_error();
     	}
-    	*/
-        out = ((w->Fn-w->Fn1)/w->tempOutDenom);
+    	
+        out = (float)((w->Fn-w->Fn1)/w->tempOutDenom);
         /*
         if (isinf(out) || isnan(out))
 		{
--- a/leaf/Src/leaf-dynamics.c
+++ b/leaf/Src/leaf-dynamics.c
@@ -52,18 +52,22 @@
     _tCompressor* c = *comp = (_tCompressor*) mpool_alloc(sizeof(_tCompressor), m);
     c->mempool = m;
     LEAF* leaf = c->mempool->leaf;
+    c->sampleRate = leaf->sampleRate;
     
-    c->tauAttack = 100;
-    c->tauRelease = 100;
+    c->tauAttack = expf(-1.0f/(0.001f * 50.0f * c->sampleRate));
+    c->tauRelease = expf(-1.0f/(0.001f * 100.0f * c->sampleRate));
     
     c->isActive = 0;
-    
+    c->x_G[0] = 0.0f, c->x_G[1] = 0.0f,
+    c->y_G[0] = 0.0f, c->y_G[1] = 0.0f,
+    c->x_T[0] = 0.0f, c->x_T[1] = 0.0f,
+    c->y_T[0] = 0.0f, c->y_T[1] = 0.0f;
     c->T = 0.0f; // Threshold
     c->R = 0.5f; // compression Ratio
     c->M = 3.0f; // decibel Make-up gain
     c->W = 1.0f; // decibel Width of knee transition
     
-    c->sampleRate = leaf->sampleRate;
+
 }
 
 void tCompressor_free (tCompressor* const comp)
@@ -78,25 +82,27 @@
     _tCompressor* c = *comp;
     
     float slope, overshoot;
-    float alphaAtt, alphaRel;
     
-    float in_db = 20.0f * log10f( fmaxf( fabsf( in), 0.000001f)), out_db = 0.0f;
+    float in_db = LEAF_clip(-90.0f, atodb(fabsf(in)), 0.0f);
+    float out_db = 0.0f;
     
     c->y_T[1] = c->y_T[0];
     
-    slope = c->R - 1.0f; // feed-forward topology; was 1/C->R - 1
+    slope = 1.0f - (1.0f/c->R); // feed-forward topology; 
     
     overshoot = in_db - c->T;
     
-    
+    /*
     if (overshoot <= -(c->W * 0.5f))
     {
         out_db = in_db;
         c->isActive = 0;
     }
+
     else if ((overshoot > -(c->W * 0.5f)) && (overshoot < (c->W * 0.5f)))
     {
-        out_db = in_db + slope * (powf((overshoot + c->W*0.5f),2) / (2.0f * c->W)); // .^ 2 ???
+        float squareit = (overshoot + c->W*0.5f);
+        out_db = in_db + slope * ((squareit * squareit) / (2.0f * c->W)); // .^ 2 ???
         c->isActive = 1;
     }
     else if (overshoot >= (c->W * 0.5f))
@@ -104,21 +110,37 @@
         out_db = in_db + slope * overshoot;
         c->isActive = 1;
     }
+    */
+    if (overshoot <= 0.0f)
+    {
+        out_db = in_db;
+        c->isActive = 0;
+    }
+    else
+    {
+        out_db = in_db + slope * overshoot;
+        c->isActive = 1;
+    }
     
-    
-    
     c->x_T[0] = out_db - in_db;
     
-    alphaAtt = expf(-1.0f/(0.001f * c->tauAttack * c->sampleRate));
-    alphaRel = expf(-1.0f/(0.001f * c->tauRelease * c->sampleRate));
+
     
     if (c->x_T[0] > c->y_T[1])
-        c->y_T[0] = alphaAtt * c->y_T[1] + (1-alphaAtt) * c->x_T[0];
+        c->y_T[0] = c->tauAttack * c->y_T[1] + (1.0f-c->tauAttack) * c->x_T[0];
     else
-        c->y_T[0] = alphaRel * c->y_T[1] + (1-alphaRel) * c->x_T[0];
+        c->y_T[0] = c->tauRelease * c->y_T[1] + (1.0f-c->tauRelease) * c->x_T[0];
+    if (isnan(c->y_T[0]))
+    {
+        out_db = 1.0f;
+    }
     
-    float attenuation = powf(10.0f, ((c->M - c->y_T[0])/20.0f));
-    
+    float attenuation = fasterdbtoa(c->M - c->y_T[0]);
+    if (isnan(attenuation))
+    {
+        out_db = 1.0f;
+    }
+    //float attenuation = 1.0f;
     return attenuation * in;
 }
 
@@ -129,8 +151,8 @@
 //
 //c->T = 0.0f; // Threshold
 //c->R = 0.5f; // compression Ratio
-//c->M = 3.0f; // decibel Width of knee transition
-//c->W = 1.0f; // decibel Make-up gain
+//c->W = 3.0f; // decibel Width of knee transition
+//c->M = 1.0f; // decibel Make-up gain
 void tCompressor_setParams(tCompressor* const comp, float thresh, float ratio, float knee, float makeup, float attack, float release)
 {
     _tCompressor* c = *comp;
@@ -138,8 +160,8 @@
     c->R = ratio;
     c->W = knee;
     c->M = makeup;
-    c->tauAttack = attack;
-    c->tauRelease = release;
+    c->tauAttack = expf(-1.0f/(0.001f * attack * c->sampleRate));
+    c->tauRelease = expf(-1.0f/(0.001f * release * c->sampleRate));
 }
 
 /* Feedback Leveler */