shithub: leaf

Download patch

ref: 98913b47156bdf7e5535557e4bccdd6d545342f3
parent: 4c8de0f3852178244ffaef00f6b411c8d1b8c5e2
author: Matthew Wang <mjw7@princeton.edu>
date: Thu Feb 18 12:04:54 EST 2021

add dynamic memory allocation config option, which turns mpool_alloc/free into malloc/free wrappers

--- a/leaf/Src/leaf-analysis.c
+++ b/leaf/Src/leaf-analysis.c
@@ -18,6 +18,10 @@
 
 #endif
 
+#if LEAF_DEBUG
+#include "../../TestPlugin/JuceLibraryCode/JuceHeader.h"
+#endif
+
 //===========================================================================
 /* Envelope Follower */
 //===========================================================================
@@ -52,8 +56,8 @@
     if (x < 0.0f ) x = -x;  /* Absolute value. */
     
     if (isnan(x)) return 0.0f;
-    if ((x >= e->y) && (x > e->a_thresh)) e->y = x;                      /* If we hit a peak, ride the peak to the top. */
-    else                                    e->y = e->y * e->d_coeff;    /* Else, exponential decay of output. */
+    if ((x >= e->y) && (x > e->a_thresh)) e->y = x; /* If we hit a peak, ride the peak to the top. */
+    else e->y = e->y * e->d_coeff; /* Else, exponential decay of output. */
     
     //ef->y = envelope_pow[(uint16_t)(ef->y * (float)UINT16_MAX)] * ef->d_coeff; //not quite the right behavior - too much loss of precision?
     //ef->y = powf(ef->y, 1.000009f) * ef->d_coeff;  // too expensive
