shithub: leaf

Download patch

ref: bfd2c2b02f47612ce82a70fd668847a239760f07
parent: 55ef84edaf6a5718e261922337be9bd89789ed5b
author: Matthew Wang <mjw7@princeton.edu>
date: Thu Feb 20 06:21:25 EST 2020

add smoothing setting to period detection, add default smoothing to period detection in autotune

--- a/LEAF/Inc/leaf-analysis.h
+++ b/LEAF/Inc/leaf-analysis.h
@@ -19,6 +19,7 @@
 #include "leaf-mempool.h"
 #include "leaf-math.h"
 #include "leaf-filters.h"
+#include "leaf-envelopes.h"
     
     //==============================================================================
     
@@ -216,10 +217,13 @@
         int iLast;
         int index;
         float period;
+        float smoothPeriod;
         
         uint16_t hopSize;
         uint16_t windowSize;
         uint8_t fba;
+    
+        tExpSmooth smooth;
         
         float timeConstant;
         float radius;
@@ -236,7 +240,9 @@
     void    tPeriodDetection_initToPool         (tPeriodDetection* const, float* in, float* out, int bufSize, int frameSize, tMempool* const);
     void    tPeriodDetection_freeFromPool       (tPeriodDetection* const, tMempool* const);
     
-    float   tPeriodDetection_findPeriod         (tPeriodDetection* const, float sample);
+    float   tPeriodDetection_tick               (tPeriodDetection* const, float sample);
+    float   tPeriodDetection_getPeriod          (tPeriodDetection* const);
+    void    tPeriodDetection_setSmoothAmount    (tPeriodDetection* const, float amount);
     void    tPeriodDetection_setHopSize         (tPeriodDetection* p, int hs);
     void    tPeriodDetection_setWindowSize      (tPeriodDetection* p, int ws);
     
--- a/LEAF/Inc/leaf-effects.h
+++ b/LEAF/Inc/leaf-effects.h
@@ -19,6 +19,7 @@
 #include "leaf-mempool.h"
 #include "leaf-dynamics.h"
 #include "leaf-analysis.h"
+#include "leaf-envelopes.h"
     
     //==============================================================================
     
--- a/LEAF/Src/leaf-analysis.c
+++ b/LEAF/Src/leaf-analysis.c
@@ -840,36 +840,12 @@
 //===========================================================================
 void    tPeriodDetection_init    (tPeriodDetection* const pd, float* in, float* out, int bufSize, int frameSize)
 {
-    _tPeriodDetection* p = *pd = (_tPeriodDetection*) leaf_calloc(sizeof(_tPeriodDetection));
-    
-    p->inBuffer = in;
-    p->outBuffer = out;
-    p->bufSize = bufSize;
-    p->frameSize = frameSize;
-    p->framesPerBuffer = p->bufSize / p->frameSize;
-    p->curBlock = 1;
-    p->lastBlock = 0;
-    p->index = 0;
-    
-    p->hopSize = DEFHOPSIZE;
-    p->windowSize = DEFWINDOWSIZE;
-    p->fba = FBA;
-    
-    tEnvPD_init(&p->env, p->windowSize, p->hopSize, p->frameSize);
-    
-    tSNAC_init(&p->snac, DEFOVERLAP);
-    
-    p->timeConstant = DEFTIMECONSTANT;
-    p->radius = expf(-1000.0f * p->hopSize * leaf.invSampleRate / p->timeConstant);
+    tPeriodDetection_initToPool(pd, in, out, bufSize, frameSize, &leaf_mempool);
 }
 
 void tPeriodDetection_free (tPeriodDetection* const pd)
 {
-    _tPeriodDetection* p = *pd;
-    
-    tEnvPD_free(&p->env);
-    tSNAC_free(&p->snac);
-    leaf_free(p);
+    tPeriodDetection_freeFromPool(pd, &leaf_mempool);
 }
 
 void    tPeriodDetection_initToPool  (tPeriodDetection* const pd, float* in, float* out, int bufSize, int frameSize, tMempool* const mp)
@@ -894,6 +870,8 @@
     
     tSNAC_initToPool(&p->snac, DEFOVERLAP, mp);
     
+    tExpSmooth_initToPool(&p->smooth, 0.0f, 1.0f, mp);
+    
     p->timeConstant = DEFTIMECONSTANT;
     p->radius = expf(-1000.0f * p->hopSize * leaf.invSampleRate / p->timeConstant);
 }
@@ -905,10 +883,11 @@
     
     tEnvPD_freeFromPool(&p->env, mp);
     tSNAC_freeFromPool(&p->snac, mp);
