shithub: leaf

Download patch

ref: 5311128d625468f22f7f4b7b93672ef8372bbff7
parent: a71835e6d17ecd00bf6d37fa1e4560f4507a6c23
author: Matthew Wang <Matthew@nat-oitwireless-inside-vapornet100-10-9-74-35.princeton.edu>
date: Tue Dec 10 08:00:44 EST 2019

refactor formantshifter shift factor + add intensity param; separate sampler from its buffer

--- a/LEAF/Inc/leaf-effects.h
+++ b/LEAF/Inc/leaf-effects.h
@@ -265,6 +265,8 @@
         float fmute;
         float fmutealph;
         unsigned int cbi;
+        float shiftFactor;
+        float intensity, invIntensity;
         
     } _tFormantShifter;
     
@@ -273,10 +275,12 @@
     void        tFormantShifter_init            (tFormantShifter* const, int bufsize, int order);
     void        tFormantShifter_free            (tFormantShifter* const);
     
-    float       tFormantShifter_tick            (tFormantShifter* const, float input, float fwarp);
+    float       tFormantShifter_tick            (tFormantShifter* const, float input);
     float       tFormantShifter_remove          (tFormantShifter* const, float input);
-    float       tFormantShifter_add             (tFormantShifter* const, float input, float fwarp);
+    float       tFormantShifter_add             (tFormantShifter* const, float input);
     void        tFormantShifter_ioSamples       (tFormantShifter* const, float* in, float* out, int size, float fwarp);
+    void        tFormantShifter_setShiftFactor  (tFormantShifter* const, float shiftFactor);
+    void        tFormantShifter_setIntensity    (tFormantShifter* const, float intensity);
     
     //==============================================================================
     
--- a/LEAF/Inc/leaf-math.h
+++ b/LEAF/Inc/leaf-math.h
@@ -127,9 +127,9 @@
     
     // alternative implementation for abs()
     // REQUIRES: 32 bit floats
-    float fastabs(float f);
+    float fastabsf(float f);
     
-    float fastexp2(float f);
+    float fastexp2f(float f);
     
 #define LOGTEN 2.302585092994
     
--- a/LEAF/Inc/leaf-sampling.h
+++ b/LEAF/Inc/leaf-sampling.h
@@ -100,12 +100,12 @@
     
     typedef _tSampler* tSampler;
     
-    void    tSampler_init      (tSampler* const, uint32_t length);
+    void    tSampler_init      (tSampler* const, tBuffer* const);
     void    tSampler_free      (tSampler* const);
     
     float   tSampler_tick      (tSampler* const);
     
-    tBuffer tSampler_getBuffer (tSampler* const);
+    void    tSampler_setSample (tSampler* const, tBuffer* const);
     
     void    tSampler_setMode   (tSampler* const, PlayMode mode);
     
--- a/LEAF/Src/leaf-analysis.c
+++ b/LEAF/Src/leaf-analysis.c
@@ -333,7 +333,7 @@
     int i = 0;
     float tmp;
     for(i = 0; i < a->blocksize; ++i){
-        tmp = fastabs(in[i]);
+        tmp = fastabsf(in[i]);
         
         if(tmp > a->env)
             a->env = a->atk_coeff * (a->env - tmp) + tmp;
--- a/LEAF/Src/leaf-effects.c
+++ b/LEAF/Src/leaf-effects.c
@@ -1303,15 +1303,17 @@
 }
 
 
-float tFormantShifter_tick(tFormantShifter* fsr, float in, float fwarp)
+float tFormantShifter_tick(tFormantShifter* const fsr, float in)
 {
-    return tFormantShifter_add(fsr, tFormantShifter_remove(fsr, in), fwarp);
+    return tFormantShifter_add(fsr, tFormantShifter_remove(fsr, in));
 }
 