@@ -961,7 +965,7 @@
 void    tZeroCrossingInfo_initToPool    (tZeroCrossingInfo* const zc, tMempool* const mp)
 {
     _tMempool* m = *mp;
-    _tZeroCrossingInfo* z = *zc = (_tZeroCrossingInfo*) mpool_alloc(sizeof(_tZeroCrossingInfo), m);
+    _tZeroCrossingInfo* z = *zc = (_tZeroCrossingInfo*) mpool_calloc(sizeof(_tZeroCrossingInfo), m);
     z->mempool = m;
     
     z->_leading_edge = INT_MIN;
@@ -1050,7 +1054,9 @@
     z->_info = (tZeroCrossingInfo*) mpool_calloc(sizeof(tZeroCrossingInfo) * z->_size, m);
 
     for (unsigned i = 0; i < z->_size; i++)
-    tZeroCrossingInfo_initToPool(&z->_info[i], mp);
+    {
+        tZeroCrossingInfo_initToPool(&z->_info[i], mp);
+    }
     
     z->_pos = 0;
     
@@ -1068,8 +1074,9 @@
     _tZeroCrossingCollector* z = *zc;
     
     for (unsigned i = 0; i < z->_size; i++)
-
-    tZeroCrossingInfo_free(&z->_info[i]);
+    {
+        tZeroCrossingInfo_free(&z->_info[i]);
+    }
     
     mpool_free((char*)z->_info, z->mempool);
     mpool_free((char*)z, z->mempool);
@@ -1198,7 +1205,7 @@
         {
             --z->_pos;
             z->_pos &= z->_mask;
-            tZeroCrossingInfo crossing = z->_info[z->_pos];
+            tZeroCrossingInfo crossing = z->_info[z->_pos & z->_mask];
             crossing->_before_crossing = z->_prev;
             crossing->_after_crossing = s;
             crossing->_peak = s;
@@ -1210,7 +1217,7 @@
         }
         else
         {
-            tZeroCrossingInfo_updatePeak(&z->_info[z->_pos], s, z->_frame);
+            tZeroCrossingInfo_updatePeak(&z->_info[z->_pos & z->_mask], s, z->_frame);
         }
         if (s > z->_peak_update)
         {
@@ -1220,7 +1227,7 @@
     else if (z->_state && (s < z->_hysteresis))
     {
         z->_state = 0;
-        z->_info[z->_pos]->_trailing_edge = z->_frame;
+        z->_info[z->_pos & z->_mask]->_trailing_edge = z->_frame;
         if (z->_peak == 0.0f)
             z->_peak = z->_peak_update;
     }
@@ -1227,7 +1234,7 @@
     
     if (z->_frame > z->_window_size * 2)
         reset(zc);
-    
+
     z->_prev = s;
 }
 
@@ -1235,7 +1242,7 @@
 {
     _tZeroCrossingCollector* z = *zc;
     
-    tZeroCrossingInfo crossing = z->_info[z->_pos];
+    tZeroCrossingInfo crossing = z->_info[z->_pos & z->_mask];
     
     crossing->_leading_edge -= n;
     if (!z->_state)
--- a/leaf/Src/leaf-distortion.c
+++ b/leaf/Src/leaf-distortion.c
@@ -80,14 +80,15 @@
 void tOversampler_initToPool (tOversampler* const osr, int ratio, int extraQuality, tMempool* const mp)
 {
     _tMempool* m = *mp;
-    _tOversampler* os = *osr = (_tOversampler*) mpool_alloc(sizeof(_tOversampler), m);
-    os->mempool = m;
-    
     uint8_t offset = 0;
     if (extraQuality) offset = 6;
     if (ratio == 2 || ratio == 4  ||
         ratio == 8 || ratio == 16 ||
         ratio == 32 || ratio == 64) {
+        
+        _tOversampler* os = *osr = (_tOversampler*) mpool_alloc(sizeof(_tOversampler), m);
+        os->mempool = m;
+        
         os->ratio = ratio;
         int idx = (int)(log2f(os->ratio))-1+offset;
         os->numTaps = __leaf_tablesize_firNumTaps[idx];
--- a/leaf/Src/leaf-effects.c
+++ b/leaf/Src/leaf-effects.c
@@ -904,9 +904,8 @@
     g->pulseLength = 0.0f;
     g->freq = 0.0f;
     g->inc = 0.0f;
-
-
 }
+
 void tRosenbergGlottalPulse_free (tRosenbergGlottalPulse* const gp)
 {
     _tRosenbergGlottalPulse* g = *gp;
@@ -977,6 +976,7 @@
     
     g->freq = freq;
     g->inc = freq * leaf->invSampleRate;
+    g->inc -= (int) g->inc;
 }
 
 void   tRosenbergGlottalPulse_setOpenLength           (tRosenbergGlottalPulse* const gp, float openLength)
@@ -1066,7 +1066,7 @@
     {
         float sample = tHighpass_tick(&w->hp, in[0]);
         w->delaybuf[0] = sample;
-        w->delaybuf[w->loopSize] = sample;   // copy one sample for interpolation
+        w->delaybuf[w->loopSize-1] = sample;   // copy one sample for interpolation
         n--;
         i++;
         in++;
--- a/leaf/Src/leaf-envelopes.c
+++ b/leaf/Src/leaf-envelopes.c
@@ -746,8 +746,6 @@
 void     tADSRT_setLeakFactor(tADSRT* const adsrenv, float leakFactor)
 {
     _tADSRT* adsr = *adsrenv;
-
-
     adsr->leakFactor = leakFactor;
 }
 
--- a/leaf/Src/leaf-mempool.c
+++ b/leaf/Src/leaf-mempool.c
@@ -48,6 +48,12 @@
 
 #endif
 
+#include <stdlib.h>
+
+#if LEAF_DEBUG
+#include "../../TestPlugin/JuceLibraryCode/JuceHeader.h"
+#endif
+
 /**
  * private function
  */
@@ -89,6 +95,23 @@
  */
 char* mpool_alloc(size_t asize, _tMempool* pool)
 {
+#if LEAF_DEBUG
+    DBG("malloc " + String(asize));
+#endif
+#if LEAF_USE_DYNAMIC_ALLOCATION
+    char* temp = (char*) malloc(asize);
+    if (temp == NULL)
+    {
+        // allocation failed, exit from the program
+        fprintf(stderr, "Out of memory.\n");
+        exit(1);
+    }
+    if (pool->leaf->clearOnAllocation > 0)
+    {
+        memset(temp, 0, asize);
+    }
+    return temp;
+#else
     // If the head is NULL, the mempool is full
     if (pool->head == NULL)
     {
@@ -169,6 +192,7 @@
     
     // Return the pool of the allocated node;
     return node_to_alloc->pool;
+#endif
 }
 
 
@@ -177,6 +201,20 @@
  */
 char* mpool_calloc(size_t asize, _tMempool* pool)
 {
+#if LEAF_DEBUG
+    DBG("calloc " + String(asize));
+#endif
+#if LEAF_USE_DYNAMIC_ALLOCATION
+    char* ret = (char*) malloc(asize);
+    if (ret == NULL)
+    {
+        // allocation failed, exit from the program
+        fprintf(stderr, "Out of memory.\n");
+        exit(1);
+    }
+    memset(ret, 0, asize);
+    return ret;
+#else
     // If the head is NULL, the mempool is full
     if (pool->head == NULL)
     {
@@ -252,26 +290,29 @@
     for (int i = 0; i < node_to_alloc->size; i++) node_to_alloc->pool[i] = 0;
     // Return the pool of the allocated node;
     return node_to_alloc->pool;
+#endif
 }
 
 char* leaf_alloc(LEAF* const leaf, size_t size)
 {
     //printf("alloc %i\n", size);
-    char* block = mpool_alloc(size, &leaf->_internal_mempool);
-    
-    return block;
+    return mpool_alloc(size, &leaf->_internal_mempool);
 }
 
 char* leaf_calloc(LEAF* const leaf, size_t size)
 {
     //printf("alloc %i\n", size);
-    char* block = mpool_calloc(size, &leaf->_internal_mempool);
-
-    return block;
+    return mpool_calloc(size, &leaf->_internal_mempool);
 }
 
 void mpool_free(char* ptr, _tMempool* pool)
 {
+#if LEAF_DEBUG
+    DBG("free");
+#endif
+#if LEAF_USE_DYNAMIC_ALLOCATION
+    free(ptr);
+#else
     //if (ptr < pool->mpool || ptr >= pool->mpool + pool->msize)
     // Get the node at the freed space
     mpool_node_t* freed_node = (mpool_node_t*) (ptr - pool->leaf->header_size);
@@ -336,6 +377,7 @@
     // Format the freed pool
     //    char* freed_pool = (char*)freed_node->pool;
     //    for (int i = 0; i < freed_node->size; i++) freed_pool[i] = 0;
+#endif
 }
 
 void leaf_free(LEAF* const leaf, char* ptr)
--- a/leaf/Src/leaf-midi.c
+++ b/leaf/Src/leaf-midi.c
@@ -633,11 +633,11 @@
     }
     poly->stealing_on = 1;
     poly->recover_stolen = 1;
-    poly->voices = (int**) mpool_alloc(sizeof(int*) * poly->maxNumVoices, m);
+    poly->voices = (int**) mpool_calloc(sizeof(int*) * poly->maxNumVoices, m);
 
     for (int i = 0; i < poly->maxNumVoices; ++i)
     {
-        poly->voices[i] = (int*) mpool_alloc(sizeof(int) * 3, m);
+        poly->voices[i] = (int*) mpool_calloc(sizeof(int) * 3, m);
         poly->voices[i][0] = -1;
     }
     tStack_initToPool(&poly->stack, mp);
--- a/leaf/leaf-config.h
+++ b/leaf/leaf-config.h
@@ -58,6 +58,9 @@
 
 #define LEAF_USE_CMSIS 0
 
+//! Use stdlib malloc() and free() internally instead of  LEAF's normal mempool behavior for when you want to avoid being limited to and managing mempool a fixed mempool size. Usage of all object remains essentially the same.
+#define LEAF_USE_DYNAMIC_ALLOCATION 0
+
 //==============================================================================
 
 #endif // LEAF_CONFIG_H_INCLUDED
--- a/leaf/leaf.h
+++ b/leaf/leaf.h
@@ -27,9 +27,9 @@
 
 #define LEAF_DEBUG 0
 
-#if LEAF_DEBUG
-#include "../LEAF_JUCEPlugin/JuceLibraryCode/JuceHeader.h"
-#endif
+//#if LEAF_DEBUG
+//#include "../TestPlugin/JuceLibraryCode/JuceHeader.h"
+//#endif
 
 #if _WIN32 || _WIN64