+    tExpSmooth_freeFromPool(&p->smooth, mp);
     mpool_free(p, &m->pool);
 }
 
-float tPeriodDetection_findPeriod (tPeriodDetection* pd, float sample)
+float tPeriodDetection_tick (tPeriodDetection* pd, float sample)
 {
     _tPeriodDetection* p = *pd;
     
@@ -939,8 +918,22 @@
         if (p->lastBlock >= p->framesPerBuffer) p->lastBlock = 0;
     }
     
-    // changed from period to p->period
-    return p->period;
+    tExpSmooth_setDest(&p->smooth, p->period);
+    p->smoothPeriod = tExpSmooth_tick(&p->smooth);
+    return p->smoothPeriod;
+}
+
+float tPeriodDetection_getPeriod(tPeriodDetection* pd)
+{
+    _tPeriodDetection* p = *pd;
+    return p->smoothPeriod;
+}
+
+void tPeriodDetection_setSmoothAmount(tPeriodDetection* pd, float amount)
+{
+    _tPeriodDetection* p = *pd;
+    LEAF_clip(0.0f, amount, 1.0f);
+    tExpSmooth_setFactor(&p->smooth, 1.0f - amount);
 }
 
 void tPeriodDetection_setHopSize(tPeriodDetection* pd, int hs)
