History

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

Matthew Wang committed 5 years ago

Commit a1615ef1 parent 98913b46 files · +56 −12Browse files

Unified · Split

6 files
  1. leaf/Inc/leaf-distortion.h +10 −1
  2. leaf/Inc/leaf-global.h +2 −1
  3. leaf/Src/leaf-distortion.c +35 −8
  4. leaf/Src/leaf-mempool.c +4 −1
  5. leaf/Src/leaf.c +4 −0
  6. leaf/leaf-config.h +1 −1
leaf/Inc/leaf-distortion.h+10−1
@@ -111,6 +111,11 @@ extern "C" { · 110 unmodified lines
111111 @brief
112112 @param oversampler A pointer to the relevant tOversampler.
113113
114+ @fn void tOversampler_setRatio (tOversampler* const, int ratio)
115+ @brief
116+ @param oversampler A pointer to the relevant tOversampler.
117+ @param ratio
118+
114119 @fn int tOversampler_getLatency (tOversampler* const os)
115120 @brief
116121 @param oversampler A pointer to the relevant tOversampler.
@@ -121,7 +126,9 @@ extern "C" { · 4 unmodified lines
121126 {
122127
123128 tMempool mempool;
129+ int maxRatio;
124130 int ratio;
131+ int offset;
125132 float* pCoeffs;
126133 float* upState;
127134 float* downState;
@@ -137,7 +144,9 @@ extern "C" { · 9 unmodified lines
137144
138145 void tOversampler_upsample (tOversampler* const, float input, float* output);
139146 float tOversampler_downsample (tOversampler* const os, float* input);
140− float tOversampler_tick (tOversampler* const, float input, float* oversample, float (*effectTick)(float));
147+ float tOversampler_tick (tOversampler* const, float input, float* oversample,
148+ float (*effectTick)(float));
149+ void tOversampler_setRatio (tOversampler* const, int ratio);
141150 int tOversampler_getLatency (tOversampler* const os);
142151
143152 //==============================================================================
leaf/Inc/leaf-global.h+2−1
@@ -42,7 +42,8 @@ extern "C" { · 41 unmodified lines
4242 size_t header_size; //!< The size in bytes of memory region headers within mempools.
4343 void (*errorCallback)(LEAF* const, LEAFErrorType); //!< A pointer to the callback function for LEAF errors. Can be set by the user.
4444 int errorState[LEAFErrorNil]; //!< An array of flags that indicate which errors have occurred.
45−
45+ unsigned int allocCount; //!< A count of LEAF memory allocations.
46+ unsigned int freeCount; //!< A count of LEAF memory frees.
4647 ///@}
4748 };
4849
leaf/Src/leaf-distortion.c+35−8
@@ -77,20 +77,21 @@ void tOversampler_init (tOversampler* const osr, int ratio, int extraQuality, LE · 76 unmodified lines
7777 tOversampler_initToPool(osr, ratio, extraQuality, &leaf->mempool);
7878 }
7979
80−void tOversampler_initToPool (tOversampler* const osr, int ratio, int extraQuality, tMempool* const mp)
80+void tOversampler_initToPool (tOversampler* const osr, int maxRatio, int extraQuality, tMempool* const mp)
8181 {
8282 _tMempool* m = *mp;
83− uint8_t offset = 0;
83+ int offset = 0;
8484 if (extraQuality) offset = 6;
85− if (ratio == 2 || ratio == 4 ||
86− ratio == 8 || ratio == 16 ||
87− ratio == 32 || ratio == 64) {
88−
85+ if (maxRatio == 2 || maxRatio == 4 || maxRatio == 8 ||
86+ maxRatio == 16 || maxRatio == 32 || maxRatio == 64)
87+ {
8988 _tOversampler* os = *osr = (_tOversampler*) mpool_alloc(sizeof(_tOversampler), m);
9089 os->mempool = m;
9190
92− os->ratio = ratio;
93− int idx = (int)(log2f(os->ratio))-1+offset;
91+ os->offset = offset;
92+ os->maxRatio = maxRatio;
93+ os->ratio = os->maxRatio;
94+ int idx = (int)(log2f(os->ratio))-1+os->offset;
9495 os->numTaps = __leaf_tablesize_firNumTaps[idx];
9596 os->phaseLength = os->numTaps / os->ratio;
9697 os->pCoeffs = (float*) __leaf_tableref_firCoeffs[idx];
@@ -126,6 +127,12 @@ void tOversampler_upsample(tOversampler* const osr, float input, float* output) · 29 unmodified lines
126127 {
127128 _tOversampler* os = *osr;
128129
130+ if (os->ratio == 1)
131+ {
132+ output[0] = input;
133+ return;
134+ }
135+
129136 float *pState = os->upState; /* State pointer */
130137 float *pCoeffs = os->pCoeffs; /* Coefficient pointer */
131138 float *pStateCur;
@@ -219,6 +226,8 @@ float tOversampler_downsample(tOversampler *const osr, float* input) · 87 unmodified lines
219226 {
220227 _tOversampler* os = *osr;
221228
229+ if (os->ratio == 1) return input[0];
230+
222231 float *pState = os->downState; /* State pointer */
223232 float *pCoeffs = os->pCoeffs; /* Coefficient pointer */
224233 float *pStateCur; /* Points to the current sample of the state */
@@ -299,6 +308,24 @@ float tOversampler_downsample(tOversampler *const osr, float* input) · 74 unmodified lines
299308 return output;
300309 }
301310
311+void tOversampler_setRatio (tOversampler* const osr, int ratio)
312+{
313+ _tOversampler* os = *osr;
314+
315+ if (ratio > os->maxRatio) ratio = os->maxRatio;
316+
317+ if (ratio == 1) os->ratio = ratio;
318+ else if (ratio == 2 || ratio == 4 || ratio == 8 ||
319+ ratio == 16 || ratio == 32 || ratio == 64)
320+ {
321+ os->ratio = os->maxRatio;
322+ int idx = (int)(log2f(os->ratio))-1+os->offset;
323+ os->numTaps = __leaf_tablesize_firNumTaps[idx];
324+ os->phaseLength = os->numTaps / os->ratio;
325+ os->pCoeffs = (float*) __leaf_tableref_firCoeffs[idx];
326+ }
327+}
328+
302329 int tOversampler_getLatency(tOversampler* const osr)
303330 {
304331 _tOversampler* os = *osr;
leaf/Src/leaf-mempool.c+4−1
@@ -95,8 +95,9 @@ void leaf_pool_init(LEAF* const leaf, char* memory, size_t size) · 94 unmodified lines
9595 */
9696 char* mpool_alloc(size_t asize, _tMempool* pool)
9797 {
98+ pool->leaf->allocCount++;
9899 #if LEAF_DEBUG
99− DBG("malloc " + String(asize));
100+ DBG("alloc " + String(asize));
100101 #endif
101102 #if LEAF_USE_DYNAMIC_ALLOCATION
102103 char* temp = (char*) malloc(asize);
@@ -201,6 +202,7 @@ char* mpool_alloc(size_t asize, _tMempool* pool) · 98 unmodified lines
201202 */
202203 char* mpool_calloc(size_t asize, _tMempool* pool)
203204 {
205+ pool->leaf->allocCount++;
204206 #if LEAF_DEBUG
205207 DBG("calloc " + String(asize));
206208 #endif
@@ -307,6 +309,7 @@ char* leaf_calloc(LEAF* const leaf, size_t size) · 100 unmodified lines
307309
308310 void mpool_free(char* ptr, _tMempool* pool)
309311 {
312+ pool->leaf->freeCount++;
310313 #if LEAF_DEBUG
311314 DBG("free");
312315 #endif
leaf/Src/leaf.c+4−0
@@ -39,6 +39,10 @@ void LEAF_init(LEAF* const leaf, float sr, int blocksize, char* memory, size_t m · 38 unmodified lines
3939
4040 for (int i = 0; i < LEAFErrorNil; ++i)
4141 leaf->errorState[i] = 0;
42+
43+ leaf->allocCount = 0;
44+
45+ leaf->freeCount = 0;
4246 }
4347
4448
leaf/leaf-config.h+1−1
@@ -59,7 +59,7 @@ · 58 unmodified lines
5959 #define LEAF_USE_CMSIS 0
6060
6161 //! 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.
62−#define LEAF_USE_DYNAMIC_ALLOCATION 0
62+#define LEAF_USE_DYNAMIC_ALLOCATION 1
6363
6464 //==============================================================================
6565

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t