shithub: leaf

Download patch

ref: a1615efe9c4e692b81776df864c1244b218e433a
parent: 98913b47156bdf7e5535557e4bccdd6d545342f3
author: Matthew Wang <mjw7@princeton.edu>
date: Fri Feb 19 06:03:02 EST 2021

rework tOversampler to init with maxRatio and let ratio be settable without reallocating memory; add alloc and free counts to leaf object

--- a/leaf/Inc/leaf-distortion.h
+++ b/leaf/Inc/leaf-distortion.h
@@ -111,6 +111,11 @@
      @brief
      @param oversampler A pointer to the relevant tOversampler.
      
+     @fn void    tOversampler_setRatio       (tOversampler* const, int ratio)
+     @brief
+     @param oversampler A pointer to the relevant tOversampler.
+     @param ratio
+     
      @fn int     tOversampler_getLatency     (tOversampler* const os)
      @brief
      @param oversampler A pointer to the relevant tOversampler.
@@ -121,7 +126,9 @@
     {
         
         tMempool mempool;
+        int maxRatio;
         int ratio;
+        int offset;
         float* pCoeffs;
         float* upState;
         float* downState;
@@ -137,7 +144,9 @@
     
     void    tOversampler_upsample       (tOversampler* const, float input, float* output);
     float   tOversampler_downsample     (tOversampler* const os, float* input);
-    float   tOversampler_tick           (tOversampler* const, float input, float* oversample, float (*effectTick)(float));
+    float   tOversampler_tick           (tOversampler* const, float input, float* oversample,
+                                         float (*effectTick)(float));
+    void    tOversampler_setRatio       (tOversampler* const, int ratio);
     int     tOversampler_getLatency     (tOversampler* const os);
     
     //==============================================================================
--- a/leaf/Inc/leaf-global.h
+++ b/leaf/Inc/leaf-global.h
@@ -42,7 +42,8 @@
         size_t header_size; //!< The size in bytes of memory region headers within mempools.
         void (*errorCallback)(LEAF* const, LEAFErrorType); //!< A pointer to the callback function for LEAF errors. Can be set by the user.
         int     errorState[LEAFErrorNil]; //!< An array of flags that indicate which errors have occurred.
-        
+        unsigned int allocCount; //!< A count of LEAF memory allocations.
+        unsigned int freeCount; //!< A count of LEAF memory frees.
         ///@}
     };
     
--- a/leaf/Src/leaf-distortion.c
+++ b/leaf/Src/leaf-distortion.c
@@ -77,20 +77,21 @@
     tOversampler_initToPool(osr, ratio, extraQuality, &leaf->mempool);
 }
 
-void tOversampler_initToPool (tOversampler* const osr, int ratio, int extraQuality, tMempool* const mp)
+void tOversampler_initToPool (tOversampler* const osr, int maxRatio, int extraQuality, tMempool* const mp)
 {
     _tMempool* m = *mp;
-    uint8_t offset = 0;
+    int offset = 0;
     if (extraQuality) offset = 6;
-    if (ratio == 2 || ratio == 4  ||
-        ratio == 8 || ratio == 16 ||
-        ratio == 32 || ratio == 64) {
-        
+    if (maxRatio == 2 || maxRatio == 4  || maxRatio == 8 ||
+        maxRatio == 16 || maxRatio == 32 || maxRatio == 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->offset = offset;
+        os->maxRatio = maxRatio;
+        os->ratio = os->maxRatio;
+        int idx = (int)(log2f(os->ratio))-1+os->offset;
         os->numTaps = __leaf_tablesize_firNumTaps[idx];
         os->phaseLength = os->numTaps / os->ratio;
         os->pCoeffs = (float*) __leaf_tableref_firCoeffs[idx];
@@ -126,6 +127,12 @@
 {
     _tOversampler* os = *osr;
     
+    if (os->ratio == 1)
+    {
+        output[0] = input;
+        return;
+    }
+    
     float *pState = os->upState;                 /* State pointer */
     float *pCoeffs = os->pCoeffs;               /* Coefficient pointer */
     float *pStateCur;
@@ -219,6 +226,8 @@
 {
     _tOversampler* os = *osr;
     
+    if (os->ratio == 1) return input[0];
+    
     float *pState = os->downState;                 /* State pointer */
     float *pCoeffs = os->pCoeffs;               /* Coefficient pointer */
     float *pStateCur;                          /* Points to the current sample of the state */
@@ -297,6 +306,24 @@
     }
     
     return output;
+}
+
+void    tOversampler_setRatio       (tOversampler* const osr, int ratio)
+{
+    _tOversampler* os = *osr;
+    
+    if (ratio > os->maxRatio) ratio = os->maxRatio;
+    
+    if (ratio == 1) os->ratio = ratio;
+    else if (ratio == 2 || ratio == 4  || ratio == 8 ||
+        ratio == 16 || ratio == 32 || ratio == 64)
+    {
+        os->ratio = os->maxRatio;
+        int idx = (int)(log2f(os->ratio))-1+os->offset;
+        os->numTaps = __leaf_tablesize_firNumTaps[idx];
+        os->phaseLength = os->numTaps / os->ratio;
+        os->pCoeffs = (float*) __leaf_tableref_firCoeffs[idx];
+    }
 }
 
 int tOversampler_getLatency(tOversampler* const osr)
--- a/leaf/Src/leaf-mempool.c
+++ b/leaf/Src/leaf-mempool.c
@@ -95,8 +95,9 @@
  */
 char* mpool_alloc(size_t asize, _tMempool* pool)
 {
+    pool->leaf->allocCount++;
 #if LEAF_DEBUG
-    DBG("malloc " + String(asize));
+    DBG("alloc " + String(asize));
 #endif
 #if LEAF_USE_DYNAMIC_ALLOCATION
     char* temp = (char*) malloc(asize);
@@ -201,6 +202,7 @@
  */
 char* mpool_calloc(size_t asize, _tMempool* pool)
 {
+    pool->leaf->allocCount++;
 #if LEAF_DEBUG
     DBG("calloc " + String(asize));
 #endif
@@ -307,6 +309,7 @@
 
 void mpool_free(char* ptr, _tMempool* pool)
 {
+    pool->leaf->freeCount++;
 #if LEAF_DEBUG
     DBG("free");
 #endif
--- a/leaf/Src/leaf.c
+++ b/leaf/Src/leaf.c
@@ -39,6 +39,10 @@
     
     for (int i = 0; i < LEAFErrorNil; ++i)
         leaf->errorState[i] = 0;
+    
+    leaf->allocCount = 0;
+    
+    leaf->freeCount = 0;
 }
 
 
--- a/leaf/leaf-config.h
+++ b/leaf/leaf-config.h
@@ -59,7 +59,7 @@
 #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
+#define LEAF_USE_DYNAMIC_ALLOCATION 1
 
 //==============================================================================