shithub: leaf

Download patch

ref: 00d43ea2f51f8ccbd90d3f43b05d3eed8c6298d2
parent: 4c483c67a639b349bdfdfc02b4388a679081cd51
author: Matthew Wang <mjw7@princeton.edu>
date: Fri Feb 14 07:12:02 EST 2020

add autosampler object; default mempool declaration fix

--- a/LEAF/Inc/leaf-mempool.h
+++ b/LEAF/Inc/leaf-mempool.h
@@ -113,6 +113,8 @@
     
     //==============================================================================
     
+    extern tMempool leaf_mempool;
+    
 #ifdef __cplusplus
 }
 #endif
--- a/LEAF/Inc/leaf-sampling.h
+++ b/LEAF/Inc/leaf-sampling.h
@@ -21,6 +21,7 @@
 #include "leaf-mempool.h"
 #include "leaf-envelopes.h"
 #include "leaf-mempool.h"
+#include "leaf-analysis.h"
     
     //==============================================================================
     
@@ -131,6 +132,40 @@
     void    tSampler_setRate            (tSampler* const, float rate);
     
     //==============================================================================
+    
+    typedef struct _tAutoSampler
+    {
+        tSampler sampler;
+        tEnvelopeFollower ef;
+        uint32_t windowSize;
+        float threshold;
+        float previousPower;
+        uint32_t sampleCounter;
+        uint32_t powerCounter;
+        uint8_t sampleTriggered;
+    } _tAutoSampler;
+    
+    typedef _tAutoSampler* tAutoSampler;
+    
+    void    tAutoSampler_init               (tAutoSampler* const, tBuffer* const);
+    void    tAutoSampler_free               (tAutoSampler* const);
+    void    tAutoSampler_initToPool         (tAutoSampler* const, tBuffer* const, tMempool* const);
+    void    tAutoSampler_freeFromPool       (tAutoSampler* const, tMempool* const);
+    
+    float   tAutoSampler_tick               (tAutoSampler* const, float input);
+    
+    void    tAutoSampler_setBuffer          (tAutoSampler* const, tBuffer* const);
+    
+    void    tAutoSampler_setMode            (tAutoSampler* const, PlayMode mode);
+    
+    void    tAutoSampler_play               (tAutoSampler* const);
+    void    tAutoSampler_stop               (tAutoSampler* const);
+    
+    void    tAutoSampler_setThreshold       (tAutoSampler* const, float thresh);
+    void    tAutoSampler_setWindowSize      (tAutoSampler* const, uint32_t size);
+    void    tAutoSampler_setCrossfadeLength (tAutoSampler* const, uint32_t length);
+    
+    void    tAutoSampler_setRate            (tAutoSampler* const, float rate);
     
 #ifdef __cplusplus
 }
--- a/LEAF/Src/leaf-delay.c
+++ b/LEAF/Src/leaf-delay.c
@@ -903,7 +903,8 @@
     if (d->idx >= d->maxDelay) d->idx = 0.0f;
 
     if (d->lastOut)
-    return d->lastOut;
+        return d->lastOut;
+    return 0.0f;
 }
 
 void  tTapeDelay_incrementInPoint(tTapeDelay* const dl)
--- a/LEAF/Src/leaf-mempool.c
+++ b/LEAF/Src/leaf-mempool.c
@@ -48,9 +48,11 @@
 
 #endif
 
-tMempool leaf_pool;
+_tMempool leaf_pool;
+tMempool leaf_mempool;
 size_t header_size;
 
+
 /**
  * private function
  */
