ref: a8e415891948209f24d7add13c3930706f895c18
parent: c6f34c374aac46d33bf9b9abc2e556033aa9194a
author: Matthew Wang <mjw7@princeton.edu>
date: Fri Nov 6 09:10:57 EST 2020
combine retune and autotune into one object with changeable mode; add simpleretune; fix memory issue in zerocrossingcollector
--- a/TestPlugin/Source/MyTest.cpp
+++ b/TestPlugin/Source/MyTest.cpp
@@ -19,7 +19,7 @@
tMBPulse bpulse;
tRetune retune;
-tAutotune autotune;
+tSimpleRetune sretune;
tCompressor compressor;
@@ -56,7 +56,8 @@
LEAF_init(&leaf, sampleRate, blockSize, memory, MSIZE, &getRandomFloat);
tRetune_init(&retune, 1, mtof(48), mtof(72), 2048, &leaf);
- tAutotune_init(&autotune, 1, mtof(48), mtof(72), 2048, &leaf);
+ tSimpleRetune_init(&sretune, 1, mtof(48), mtof(72), 2048, &leaf);
+ tSimpleRetune_setMode(&sretune, 1);
}
inline double getSawFall(double angle) {
@@ -70,8 +71,8 @@
float LEAFTest_tick (float input)
{
-// return tRetune_tick(&retune, input);
- return tAutotune_tick(&autotune, input);
+// return tRetune_tick(&retune, input)[0];
+ return tSimpleRetune_tick(&sretune, input);
}
int firstFrame = 1;
@@ -79,8 +80,8 @@
void LEAFTest_block (void)
{
float val = getSliderValue("slider1");
-// tRetune_setPitchFactor(&retune, val * 3.0f + 0.5f, 0);
- tAutotune_setFreq(&autotune, 300, 0);
+ tRetune_tuneVoice(&retune, 0, val * 3.0f + 0.5f);
+ tSimpleRetune_tuneVoice(&sretune, 0, 300);
val = getSliderValue("slider2");
// tRetune_setPitchFactor(&retune, val * 3.0f + 0.5f, 1);
--- a/leaf/Inc/leaf-effects.h
+++ b/leaf/Inc/leaf-effects.h
@@ -533,42 +533,42 @@
void tPitchShift_shiftTo (tPitchShift* const, float freq, float* in, float* out, int bufSize);
/*!
- @defgroup tretune tRetune
+ @defgroup tsimpleretune tSimpleRetune
@ingroup effects
@brief
@{
- @fn void tRetune_init (tRetune* const, int numVoices, int bufSize, int frameSize, LEAF* const leaf)
+ @fn void tSimpleRetune_init (tSimpleRetune* const, int numVoices, int bufSize, int frameSize, LEAF* const leaf)
@brief
@param
- @fn void tRetune_initToPool (tRetune* const, int numVoices, int bufSize, int frameSize, tMempool* const)
+ @fn void tSimpleRetune_initToPool (tSimpleRetune* const, int numVoices, int bufSize, int frameSize, tMempool* const)
@brief
@param
- @fn void tRetune_free (tRetune* const)
+ @fn void tSimpleRetune_free (tSimpleRetune* const)
@brief
@param
- @fn float* tRetune_tick (tRetune* const, float sample)
+ @fn float* tSimpleRetune_tick (tSimpleRetune* const, float sample)
@brief
@param
- @fn void tRetune_setNumVoices (tRetune* const, int numVoices)
+ @fn void tSimpleRetune_setNumVoices (tSimpleRetune* const, int numVoices)
@brief
@param
- @fn void tRetune_setPitchFactors (tRetune* const, float pf)
+ @fn void tSimpleRetune_tuneVoices (tSimpleRetune* const, float f)
@brief
@param
- @fn void tRetune_setPitchFactor (tRetune* const, float pf, int voice)
+ @fn void tSimpleRetune_tuneVoice (tSimpleRetune* const, float f, int voice)
@brief
@param
@} */
- typedef struct _tRetune
+ typedef struct _tSimpleRetune
{
tMempool mempool;
@@ -579,62 +579,65 @@
float* inBuffer;
float* outBuffer;
- float* lastOutBuffer;
int bufSize;
int index;
- float* pitchFactors;
+ void (*shiftFunction)(tPitchShift* const, float, float*, float*, int);
+
+ float* shiftValues;
int numVoices;
- } _tRetune;
+ } _tSimpleRetune;
- typedef _tRetune* tRetune;
+ typedef _tSimpleRetune* tSimpleRetune;
- void tRetune_init (tRetune* const, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, LEAF* const leaf);
- void tRetune_initToPool (tRetune* const, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, tMempool* const);
- void tRetune_free (tRetune* const);
+ void tSimpleRetune_init (tSimpleRetune* const, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, LEAF* const leaf);
+ void tSimpleRetune_initToPool (tSimpleRetune* const, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, tMempool* const);
+ void tSimpleRetune_free (tSimpleRetune* const);
- float tRetune_tick (tRetune* const, float sample);
- void tRetune_setNumVoices (tRetune* const, int numVoices);
- void tRetune_setPitchFactors (tRetune* const, float pf);
- void tRetune_setPitchFactor (tRetune* const, float pf, int voice);
+ float tSimpleRetune_tick (tSimpleRetune* const, float sample);
+ void tSimpleRetune_setMode (tSimpleRetune* const, int mode);
+ void tSimpleRetune_setNumVoices (tSimpleRetune* const, int numVoices);
+ void tSimpleRetune_tuneVoices (tSimpleRetune* const, float* t);
+ void tSimpleRetune_tuneVoice (tSimpleRetune* const, int voice, float t);
+ float tSimpleRetune_getInputFrequency (tSimpleRetune* const);
/*!
- @defgroup tautotune tAutotune
+ @defgroup tretune tRetune
@ingroup effects
@brief
@{
- @fn void tAutotune_init (tAutotune* const, int numVoices, int bufSize, int frameSize, LEAF* const leaf)
+ @fn void tRetune_init (tRetune* const, int numVoices, int bufSize, int frameSize, LEAF* const leaf)
@brief
@param
- @fn void tAutotune_initToPool (tAutotune* const, int numVoices, int bufSize, int frameSize, tMempool* const)
+ @fn void tRetune_initToPool (tRetune* const, int numVoices, int bufSize, int frameSize, tMempool* const)
@brief
@param
- @fn void tAutotune_free (tAutotune* const)
+ @fn void tRetune_free (tRetune* const)
@brief
@param
- @fn float* tAutotune_tick (tAutotune* const, float sample)
+ @fn float* tRetune_tick (tRetune* const, float sample)
@brief
@param
- @fn void tAutotune_setNumVoices (tAutotune* const, int numVoices)
+ @fn void tRetune_setNumVoices (tRetune* const, int numVoices)
@brief
@param
- @fn void tAutotune_setFreqs (tAutotune* const, float f)
+ @fn void tRetune_setPitchFactors (tRetune* const, float pf)
@brief
@param
- @fn void tAutotune_setFreq (tAutotune* const, float f, int voice)
+ @fn void tRetune_setPitchFactor (tRetune* const, float pf, int voice)
@brief
@param
@} */
- typedef struct _tAutotune
+ typedef struct _tRetune
{
tMempool mempool;
@@ -644,25 +647,30 @@
tPitchShift* ps;
float* inBuffer;
- float* outBuffer;
- float* lastOutBuffer;
+ float** outBuffers;
int bufSize;
int index;
- float* freqs;
+ float* output;
+
+ void (*shiftFunction)(tPitchShift* const, float, float*, float*, int);
+
+ float* shiftValues;
int numVoices;
- } _tAutotune;
+ } _tRetune;
- typedef _tAutotune* tAutotune;
+ typedef _tRetune* tRetune;
- void tAutotune_init (tAutotune* const, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, LEAF* const leaf);
- void tAutotune_initToPool (tAutotune* const, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, tMempool* const);
- void tAutotune_free (tAutotune* const);
+ void tRetune_init (tRetune* const, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, LEAF* const leaf);
+ void tRetune_initToPool (tRetune* const, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, tMempool* const);
+ void tRetune_free (tRetune* const);
- float tAutotune_tick (tAutotune* const, float sample);
- void tAutotune_setNumVoices (tAutotune* const, int numVoices);
- void tAutotune_setFreqs (tAutotune* const, float f);
- void tAutotune_setFreq (tAutotune* const, float f, int voice);
+ float* tRetune_tick (tRetune* const, float sample);
+ void tRetune_setMode (tRetune* const, int mode);
+ void tRetune_setNumVoices (tRetune* const, int numVoices);
+ void tRetune_tuneVoices (tRetune* const, float* t);
+ void tRetune_tuneVoice (tRetune* const, int voice, float t);
+ float tRetune_getInputFrequency (tRetune* const);
//==============================================================================
--- a/leaf/Src/leaf-analysis.c
+++ b/leaf/Src/leaf-analysis.c
@@ -1044,7 +1044,7 @@
z->_mask = z->_size - 1;
- z->_info = (tZeroCrossingInfo*) mpool_alloc(sizeof(tZeroCrossingInfo) * z->_size, m);
+ z->_info = (tZeroCrossingInfo*) mpool_calloc(sizeof(tZeroCrossingInfo) * z->_size, m);
for (unsigned i = 0; i < z->_size; i++)
tZeroCrossingInfo_initToPool(&z->_info[i], mp);
@@ -1063,6 +1063,9 @@
{
_tZeroCrossingCollector* z = *zc;
+ for (unsigned i = 0; i < z->_size; i++)
+ tZeroCrossingInfo_free(&z->_info[i]);
+ mpool_free((char*)z->_info, z->mempool);
mpool_free((char*)z, z->mempool);
}
--- a/leaf/Src/leaf-effects.c
+++ b/leaf/Src/leaf-effects.c
@@ -1047,6 +1047,8 @@
{
_tSOLAD* w = *wp;
+ tAttackDetection_free(&w->ad);
+ tHighpass_free(&w->hp);
mpool_free((char*)w->delaybuf, w->mempool);
mpool_free((char*)w, w->mempool);
}
@@ -1374,7 +1376,7 @@
void tPitchShift_initToPool (tPitchShift* const psr, tDualPitchDetector* const dpd, tMempool* const mp)
{
_tMempool* m = *mp;
- _tPitchShift* ps = *psr = (_tPitchShift*) mpool_calloc(sizeof(_tPitchShift), m);
+ _tPitchShift* ps = *psr = (_tPitchShift*) mpool_alloc(sizeof(_tPitchShift), m);
ps->mempool = m;
ps->pd = *dpd;
@@ -1424,19 +1426,20 @@
tSOLAD_ioSamples(&ps->sola, in, out, bufSize);
}
+
//============================================================================================================
-// RETUNE
+// SIMPLERETUNE
//============================================================================================================
-void tRetune_init(tRetune* const rt, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, LEAF* const leaf)
+void tSimpleRetune_init (tSimpleRetune* const rt, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, LEAF* const leaf)
{
- tRetune_initToPool(rt, numVoices, minInputFreq, maxInputFreq, bufSize, &leaf->mempool);
+ tSimpleRetune_initToPool(rt, numVoices, minInputFreq, maxInputFreq, bufSize, &leaf->mempool);
}
-void tRetune_initToPool (tRetune* const rt, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, tMempool* const mp)
+void tSimpleRetune_initToPool (tSimpleRetune* const rt, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, tMempool* const mp)
{
_tMempool* m = *mp;
- _tRetune* r = *rt = (_tRetune*) mpool_alloc(sizeof(_tRetune), m);
+ _tSimpleRetune* r = *rt = (_tSimpleRetune*) mpool_calloc(sizeof(_tSimpleRetune), m);
r->mempool = *mp;
r->bufSize = bufSize;
@@ -1444,26 +1447,27 @@
r->inBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
r->outBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
- r->lastOutBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
r->index = 0;
-
+
r->ps = (tPitchShift*) mpool_calloc(sizeof(tPitchShift) * r->numVoices, m);
- r->pitchFactors = (float*) mpool_calloc(sizeof(float) * r->numVoices, m);
+ r->shiftValues = (float*) mpool_calloc(sizeof(float) * r->numVoices, m);
r->minInputFreq = minInputFreq;
r->maxInputFreq = maxInputFreq;
tDualPitchDetector_initToPool(&r->dp, r->minInputFreq, r->maxInputFreq, mp);
-
+
for (int i = 0; i < r->numVoices; ++i)
{
tPitchShift_initToPool(&r->ps[i], &r->dp, mp);
}
+
+ r->shiftFunction = &tPitchShift_shiftBy;
}
-void tRetune_free (tRetune* const rt)
+void tSimpleRetune_free (tSimpleRetune* const rt)
{
- _tRetune* r = *rt;
+ _tSimpleRetune* r = *rt;
tDualPitchDetector_free(&r->dp);
for (int i = 0; i < r->numVoices; ++i)
@@ -1470,7 +1474,7 @@
{
tPitchShift_free(&r->ps[i]);
}
- mpool_free((char*)r->pitchFactors, r->mempool);
+ mpool_free((char*)r->shiftValues, r->mempool);
mpool_free((char*)r->ps, r->mempool);
mpool_free((char*)r->inBuffer, r->mempool);
mpool_free((char*)r->outBuffer, r->mempool);
@@ -1477,21 +1481,21 @@
mpool_free((char*)r, r->mempool);
}
-float tRetune_tick(tRetune* const rt, float sample)
+float tSimpleRetune_tick(tSimpleRetune* const rt, float sample)
{
- _tRetune* r = *rt;
+ _tSimpleRetune* r = *rt;
tDualPitchDetector_tick(&r->dp, sample);
r->inBuffer[r->index] = sample;
float out = r->outBuffer[r->index];
- r->outBuffer[r->index++] = 0.0f;
+ r->outBuffer[r->index] = 0.0f;
- if (r->index >= r->bufSize)
+ if (++r->index >= r->bufSize)
{
for (int i = 0; i < r->numVoices; ++i)
{
- tPitchShift_shiftBy(&r->ps[i], r->pitchFactors[i], r->inBuffer, r->outBuffer, r->bufSize);
+ r->shiftFunction(&r->ps[i], r->shiftValues[i], r->inBuffer, r->outBuffer, r->bufSize);
}
r->index = 0;
}
@@ -1499,49 +1503,64 @@
return out;
}
-void tRetune_setNumVoices(tRetune* const rt, int numVoices)
+void tSimpleRetune_setMode (tSimpleRetune* const rt, int mode)
{
- _tRetune* r = *rt;
+ _tSimpleRetune* r = *rt;
+ if (mode > 0) r->shiftFunction = &tPitchShift_shiftTo;
+ else r->shiftFunction = &tPitchShift_shiftBy;
+}
+
+void tSimpleRetune_setNumVoices(tSimpleRetune* const rt, int numVoices)
+{
+ _tSimpleRetune* r = *rt;
+
int bufSize = r->bufSize;
float minInputFreq = r->minInputFreq;
float maxInputFreq = r->maxInputFreq;
tMempool mempool = r->mempool;
- tRetune_free(rt);
- tRetune_initToPool(rt, minInputFreq, maxInputFreq, numVoices, bufSize, &mempool);
+ tSimpleRetune_free(rt);
+ tSimpleRetune_initToPool(rt, minInputFreq, maxInputFreq, numVoices, bufSize, &mempool);
}
-void tRetune_setPitchFactors(tRetune* const rt, float pf)
+void tSimpleRetune_tuneVoices(tSimpleRetune* const rt, float* t)
{
- _tRetune* r = *rt;
+ _tSimpleRetune* r = *rt;
for (int i = 0; i < r->numVoices; ++i)
{
- r->pitchFactors[i] = pf;
+ r->shiftValues[i] = t[i];
}
}
-void tRetune_setPitchFactor(tRetune* const rt, float pf, int voice)
+void tSimpleRetune_tuneVoice(tSimpleRetune* const rt, int voice, float t)
{
- _tRetune* r = *rt;
+ _tSimpleRetune* r = *rt;
- r->pitchFactors[voice] = pf;
+ r->shiftValues[voice] = t;
}
+float tSimpleRetune_getInputFrequency (tSimpleRetune* const rt)
+{
+ _tSimpleRetune* r = *rt;
+
+ return tDualPitchDetector_getFrequency(&r->dp);
+}
+
//============================================================================================================
-// AUTOTUNE
+// RETUNE
//============================================================================================================
-void tAutotune_init (tAutotune* const rt, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, LEAF* const leaf)
+void tRetune_init(tRetune* const rt, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, LEAF* const leaf)
{
- tAutotune_initToPool(rt, numVoices, minInputFreq, maxInputFreq, bufSize, &leaf->mempool);
+ tRetune_initToPool(rt, numVoices, minInputFreq, maxInputFreq, bufSize, &leaf->mempool);
}
-void tAutotune_initToPool (tAutotune* const rt, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, tMempool* const mp)
+void tRetune_initToPool (tRetune* const rt, int numVoices, float minInputFreq, float maxInputFreq, int bufSize, tMempool* const mp)
{
_tMempool* m = *mp;
- _tAutotune* r = *rt = (_tAutotune*) mpool_alloc(sizeof(_tAutotune), m);
+ _tRetune* r = *rt = (_tRetune*) mpool_calloc(sizeof(_tRetune), m);
r->mempool = *mp;
r->bufSize = bufSize;
@@ -1548,65 +1567,81 @@
r->numVoices = numVoices;
r->inBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
- r->outBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
- r->lastOutBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
-
+
r->index = 0;
-
+
r->ps = (tPitchShift*) mpool_calloc(sizeof(tPitchShift) * r->numVoices, m);
- r->freqs = (float*) mpool_calloc(sizeof(float) * r->numVoices, m);
+ r->shiftValues = (float*) mpool_calloc(sizeof(float) * r->numVoices, m);
+ r->outBuffers = (float**) mpool_calloc(sizeof(float*) * r->numVoices, m);
+ r->output = (float*) mpool_calloc(sizeof(float) * r->numVoices, m);
r->minInputFreq = minInputFreq;
r->maxInputFreq = maxInputFreq;
tDualPitchDetector_initToPool(&r->dp, r->minInputFreq, r->maxInputFreq, mp);
-
+
for (int i = 0; i < r->numVoices; ++i)
{
tPitchShift_initToPool(&r->ps[i], &r->dp, mp);
+ r->outBuffers[i] = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
}
+
+ r->shiftFunction = &tPitchShift_shiftBy;
}
-void tAutotune_free (tAutotune* const rt)
+void tRetune_free (tRetune* const rt)
{
- _tAutotune* r = *rt;
+ _tRetune* r = *rt;
tDualPitchDetector_free(&r->dp);
for (int i = 0; i < r->numVoices; ++i)
{
tPitchShift_free(&r->ps[i]);
+ mpool_free((char*)r->outBuffers[i], r->mempool);
}
- mpool_free((char*)r->freqs, r->mempool);
+ mpool_free((char*)r->shiftValues, r->mempool);
mpool_free((char*)r->ps, r->mempool);
mpool_free((char*)r->inBuffer, r->mempool);
- mpool_free((char*)r->outBuffer, r->mempool);
+ mpool_free((char*)r->outBuffers, r->mempool);
+ mpool_free((char*)r->output, r->mempool);
mpool_free((char*)r, r->mempool);
}
-float tAutotune_tick(tAutotune* const rt, float sample)
+float* tRetune_tick(tRetune* const rt, float sample)
{
- _tAutotune* r = *rt;
+ _tRetune* r = *rt;
tDualPitchDetector_tick(&r->dp, sample);
r->inBuffer[r->index] = sample;
- float out = r->outBuffer[r->index];
- r->outBuffer[r->index++] = 0.0f;
-
- if (r->index >= r->bufSize)
+ for (int i = 0; i < r->numVoices; ++i)
{
+ r->output[i] = r->outBuffers[i][r->index];
+ r->outBuffers[i][r->index] = 0.0f;
+ }
+
+ if (++r->index >= r->bufSize)
+ {
for (int i = 0; i < r->numVoices; ++i)
{
- tPitchShift_shiftTo(&r->ps[i], r->freqs[i], r->inBuffer, r->outBuffer, r->bufSize);
+ r->shiftFunction(&r->ps[i], r->shiftValues[i], r->inBuffer, r->outBuffers[i], r->bufSize);
}
r->index = 0;
}
+
+ return r->output;
+}
+
+void tRetune_setMode (tRetune* const rt, int mode)
+{
+ _tRetune* r = *rt;
- return out;
+ if (mode > 0) r->shiftFunction = &tPitchShift_shiftTo;
+ else r->shiftFunction = &tPitchShift_shiftBy;
}
-void tAutotune_setNumVoices(tAutotune* const rt, int numVoices)
+void tRetune_setNumVoices(tRetune* const rt, int numVoices)
{
- _tAutotune* r = *rt;
+ _tRetune* r = *rt;
int bufSize = r->bufSize;
float minInputFreq = r->minInputFreq;
@@ -1613,25 +1648,32 @@
float maxInputFreq = r->maxInputFreq;
tMempool mempool = r->mempool;
- tAutotune_free(rt);
- tAutotune_initToPool(rt, minInputFreq, maxInputFreq, numVoices, bufSize, &mempool);
+ tRetune_free(rt);
+ tRetune_initToPool(rt, minInputFreq, maxInputFreq, numVoices, bufSize, &mempool);
}
-void tAutotune_setFreqs(tAutotune* const rt, float f)
+void tRetune_tuneVoices(tRetune* const rt, float* t)
{
- _tAutotune* r = *rt;
+ _tRetune* r = *rt;
for (int i = 0; i < r->numVoices; ++i)
{
- r->freqs[i] = f;
+ r->shiftValues[i] = t[i];
}
}
-void tAutotune_setFreq(tAutotune* const rt, float f, int voice)
+void tRetune_tuneVoice(tRetune* const rt, int voice, float t)
{
- _tAutotune* r = *rt;
+ _tRetune* r = *rt;
- r->freqs[voice] = f;
+ r->shiftValues[voice] = t;
+}
+
+float tRetune_getInputFrequency (tRetune* const rt)
+{
+ _tRetune* r = *rt;
+
+ return tDualPitchDetector_getFrequency(&r->dp);
}
//============================================================================================================
--- a/leaf/Src/leaf-mempool.c
+++ b/leaf/Src/leaf-mempool.c
@@ -249,8 +249,7 @@
pool->usize += pool->leaf->header_size + node_to_alloc->size;
// Format the new pool
- char* new_pool = (char*)node_to_alloc->pool;
- for (int i = 0; i < node_to_alloc->size; i++) new_pool[i] = 0;
+ 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;
}