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
111 @brief111 @brief
112 @param oversampler A pointer to the relevant tOversampler.112 @param oversampler A pointer to the relevant tOversampler.
113 113
114 @fn void tOversampler_setRatio (tOversampler* const, int ratio)
115 @brief
116 @param oversampler A pointer to the relevant tOversampler.
117 @param ratio
118
114 @fn int tOversampler_getLatency (tOversampler* const os)119 @fn int tOversampler_getLatency (tOversampler* const os)
115 @brief120 @brief
116 @param oversampler A pointer to the relevant tOversampler.121 @param oversampler A pointer to the relevant tOversampler.
@@ -121,7 +126,9 @@ extern "C" { · 4 unmodified lines
121 {126 {
122 127
123 tMempool mempool;128 tMempool mempool;
129 int maxRatio;
124 int ratio;130 int ratio;
131 int offset;
125 float* pCoeffs;132 float* pCoeffs;
126 float* upState;133 float* upState;
127 float* downState;134 float* downState;
@@ -137,7 +144,9 @@ extern "C" { · 9 unmodified lines
137 144
138 void tOversampler_upsample (tOversampler* const, float input, float* output);145 void tOversampler_upsample (tOversampler* const, float input, float* output);
139 float tOversampler_downsample (tOversampler* const os, float* input);146 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);
141 int tOversampler_getLatency (tOversampler* const os);150 int tOversampler_getLatency (tOversampler* const os);
142 151
143 //==============================================================================152 //==============================================================================
leaf/Inc/leaf-global.h+2−1
@@ -42,7 +42,8 @@ extern "C" { · 41 unmodified lines
42 size_t header_size; //!< The size in bytes of memory region headers within mempools.42 size_t header_size; //!< The size in bytes of memory region headers within mempools.
43 void (*errorCallback)(LEAF* const, LEAFErrorType); //!< A pointer to the callback function for LEAF errors. Can be set by the user.43 void (*errorCallback)(LEAF* const, LEAFErrorType); //!< A pointer to the callback function for LEAF errors. Can be set by the user.
44 int errorState[LEAFErrorNil]; //!< An array of flags that indicate which errors have occurred.44 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.
46 ///@}47 ///@}
47 };48 };
48 49
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
77 tOversampler_initToPool(osr, ratio, extraQuality, &leaf->mempool);77 tOversampler_initToPool(osr, ratio, extraQuality, &leaf->mempool);
78}78}
79 79
80void tOversampler_initToPool (tOversampler* const osr, int ratio, int extraQuality, tMempool* const mp)80void tOversampler_initToPool (tOversampler* const osr, int maxRatio, int extraQuality, tMempool* const mp)
81{81{
82 _tMempool* m = *mp;82 _tMempool* m = *mp;
83 uint8_t offset = 0;83 int offset = 0;
84 if (extraQuality) offset = 6;84 if (extraQuality) offset = 6;
85 if (ratio == 2 || ratio == 4 ||85 if (maxRatio == 2 || maxRatio == 4 || maxRatio == 8 ||
86 ratio == 8 || ratio == 16 ||86 maxRatio == 16 || maxRatio == 32 || maxRatio == 64)
87 ratio == 32 || ratio == 64) {87 {
88
89 _tOversampler* os = *osr = (_tOversampler*) mpool_alloc(sizeof(_tOversampler), m);88 _tOversampler* os = *osr = (_tOversampler*) mpool_alloc(sizeof(_tOversampler), m);
90 os->mempool = m;89 os->mempool = m;
91 90
92 os->ratio = ratio;91 os->offset = offset;
93 int idx = (int)(log2f(os->ratio))-1+offset;92 os->maxRatio = maxRatio;
93 os->ratio = os->maxRatio;
94 int idx = (int)(log2f(os->ratio))-1+os->offset;
94 os->numTaps = __leaf_tablesize_firNumTaps[idx];95 os->numTaps = __leaf_tablesize_firNumTaps[idx];
95 os->phaseLength = os->numTaps / os->ratio;96 os->phaseLength = os->numTaps / os->ratio;
96 os->pCoeffs = (float*) __leaf_tableref_firCoeffs[idx];97 os->pCoeffs = (float*) __leaf_tableref_firCoeffs[idx];
@@ -126,6 +127,12 @@ void tOversampler_upsample(tOversampler* const osr, float input, float* output) · 29 unmodified lines
126{127{
127 _tOversampler* os = *osr;128 _tOversampler* os = *osr;
128 129
130 if (os->ratio == 1)
131 {
132 output[0] = input;
133 return;
134 }
135
129 float *pState = os->upState; /* State pointer */136 float *pState = os->upState; /* State pointer */
130 float *pCoeffs = os->pCoeffs; /* Coefficient pointer */137 float *pCoeffs = os->pCoeffs; /* Coefficient pointer */
131 float *pStateCur;138 float *pStateCur;
@@ -219,6 +226,8 @@ float tOversampler_downsample(tOversampler *const osr, float* input) · 87 unmodified lines
219{226{
220 _tOversampler* os = *osr;227 _tOversampler* os = *osr;
221 228
229 if (os->ratio == 1) return input[0];
230
222 float *pState = os->downState; /* State pointer */231 float *pState = os->downState; /* State pointer */
223 float *pCoeffs = os->pCoeffs; /* Coefficient pointer */232 float *pCoeffs = os->pCoeffs; /* Coefficient pointer */
224 float *pStateCur; /* Points to the current sample of the state */233 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
299 return output;308 return output;
300}309}
301 310
311void 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
302int tOversampler_getLatency(tOversampler* const osr)329int tOversampler_getLatency(tOversampler* const osr)
303{330{
304 _tOversampler* os = *osr;331 _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
95 */95 */
96char* mpool_alloc(size_t asize, _tMempool* pool)96char* mpool_alloc(size_t asize, _tMempool* pool)
97{97{
98 pool->leaf->allocCount++;
98#if LEAF_DEBUG99#if LEAF_DEBUG
99 DBG("malloc " + String(asize));100 DBG("alloc " + String(asize));
100#endif101#endif
101#if LEAF_USE_DYNAMIC_ALLOCATION102#if LEAF_USE_DYNAMIC_ALLOCATION
102 char* temp = (char*) malloc(asize);103 char* temp = (char*) malloc(asize);
@@ -201,6 +202,7 @@ char* mpool_alloc(size_t asize, _tMempool* pool) · 98 unmodified lines
201 */202 */
202char* mpool_calloc(size_t asize, _tMempool* pool)203char* mpool_calloc(size_t asize, _tMempool* pool)
203{204{
205 pool->leaf->allocCount++;
204#if LEAF_DEBUG206#if LEAF_DEBUG
205 DBG("calloc " + String(asize));207 DBG("calloc " + String(asize));
206#endif208#endif
@@ -307,6 +309,7 @@ char* leaf_calloc(LEAF* const leaf, size_t size) · 100 unmodified lines
307 309
308void mpool_free(char* ptr, _tMempool* pool)310void mpool_free(char* ptr, _tMempool* pool)
309{311{
312 pool->leaf->freeCount++;
310#if LEAF_DEBUG313#if LEAF_DEBUG
311 DBG("free");314 DBG("free");
312#endif315#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
39 39
40 for (int i = 0; i < LEAFErrorNil; ++i)40 for (int i = 0; i < LEAFErrorNil; ++i)
41 leaf->errorState[i] = 0;41 leaf->errorState[i] = 0;
42
43 leaf->allocCount = 0;
44
45 leaf->freeCount = 0;
42}46}
43 47
44 48
leaf/leaf-config.h+1−1
@@ -59,7 +59,7 @@ · 58 unmodified lines
59#define LEAF_USE_CMSIS 059#define LEAF_USE_CMSIS 0
60 60
61//! 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.61//! 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 062#define LEAF_USE_DYNAMIC_ALLOCATION 1
63 63
64//==============================================================================64//==============================================================================
65 65

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