shithub: leaf

Download patch

ref: 3eca39b695e0bd406858823fa977cb74e1936730
parent: 09c6e97e29d927483135b5e159318ed1f97b6f30
author: Matthew Wang <mjw7@princeton.edu>
date: Thu Sep 24 13:38:00 EDT 2020

allow negative mbsaw freq

--- a/TestPlugin/Source/MyTest.cpp
+++ b/TestPlugin/Source/MyTest.cpp
@@ -55,6 +55,7 @@
     LEAF_init(&leaf, sampleRate, blockSize, memory, MSIZE, &getRandomFloat);
     
     tMBSaw_init(&bsaw, &leaf);
+    tMBSaw_setFreq(&bsaw, -500);
     tMBTriangle_init(&btri, &leaf);
     tMBPulse_init(&bpulse, &leaf);
     
@@ -94,9 +95,11 @@
 
 float   LEAFTest_tick            (float input)
 {
-    tBuffer_tick(&samp, input);
-
-    return tMBSampler_tick(&sampler);
+//    tBuffer_tick(&samp, input);
+//
+//    return tMBSampler_tick(&sampler);
+    
+    return tMBSaw_tick(&bsaw);
     
     
 //    tMBSaw_setFreq(&bsaw, x);
--- a/leaf/Inc/leaf-math.h
+++ b/leaf/Inc/leaf-math.h
@@ -190,6 +190,8 @@
     double fasterexp(double x);
 
     float fasterexpf(float x);
+    
+    float fastsqrtf(float x);
 
     float mtof(float f);
     
--- a/leaf/Src/leaf-oscillators.c
+++ b/leaf/Src/leaf-oscillators.c
@@ -1166,13 +1166,23 @@
     a = 0.5f; // when a = 1, LPfilter is disabled
     
     t = freq / leaf->sampleRate;
-    if (t < 1e-5f) t = 1e-5f;
-    if (t > 0.5f) t = 0.5f;
-    dw = (t - w) ;
+    
+    if (t >= 0)
+    {
+        if (t < 1e-5f) t = 1e-5f;
+        if (t > 0.5f) t = 0.5f;
+    }
+    else
+    {
+        if (t > -1e-5f) t = -1e-5f;
+        if (t < -0.5f) t = -0.5f;
+    }
+    
+    dw = (t - w);
     t = 0.5f * (1.0f + c->waveform );
     if (t < w) t = w;
     if (t > 1.0f - w) t = 1.0f - w;
-    db = (t - b) ;
+    db = (t - b);
     
     w += dw;
     b += db;
@@ -1357,8 +1367,18 @@
     if (c->_init) {
         p = 0.5f;
         w = freq / leaf->sampleRate;
-        if (w < 1e-5f) w = 1e-5f;
-        if (w > 0.5f) w = 0.5f;
+        
+        if (w >= 0)
+        {
+            if (w < 1e-5f) w = 1e-5f;
+            if (w > 0.5f) w = 0.5f;
+        }
+        else
+        {
+            if (w > -1e-5f) w = -1e-5f;
+            if (w < -0.5f) w = -0.5f;
+        }
+        
         /* if we valued alias-free startup over low startup time, we could do:
          *   p -= w;
          *   place_slope_dd(_f, j, 0.0f, w, -1.0f); */
@@ -1369,8 +1389,18 @@
     a = 0.5f; // when a = 1, LPfilter is disabled
     
     t = freq / leaf->sampleRate;
-    if (t < 1e-5f) t = 1e-5f;
-    if (t > 0.5f) t = 0.5f;
+
+    if (t >= 0)
+    {
+        if (t < 1e-5f) t = 1e-5f;
+        if (t > 0.5f) t = 0.5f;
+    }
+    else
+    {
+        if (t > -1e-5f) t = -1e-5f;
+        if (t < -0.5f) t = -0.5f;
+    }
+    
     dw = (t - w); // n= 1
     w += dw;
     p += w;
@@ -1398,8 +1428,12 @@
         c->syncout = p / w + 1e-20f;
         place_step_dd(c->_f, j, p, w, 1.0f);
         
-    } else {
-        
+    } else if (p < 0.0f) {
+        p += 1.0f;
+        c->syncout = p / w + 1e-20f;
+        place_step_dd(c->_f, j, 1.0f - p, -w, -1.0f);
+    }
+    else {
         c->syncout = 0.0f;
     }
     c->_f[j + DD_SAMPLE_DELAY] += 0.5f - p;