shithub: leaf

Download patch

ref: 77f3ac50b351c86af9a608e402428cc73b0cd726
parent: 3473477eed2e947fe4b72dc92f3e5400f18cc4a8
author: Matthew Wang <mjw7@princeton.edu>
date: Tue Jun 2 08:12:15 EDT 2020

fix memory leak in vzfilter and diode filter; change remaining init functions to wrap initToPool

--- a/leaf/Inc/leaf-effects.h
+++ b/leaf/Inc/leaf-effects.h
@@ -221,6 +221,7 @@
     // Retune
     typedef struct _tRetune
     {
+        tMempool* mempool;
         tPeriodDetection pd;
         tPitchShift* ps;
         
@@ -264,6 +265,7 @@
     // Autotune
     typedef struct _tAutotune
     {
+        tMempool* mempool;
         tPeriodDetection pd;
         tPitchShift* ps;
         
--- a/leaf/Src/leaf-analysis.c
+++ b/leaf/Src/leaf-analysis.c
@@ -24,18 +24,12 @@
 
 void    tEnvelopeFollower_init(tEnvelopeFollower* const ef, float attackThreshold, float decayCoeff)
 {
-    _tEnvelopeFollower* e = *ef = (_tEnvelopeFollower*) leaf_alloc(sizeof(_tEnvelopeFollower));
-    
-    e->y = 0.0f;
-    e->a_thresh = attackThreshold;
-    e->d_coeff = decayCoeff;
+    tEnvelopeFollower_initToPool(ef, attackThreshold, decayCoeff, &leaf.mempool);
 }
 
 void tEnvelopeFollower_free(tEnvelopeFollower* const ef)
 {
-    _tEnvelopeFollower* e = *ef;
-    
-    leaf_free((char*)e);
+    tEnvelopeFollower_freeFromPool(ef, &leaf.mempool);
 }
 
 void    tEnvelopeFollower_initToPool    (tEnvelopeFollower* const ef, float attackThreshold, float decayCoeff, tMempool* const mp)
@@ -186,18 +180,12 @@
 //===========================================================================
 void    tPowerFollower_init(tPowerFollower* const pf, float factor)
 {
-    _tPowerFollower* p = *pf = (_tPowerFollower*) leaf_alloc(sizeof(_tPowerFollower));
-    
-    p->curr=0.0f;
-    p->factor=factor;
-    p->oneminusfactor=1.0f-factor;
+    tPowerFollower_initToPool(pf, factor, &leaf.mempool);
 }
 
 void tPowerFollower_free(tPowerFollower* const pf)
 {
-    _tPowerFollower* p = *pf;
-    
-    leaf_free((char*)p);
+    tPowerFollower_freeFromPool(pf, &leaf.mempool);
 }
 
 void    tPowerFollower_initToPool   (tPowerFollower* const pf, float factor, tMempool* const mp)
@@ -251,51 +239,12 @@
 
 void tEnvPD_init(tEnvPD* const xpd, int ws, int hs, int bs)
 {
-    _tEnvPD* x = *xpd = (_tEnvPD*) leaf_calloc(sizeof(_tEnvPD));
-    
-    int period = hs, npoints = ws;
-    
-    int i;
-    
-    if (npoints < 1) npoints = 1024;
-    if (period < 1) period = npoints/2;
-    if (period < npoints / MAXOVERLAP + 1)
-        period = npoints / MAXOVERLAP + 1;
-    
-    x->x_npoints = npoints;
-    x->x_phase = 0;
-    x->x_period = period;
-    
-    x->windowSize = npoints;
-    x->hopSize = period;
-    x->blockSize = bs;
-    
-    for (i = 0; i < MAXOVERLAP; i++) x->x_sumbuf[i] = 0.0f;
-    for (i = 0; i < npoints; i++)
-        x->buf[i] = (1.0f - cosf((TWO_PI * i) / npoints))/npoints;
-    for (; i < npoints+INITVSTAKEN; i++) x->buf[i] = 0.0f;
-    
-    x->x_f = 0.0f;
-    
-    x->x_allocforvs = INITVSTAKEN;
-    
-    // ~ ~ ~ dsp ~ ~ ~
-    if (x->x_period % x->blockSize)
-    {
-        x->x_realperiod = x->x_period + x->blockSize - (x->x_period % x->blockSize);
-    }
-    else
-    {
-        x->x_realperiod = x->x_period;
-    }
-    // ~ ~ ~ ~ ~ ~ ~ ~
+    tEnvPD_initToPool(xpd, ws, hs, bs, &leaf.mempool);
 }
 
 void tEnvPD_free (tEnvPD* const xpd)
 {
-    _tEnvPD* x = *xpd;
-    
-    leaf_free((char*)x);
+    tEnvPD_freeFromPool(xpd, &leaf.mempool);
 }
 
 void    tEnvPD_initToPool       (tEnvPD* const xpd, int ws, int hs, int bs, tMempool* const mp)
@@ -403,16 +352,12 @@
 
 void tAttackDetection_init(tAttackDetection* const ad, int blocksize, int atk, int rel)
 {
-    *ad = (_tAttackDetection*) leaf_alloc(sizeof(_tAttackDetection));
-    
-    atkdtk_init(ad, blocksize, atk, rel);
+    tAttackDetection_initToPool(ad, blocksize, atk, rel, &leaf.mempool);
 }
 
 void tAttackDetection_free(tAttackDetection* const ad)
 {
-    _tAttackDetection* a = *ad;
-    
-    leaf_free((char*)a);
+    tAttackDetection_freeFromPool(ad, &leaf.mempool);
 }
 
 void    tAttackDetection_initToPool     (tAttackDetection* const ad, int blocksize, int atk, int rel, tMempool* const mp)
@@ -557,34 +502,12 @@
 
 void tSNAC_init(tSNAC* const snac, int overlaparg)
 {
-    _tSNAC* s = *snac = (_tSNAC*) leaf_calloc(sizeof(_tSNAC));
-    
-    s->biasfactor = DEFBIAS;
-    s->timeindex = 0;
-    s->periodindex = 0;
-    s->periodlength = 0.;
-    s->fidelity = 0.;
-    s->minrms = DEFMINRMS;
-    s->framesize = SNAC_FRAME_SIZE;
-    
-    s->inputbuf = (float*) leaf_calloc(sizeof(float) * SNAC_FRAME_SIZE);
-    s->processbuf = (float*) leaf_calloc(sizeof(float) * (SNAC_FRAME_SIZE * 2));
-    s->spectrumbuf = (float*) leaf_calloc(sizeof(float) * (SNAC_FRAME_SIZE / 2));
-    s->biasbuf = (float*) leaf_calloc(sizeof(float) * SNAC_FRAME_SIZE);
-    
-    snac_biasbuf(snac);
-    tSNAC_setOverlap(snac, overlaparg);
+    tSNAC_initToPool(snac, overlaparg, &leaf.mempool);
 }
 
 void tSNAC_free(tSNAC* const snac)
 {
-    _tSNAC* s = *snac;
-    
-    leaf_free((char*)s->inputbuf);
-    leaf_free((char*)s->processbuf);
-    leaf_free((char*)s->spectrumbuf);
-    leaf_free((char*)s->biasbuf);
-    leaf_free((char*)s);
+    tSNAC_freeFromPool(snac, &leaf.mempool);
 }
 
 void    tSNAC_initToPool    (tSNAC* const snac, int overlaparg, tMempool* const mp)
--- a/leaf/Src/leaf-distortion.c
+++ b/leaf/Src/leaf-distortion.c
@@ -25,18 +25,12 @@
 
 void tSampleReducer_init(tSampleReducer* const sr)
 {
-    _tSampleReducer* s = *sr = (_tSampleReducer*) leaf_alloc(sizeof(_tSampleReducer));
-    
-    s->invRatio = 1.0f;
-    s->hold = 0.0f;
-    s->count = 0;
+    tSampleReducer_initToPool(sr, &leaf.mempool);
 }
 
 void    tSampleReducer_free    (tSampleReducer* const sr)
 {
-    _tSampleReducer* s = *sr;
-    
-    leaf_free((char*)s);
+    tSampleReducer_freeFromPool(sr, &leaf.mempool);
 }
 
 void    tSampleReducer_initToPool   (tSampleReducer* const sr, tMempool* const mp)
@@ -85,30 +79,12 @@
 // Latency is equal to the phase length (numTaps / ratio)
 void tOversampler_init(tOversampler* const osr, int ratio, oBool extraQuality)
 {
-    _tOversampler* os = *osr = (_tOversampler*) leaf_alloc(sizeof(_tOversampler));
-    
-    uint8_t offset = 0;
-    if (extraQuality) offset = 6;
-    if (ratio == 2 || ratio == 4  ||
-        ratio == 8 || ratio == 16 ||
-        ratio == 32 || ratio == 64) {
-        os->ratio = ratio;
-        int idx = (int)(log2f(os->ratio))-1+offset;
-        os->numTaps = __leaf_tablesize_firNumTaps[idx];
-        os->phaseLength = os->numTaps / os->ratio;
-        os->pCoeffs = (float*) __leaf_tableref_firCoeffs[idx];
-        os->upState = (float*) leaf_alloc(sizeof(float) * os->numTaps * 2);
-        os->downState = (float*) leaf_alloc(sizeof(float) * os->numTaps * 2);
-    }
+    tOversampler_initToPool(osr, ratio, extraQuality, &leaf.mempool);
 }
 
 void tOversampler_free(tOversampler* const osr)
 {
-    _tOversampler* os = *osr;
-    
-    leaf_free((char*)os->upState);
-    leaf_free((char*)os->downState);
-    leaf_free((char*)os);
+    tOversampler_freeFromPool(osr, &leaf.mempool);
 }
 
 void    tOversampler_initToPool     (tOversampler* const osr, int ratio, oBool extraQuality, tMempool* const mp)
@@ -597,21 +573,12 @@
 
 void    tCrusher_init    (tCrusher* const cr)
 {
-    _tCrusher* c = *cr = (_tCrusher*) leaf_alloc(sizeof(_tCrusher));
-    
-    c->op = 4;
-    c->div = SCALAR;
-    c->rnd = 0.25f;
-    c->srr = 0.25f;
-    tSampleReducer_init(&c->sReducer);
-    c->gain = (c->div / SCALAR) * 0.7f + 0.3f;
+    tCrusher_freeFromPool(cr, &leaf.mempool);
 }
 
 void    tCrusher_free    (tCrusher* const cr)
 {
-    _tCrusher* c = *cr;
-    tSampleReducer_free(&c->sReducer);
-    leaf_free((char*)c);
+    tCrusher_freeFromPool(cr, &leaf.mempool);
 }
 
 void    tCrusher_initToPool   (tCrusher* const cr, tMempool* const mp)
--- a/leaf/Src/leaf-dynamics.c
+++ b/leaf/Src/leaf-dynamics.c
@@ -43,24 +43,12 @@
  */
 void    tCompressor_init(tCompressor* const comp)
 {
-    _tCompressor* c = *comp = (_tCompressor*) leaf_alloc(sizeof(_tCompressor));
-    
-    c->tauAttack = 100;
-    c->tauRelease = 100;
-    
-    c->isActive = OFALSE;
-    
-    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
+    tCompressor_initToPool(comp, &leaf.mempool);
 }
 
 void tCompressor_free(tCompressor* const comp)
 {
-    _tCompressor* c = *comp;
-    
-    leaf_free((char*)c);
+    tCompressor_freeFromPool(comp, &leaf.mempool);
 }
 
 void    tCompressor_initToPool  (tCompressor* const comp, tMempool* const mp)
@@ -140,21 +128,12 @@
 
 void    tFeedbackLeveler_init(tFeedbackLeveler* const fb, float targetLevel, float factor, float strength, int mode)
 {
-    _tFeedbackLeveler* p = *fb = (_tFeedbackLeveler*) leaf_alloc(sizeof(_tFeedbackLeveler));
-    
-    p->curr=0.0f;
-    p->targetLevel=targetLevel;
-    tPowerFollower_init(&p->pwrFlw,factor);
-    p->mode=mode;
-    p->strength=strength;
+    tFeedbackLeveler_initToPool(fb, targetLevel, factor, strength, mode, &leaf.mempool);
 }
 
 void tFeedbackLeveler_free(tFeedbackLeveler* const fb)
 {
-    _tFeedbackLeveler* p = *fb;
-    
-    tPowerFollower_free(&p->pwrFlw);
-    leaf_free((char*)p);
+    tFeedbackLeveler_freeFromPool(fb, &leaf.mempool);
 }
 
 void    tFeedbackLeveler_initToPool     (tFeedbackLeveler* const fb, float targetLevel, float factor, float strength, int mode, tMempool* const mp)
--- a/leaf/Src/leaf-effects.c
+++ b/leaf/Src/leaf-effects.c
@@ -335,25 +335,12 @@
 
 void   tVocoder_init        (tVocoder* const voc)
 {
-    _tVocoder* v = *voc = (_tVocoder*) leaf_alloc(sizeof(_tVocoder));
-    
-    v->param[0] = 0.33f;  //input select
-    v->param[1] = 0.50f;  //output dB
-    v->param[2] = 0.40f;  //hi thru
-    v->param[3] = 0.40f;  //hi band
-    v->param[4] = 0.16f;  //envelope
-    v->param[5] = 0.55f;  //filter q
-    v->param[6] = 0.6667f;//freq range
-    v->param[7] = 0.33f;  //num bands
-    
-    tVocoder_update(voc);
+    tVocoder_initToPool(voc, &leaf.mempool);
 }
 
 void tVocoder_free (tVocoder* const voc)
 {
-    _tVocoder* v = *voc;
-    
-    leaf_free((char*)v);
+    tVocoder_freeFromPool(voc, &leaf.mempool);
 }
 
 void    tVocoder_initToPool     (tVocoder* const voc, tMempool* const mp)
@@ -570,7 +557,7 @@
 {
     _tMempool* m = *mp;
     _tRosenbergGlottalPulse* g = *gp;
-    mpool_free(g, m);
+    mpool_free((char*)g, m);
 }
 
 float   tRosenbergGlottalPulse_tick           (tRosenbergGlottalPulse* const gp)
@@ -651,20 +638,12 @@
 // init
 void     tSOLAD_init(tSOLAD* const wp)
 {
-    _tSOLAD* w = *wp = (_tSOLAD*) leaf_calloc(sizeof(_tSOLAD));
-    
-    w->pitchfactor = 1.;
-    w->delaybuf = (float*) leaf_calloc(sizeof(float) * (LOOPSIZE+16));
-    
-    solad_init(w);
+    tSOLAD_initToPool(wp, &leaf.mempool);
 }
 
 void tSOLAD_free(tSOLAD* const wp)
 {
-    _tSOLAD* w = *wp;
-    
-    leaf_free((char*)w->delaybuf);
-    leaf_free((char*)w);
+    tSOLAD_freeFromPool(wp, &leaf.mempool);
 }
 
 void    tSOLAD_initToPool       (tSOLAD* const wp, tMempool* const mp)
@@ -1020,42 +999,18 @@
 
 void tPitchShift_init (tPitchShift* const psr, tPeriodDetection* pd, float* out, int bufSize)
 {
-    _tPitchShift* ps = *psr = (_tPitchShift*) leaf_calloc(sizeof(_tPitchShift));
-    _tPeriodDetection* p = *pd;
-    
-    ps->p = pd;
-    
-    ps->outBuffer = out;
-    ps->bufSize = bufSize;
-    ps->frameSize = p->frameSize;
-    ps->framesPerBuffer = ps->bufSize / ps->frameSize;
-    ps->curBlock = 1;
-    ps->lastBlock = 0;
-    ps->index = 0;
-    ps->pitchFactor = 1.0f;
-    
-    tSOLAD_init(&ps->sola);
-    
-    tHighpass_init(&ps->hp, HPFREQ);
-    
-    tSOLAD_setPitchFactor(&ps->sola, DEFPITCHRATIO);
+    tPitchShift_freeFromPool(psr, &leaf.mempool);
 }
 
 void tPitchShift_free(tPitchShift* const psr)
 {
-    _tPitchShift* ps = *psr;
-    
-    tSOLAD_free(&ps->sola);
-    tHighpass_free(&ps->hp);
-    leaf_free((char*)ps);
+    tPitchShift_freeFromPool(psr, &leaf.mempool);
 }
 
 void    tPitchShift_initToPool      (tPitchShift* const psr, tPeriodDetection* const pd, float* out, int bufSize, tMempool* const mp)
 {
     _tMempool* m = *mp;
-
     _tPitchShift* ps = *psr = (_tPitchShift*) mpool_calloc(sizeof(_tPitchShift), m);
-
     _tPeriodDetection* p = *pd;
     
     ps->p = pd;
@@ -1217,6 +1172,7 @@
 {
     _tMempool* m = *mp;
     _tRetune* r = *rt = (_tRetune*) mpool_alloc(sizeof(_tRetune), m);
+    r->mempool = mp;
     
     r->bufSize = bufSize;
     r->frameSize = frameSize;
@@ -1285,27 +1241,37 @@
 {
     _tRetune* r = *rt;
     
-    for (int i = 0; i < r->numVoices; ++i)
-    {
-        tPitchShift_free(&r->ps[i]);
-        leaf_free((char*)r->outBuffers[i]);
-    }
-    leaf_free((char*)r->tickOutput);
-    leaf_free((char*)r->pitchFactor);
-    leaf_free((char*)r->ps);
-    leaf_free((char*)r->outBuffers);
+    int bufSize = r->bufSize;
+    int frameSize = r->frameSize;
+    tMempool* mempool = r->mempool;
     
-    r->numVoices = numVoices;
+    tRetune_freeFromPool(rt, mempool);
+    tRetune_initToPool(rt, numVoices, bufSize, frameSize, mempool);
     
-    r->outBuffers = (float**) leaf_alloc(sizeof(float*) * r->numVoices);
-    r->ps = (tPitchShift*) leaf_alloc(sizeof(tPitchShift) * r->numVoices);
-    r->pitchFactor = (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);
-        tPitchShift_init(&r->ps[i], &r->pd, r->outBuffers[i], r->bufSize);
-    }
+    // Could this run into fragmentation problems?
+    // Trying out reiniting the whole object (above) since it shouldn't be much slower anyway.
+    
+//    for (int i = 0; i < r->numVoices; ++i)
+//    {
+//        tPitchShift_freeFromPool(&r->ps[i], r->mempool);
+//        mpool_free((char*)r->outBuffers[i], *r->mempool);
+//    }
+//    mpool_free((char*)r->tickOutput, *r->mempool);
+//    mpool_free((char*)r->pitchFactor, *r->mempool);
+//    mpool_free((char*)r->ps, *r->mempool);
+//    mpool_free((char*)r->outBuffers, *r->mempool);
+//
+//    r->numVoices = numVoices;
+//
+//    r->outBuffers = (float**) mpool_alloc(sizeof(float*) * r->numVoices, *r->mempool);
+//    r->ps = (tPitchShift*) mpool_alloc(sizeof(tPitchShift) * r->numVoices, *r->mempool);
+//    r->pitchFactor = (float*) mpool_alloc(sizeof(float) * r->numVoices, *r->mempool);
+//    r->tickOutput = (float*) mpool_alloc(sizeof(float) * r->numVoices, *r->mempool);
+//    for (int i = 0; i < r->numVoices; ++i)
+//    {
+//        r->outBuffers[i] = (float*) mpool_alloc(sizeof(float) * r->bufSize, *r->mempool);
+//        tPitchShift_initToPool(&r->ps[i], &r->pd, r->outBuffers[i], r->bufSize, r->mempool);
+//    }
 }
 
 void tRetune_setPitchFactors(tRetune* const rt, float pf)
@@ -1390,6 +1356,7 @@
 {
     _tMempool* m = *mp;
     _tAutotune* r = *rt = (_tAutotune*) mpool_alloc(sizeof(_tAutotune), m);
+    r->mempool = mp;
     
     r->bufSize = bufSize;
     r->frameSize = frameSize;
@@ -1462,27 +1429,36 @@
 {
     _tAutotune* r = *rt;
     
-    for (int i = 0; i < r->numVoices; ++i)
-    {
-        tPitchShift_free(&r->ps[i]);
-        leaf_free((char*)r->outBuffers[i]);
-    }
-    leaf_free((char*)r->tickOutput);
-    leaf_free((char*)r->freq);
-    leaf_free((char*)r->ps);
-    leaf_free((char*)r->outBuffers);
+    int bufSize = r->bufSize;
+    int frameSize = r->frameSize;
+    tMempool* mempool = r->mempool;
     
-    r->numVoices = numVoices;
+    tAutotune_freeFromPool(rt, mempool);
+    tAutotune_initToPool(rt, numVoices, bufSize, frameSize, mempool);
     
-    r->outBuffers = (float**) leaf_alloc(sizeof(float*) * r->numVoices);
-    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);
-        tPitchShift_init(&r->ps[i], &r->pd, r->outBuffers[i], r->bufSize);
-    }
+    // Could this run into fragmentation problems?
+    // Trying out reiniting the whole object (above) since it shouldn't be much slower anyway.
+//    for (int i = 0; i < r->numVoices; ++i)
+//    {
+//        tPitchShift_freeFromPool(&r->ps[i], r->mempool);
+//        mpool_free((char*)r->outBuffers[i], *r->mempool);
+//    }
+//    mpool_free((char*)r->tickOutput, *r->mempool);
+//    mpool_free((char*)r->freq, *r->mempool);
+//    mpool_free((char*)r->ps, *r->mempool);
+//    mpool_free((char*)r->outBuffers, *r->mempool);
+//
+//    r->numVoices = numVoices;
+//
+//    r->outBuffers = (float**) mpool_alloc(sizeof(float*) * r->numVoices, *r->mempool);
+//    r->ps = (tPitchShift*) mpool_alloc(sizeof(tPitchShift) * r->numVoices, *r->mempool);
+//    r->freq = (float*) mpool_alloc(sizeof(float) * r->numVoices, *r->mempool);
+//    r->tickOutput = (float*) mpool_alloc(sizeof(float) * r->numVoices, *r->mempool);
+//    for (int i = 0; i < r->numVoices; ++i)
+//    {
+//        r->outBuffers[i] = (float*) mpool_alloc(sizeof(float) * r->bufSize, *r->mempool);
+//        tPitchShift_initToPool(&r->ps[i], &r->pd, r->outBuffers[i], r->bufSize, r->mempool);
+//    }
 }
 
 void tAutotune_setFreqs(tAutotune* const rt, float f)
@@ -1566,53 +1542,12 @@
 
 void tFormantShifter_init(tFormantShifter* const fsr, int order)
 {
-    _tFormantShifter* fs = *fsr = (_tFormantShifter*) leaf_alloc(sizeof(_tFormantShifter));
-    
-    fs->ford = order;
-    fs->fk = (float*) leaf_calloc(sizeof(float) * fs->ford);
-    fs->fb = (float*) leaf_calloc(sizeof(float) * fs->ford);
-    fs->fc = (float*) leaf_calloc(sizeof(float) * fs->ford);
-    fs->frb = (float*) leaf_calloc(sizeof(float) * fs->ford);
-    fs->frc = (float*) leaf_calloc(sizeof(float) * fs->ford);
-    fs->fsig = (float*) leaf_calloc(sizeof(float) * fs->ford);
-    fs->fsmooth = (float*) leaf_calloc(sizeof(float) * fs->ford);
-    fs->ftvec = (float*) leaf_calloc(sizeof(float) * fs->ford);
-    fs->fbuff = (float*) leaf_calloc(sizeof(float*) * fs->ford);
-
-    fs->falph = powf(0.001f, 40.0f * leaf.invSampleRate);
-    fs->flamb = -(0.8517f*sqrtf(atanf(0.06583f*leaf.sampleRate))-0.1916f);
-    fs->fhp = 0.0f;
-    fs->flp = 0.0f;
-    fs->flpa = powf(0.001f, 10.0f * leaf.invSampleRate);
-    fs->fmute = 1.0f;
-    fs->fmutealph = powf(0.001f, 0.5f * leaf.invSampleRate);
-    fs->cbi = 0;
-    fs->intensity = 1.0f;
-	fs->invIntensity = 1.0f;
-	tHighpass_init(&fs->hp, 10.0f);
-	tHighpass_init(&fs->hp2, 10.0f);
-	tFeedbackLeveler_init(&fs->fbl1, 0.99f, 0.005f, 0.125f, 0);
-	tFeedbackLeveler_init(&fs->fbl2, 0.99f, 0.005f, 0.125f, 0);
+    tFormantShifter_initToPool(fsr, order, &leaf.mempool);
 }
 
 void tFormantShifter_free(tFormantShifter* const fsr)
 {
-    _tFormantShifter* fs = *fsr;
-    
-    leaf_free((char*)fs->fk);
-    leaf_free((char*)fs->fb);
-    leaf_free((char*)fs->fc);
-    leaf_free((char*)fs->frb);
-    leaf_free((char*)fs->frc);
-    leaf_free((char*)fs->fsig);
-    leaf_free((char*)fs->fsmooth);
-    leaf_free((char*)fs->ftvec);
-    leaf_free((char*)fs->fbuff);
-    tHighpass_free(&fs->hp);
-    tHighpass_free(&fs->hp2);
-	tFeedbackLeveler_free(&fs->fbl1);
-	tFeedbackLeveler_free(&fs->fbl2);
-    leaf_free((char*)fs);
+    tFormantShifter_freeFromPool(fsr, &leaf.mempool);
 }
 
 void    tFormantShifter_initToPool      (tFormantShifter* const fsr, int order, tMempool* const mp)
--- a/leaf/Src/leaf-electrical.c
+++ b/leaf/Src/leaf-electrical.c
@@ -166,19 +166,15 @@
 //WDF
 void tWDF_init(tWDF* const wdf, WDFComponentType type, float value, tWDF* const rL, tWDF* const rR)
 {
-    *wdf = (_tWDF*) leaf_alloc(sizeof(_tWDF));
-    
-    wdf_init(wdf, type, value, rL, rR);
+    tWDF_initToPool(wdf, type, value, rL, rR, &leaf.mempool);
 }
 
 void tWDF_free(tWDF* const wdf)
 {
-    _tWDF* r = *wdf;
-    
-    leaf_free((char*)r);
+    tWDF_freeFromPool(wdf, &leaf.mempool);
 }
 
-void    tWDF_initToPool             (tWDF* const wdf, WDFComponentType type, float value, tWDF* const rL, tWDF* const rR, tMempool* const mp)
+void    tWDF_initToPool(tWDF* const wdf, WDFComponentType type, float value, tWDF* const rL, tWDF* const rR, tMempool* const mp)
 {
     _tMempool* m = *mp;
     *wdf = (_tWDF*) mpool_alloc(sizeof(_tWDF), m);
--- a/leaf/Src/leaf-envelopes.c
+++ b/leaf/Src/leaf-envelopes.c
@@ -26,48 +26,12 @@
 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Envelope ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
 void    tEnvelope_init(tEnvelope* const envlp, float attack, float decay, int loop)
 {
-    _tEnvelope* env = *envlp = (_tEnvelope*) leaf_alloc(sizeof(_tEnvelope));
-    
-    env->exp_buff = __leaf_table_exp_decay;
-    env->inc_buff = __leaf_table_attack_decay_inc;
-    env->buff_size = sizeof(__leaf_table_exp_decay);
-    
-    env->loop = loop;
-    
-    if (attack > 8192.0f)
-        attack = 8192.0f;
-    if (attack < 0.0f)
-        attack = 0.0f;
-    
-    if (decay > 8192.0f)
-        decay = 8192.0f;
-    if (decay < 0.0f)
-        decay = 0.0f;
-    
-    int16_t attackIndex = ((int16_t)(attack * 8.0f))-1;
-    int16_t decayIndex = ((int16_t)(decay * 8.0f))-1;
-    int16_t rampIndex = ((int16_t)(2.0f * 8.0f))-1;
-    
-    if (attackIndex < 0)
-        attackIndex = 0;
-    if (decayIndex < 0)
-        decayIndex = 0;
-    if (rampIndex < 0)
-        rampIndex = 0;
-    
-    env->inRamp = OFALSE;
-    env->inAttack = OFALSE;
-    env->inDecay = OFALSE;
-    
-    env->attackInc = env->inc_buff[attackIndex];
-    env->decayInc = env->inc_buff[decayIndex];
-    env->rampInc = env->inc_buff[rampIndex];
+    tEnvelope_initToPool(envlp, attack, decay, loop, &leaf.mempool);
 }
 
 void tEnvelope_free(tEnvelope* const envlp)
 {
-    _tEnvelope* env = *envlp;
-    leaf_free((char*)env);
+    tEnvelope_freeFromPool(envlp, &leaf.mempool);
 }
 
 void    tEnvelope_initToPool    (tEnvelope* const envlp, float attack, float decay, int loop, tMempool* const mp)
@@ -1203,31 +1167,12 @@
 /* Ramp */
 void    tRamp_init(tRamp* const r, float time, int samples_per_tick)
 {
-    _tRamp* ramp = *r = (_tRamp*) leaf_alloc(sizeof(_tRamp));
-    
-    ramp->inv_sr_ms = 1.0f/(leaf.sampleRate*0.001f);
-	ramp->minimum_time = ramp->inv_sr_ms * samples_per_tick;
-    ramp->curr = 0.0f;
-    ramp->dest = 0.0f;
-    
-    if (time < ramp->minimum_time)
-    {
-        ramp->time = ramp->minimum_time;
-    }
-    else
-    {
-        ramp->time = time;
-    }
-    ramp->factor = (1.0f / ramp->time) * ramp->inv_sr_ms;
-    ramp->samples_per_tick = samples_per_tick;
-    ramp->inc = ((ramp->dest - ramp->curr) / ramp->time * ramp->inv_sr_ms) * (float)ramp->samples_per_tick;
+    tRamp_initToPool(r, time, samples_per_tick, &leaf.mempool);
 }
 
 void tRamp_free(tRamp* const r)
 {
-    _tRamp* ramp = *r;
-    
-    leaf_free((char*)ramp);
+    tRamp_freeFromPool(r, &leaf.mempool);
 }
 
 void    tRamp_initToPool    (tRamp* const r, float time, int samples_per_tick, tMempool* const mp)
@@ -1470,21 +1415,12 @@
 /* Exponential Smoother */
 void    tExpSmooth_init(tExpSmooth* const expsmooth, float val, float factor)
 {	// factor is usually a value between 0 and 0.1. Lower value is slower. 0.01 for example gives you a smoothing time of about 10ms
-    _tExpSmooth* smooth = *expsmooth = (_tExpSmooth*) leaf_alloc(sizeof(_tExpSmooth));
-    
-	smooth->curr=val;
-	smooth->dest=val;
-	if (factor<0) factor=0;
-	if (factor>1) factor=1;
-	smooth->factor=factor;
-	smooth->oneminusfactor=1.0f-factor;
+    tExpSmooth_initToPool(expsmooth, val, factor, &leaf.mempool);
 }
 
 void tExpSmooth_free(tExpSmooth* const expsmooth)
 {
-    _tExpSmooth* smooth = *expsmooth;
-    
-    leaf_free((char*)smooth);
+    tExpSmooth_freeFromPool(expsmooth, &leaf.mempool);
 }
 
 void    tExpSmooth_initToPool   (tExpSmooth* const expsmooth, float val, float factor, tMempool* const mp)
--- a/leaf/Src/leaf-filters.c
+++ b/leaf/Src/leaf-filters.c
@@ -23,21 +23,12 @@
 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ OnePole Filter ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
 void    tAllpass_init(tAllpass* const ft, float initDelay, uint32_t maxDelay)
 {
-    _tAllpass* f = *ft = (_tAllpass*) leaf_alloc(sizeof(_tAllpass));
-    
-    f->gain = 0.7f;
-    
-    f->lastOut = 0.0f;
-    
-    tLinearDelay_init(&f->delay, initDelay, maxDelay);
+    tAllpass_initToPool(ft, initDelay, maxDelay, &leaf.mempool);
 }
 
 void tAllpass_free(tAllpass* const ft)
 {
-    _tAllpass* f = *ft;
-    
-    tLinearDelay_free(&f->delay);
-    leaf_free((char*)f);
+    tAllpass_freeFromPool(ft, &leaf.mempool);
 }
 
 void    tAllpass_initToPool     (tAllpass* const ft, float initDelay, uint32_t maxDelay, tMempool* const mp)
@@ -91,22 +82,12 @@
 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ OnePole Filter ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
 void    tOnePole_init(tOnePole* const ft, float freq)
 {
-    _tOnePole* f = *ft = (_tOnePole*) leaf_alloc(sizeof(_tOnePole));
-    
-    f->gain = 1.0f;
-    f->a0 = 1.0;
-    
-    tOnePole_setFreq(ft, freq);
-    
-    f->lastIn = 0.0f;
-    f->lastOut = 0.0f;
+    tOnePole_initToPool(ft, freq, &leaf.mempool);
 }
 
 void    tOnePole_free(tOnePole* const ft)
 {
-    _tOnePole* f = *ft;
-    
-    leaf_free((char*)f);
+    tOnePole_freeFromPool(ft, &leaf.mempool);
 }
 
 void    tOnePole_initToPool     (tOnePole* const ft, float freq, tMempool* const mp)
@@ -195,21 +176,12 @@
 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ TwoPole Filter ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
 void    tTwoPole_init(tTwoPole* const ft)
 {
-    _tTwoPole* f = *ft = (_tTwoPole*) leaf_alloc(sizeof(_tTwoPole));
-    
-    f->gain = 1.0f;
-    f->a0 = 1.0;
-    f->b0 = 1.0;
-    
-    f->lastOut[0] = 0.0f;
-    f->lastOut[1] = 0.0f;
+    tTwoPole_initToPool(ft, &leaf.mempool);
 }
 
 void    tTwoPole_free(tTwoPole* const ft)
 {
-    _tTwoPole* f = *ft;
-    
-    leaf_free((char*)f);
+    tTwoPole_freeFromPool(ft, &leaf.mempool);
 }
 
 void    tTwoPole_initToPool     (tTwoPole* const ft, tMempool* const mp)
@@ -323,19 +295,12 @@
 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ OneZero Filter ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
 void    tOneZero_init(tOneZero* const ft, float theZero)
 {
-    _tOneZero* f = *ft = (_tOneZero*) leaf_alloc(sizeof(_tOneZero));
-    
-    f->gain = 1.0f;
-    f->lastIn = 0.0f;
-    f->lastOut = 0.0f;
-    tOneZero_setZero(ft, theZero);
+    tOneZero_initToPool(ft, theZero, &leaf.mempool);
 }
 
 void    tOneZero_free(tOneZero* const ft)
 {
-    _tOneZero* f = *ft;
-    
-    leaf_free((char*)f);
+    tOneZero_freeFromPool(ft, &leaf.mempool);
 }
 
 void    tOneZero_initToPool     (tOneZero* const ft, float theZero, tMempool* const mp)
@@ -439,18 +404,12 @@
 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ TwoZero Filter ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
 void    tTwoZero_init(tTwoZero* const ft)
 {
-    _tTwoZero* f = *ft = (_tTwoZero*) leaf_alloc(sizeof(_tTwoZero));
-    
-    f->gain = 1.0f;
-    f->lastIn[0] = 0.0f;
-    f->lastIn[1] = 0.0f;
+    tTwoZero_initToPool(ft, &leaf.mempool);
 }
 
 void    tTwoZero_free(tTwoZero* const ft)
 {
-    _tTwoZero* f = *ft;
-    
-    leaf_free((char*)f);
+    tTwoZero_freeFromPool(ft, &leaf.mempool);
 }
 
 void    tTwoZero_initToPool     (tTwoZero* const ft, tMempool* const mp)
@@ -544,21 +503,12 @@
 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ PoleZero Filter ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
 void   tPoleZero_init(tPoleZero* const pzf)
 {
-    _tPoleZero* f = *pzf = (_tPoleZero*) leaf_alloc(sizeof(_tPoleZero));
-    
-    f->gain = 1.0f;
-    f->b0 = 1.0;
-    f->a0 = 1.0;
-    
-    f->lastIn = 0.0f;
-    f->lastOut = 0.0f;
+    tPoleZero_initToPool(pzf, &leaf.mempool);
 }
 
 void   tPoleZero_free(tPoleZero* const pzf)
 {
-    _tPoleZero* f = *pzf;
-    
-    leaf_free((char*)f);
+    tPoleZero_freeFromPool(pzf, &leaf.mempool);
 }
 
 void    tPoleZero_initToPool        (tPoleZero* const pzf, tMempool* const mp)
@@ -672,24 +622,12 @@
 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ BiQuad Filter ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
 void    tBiQuad_init(tBiQuad* const ft)
 {
-    _tBiQuad* f = *ft = (_tBiQuad*) leaf_alloc(sizeof(_tBiQuad));
-    
-    f->gain = 1.0f;
-    
-    f->b0 = 0.0f;
-    f->a0 = 0.0f;
-    
-    f->lastIn[0] = 0.0f;
-    f->lastIn[1] = 0.0f;
-    f->lastOut[0] = 0.0f;
-    f->lastOut[1] = 0.0f;
+    tBiQuad_initToPool(ft, &leaf.mempool);
 }
 
 void    tBiQuad_free(tBiQuad* const ft)
 {
-    _tBiQuad* f = *ft;
-    
-    leaf_free((char*)f);
+    tBiQuad_freeFromPool(ft, &leaf.mempool);
 }
 
 void    tBiQuad_initToPool     (tBiQuad* const ft, tMempool* const mp)
@@ -974,25 +912,12 @@
 // Efficient version of tSVF where frequency is set based on 12-bit integer input for lookup in tanh wavetable.
 void   tEfficientSVF_init(tEfficientSVF* const svff, SVFType type, uint16_t input, float Q)
 {
-    _tEfficientSVF* svf = *svff = (_tEfficientSVF*) leaf_alloc(sizeof(_tEfficientSVF));
-    
-    svf->type = type;
-    
-    svf->ic1eq = 0;
-    svf->ic2eq = 0;
-    
-    svf->g = __leaf_table_filtertan[input];
-    svf->k = 1.0f/Q;
-    svf->a1 = 1.0f/(1.0f+svf->g*(svf->g+svf->k));
-    svf->a2 = svf->g*svf->a1;
-    svf->a3 = svf->g*svf->a2;
+    tEfficientSVF_initToPool(svff, type, input, Q, &leaf.mempool);
 }
 
 void tEfficientSVF_free(tEfficientSVF* const svff)
 {
-    _tEfficientSVF* svf = *svff;
-    
-    leaf_free((char*)svf);
+    tEfficientSVF_freeFromPool(svff, &leaf.mempool);
 }
 
 void    tEfficientSVF_initToPool    (tEfficientSVF* const svff, SVFType type, uint16_t input, float Q, tMempool* const mp)
@@ -1063,20 +988,12 @@
 /* Highpass */
 void    tHighpass_init(tHighpass* const ft, float freq)
 {
-    _tHighpass* f = *ft = (_tHighpass*) leaf_alloc(sizeof(_tHighpass));
-    
-    f->R = (1.0f - (freq * leaf.twoPiTimesInvSampleRate));
-    f->ys = 0.0f;
-    f->xs = 0.0f;
-    
-    f->frequency = freq;
+    tHighpass_initToPool(ft, freq, &leaf.mempool);
 }
 
 void    tHighpass_free(tHighpass* const ft)
 {
-    _tHighpass* f = *ft;
-    
-    leaf_free((char*)f);
+    tHighpass_freeFromPool(ft, &leaf.mempool);
 }
 
 void    tHighpass_initToPool    (tHighpass* const ft, float freq, tMempool* const mp)
@@ -1130,34 +1047,12 @@
 
 void tButterworth_init(tButterworth* const ft, int N, float f1, float f2)
 {
-    _tButterworth* f = *ft = (_tButterworth*) leaf_alloc(sizeof(_tButterworth));
-    
-    f->f1 = f1;
-    f->f2 = f2;
-    f->gain = 1.0f;
-    
-    f->N = N;
-    
-    if (f->N > NUM_SVF_BW) f->N = NUM_SVF_BW;
-    
-    for(int i = 0; i < N/2; ++i)
-    {
-        tSVF_init(&f->low[i], SVFTypeHighpass, f1, 0.5f/cosf((1.0f+2.0f*i)*PI/(2*N)));
-        tSVF_init(&f->high[i], SVFTypeLowpass, f2, 0.5f/cosf((1.0f+2.0f*i)*PI/(2*N)));
-    }
+    tButterworth_initToPool(ft, N, f1, f2, &leaf.mempool);
 }
 
 void tButterworth_free(tButterworth* const ft)
 {
-    _tButterworth* f = *ft;
-    
-    for(int i = 0; i < f->N/2; ++i)
-    {
-        tSVF_free(&f->low[i]);
-        tSVF_free(&f->high[i]);
-    }
-    
-    leaf_free((char*)f);
+    tButterworth_freeFromPool(ft, &leaf.mempool);
 }
 
 void    tButterworth_initToPool     (tButterworth* const ft, int N, float f1, float f2, tMempool* const mp)
@@ -1237,20 +1132,12 @@
 
 void	tFIR_init(tFIR* const firf, float* coeffs, int numTaps)
 {
-    _tFIR* fir = *firf = (_tFIR*) leaf_alloc(sizeof(_tFIR));
-    
-    fir->numTaps = numTaps;
-    fir->coeff = coeffs;
-    fir->past = (float*)leaf_alloc(sizeof(float) * fir->numTaps);
-    for (int i = 0; i < fir->numTaps; ++i) fir->past[i] = 0.0f;
+    tFIR_initToPool(firf, coeffs, numTaps, &leaf.mempool);
 }
 
 void    tFIR_free(tFIR* const firf)
 {
-    _tFIR* fir = *firf;
-    
-    leaf_free((char*)fir->past);
-    leaf_free((char*)fir);
+    tFIR_freeFromPool(firf, &leaf.mempool);
 }
 
 void    tFIR_initToPool     (tFIR* const firf, float* coeffs, int numTaps, tMempool* const mp)
@@ -1376,11 +1263,12 @@
 {
 	tVZFilter_freeFromPool(vf, &leaf.mempool);
 }
+
 void    tVZFilter_initToPool     (tVZFilter* const vf, VZFilterType type, float freq, float bandWidth, tMempool* const mp)
 {
-
-	 _tMempool* m = *mp;
-	 _tVZFilter* f = *vf = (_tVZFilter*) mpool_alloc(sizeof(_tVZFilter), m);
+    _tMempool* m = *mp;
+    _tVZFilter* f = *vf = (_tVZFilter*) mpool_alloc(sizeof(_tVZFilter), m);
+    
 	f->fc   = freq;
 	f->type = type;
 	f->G    = ONE_OVER_SQRT2;
@@ -1397,9 +1285,9 @@
 }
 void    tVZFilter_freeFromPool   (tVZFilter* const vf, tMempool* const mp)
 {
-	 _tMempool* m = *mp;
-		 _tVZFilter* f = *vf = (_tVZFilter*) mpool_alloc(sizeof(_tVZFilter), m);
-		 mpool_free((char*)f, m);
+    _tMempool* m = *mp;
+    _tVZFilter* f = *vf;
+    mpool_free((char*)f, m);
 }
 
 void 	tVZFilter_setSampleRate  (tVZFilter* const vf, float sampleRate)
@@ -1675,7 +1563,6 @@
 }
 void    tDiodeFilter_initToPool     (tDiodeFilter* const vf, float cutoff, float resonance, tMempool* const mp)
 {
-
 	 _tMempool* m = *mp;
 	 _tDiodeFilter* f = *vf = (_tDiodeFilter*) mpool_alloc(sizeof(_tDiodeFilter), m);
 	 // initialization (the resonance factor is between 0 and 8 according to the article)
@@ -1697,9 +1584,9 @@
 }
 void    tDiodeFilter_freeFromPool   (tDiodeFilter* const vf, tMempool* const mp)
 {
-	 _tMempool* m = *mp;
-	 _tDiodeFilter* f = *vf = (_tDiodeFilter*) mpool_alloc(sizeof(_tDiodeFilter), m);
-	 mpool_free((char*)f, m);
+    _tMempool* m = *mp;
+    _tDiodeFilter* f = *vf;
+    mpool_free((char*)f, m);
 }
 
 float tanhXdX(float x)
--- a/leaf/Src/leaf-instruments.c
+++ b/leaf/Src/leaf-instruments.c
@@ -20,47 +20,12 @@
 
 void t808Cowbell_init(t808Cowbell* const cowbellInst, int useStick)
 {
-    _t808Cowbell* cowbell = *cowbellInst = (_t808Cowbell*) leaf_alloc(sizeof(_t808Cowbell));
-    
-    tSquare_init(&cowbell->p[0]);
-    tSquare_setFreq(&cowbell->p[0], 540.0f);
-    
-    tSquare_init(&cowbell->p[1]);
-    tSquare_setFreq(&cowbell->p[1], 1.48148f * 540.0f);
-    
-    cowbell->oscMix = 0.5f;
-    
-    tSVF_init(&cowbell->bandpassOsc, SVFTypeBandpass, 2500, 1.0f);
-    
-    tSVF_init(&cowbell->bandpassStick, SVFTypeBandpass, 1800, 1.0f);
-    
-    tEnvelope_init(&cowbell->envGain, 5.0f, 100.0f, OFALSE);
-    
-    tEnvelope_init(&cowbell->envFilter, 5.0, 100.0f, OFALSE);
-    
-    tHighpass_init(&cowbell->highpass, 1000.0f);
-    
-    tNoise_init(&cowbell->stick, WhiteNoise);
-    
-    tEnvelope_init(&cowbell->envStick, 5.0f, 5.0f, 0);
-    
-    cowbell->useStick = useStick;
+    t808Cowbell_initToPool(cowbellInst, useStick, &leaf.mempool);
 }
 
 void t808Cowebell_free(t808Cowbell* const cowbellInst)
 {
-    _t808Cowbell* cowbell = *cowbellInst;
-    
-    tSquare_free(&cowbell->p[0]);
-    tSquare_free(&cowbell->p[1]);
-    tSVF_free(&cowbell->bandpassOsc);
-    tSVF_free(&cowbell->bandpassStick);
-    tEnvelope_free(&cowbell->envGain);
-    tEnvelope_free(&cowbell->envFilter);
-    tHighpass_free(&cowbell->highpass);
-    tNoise_free(&cowbell->stick);
-    tEnvelope_free(&cowbell->envStick);
-    leaf_free((char*)cowbell);
+    t808Cowbell_freeFromPool(cowbellInst, &leaf.mempool);
 }
 
 void        t808Cowbell_initToPool      (t808Cowbell* const cowbellInst, int useStick, tMempool* const mp)
@@ -185,58 +150,12 @@
 
 void t808Hihat_init(t808Hihat* const hihatInst)
 {
-    _t808Hihat* hihat = *hihatInst = (_t808Hihat*) leaf_alloc(sizeof(_t808Hihat));
-    
-    for (int i = 0; i < 6; i++)
-    {
-        tSquare_init(&hihat->p[i]);
-    }
-    
-    tNoise_init(&hihat->stick, PinkNoise);
-    tNoise_init(&hihat->n, WhiteNoise);
-    
-    // need to fix SVF to be generic
-    tSVF_init(&hihat->bandpassStick, SVFTypeBandpass,2500.0f,1.2f);
-    tSVF_init(&hihat->bandpassOsc, SVFTypeBandpass,3500.0f,0.3f);
-    
-    tEnvelope_init(&hihat->envGain, 0.0f, 50.0f, OFALSE);
-    tEnvelope_init(&hihat->envStick, 0.0f, 7.0f, OFALSE);
-    
-    
-    tHighpass_init(&hihat->highpass, 7000.0f);
-    
-    hihat->freq = 40.0f;
-    hihat->stretch = 0.0f;
-    
-    tSquare_setFreq(&hihat->p[0], 2.0f * hihat->freq);
-    tSquare_setFreq(&hihat->p[1], 3.00f * hihat->freq);
-    tSquare_setFreq(&hihat->p[2], 4.16f * hihat->freq);
-    tSquare_setFreq(&hihat->p[3], 5.43f * hihat->freq);
-    tSquare_setFreq(&hihat->p[4], 6.79f * hihat->freq);
-    tSquare_setFreq(&hihat->p[5], 8.21f * hihat->freq);
+    t808Hihat_initToPool(hihatInst, &leaf.mempool);
 }
 
 void t808Hihat_free(t808Hihat* const hihatInst)
 {
-    _t808Hihat* hihat = *hihatInst;
-    
-    for (int i = 0; i < 6; i++)
-    {
-        tSquare_free(&hihat->p[i]);
-    }
-    
-    tNoise_free(&hihat->stick);
-    tNoise_free(&hihat->n);
-    
-    // need to fix SVF to be generic
-    tSVF_free(&hihat->bandpassStick);
-    tSVF_free(&hihat->bandpassOsc);
-    tEnvelope_free(&hihat->envGain);
-    tEnvelope_free(&hihat->envStick);
-    
-    tHighpass_free(&hihat->highpass);
-    
-    leaf_free((char*)hihat);
+    t808Hihat_freeFromPool(hihatInst, &leaf.mempool);
 }
 
 void    t808Hihat_initToPool  (t808Hihat* const hihatInst, tMempool* const mp)
@@ -402,53 +321,14 @@
 
 // ----------------- SNARE ----------------------------//
 
-void t808Snare_init(t808Snare* const snareInst)
+void t808Snare_init (t808Snare* const snareInst)
 {
-    _t808Snare* snare = *snareInst = (_t808Snare*) leaf_alloc(sizeof(_t808Snare));
-    
-    float ratio[2] = {1.0, 1.5};
-    for (int i = 0; i < 2; i++)
-    {
-        tTriangle_init(&snare->tone[i]);
-        
-        tTriangle_setFreq(&snare->tone[i], ratio[i] * 400.0f);
-        tSVF_init(&snare->toneLowpass[i], SVFTypeLowpass, 4000, 1.0f);
-        tEnvelope_init(&snare->toneEnvOsc[i], 0.0f, 50.0f, OFALSE);
-        tEnvelope_init(&snare->toneEnvGain[i], 1.0f, 150.0f, OFALSE);
-        tEnvelope_init(&snare->toneEnvFilter[i], 1.0f, 2000.0f, OFALSE);
-        
-        snare->toneGain[i] = 0.5f;
-    }
-    
-    snare->tone1Freq = ratio[0] * 100.0f;
-    snare->tone2Freq = ratio[1] * 100.0f;
-    snare->noiseFilterFreq = 3000.0f;
-    tNoise_init(&snare->noiseOsc, WhiteNoise);
-    tSVF_init(&snare->noiseLowpass, SVFTypeLowpass, 12000.0f, 0.8f);
-    tEnvelope_init(&snare->noiseEnvGain, 0.0f, 100.0f, OFALSE);
-    tEnvelope_init(&snare->noiseEnvFilter, 0.0f, 1000.0f, OFALSE);
-    snare->noiseGain = 1.0f;
+    t808Snare_initToPool(snareInst, &leaf.mempool);
 }
 
-void        t808Snare_free                  (t808Snare* const snareInst)
+void t808Snare_free (t808Snare* const snareInst)
 {
-    _t808Snare* snare = *snareInst;
-    
-    for (int i = 0; i < 2; i++)
-    {
-        tTriangle_free(&snare->tone[i]);
-        tSVF_free(&snare->toneLowpass[i]);
-        tEnvelope_free(&snare->toneEnvOsc[i]);
-        tEnvelope_free(&snare->toneEnvGain[i]);
-        tEnvelope_free(&snare->toneEnvFilter[i]);
-    }
-    
-    tNoise_free(&snare->noiseOsc);
-    tSVF_free(&snare->noiseLowpass);
-    tEnvelope_free(&snare->noiseEnvGain);
-    tEnvelope_free(&snare->noiseEnvFilter);
-    
-    leaf_free((char*)snare);
+    t808Snare_freeFromPool(snareInst, &leaf.mempool);
 }
 
 void    t808Snare_initToPool    (t808Snare* const snareInst, tMempool* const mp)
@@ -593,40 +473,17 @@
 
 // ----------------- KICK ----------------------------//
 
-void        t808Kick_init        			(t808Kick* const kickInst)
+void t808Kick_init (t808Kick* const kickInst)
 {
-    _t808Kick* kick = *kickInst = (_t808Kick*) leaf_alloc(sizeof(_t808Kick));
-    
-	tCycle_init(&kick->tone);
-	kick->toneInitialFreq = 40.0f;
-	kick->sighAmountInHz = 7.0f;
-	kick->chirpRatioMinusOne = 3.3f;
-	tCycle_setFreq(&kick->tone, 50.0f);
-	tSVF_init(&kick->toneLowpass, SVFTypeLowpass, 2000.0f, 0.5f);
-	tEnvelope_init(&kick->toneEnvOscChirp, 0.0f, 20.0f, OFALSE);
-	tEnvelope_init(&kick->toneEnvOscSigh, 0.0f, 2500.0f, OFALSE);
-	tEnvelope_init(&kick->toneEnvGain, 0.0f, 800.0f, OFALSE);
-	tNoise_init(&kick->noiseOsc, PinkNoise);
-	tEnvelope_init(&kick->noiseEnvGain, 0.0f, 1.0f, OFALSE);
-	kick->noiseGain = 0.3f;
+    t808Kick_initToPool(kickInst, &leaf.mempool);
 }
 
-void        t808Kick_free                  (t808Kick* const kickInst)
+void t808Kick_free (t808Kick* const kickInst)
 {
-    _t808Kick* kick = *kickInst;
-    
-	tCycle_free(&kick->tone);
-	tSVF_free(&kick->toneLowpass);
-	tEnvelope_free(&kick->toneEnvOscChirp);
-	tEnvelope_free(&kick->toneEnvOscSigh);
-	tEnvelope_free(&kick->toneEnvGain);
-	tNoise_free(&kick->noiseOsc);
-	tEnvelope_free(&kick->noiseEnvGain);
-    
-    leaf_free((char*)kick);
+    t808Kick_freeFromPool(kickInst, &leaf.mempool);
 }
 
-void    t808Kick_initToPool (t808Kick* const kickInst, tMempool* const mp)
+void t808Kick_initToPool (t808Kick* const kickInst, tMempool* const mp)
 {
     _tMempool* m = *mp;
     _t808Kick* kick = *kickInst = (_t808Kick*) mpool_alloc(sizeof(_t808Kick), m);
--- a/leaf/Src/leaf-midi.c
+++ b/leaf/Src/leaf-midi.c
@@ -22,21 +22,12 @@
 
 void tStack_init(tStack* const stack)
 {
-    _tStack* ns = *stack = (_tStack*) leaf_alloc(sizeof(_tStack));
-    
-    ns->ordered = OFALSE;
-    ns->size = 0;
-    ns->pos = 0;
-    ns->capacity = STACK_SIZE;
-    
-    for (int i = 0; i < STACK_SIZE; i++) ns->data[i] = -1;
+    tStack_initToPool(stack, &leaf.mempool);
 }
 
 void tStack_free(tStack* const stack)
 {
-    _tStack* ns = *stack;
-    
-    leaf_free((char*)ns);
+    tStack_freeFromPool(stack, &leaf.mempool);
 }
 
 void    tStack_initToPool           (tStack* const stack, tMempool* const mp)
@@ -272,68 +263,12 @@
 // POLY
 void tPoly_init(tPoly* const polyh, int maxNumVoices)
 {
-    _tPoly* poly = *polyh = (_tPoly*) leaf_alloc(sizeof(_tPoly));
-    
-    poly->numVoices = maxNumVoices;
-    poly->maxNumVoices = maxNumVoices;
-    poly->lastVoiceToChange = 0;
-    
-    // Arp mode stuff
-    poly->currentVoice = 0;
-    poly->maxLength = 128;
-    poly->currentNote = -1;
-    
-    //default learned CCs and notes are just the CCs 1-128 - notes are skipped
-    for (int i = 0; i < 128; i++)
-    {
-        poly->notes[i][0] = 0;
-        poly->notes[i][1] = -1;
-    }
-    
-    poly->glideTime = 5.0f;
-    
-    poly->ramps = (tRamp*) leaf_alloc(sizeof(tRamp) * poly->maxNumVoices);
-    poly->rampVals = (float*) leaf_alloc(sizeof(float) * poly->maxNumVoices);
-    poly->firstReceived = (oBool*) leaf_alloc(sizeof(oBool) * poly->maxNumVoices);
-    poly->voices = (int**) leaf_alloc(sizeof(int*) * poly->maxNumVoices);
-    
-    for (int i = 0; i < poly->maxNumVoices; ++i)
-    {
-        poly->voices[i] = (int*) leaf_alloc(sizeof(int) * 2);
-        poly->voices[i][0] = -1;
-        poly->firstReceived[i] = OFALSE;
-        
-        tRamp_init(&poly->ramps[i], poly->glideTime, 1);
-    }
-    
-    poly->pitchBend = 0.0f;
-    
-    tRamp_init(&poly->pitchBendRamp, 1.0f, 1);
-    tStack_init(&poly->stack);
-    tStack_init(&poly->orderStack);
-    
-    poly->pitchGlideIsActive = OFALSE;
+    tPoly_initToPool(polyh, maxNumVoices, &leaf.mempool);
 }
 
 void tPoly_free(tPoly* const polyh)
 {
-    _tPoly* poly = *polyh;
-    
-    for (int i = 0; i < poly->maxNumVoices; i++)
-    {
-        tRamp_free(&poly->ramps[i]);
-        leaf_free((char*)poly->voices[i]);
-    }
-    tRamp_free(&poly->pitchBendRamp);
-    tStack_free(&poly->stack);
-    tStack_free(&poly->orderStack);
-    
-    leaf_free((char*)poly->voices);
-    leaf_free((char*)poly->ramps);
-    leaf_free((char*)poly->rampVals);
-    leaf_free((char*)poly->firstReceived);
-    
-    leaf_free((char*)poly);
+    tPoly_freeFromPool(polyh, &leaf.mempool);
 }
 
 void    tPoly_initToPool            (tPoly* const polyh, int maxNumVoices, tMempool* const mp)
--- a/leaf/Src/leaf-physical.c
+++ b/leaf/Src/leaf-physical.c
@@ -673,17 +673,12 @@
 
 void    tReedTable_init      (tReedTable* const pm, float offset, float slope)
 {
-    _tReedTable* p = *pm = (_tReedTable*) leaf_alloc(sizeof(_tReedTable));
-    
-    p->offset = offset;
-    p->slope = slope;
+    tReedTable_initToPool(pm, offset, slope, &leaf.mempool);
 }
 
 void    tReedTable_free      (tReedTable* const pm)
 {
-    _tReedTable* p = *pm;
-    
-    leaf_free((char*)p);
+    tReedTable_freeFromPool(pm, &leaf.mempool);
 }
 
 void    tReedTable_initToPool   (tReedTable* const pm, float offset, float slope, tMempool* const mp)
--- a/leaf/Src/leaf-reverb.c
+++ b/leaf/Src/leaf-reverb.c
@@ -21,48 +21,12 @@
 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ PRCReverb ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
 void    tPRCReverb_init(tPRCReverb* const rev, float t60)
 {
-    _tPRCReverb* r = *rev = (_tPRCReverb*) leaf_alloc(sizeof(_tPRCReverb));
-    
-    if (t60 <= 0.0f) t60 = 0.001f;
-    
-    r->inv_441 = 1.0f/44100.0f;
-    
-    int lengths[4] = { 341, 613, 1557, 2137 }; // Delay lengths for 44100 Hz sample rate.
-    double scaler = leaf.sampleRate * r->inv_441;
-    
-    int delay, i;
-    if (scaler != 1.0f)
-    {
-        for (i=0; i<4; i++)
-        {
-            delay = (int) scaler * lengths[i];
-            
-            if ( (delay & 1) == 0)          delay++;
-            
-            while ( !LEAF_isPrime(delay) )  delay += 2;
-            
-            lengths[i] = delay;
-        }
-    }
-    
-    tDelay_init(&r->allpassDelays[0], lengths[0], lengths[0] * 2);
-    tDelay_init(&r->allpassDelays[1], lengths[1], lengths[1] * 2);
-    tDelay_init(&r->combDelay, lengths[2], lengths[2] * 2);
-    
-    tPRCReverb_setT60(rev, t60);
-    
-    r->allpassCoeff = 0.7f;
-    r->mix = 0.5f;
+    tPRCReverb_initToPool(rev, t60, &leaf.mempool);
 }
 
 void tPRCReverb_free(tPRCReverb* const rev)
 {
-    _tPRCReverb* r = *rev;
-    
-    tDelay_free(&r->allpassDelays[0]);
-    tDelay_free(&r->allpassDelays[1]);
-    tDelay_free(&r->combDelay);
-    leaf_free((char*)r);
+    tPRCReverb_freeFromPool(rev, &leaf.mempool);
 }
 
 void    tPRCReverb_initToPool   (tPRCReverb* const rev, float t60, tMempool* const mp)
@@ -183,61 +147,12 @@
 /* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ NReverb ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ */
 void    tNReverb_init(tNReverb* const rev, float t60)
 {
-    _tNReverb* r = *rev = (_tNReverb*) leaf_alloc(sizeof(_tNReverb));
-    
-    if (t60 <= 0.0f) t60 = 0.001f;
-    
-    r->inv_441 = 1.0f/44100.0f;
-    
-    int lengths[15] = {1433, 1601, 1867, 2053, 2251, 2399, 347, 113, 37, 59, 53, 43, 37, 29, 19}; // Delay lengths for 44100 Hz sample rate.
-    double scaler = leaf.sampleRate / 25641.0f;
-    
-    int delay, i;
-    
-    for (i=0; i < 15; i++)
-    {
-        delay = (int) scaler * lengths[i];
-        if ( (delay & 1) == 0)
-            delay++;
-        while ( !LEAF_isPrime(delay) )
-            delay += 2;
-        lengths[i] = delay;
-    }
-    
-    for ( i=0; i<6; i++ )
-    {
-    	tLinearDelay_init(&r->combDelays[i], lengths[i], lengths[i] * 2.0f);
-    	tLinearDelay_clear(&r->combDelays[i]);
-        r->combCoeffs[i] = pow(10.0, (-3 * lengths[i] * leaf.invSampleRate / t60));
-    }
-    
-    for ( i=0; i<8; i++ )
-    {
-    	tLinearDelay_init(&r->allpassDelays[i], lengths[i+6], lengths[i+6] * 2.0f);
-    	tLinearDelay_clear(&r->allpassDelays[i]);
-    }
-
-    
-    tNReverb_setT60(rev, t60);
-    r->allpassCoeff = 0.7f;
-    r->mix = 0.3f;
+    tNReverb_initToPool(rev, t60, &leaf.mempool);
 }
 
 void    tNReverb_free(tNReverb* const rev)
 {
-    _tNReverb* r = *rev;
-    
-    for (int i = 0; i < 6; i++)
-    {
-    	tLinearDelay_free(&r->combDelays[i]);
-    }
-    
-    for (int i = 0; i < 8; i++)
-    {
-    	tLinearDelay_free(&r->allpassDelays[i]);
-    }
-    
-    leaf_free((char*)r);
+    tNReverb_freeFromPool(rev, &leaf.mempool);
 }
 
 void    tNReverb_initToPool     (tNReverb* const rev, float t60, tMempool* const mp)
@@ -267,6 +182,7 @@
     for ( i=0; i<6; i++ )
     {
         tLinearDelay_initToPool(&r->combDelays[i], lengths[i], lengths[i] * 2.0f, mp);
+        tLinearDelay_clear(&r->combDelays[i]);
         r->combCoeffs[i] = pow(10.0, (-3 * lengths[i] * leaf.invSampleRate / t60));
     }
     
@@ -273,6 +189,7 @@
     for ( i=0; i<8; i++ )
     {
         tLinearDelay_initToPool(&r->allpassDelays[i], lengths[i+6], lengths[i+6] * 2.0f, mp);
+        tLinearDelay_clear(&r->allpassDelays[i]);
     }