--- a/LEAF/Src/leaf-effects.c
+++ b/LEAF/Src/leaf-effects.c
@@ -963,7 +963,7 @@
     
     if (p->indexstore >= ps->frameSize)
     {
-        period = p->period;
+        period = tPeriodDetection_getPeriod(&p);
         
         if(pitchshift_attackdetect(ps) == 1)
         {
@@ -995,7 +995,7 @@
     
     if (p->indexstore >= ps->frameSize)
     {
-        period = p->period;
+        period = tPeriodDetection_getPeriod(&p);
         
         if(pitchshift_attackdetect(ps) == 1)
         {
@@ -1030,7 +1030,7 @@
     
     if (p->indexstore >= ps->frameSize)
     {
-        period = p->period;
+        period = tPeriodDetection_getPeriod(&p);
         
         if(pitchshift_attackdetect(ps) == 1)
         {
@@ -1060,54 +1060,12 @@
 
 void tRetune_init(tRetune* const rt, int numVoices, int bufSize, int frameSize)
 {
-    _tRetune* r = *rt = (_tRetune*) leaf_calloc(sizeof(_tRetune));
-    
-    r->bufSize = bufSize;
-    r->frameSize = frameSize;
-    r->numVoices = numVoices;
-    
-    r->inBuffer = (float*) leaf_calloc(sizeof(float) * r->bufSize);
-    r->outBuffers = (float**) leaf_calloc(sizeof(float*) * r->numVoices);
-    
-    r->hopSize = DEFHOPSIZE;
-    r->windowSize = DEFWINDOWSIZE;
-    r->fba = FBA;
-    tRetune_setTimeConstant(rt, DEFTIMECONSTANT);
-    
-    r->inputPeriod = 0.0f;
-    
-    r->ps = (tPitchShift*) leaf_calloc(sizeof(tPitchShift) * r->numVoices);
-    r->pitchFactor = (float*) leaf_calloc(sizeof(float) * r->numVoices);
-    r->tickOutput = (float*) leaf_calloc(sizeof(float) * r->numVoices);
-    for (int i = 0; i < r->numVoices; ++i)
-    {
-        r->outBuffers[i] = (float*) leaf_calloc(sizeof(float) * r->bufSize);
-    }
-    
-    tPeriodDetection_init(&r->pd, r->inBuffer, r->outBuffers[0], r->bufSize, r->frameSize);
-    
-    for (int i = 0; i < r->numVoices; ++i)
-    {
-        tPitchShift_init(&r->ps[i], &r->pd, r->outBuffers[i], r->bufSize);
-    }
+    tRetune_initToPool(rt, numVoices, bufSize, frameSize, &leaf_mempool);
 }
 
 void tRetune_free(tRetune* const rt)
 {
-    _tRetune* r = *rt;
-    
-    tPeriodDetection_free(&r->pd);
-    for (int i = 0; i < r->numVoices; ++i)
-    {
-        tPitchShift_free(&r->ps[i]);
-        leaf_free(r->outBuffers[i]);
-    }
-    leaf_free(r->tickOutput);
-    leaf_free(r->pitchFactor);
-    leaf_free(r->ps);
-    leaf_free(r->inBuffer);
-    leaf_free(r->outBuffers);
-    leaf_free(r);
+    tRetune_freeFromPool(rt, &leaf_mempool);
 }
 
 void    tRetune_initToPool      (tRetune* const rt, int numVoices, int bufSize, int frameSize, tMempool* const mp)
@@ -1138,6 +1096,7 @@
     }
     
     tPeriodDetection_initToPool(&r->pd, r->inBuffer, r->outBuffers[0], r->bufSize, r->frameSize, mp);
+    tPeriodDetection_setSmoothAmount(&r->pd, 0.0f);
     
     for (int i = 0; i < r->numVoices; ++i)
     {
@@ -1168,7 +1127,7 @@
 {
     _tRetune* r = *rt;
     
-    r->inputPeriod = tPeriodDetection_findPeriod(&r->pd, sample);
+    r->inputPeriod = tPeriodDetection_tick(&r->pd, sample);
     
     for (int v = 0; v < r->numVoices; ++v)
     {
@@ -1203,8 +1162,6 @@
         r->outBuffers[i] = (float*) leaf_alloc(sizeof(float) * r->bufSize);
         tPitchShift_init(&r->ps[i], &r->pd, r->outBuffers[i], r->bufSize);
     }
-    
-    
 }
 
 void tRetune_setPitchFactors(tRetune* const rt, float pf)
@@ -1270,56 +1227,12 @@
 
 void tAutotune_init(tAutotune* const rt, int numVoices, int bufSize, int frameSize)
 {
-    _tAutotune* r = *rt = (_tAutotune*) leaf_alloc(sizeof(_tAutotune));
-    
-    r->bufSize = bufSize;
-    r->frameSize = frameSize;
-    r->numVoices = numVoices;
-    
-    r->inBuffer = (float*) leaf_alloc(sizeof(float) * r->bufSize);
-    r->outBuffers = (float**) leaf_alloc(sizeof(float*) * r->numVoices);
-    
-    r->hopSize = DEFHOPSIZE;
-    r->windowSize = DEFWINDOWSIZE;
-    r->fba = FBA;
-    tAutotune_setTimeConstant(rt, DEFTIMECONSTANT);
-    
-    
-    
-    r->ps = (tPitchShift*) leaf_alloc(sizeof(tPitchShift) * r->numVoices);
-    r->freq = (float*) leaf_alloc(sizeof(float) * r->numVoices);
-    r->tickOutput = (float*) leaf_alloc(sizeof(float) * r->numVoices);
-    for (int i = 0; i < r->numVoices; ++i)
-    {
-        r->outBuffers[i] = (float*) leaf_alloc(sizeof(float) * r->bufSize);
-    }
-    
-    tPeriodDetection_init(&r->pd, r->inBuffer, r->outBuffers[0], r->bufSize, r->frameSize);
-    
-    for (int i = 0; i < r->numVoices; ++i)
-    {
-        tPitchShift_init(&r->ps[i], &r->pd, r->outBuffers[i], r->bufSize);
-    }
-    
-    r->inputPeriod = 0.0f;
+    tAutotune_initToPool(rt, numVoices, bufSize, frameSize, &leaf_mempool);
 }
 
 void tAutotune_free(tAutotune* const rt)
 {
-    _tAutotune* r = *rt;
-    
-    tPeriodDetection_free(&r->pd);
-    for (int i = 0; i < r->numVoices; ++i)
-    {
-        tPitchShift_free(&r->ps[i]);
-        leaf_free(r->outBuffers[i]);
-    }
-    leaf_free(r->tickOutput);
-    leaf_free(r->freq);
-    leaf_free(r->ps);
-    leaf_free(r->inBuffer);
-    leaf_free(r->outBuffers);
-    leaf_free(r);
+    tAutotune_freeFromPool(rt, &leaf_mempool);
 }
 
 void    tAutotune_initToPool        (tAutotune* const rt, int numVoices, int bufSize, int frameSize, tMempool* const mp)
@@ -1339,8 +1252,6 @@
     r->fba = FBA;
     tAutotune_setTimeConstant(rt, DEFTIMECONSTANT);
     
-    
-    
     r->ps = (tPitchShift*) mpool_alloc(sizeof(tPitchShift) * r->numVoices, &m->pool);
     r->freq = (float*) mpool_alloc(sizeof(float) * r->numVoices, &m->pool);
     r->tickOutput = (float*) mpool_alloc(sizeof(float) * r->numVoices, &m->pool);
@@ -1350,6 +1261,7 @@
     }
     
     tPeriodDetection_initToPool(&r->pd, r->inBuffer, r->outBuffers[0], r->bufSize, r->frameSize, mp);
+    tPeriodDetection_setSmoothAmount(&r->pd, 0.5f);
     
     for (int i = 0; i < r->numVoices; ++i)
     {
@@ -1382,7 +1294,7 @@
 {
     _tAutotune* r = *rt;
     
-    float tempPeriod = tPeriodDetection_findPeriod(&r->pd, sample);
+    float tempPeriod = tPeriodDetection_tick(&r->pd, sample);
     if (tempPeriod < 1000.0f) //to avoid trying to follow consonants JS
 	{
 		r->inputPeriod = tempPeriod;
--- a/LEAF_JUCEPlugin/Source/MyTest.cpp
+++ b/LEAF_JUCEPlugin/Source/MyTest.cpp
@@ -25,6 +25,8 @@
 tBuffer buff;
 tEnvelope env;
 
+tAutotune at;
+
 float gain;
 float freq;
 float dtime;
@@ -43,19 +45,21 @@
     
     tNoise_init(&noise, WhiteNoise);
     
-    tSVF_init(&bp1, SVFTypeBandpass, 100, 4.0f);
-    tSVF_init(&bp2, SVFTypeBandpass, 1000, 4.0f);
+    tAutotune_init(&at, 1, 1024, 512);
     
-    tFormantShifter_init(&fs, 20);
-    
-    // Init and set record
-    tBuffer_init (&buff, leaf.sampleRate); // init, 1 second buffer
-    tBuffer_setRecordMode (&buff, RecordOneShot); // RecordOneShot records once through
-    
-    // Init and set play
-    tSampler_init (&samp, &buff); // init, give address of record buffer
-    tSampler_setMode (&samp, PlayLoop); //set in Loop Mode
-    tSampler_setRate(&samp, 1.763f); // Rate of 1.0
+//    tSVF_init(&bp1, SVFTypeBandpass, 100, 4.0f);
+//    tSVF_init(&bp2, SVFTypeBandpass, 1000, 4.0f);
+//
+//    tFormantShifter_init(&fs, 20);
+//
+//    // Init and set record
+//    tBuffer_init (&buff, leaf.sampleRate); // init, 1 second buffer
+//    tBuffer_setRecordMode (&buff, RecordOneShot); // RecordOneShot records once through
+//
+//    // Init and set play
+//    tSampler_init (&samp, &buff); // init, give address of record buffer
+//    tSampler_setMode (&samp, PlayLoop); //set in Loop Mode
+//    tSampler_setRate(&samp, 1.763f); // Rate of 1.0
 }
 
 float   LEAFTest_tick            (float input)
@@ -66,10 +70,14 @@
 //    b += tSVF_tick(&bp2, sample);
 //
 //    return (tFormantShifter_tick(&fs, input));
+//
+//    tBuffer_tick(&buff, input);
     
-    tBuffer_tick(&buff, input);
+//    return tSampler_tick(&samp);
     
-    return tSampler_tick(&samp);
+    tAutotune_setFreq(&at, 440.0f, 0);
+    
+    return tAutotune_tick(&at, input)[0];
 }
 
 int firstFrame = 1;
@@ -76,18 +84,18 @@
 bool lastState = false, lastPlayState = false;
 void    LEAFTest_block           (void)
 {
-    if (firstFrame == 1)
-    {
-        tBuffer_record(&buff); // starts recording
-        tSampler_play(&samp); // start spitting samples out
-        firstFrame = 0;
-    }
+//    if (firstFrame == 1)
+//    {
+//        tBuffer_record(&buff); // starts recording
+//        tSampler_play(&samp); // start spitting samples out
+//        firstFrame = 0;
+//    }
     
     float val = getSliderValue("mod freq");
     
     x = val * 3.5f + 0.5f;
     
-    a = val * tBuffer_getBufferLength(&buff);
+//    a = val * tBuffer_getBufferLength(&buff);
     
     DBG("start: " + String(a));
     
@@ -95,12 +103,12 @@
     
     y = val * 49.0f + 1.0f;
     b = val * 20.0f - 5.0f;
-    b = val * tBuffer_getBufferLength(&buff);
+//    b = val * tBuffer_getBufferLength(&buff);
     
     DBG("rate: " + String(b));
-    
-    tSampler_setStart(&samp, a);
-    tSampler_setEnd(&samp, b);
+//    
+//    tSampler_setStart(&samp, a);
+//    tSampler_setEnd(&samp, b);
 //    tSampler_setRate(&samp, b);
     
 //    tFormantShifter_setShiftFactor(&fs, x);