@@ -79,7 +81,9 @@
 
 void leaf_pool_init(char* memory, size_t size)
 {
-    mpool_create(memory, size, &leaf_pool->pool);
+    mpool_create(memory, size, &leaf_pool.pool);
+    
+    leaf_mempool = &leaf_pool;
 }
 
 /**
@@ -206,7 +210,7 @@
 void* leaf_alloc(size_t size)
 {
     //printf("alloc %i\n", size);
-    void* block = mpool_alloc(size, &leaf_pool->pool);
+    void* block = mpool_alloc(size, &leaf_pool.pool);
     
     if (block == NULL) leaf_mempool_overrun();
     
@@ -216,7 +220,7 @@
 void* leaf_calloc(size_t size)
 {
     //printf("alloc %i\n", size);
-    void* block = mpool_calloc(size, &leaf_pool->pool);
+    void* block = mpool_calloc(size, &leaf_pool.pool);
     
     if (block == NULL) leaf_mempool_overrun();
     
@@ -288,7 +292,7 @@
 
 void leaf_free(void* ptr)
 {
-    mpool_free(ptr, &leaf_pool->pool);
+    mpool_free(ptr, &leaf_pool.pool);
 }
 
 size_t mpool_get_size(mpool_t* pool)
@@ -303,17 +307,17 @@
 
 size_t leaf_pool_get_size(void)
 {
-    return mpool_get_size(&leaf_pool->pool);
+    return mpool_get_size(&leaf_pool.pool);
 }
 
 size_t leaf_pool_get_used(void)
 {
-    return mpool_get_used(&leaf_pool->pool);
+    return mpool_get_used(&leaf_pool.pool);
 }
 
 void* leaf_pool_get_pool(void)
 {
-    float* buff = (float*)leaf_pool->pool.mpool;
+    float* buff = (float*)leaf_pool.pool.mpool;
     
     return buff;
 }
@@ -362,12 +366,12 @@
 
 void tMempool_init(tMempool* const mp, char* memory, size_t size)
 {
-    tMempool_initToPool(mp, memory, size, &leaf_pool);
+    tMempool_initToPool(mp, memory, size, &leaf_mempool);
 }
 
 void tMempool_free(tMempool* const mp)
 {
-    tMempool_freeFromPool(mp, &leaf_pool);
+    tMempool_freeFromPool(mp, &leaf_mempool);
 }
 
 void    tMempool_initToPool     (tMempool* const mp, char* memory, size_t size, tMempool* const mem)
--- a/LEAF/Src/leaf-sampling.c
+++ b/LEAF/Src/leaf-sampling.c
@@ -164,7 +164,18 @@
 
 void tSampler_init(tSampler* const sp, tBuffer* const b)
 {
-    _tSampler* p = *sp = (_tSampler*) leaf_alloc(sizeof(_tSampler));
+    tSampler_initToPool(sp, b, &leaf_mempool);
+}
+
+void tSampler_free         (tSampler* const sp)
+{
+    tSampler_freeFromPool(sp, &leaf_mempool);
+}
+
+void tSampler_initToPool(tSampler* const sp, tBuffer* const b, tMempool* const mp)
+{
+    _tMempool* m = *mp;
+    _tSampler* p = *sp = (_tSampler*) mpool_alloc(sizeof(_tSampler), &m->pool);
     _tBuffer* s = *b;
     
     p->samp = s;
@@ -188,7 +199,7 @@
     
     p->cfxlen = 500; // default 300 sample crossfade
     
-    tRamp_init(&p->gain, 7.0f, 1);
+    tRamp_initToPool(&p->gain, 7.0f, 1, mp);
     tRamp_setVal(&p->gain, 0.f);
     
     p->targetstart = -1;
@@ -195,12 +206,13 @@
     p->targetend = -1;
 }
 
-void tSampler_free         (tSampler* const sp)
+void tSampler_freeFromPool         (tSampler* const sp, tMempool* const mp)
 {
+    _tMempool* m = *mp;
     _tSampler* p = *sp;
-    tRamp_free(&p->gain);
+    tRamp_freeFromPool(&p->gain, mp);
     
-    leaf_free(p);
+    mpool_free(p, &m->pool);
 }
 
 void tSampler_setSample (tSampler* const sp, tBuffer* const b)
@@ -704,3 +716,129 @@
 }
 
 //==============================================================================
+
+void    tAutoSampler_init   (tAutoSampler* const as, tBuffer* const b)
+{
+    tAutoSampler_initToPool(as, b, &leaf_mempool);
+}
+
+void    tAutoSampler_free   (tAutoSampler* const as)
+{
+    tAutoSampler_freeFromPool(as, &leaf_mempool);
+}
+
+void    tAutoSampler_initToPool (tAutoSampler* const as, tBuffer* const b, tMempool* const mp)
+{
+    _tMempool* m = *mp;
+    _tAutoSampler* a = *as = (_tAutoSampler*) mpool_alloc(sizeof(_tAutoSampler), &m->pool);
+    
+    tBuffer_setRecordMode(b, RecordOneShot);
+    tSampler_initToPool(&a->sampler, b, mp);
+    tSampler_setMode(&a->sampler, PlayLoop);
+    tEnvelopeFollower_initToPool(&a->ef, 0.05f, 0.9999f, mp);
+}
+
+void    tAutoSampler_freeFromPool       (tAutoSampler* const as, tMempool* const mp)
+{
+    _tMempool* m = *mp;
+    _tAutoSampler* a = *as;
+    
+    tEnvelopeFollower_freeFromPool(&a->ef, mp);
+    tSampler_freeFromPool(&a->sampler, mp);
+    
+    mpool_free(a, &m->pool);
+}
+
+float   tAutoSampler_tick               (tAutoSampler* const as, float input)
+{
+    _tAutoSampler* a = *as;
+    float currentPower = tEnvelopeFollower_tick(&a->ef, input);
+    
+    if ((currentPower > (a->threshold)) &&
+        (currentPower > a->previousPower + 0.001f) &&
+        (a->sampleTriggered == 0) &&
+        (a->sampleCounter == 0))
+    {
+        a->sampleTriggered = 1;
+        tBuffer_record(&a->sampler->samp);
+        a->sampler->samp->recordedLength = a->sampler->samp->bufferLength;
+        a->sampleCounter = a->windowSize + 24;//arbitrary extra time to avoid resampling while playing previous sample - better solution would be alternating buffers and crossfading
+        a->powerCounter = 1000;
+    }
+    
+    if (a->sampleCounter > 0)
+    {
+        a->sampleCounter--;
+    }
+    
+    
+    tSampler_setEnd(&a->sampler, a->windowSize);
+    tBuffer_tick(&a->sampler->samp, input);
+    //on it's way down
+    if (currentPower <= a->previousPower)
+    {
+        if (a->powerCounter > 0)
+        {
+            a->powerCounter--;
+        }
+        else if (a->sampleTriggered == 1)
+        {
+            a->sampleTriggered = 0;
+        }
+    }
+    
+    a->previousPower = currentPower;
+    
+    return tSampler_tick(&a->sampler);
+}
+
+void    tAutoSampler_setBuffer         (tAutoSampler* const as, tBuffer* const b)
+{
+    _tAutoSampler* a = *as;
+    tBuffer_setRecordMode(b, RecordOneShot);
+    if (a->windowSize > tBuffer_getBufferLength(b))
+        a->windowSize = tBuffer_getBufferLength(b);
+    tSampler_setSample(&a->sampler, b);
+}
+
+void    tAutoSampler_setMode            (tAutoSampler* const as, PlayMode mode)
+{
+    _tAutoSampler* a = *as;
+    tSampler_setMode(&a->sampler, mode);
+}
+
+void    tAutoSampler_play               (tAutoSampler* const as)
+{
+    _tAutoSampler* a = *as;
+    tSampler_play(&a->sampler);
+}
+void    tAutoSampler_stop               (tAutoSampler* const as)
+{
+    _tAutoSampler* a = *as;
+    tSampler_stop(&a->sampler);
+}
+
+void    tAutoSampler_setThreshold       (tAutoSampler* const as, float thresh)
+{
+    _tAutoSampler* a = *as;
+    a->threshold = thresh;
+}
+
+void    tAutoSampler_setWindowSize      (tAutoSampler* const as, uint32_t size)
+{
+    _tAutoSampler* a = *as;
+    if (size > tBuffer_getBufferLength(&a->sampler->samp))
+        a->windowSize = tBuffer_getBufferLength(&a->sampler->samp);
+    else a->windowSize = size;
+}
+
+void    tAutoSampler_setCrossfadeLength (tAutoSampler* const as, uint32_t length)
+{
+    _tAutoSampler* a = *as;
+    tSampler_setCrossfadeLength(&a->sampler, length);
+}
+
+void    tAutoSampler_setRate    (tAutoSampler* const as, float rate)
+{
+    ;
+}
--- a/LEAF_JUCEPlugin/JuceLibraryCode/AppConfig.h
+++ b/LEAF_JUCEPlugin/JuceLibraryCode/AppConfig.h
@@ -317,7 +317,7 @@
 // Audio plugin settings..
 
 #ifndef  JucePlugin_Build_VST
- #define JucePlugin_Build_VST              1
+ #define JucePlugin_Build_VST              0
 #endif
 #ifndef  JucePlugin_Build_VST3
  #define JucePlugin_Build_VST3             0
--- a/LEAF_JUCEPlugin/LEAF.jucer
+++ b/LEAF_JUCEPlugin/LEAF.jucer
@@ -2,7 +2,7 @@
 
 <JUCERPROJECT id="ArQetv" name="LEAF" projectType="audioplug" version="1.0.0"
               bundleIdentifier="com.pumusic.LEAF" includeBinaryInAppConfig="1"
-              buildVST="1" buildVST3="0" buildAU="1" buildAUv3="0" buildRTAS="0"
+              buildVST="0" buildVST3="0" buildAU="1" buildAUv3="0" buildRTAS="0"
               buildAAX="0" pluginName="LEAF" pluginDesc="LEAF" pluginManufacturer="pumusic"
               pluginManufacturerCode="Manu" pluginCode="Arqe" pluginChannelConfigs="{2,2}"
               pluginIsSynth="0" pluginWantsMidiIn="1" pluginProducesMidiOut="0"
@@ -11,7 +11,7 @@
               jucerVersion="5.4.7" companyName="Princeton University" companyEmail="mrmulshine@gmail.com"
               displaySplashScreen="1" reportAppUsage="1" splashScreenColour="Dark"
               buildStandalone="1" enableIAA="0" cppLanguageStandard="11" companyCopyright="Princeton University"
-              pluginFormats="buildVST,buildAU,buildStandalone" pluginCharacteristicsValue="pluginWantsMidiIn">
+              pluginFormats="buildAU,buildStandalone" pluginCharacteristicsValue="pluginWantsMidiIn">
   <MAINGROUP id="F7Bywq" name="LEAF">
     <GROUP id="{14C5E2CD-5D51-3FD3-5677-28BECA29E95E}" name="Source">
       <GROUP id="{C9F79DF0-2C57-F2B0-7B1B-78A14937E038}" name="LEAF">
--- a/LEAF_JUCEPlugin/Source/MyTest.cpp
+++ b/LEAF_JUCEPlugin/Source/MyTest.cpp
@@ -46,7 +46,7 @@
     tSVF_init(&bp1, SVFTypeBandpass, 100, 4.0f);
     tSVF_init(&bp2, SVFTypeBandpass, 1000, 4.0f);
     
-    tFormantShifter_init(&fs, 2048, 20);
+    tFormantShifter_init(&fs, 20);
     
     // Init and set record
     tBuffer_init (&buff, leaf.sampleRate); // init, 1 second buffer