-float tFormantShifter_remove(tFormantShifter* fsr, float in)
+float tFormantShifter_remove(tFormantShifter* const fsr, float in)
 {
     _tFormantShifter* fs = *fsr;
     
+    in *= fs->intensity;
+    
     float fa, fb, fc, foma, falph, ford, flpa, flamb, tf, fk;
     int ti4;
     ford = fs->ford;
@@ -1347,10 +1349,10 @@
         fs->cbi = 0;
     }
     
-    return fa;
+    return fa * fs->invIntensity;
 }
 
-float tFormantShifter_add(tFormantShifter* fsr, float in, float fwarp)
+float tFormantShifter_add(tFormantShifter* const fsr, float in)
 {
     _tFormantShifter* fs = *fsr;
     
@@ -1361,7 +1363,7 @@
     foma = (1.0f - falph);
     flpa = fs->flpa;
     flamb = fs->flamb;
-    tf = exp2f(fwarp*0.5f) * (1+flamb)/(1-flamb);
+    tf = fs->shiftFactor * (1+flamb)/(1-flamb);
     frlamb = (tf-1)/(tf+1);
     ti4 = fs->cbi;
     
@@ -1402,9 +1404,9 @@
     f1resp = tf;
     
     //  now solve equations for output, based on 0-response and 1-response
-    tf = (float)2*tf2;
+    tf = 2.0f*tf2;
     tf2 = tf;
-    tf = ((float)1 - f1resp + f0resp);
+    tf = (1.0f - f1resp + f0resp);
     if (tf!=0)
     {
         tf2 = (tf2 + f0resp) / tf;
@@ -1446,4 +1448,18 @@
     // ...and we're done messing with formants
     
     return tf;
+}
+
+// 1.0f is no change, 2.0f is an octave up, 0.5f is an octave down
+void tFormantShifter_setShiftFactor(tFormantShifter* const fsr, float shiftFactor)
+{
+    _tFormantShifter* fs = *fsr;
+    fs->shiftFactor = shiftFactor;
+}
+
+void tFormantShifter_setIntensity(tFormantShifter* const fsr, float intensity)
+{
+    _tFormantShifter* fs = *fsr;
+    fs->intensity = intensity;
+    fs->invIntensity = 1.0f/fs->intensity;
 }
--- a/LEAF/Src/leaf-math.c
+++ b/LEAF/Src/leaf-math.c
@@ -60,7 +60,7 @@
 
 // alternative implementation for abs()
 // REQUIRES: 32 bit floats
-float fastabs(float f)
+float fastabsf(float f)
 {
     union
     {
@@ -75,7 +75,7 @@
 
 // fast floating-point exp2 function taken from Robert Bristow Johnson's
 // post in the music-dsp list on Date: Tue, 02 Sep 2014 16:50:11 -0400
-float fastexp2(float x)
+float fastexp2f(float x)
 {
     if (x >= -127.0)
     {
--- a/LEAF/Src/leaf-sampling.c
+++ b/LEAF/Src/leaf-sampling.c
@@ -122,11 +122,12 @@
 
 //================================tSampler=====================================
 
-void tSampler_init(tSampler* const sp, uint32_t length)
+void tSampler_init(tSampler* const sp, tBuffer* const b)
 {
     _tSampler* p = *sp = (_tSampler*) leaf_alloc(sizeof(_tSampler));
+    _tBuffer* s = *b;
     
-    tBuffer_init(&p->samp, length);
+    p->samp = s;
     
     p->active = 0;
     
@@ -159,10 +160,12 @@
     leaf_free(p);
 }
 
-tBuffer tSampler_getBuffer (tSampler* const sp)
+void tSampler_setSample (tSampler* const sp, tBuffer* const b)
 {
     _tSampler* p = *sp;
-    return p->samp;
+    _tBuffer* s = *b;
+    
+    p->samp = s;
 }
 
 float tSampler_tick        (tSampler* const sp)
--- a/LEAF_JUCEPlugin/Source/MyTest.cpp
+++ b/LEAF_JUCEPlugin/Source/MyTest.cpp
@@ -17,11 +17,9 @@
 static void run_pool_test(void);
 
 tNoise noise;
-tCycle sine;
-tDelay delay;
-tRetune retune;
-tAutotune autotune;
-tTalkbox test;
+tSVF bp1;
+tSVF bp2;
+tFormantShifter fs;
 
 float gain;
 float freq;
@@ -28,8 +26,8 @@
 float dtime;
 bool buttonState;
 int ratio = 2;
-int x = 0;
-int y = 0;
+float x = 0.0f;
+float y = 0.0f;
 
 #define MSIZE 500000
 char memory[MSIZE];
@@ -39,68 +37,42 @@
     LEAF_init(sampleRate, blockSize, memory, MSIZE, &getRandomFloat);
     
     tNoise_init(&noise, WhiteNoise);
-    tCycle_init(&sine);
-    tCycle_setFreq(&sine, 200);
-    tDelay_init(&delay, 44100, 44100);
-    tDelay_free(&delay);
-    tDelay_init(&delay, 44100, 44100);
     
-    tTalkbox_init(&test, 1024);
-    tTalkbox_free(&test);
-    tTalkbox_init(&test, 1024);
+    tSVF_init(&bp1, SVFTypeBandpass, 100, 4.0f);
+    tSVF_init(&bp2, SVFTypeBandpass, 1000, 4.0f);
     
-    tRetune_init(&retune, 1, 2048, 1024);
-    
-    tDelay_free(&delay);
-    tRetune_free(&retune);
-    tTalkbox_free(&test);
-    tTalkbox_init(&test, 1024);
-    tDelay_init(&delay, 44100, 44100);
-    tRetune_init(&retune, 1, 2048, 1024);
-    
-    tAutotune_init(&autotune, 8, 2048, 1024);
+    tFormantShifter_init(&fs, 2048, 20);
 }
 
 float   LEAFTest_tick            (float input)
 {
-    float sample = tCycle_tick(&sine);
-//    float* retuneOut = tRetune_tick(&retune, sample);
-//    float* autotuneOut = tAutotune_tick(&autotune, sample);
-//    float r1 = retuneOut[0];
-//    //float r2 = retuneOut[1];
-//    float a1 = autotuneOut[6];
-//    float a2 = autotuneOut[7];
-//    sample = a1 + a2;
-//
-//    sample = tTalkbox_tick(&test, tNoise_tick(&noise), input);
+    float sample = tNoise_tick(&noise);
+    sample *= 0.5f;
+    float b = tSVF_tick(&bp1, sample);
+    b += tSVF_tick(&bp2, sample);
     
-    // do more calls based on 2nd slider
-    for (int i = 0; i < x; i++)
-    {
-        // do fast if 3rd slider is all the way up
-        if (y) float exp2 = exp2f(x);
-        else float fexp2 = fastexp2(x);
-    }
-    
-    return sample * 0.25f;
+    return (tFormantShifter_tick(&fs, input));
 }
 
 
-
 bool lastState = false, lastPlayState = false;
 void    LEAFTest_block           (void)
 {
     float val = getSliderValue("mod freq");
-    tRetune_setPitchFactor(&retune, val*4.0f + 0.5f, 0);
-    tAutotune_setFreq(&autotune, val*5000.0f + 50.0f, 7);
     
-    x = (val*5000);
+    x = val * 3.5f + 0.5f;
     
+    DBG("fwarp: " + String(x));
+    
     val = getSliderValue("mod depth");
-    tRetune_setPitchFactor(&retune, val*4.0f + 0.5f, 1);
-    tAutotune_setFreq(&autotune, val*5000.0f + 50.0f, 6);
     
-    y = val;
+    y = val * 49.0f + 1.0f;
+    
+    DBG("intensity: " + String(y));
+    
+    tFormantShifter_setShiftFactor(&fs, x);
+    tFormantShifter_setIntensity(&fs, y);
+    
 }
 
 void    LEAFTest_controllerInput (int cnum, float cval)