History

getting new pitch detection working in retune and autotune

Matthew Wang committed 6 years ago

Commit 11c878e1 parent 17430886 files · +157 −531Browse files

Unified · Split

6 files
  1. TestPlugin/Source/LEAFLink.cpp +3 −3
  2. TestPlugin/Source/MyTest.cpp +14 −86
  3. leaf/Inc/leaf-analysis.h +1 −2
  4. leaf/Inc/leaf-effects.h +22 −91
  5. leaf/Inc/leaf-math.h +2 −0
  6. leaf/Src/leaf-effects.c +115 −349
TestPlugin/Source/LEAFLink.cpp+3−3
@@ -20,9 +20,9 @@ std::vector<juce::String> cButtonNames = std::vector<juce::String> · 19 unmodified lines
2020
2121 std::vector<juce::String> cSliderNames = std::vector<juce::String>
2222 {
23− "on/off",
24− "mod freq",
25− "mod depth"
23+ "slider1",
24+ "slider2",
25+ "slider3"
2626 };
2727
2828 std::vector<juce::String> cComboBoxNames = std::vector<juce::String>
TestPlugin/Source/MyTest.cpp+14−86
@@ -18,7 +18,8 @@ tMBSaw bsaw; · 17 unmodified lines
1818 tMBTriangle btri;
1919 tMBPulse bpulse;
2020
21−tDualPitchDetector detector;
21+tRetune retune;
22+tAutotune autotune;
2223
2324 tCompressor compressor;
2425
@@ -54,33 +55,8 @@ void LEAFTest_init (float sampleRate, int blockSize) · 29 unmodified lines
5455 {
5556 LEAF_init(&leaf, sampleRate, blockSize, memory, MSIZE, &getRandomFloat);
5657
57− tMBSaw_init(&bsaw, &leaf);
58− tMBTriangle_init(&btri, &leaf);
59− tMBPulse_init(&bpulse, &leaf);
60−
61− bufIn = (float*) leaf_alloc(&leaf, sizeof(float) * 4096);
62− bufOut = (float*) leaf_alloc(&leaf, sizeof(float) * 4096);
63−
64− tDualPitchDetector_init(&detector, mtof(48), mtof(84), &leaf);
65−
66− tCompressor_init(&compressor, &leaf);
67−
68− tSVF_init(&lp, SVFTypeLowpass, mtof(84) * 2.0f, 1.0f, &leaf);
69− tSVF_init(&hp, SVFTypeHighpass, mtof(48) * 0.5f, 1.0f, &leaf);
70−
71− tPeriodDetection_init(&pd, bufIn, bufOut, 4096, 1024, &leaf);
72−
73− tZeroCrossingCounter_init(&zc, 128, &leaf);
74− tEnvelopeFollower_init(&ef, 0.02f, 0.9999f, &leaf);
75−
76− tTriangle_init(&tri, &leaf);
77− tTriangle_setFreq(&tri, 100);
78−
79− tBuffer_init(&samp, 5.0f * leaf.sampleRate, &leaf);
80− tMBSampler_init(&sampler, &samp, &leaf);
81−
82− tMBSampler_setMode(&sampler, PlayLoop);
83− tMBSampler_setEnd(&sampler, samp->bufferLength);
58+ tRetune_init(&retune, 1, 1024, &leaf);
59+ tAutotune_init(&autotune, 1, 1024, &leaf);
8460 }
8561
8662 inline double getSawFall(double angle) {
@@ -94,72 +70,24 @@ inline double getSawFall(double angle) { · 7 unmodified lines
9470
9571 float LEAFTest_tick (float input)
9672 {
97− tBuffer_tick(&samp, input);
98−
99− return tMBSampler_tick(&sampler);
100−
101−
102−// tMBSaw_setFreq(&bsaw, x);
103−// tMBTriangle_setFreq(&btri, x);
104−// tMBPulse_setFreq(&bpulse, x);
105−//
106−// tMBTriangle_setWidth(&btri, y);
107−// tMBPulse_setWidth(&bpulse, y);
108−//
109−//// return tMBSaw_tick(&bsaw);
110−//// return tMBTriangle_tick(&btri);
111−// return tMBPulse_tick(&bpulse);
112−
113−// input = tSVF_tick(&hp, tSVF_tick(&lp, tCompressor_tick(&compressor, input)));
114−//
115−//// float freq = 1.0f/tPeriodDetection_tick(&pd, input) * leaf.sampleRate;
116−// tDualPitchDetector_tick(&detector, input);
117−// float altFreq = tDualPitchDetector_getFrequency(&detector);
118−//
119−//// if (fabsf(1.0f - (freq / altFreq)) < 0.05f)
120−//// if (tZeroCrossingCounter_tick(&zc, input) < 0.05 && freq > 0.0f)
121−// if (altFreq > 0.0f)
122−// {
123−// tTriangle_setFreq(&tri, altFreq);
124−// }
125−//
126−// float g = tEnvelopeFollower_tick(&ef, input);
127−//
128−// return tTriangle_tick(&tri) * g;
73+// return tRetune_tick(&retune, input);
74+ return tAutotune_tick(&autotune, input);
12975 }
13076
13177 int firstFrame = 1;
13278 bool lastState = false, lastPlayState = false;
13379 void LEAFTest_block (void)
13480 {
135− float periodicity = tDualPitchDetector_getPeriodicity(&detector);
136− if (periodicity > 0.99f)
137− {
138− DBG(tDualPitchDetector_getFrequency(&detector));
139− DBG(tDualPitchDetector_getPeriodicity(&detector));
140− }
141−
142− float val = getSliderValue("on/off");
143−
144− if (val > 0.5f && !sampler->active)
145− {
146− tBuffer_record(&samp);
147− tMBSampler_play(&sampler);
148− }
149− else if (val < 0.5f && sampler->active)
150− {
151− tMBSampler_stop(&sampler);
152− }
153−
154− val = getSliderValue("mod freq");
155−
156− tMBSampler_setStart(&sampler, val * 5.0f * leaf.sampleRate);
81+ float val = getSliderValue("slider1");
82+// tRetune_setPitchFactor(&retune, val * 3.0f + 0.5f, 0);
83+ tAutotune_setFreq(&autotune, 300, 0);
15784
85+ val = getSliderValue("slider2");
86+// tRetune_setPitchFactor(&retune, val * 3.0f + 0.5f, 1);
15887
159− val = getSliderValue("mod depth");
160−
161− tMBSampler_setRate(&sampler, val * 8.0f - 4.0f);
162−
88+ val = getSliderValue("slider3");
89+// tRetune_setPitchFactor(&retune, val * 3.0f + 0.5f, 2);
90+
16391 }
16492
16593 void LEAFTest_controllerInput (int cnum, float cval)
leaf/Inc/leaf-analysis.h+1−2
@@ -497,7 +497,7 @@ extern "C" { · 496 unmodified lines
497497 #define DEFHOPSIZE 64
498498 #define DEFWINDOWSIZE 64
499499 #define FBA 20
500−#define HPFREQ 40.0f
500+#define HPFREQ 20.0f
501501
502502 typedef struct _tPeriodDetection
503503 {
@@ -769,7 +769,6 @@ extern "C" { · 265 unmodified lines
769769
770770 typedef struct _tPeriodDetector
771771 {
772−
773772 tMempool mempool;
774773
775774 tZeroCrossingCollector _zc;
leaf/Inc/leaf-effects.h+22−91
@@ -510,38 +510,27 @@ extern "C" { · 509 unmodified lines
510510
511511 typedef struct _tPitchShift
512512 {
513−
514513 tMempool mempool;
515514
515+ _tDualPitchDetector* pd;
516+
516517 tSOLAD sola;
517518 tHighpass hp;
518− tPeriodDetection* p;
519− tDualPitchDetector dp;
520519
521520 float* outBuffer;
522− int frameSize;
521+ float* inBuffer;
523522 int bufSize;
524−
525− int framesPerBuffer;
526− int curBlock;
527− int lastBlock;
528523 int index;
529−
530− float pitchFactor;
531− float timeConstant;
532− float radius;
533524 } _tPitchShift;
534525
535526 typedef _tPitchShift* tPitchShift;
536527
537− void tPitchShift_init (tPitchShift* const, tPeriodDetection* const, tDualPitchDetector* const, float* out, int bufSize, LEAF* const leaf);
538− void tPitchShift_initToPool (tPitchShift* const, tPeriodDetection* const, tDualPitchDetector* const, float* out, int bufSize, tMempool* const);
539− void tPitchShift_free (tPitchShift* const);
528+ void tPitchShift_init (tPitchShift* const, tDualPitchDetector* const, LEAF* const leaf);
529+ void tPitchShift_initToPool (tPitchShift* const, tDualPitchDetector* const, tMempool* const);
530+ void tPitchShift_free (tPitchShift* const);
540531
541− float tPitchShift_shift (tPitchShift* const);
542− float tPitchShift_shiftToFunc (tPitchShift* const, float (*fun)(float));
543− float tPitchShift_shiftToFreq (tPitchShift* const, float freq);
544− void tPitchShift_setPitchFactor (tPitchShift* const, float pf);
532+ void tPitchShift_shiftBy (tPitchShift* const, float factor, float* in, float* out, int bufSize);
533+ void tPitchShift_shiftTo (tPitchShift* const, float freq, float* in, float* out, int bufSize);
545534
546535 /*!
547536 @defgroup tretune tRetune
@@ -577,35 +566,10 @@ extern "C" { · 29 unmodified lines
577566 @brief
578567 @param
579568
580− @fn void tRetune_setTimeConstant (tRetune* const, float tc)
581− @brief
582− @param
583−
584− @fn void tRetune_setHopSize (tRetune* const, int hs)
585− @brief
586− @param
587−
588− @fn void tRetune_setWindowSize (tRetune* const, int ws)
589− @brief
590− @param
591−
592− @fn void tRetune_setFidelityThreshold(tRetune* const, float threshold)
593− @brief
594− @param
595−
596− @fn float tRetune_getInputPeriod (tRetune* const)
597− @brief
598− @param
599−
600− @fn float tRetune_getInputFreq (tRetune* const)
601− @brief
602− @param
603−
604569 @} */
605570
606571 typedef struct _tRetune
607572 {
608−
609573 tMempool mempool;
610574
611575 tDualPitchDetector dp;
@@ -613,40 +577,25 @@ extern "C" { · 1 unmodified line
613577 tPitchShift* ps;
614578
615579 float* inBuffer;
616− float** outBuffers;
617− float* tickOutput;
618− int frameSize;
580+ float* outBuffer;
581+ float* lastOutBuffer;
619582 int bufSize;
583+ int index;
620584
621− uint16_t hopSize;
622− uint16_t windowSize;
623− uint8_t fba;
624−
625− float* pitchFactor;
626− float timeConstant;
627− float radius;
628−
629− float inputPeriod;
630−
585+ float* pitchFactors;
631586 int numVoices;
632587 } _tRetune;
633588
634589 typedef _tRetune* tRetune;
635590
636− void tRetune_init (tRetune* const, int numVoices, int bufSize, int frameSize, LEAF* const leaf);
637− void tRetune_initToPool (tRetune* const, int numVoices, int bufSize, int frameSize, tMempool* const);
591+ void tRetune_init (tRetune* const, int numVoices, int bufSize, LEAF* const leaf);
592+ void tRetune_initToPool (tRetune* const, int numVoices, int bufSize, tMempool* const);
638593 void tRetune_free (tRetune* const);
639594
640− float* tRetune_tick (tRetune* const, float sample);
595+ float tRetune_tick (tRetune* const, float sample);
641596 void tRetune_setNumVoices (tRetune* const, int numVoices);
642597 void tRetune_setPitchFactors (tRetune* const, float pf);
643598 void tRetune_setPitchFactor (tRetune* const, float pf, int voice);
644− void tRetune_setTimeConstant (tRetune* const, float tc);
645− void tRetune_setHopSize (tRetune* const, int hs);
646− void tRetune_setWindowSize (tRetune* const, int ws);
647− void tRetune_setFidelityThreshold(tRetune* const, float threshold);
648− float tRetune_getInputPeriod (tRetune* const);
649− float tRetune_getInputFreq (tRetune* const);
650599
651600 /*!
652601 @defgroup tautotune tAutotune
@@ -718,7 +667,6 @@ extern "C" { · 65 unmodified lines
718667
719668 typedef struct _tAutotune
720669 {
721−
722670 tMempool mempool;
723671
724672 tDualPitchDetector dp;
@@ -726,42 +674,25 @@ extern "C" { · 1 unmodified line
726674 tPitchShift* ps;
727675
728676 float* inBuffer;
729− float** outBuffers;
730− float* tickOutput;
731− int frameSize;
677+ float* outBuffer;
678+ float* lastOutBuffer;
732679 int bufSize;
680+ int index;
733681
734− uint16_t hopSize;
735− uint16_t windowSize;
736− uint8_t fba;
737−
738− float* freq;
739− float timeConstant;
740− float radius;
741−
742− float inputPeriod;
743− int shiftOn;
682+ float* freqs;
744683 int numVoices;
745684 } _tAutotune;
746685
747686 typedef _tAutotune* tAutotune;
748687
749− void tAutotune_init (tAutotune* const, int numVoices, int bufSize, int frameSize, LEAF* const leaf);
750− void tAutotune_initToPool (tAutotune* const, int numVoices, int bufSize, int frameSize, tMempool* const);
688+ void tAutotune_init (tAutotune* const, int numVoices, int bufSize, LEAF* const leaf);
689+ void tAutotune_initToPool (tAutotune* const, int numVoices, int bufSize, tMempool* const);
751690 void tAutotune_free (tAutotune* const);
752691
753− float* tAutotune_tick (tAutotune* const, float sample);
692+ float tAutotune_tick (tAutotune* const, float sample);
754693 void tAutotune_setNumVoices (tAutotune* const, int numVoices);
755694 void tAutotune_setFreqs (tAutotune* const, float f);
756695 void tAutotune_setFreq (tAutotune* const, float f, int voice);
757− void tAutotune_setTimeConstant (tAutotune* const, float tc);
758− void tAutotune_setHopSize (tAutotune* const, int hs);
759− void tAutotune_setWindowSize (tAutotune* const, int ws);
760− void tAutotune_setFidelityThreshold (tAutotune* const, float threshold);
761− void tAutotune_setAlpha (tAutotune* const, float alpha);
762− void tAutotune_setTolerance (tAutotune* const, float tolerance);
763− float tAutotune_getInputPeriod (tAutotune* const);
764− float tAutotune_getInputFreq (tAutotune* const);
765696
766697 //==============================================================================
767698
leaf/Inc/leaf-math.h+2−0
@@ -190,6 +190,8 @@ extern "C" { · 189 unmodified lines
190190 double fasterexp(double x);
191191
192192 float fasterexpf(float x);
193+
194+ float fastsqrtf(float x);
193195
194196 float mtof(float f);
195197
leaf/Src/leaf-effects.c+115−349
@@ -1010,7 +1010,6 @@ void tRosenbergGlottalPulse_setOpenLengthAndPulseLength (tRosenbergG · 1009 unmodified lines
10101010 /***************** static function declarations *******************************/
10111011 /******************************************************************************/
10121012
1013−static void solad_init(_tSOLAD *w);
10141013 static inline float read_sample(_tSOLAD *w, float floatindex);
10151014 static void pitchdown(_tSOLAD *w, float *out);
10161015 static void pitchup(_tSOLAD *w, float *out);
@@ -1032,9 +1031,13 @@ void tSOLAD_initToPool (tSOLAD* const wp, tMempool* const mp) · 15 unmodified lines
10321031 w->mempool = m;
10331032
10341033 w->pitchfactor = 1.;
1035− w->delaybuf = (float*) mpool_calloc(sizeof(float) * (LOOPSIZE+16), m);
1034+ w->delaybuf = (float*) mpool_calloc(sizeof(float) * LOOPSIZE, m);
10361035
1037− solad_init(w);
1036+ w->timeindex = 0;
1037+ w->xfadevalue = -1;
1038+ w->period = INITPERIOD;
1039+ w->readlag = INITPERIOD;
1040+ w->blocksize = INITPERIOD;
10381041 }
10391042
10401043 void tSOLAD_free (tSOLAD* const wp)
@@ -1077,7 +1080,10 @@ void tSOLAD_setPitchFactor(tSOLAD* const wp, float pitchfactor) · 36 unmodified lines
10771080 {
10781081 _tSOLAD* w = *wp;
10791082
1080− if (pitchfactor <= 0.0f) return;
1083+ if (pitchfactor <= 0.0f || pitchfactor > 1000.0f)
1084+ {
1085+ return;
1086+ }
10811087 w->pitchfactor = pitchfactor;
10821088 }
10831089
@@ -1101,11 +1107,16 @@ void tSOLAD_resetState(tSOLAD* const wp) · 17 unmodified lines
11011107 {
11021108 _tSOLAD* w = *wp;
11031109
1104− int n = LOOPSIZE + 1;
1110+ int n = LOOPSIZE;
11051111 float *buf = w->delaybuf;
11061112
11071113 while(n--) *buf++ = 0;
1108− solad_init(w);
1114+
1115+ w->timeindex = 0;
1116+ w->xfadevalue = -1;
1117+ w->period = INITPERIOD;
1118+ w->readlag = INITPERIOD;
1119+ w->blocksize = INITPERIOD;
11091120 }
11101121
11111122 /******************************************************************************/
@@ -1182,7 +1193,7 @@ static void pitchdown(_tSOLAD* const w, float *out) · 70 unmodified lines
11821193 xfadevalue -= xfadestep;
11831194 }
11841195
1185− *out++ = outputsample;
1196+ *out++ += outputsample;
11861197 refindex += 1;
11871198 readlag += readlagstep;
11881199 }
@@ -1312,7 +1323,7 @@ static void pitchup(_tSOLAD* const w, float *out) · 123 unmodified lines
13121323 xfadevalue -= xfadestep;
13131324 }
13141325
1315− *out++ = outputsample;
1326+ *out++ += outputsample;
13161327 refindex += 1;
13171328 readlag -= readlagstep;
13181329 }
@@ -1334,77 +1345,55 @@ static inline float read_sample(_tSOLAD* const w, float floatindex) · 15 unmodified lines
13341345 return (buf[index] + (fraction * (buf[index+1] - buf[index])));
13351346 }
13361347
1337−static void solad_init(_tSOLAD* const w)
1338−{
1339− w->timeindex = 0;
1340− w->xfadevalue = -1;
1341− w->period = INITPERIOD;
1342− w->readlag = INITPERIOD;
1343− w->blocksize = INITPERIOD;
1344−}
1345−
13461348 //============================================================================================================
13471349 // PITCHSHIFT
13481350 //============================================================================================================
13491351
13501352 static int pitchshift_attackdetect(_tPitchShift* ps)
13511353 {
1352− float envout;
1353−
1354− _tPeriodDetection* p = *ps->p;
1355−
1356− envout = tEnvPD_tick(&p->env);
1357−
1358− if (envout >= 1.0f)
1359− {
1360− p->lastmax = p->max;
1361− if (envout > p->max)
1362− {
1363− p->max = envout;
1364− }
1365− else
1366− {
1367− p->deltamax = envout - p->max;
1368− p->max = p->max * ps->radius;
1369− }
1370− p->deltamax = p->max - p->lastmax;
1371− }
1372−
1373− p->fba = p->fba ? (p->fba - 1) : 0;
1374−
1375− return (p->fba == 0 && (p->max > 60 && p->deltamax > 6)) ? 1 : 0;
1354+// float envout;
1355+//
1356+// _tPeriodDetection* p = *ps->p;
1357+//
1358+// envout = tEnvPD_tick(&p->env);
1359+//
1360+// if (envout >= 1.0f)
1361+// {
1362+// p->lastmax = p->max;
1363+// if (envout > p->max)
1364+// {
1365+// p->max = envout;
1366+// }
1367+// else
1368+// {
1369+// p->deltamax = envout - p->max;
1370+// p->max = p->max * ps->radius;
1371+// }
1372+// p->deltamax = p->max - p->lastmax;
1373+// }
1374+//
1375+// p->fba = p->fba ? (p->fba - 1) : 0;
1376+//
1377+// return (p->fba == 0 && (p->max > 60 && p->deltamax > 6)) ? 1 : 0;
13761378 }
13771379
1378−void tPitchShift_init (tPitchShift* const psr, tPeriodDetection* const pd, tDualPitchDetector* const dp, float* out, int bufSize, LEAF* const leaf)
1380+void tPitchShift_init (tPitchShift* const psr, tDualPitchDetector* const dpd, LEAF* const leaf)
13791381 {
1380− tPitchShift_initToPool(psr, pd, dp, out, bufSize, &leaf->mempool);
1382+ tPitchShift_initToPool(psr, dpd, &leaf->mempool);
13811383 }
13821384
1383−void tPitchShift_initToPool (tPitchShift* const psr, tPeriodDetection* const pd, tDualPitchDetector* const dp, float* out, int bufSize, tMempool* const mp)
1385+void tPitchShift_initToPool (tPitchShift* const psr, tDualPitchDetector* const dpd, tMempool* const mp)
13841386 {
13851387 _tMempool* m = *mp;
13861388 _tPitchShift* ps = *psr = (_tPitchShift*) mpool_calloc(sizeof(_tPitchShift), m);
13871389 ps->mempool = m;
13881390
1389− _tPeriodDetection* p = *pd;
1390−
1391− ps->p = pd;
1392−
1393− ps->outBuffer = out;
1394− ps->bufSize = bufSize;
1395− ps->frameSize = p->frameSize;
1396− ps->framesPerBuffer = ps->bufSize / ps->frameSize;
1397− ps->curBlock = 1;
1398− ps->lastBlock = 0;
1399− ps->index = 0;
1400− ps->pitchFactor = 1.0f;
1401− ps->dp = *dp;
1391+ ps->pd = *dpd;
14021392
14031393 tSOLAD_initToPool(&ps->sola, mp);
1394+ tSOLAD_setPitchFactor(&ps->sola, DEFPITCHRATIO);
14041395
14051396 tHighpass_initToPool(&ps->hp, HPFREQ, mp);
1406−
1407− tSOLAD_setPitchFactor(&ps->sola, DEFPITCHRATIO);
14081397 }
14091398
14101399 void tPitchShift_free (tPitchShift* const psr)
@@ -1416,168 +1405,63 @@ void tPitchShift_free (tPitchShift* const psr) · 5 unmodified lines
14161405 mpool_free((char*)ps, ps->mempool);
14171406 }
14181407
1419−void tPitchShift_setPitchFactor(tPitchShift* psr, float pf)
1408+void tPitchShift_shiftBy (tPitchShift* const psr, float factor, float* in, float* out, int bufSize)
14201409 {
14211410 _tPitchShift* ps = *psr;
1422−
1423− ps->pitchFactor = pf;
1424−}
1425−
1426−float tPitchShift_shift (tPitchShift* psr)
1427−{
1428− _tPitchShift* ps = *psr;
1429− _tPeriodDetection* p = *ps->p;
1430−
1431− float period, out;
1432− int i, iLast;
1433−
1434− i = p->i;
1435− iLast = p->iLast;
1436−
1437− out = tHighpass_tick(&ps->hp, ps->outBuffer[iLast]);
1438−
1439− if (p->indexstore >= ps->frameSize)
1440− {
1441−// period = tPeriodDetection_getPeriod(&p);
1442− period = 1.0f / tDualPitchDetector_getFrequency(&ps->dp);
1443−
1444− if(pitchshift_attackdetect(ps) == 1)
1445− {
1446− p->fba = 5;
1447− tSOLAD_setReadLag(&ps->sola, p->windowSize);
1448− }
1449−
1450− tSOLAD_setPeriod(&ps->sola, period);
1451− tSOLAD_setPitchFactor(&ps->sola, ps->pitchFactor);
1452−
1453− tSOLAD_ioSamples(&ps->sola, &(p->inBuffer[i]), &(ps->outBuffer[i]), ps->frameSize);
1454− }
1455−
1456− return out;
1457−}
1458−
1459−float tPitchShift_shiftToFreq (tPitchShift* psr, float freq)
1460−{
1461− _tPitchShift* ps = *psr;
1462− _tPeriodDetection* p = *ps->p;
14631411 LEAF* leaf = ps->mempool->leaf;
14641412
1465− float period = 0;
1466− float out;
1467− int i, iLast;
1413+ float period = leaf->sampleRate / tDualPitchDetector_getFrequency(&ps->pd);
1414+ tSOLAD_setPeriod(&ps->sola, period);
1415+ tSOLAD_setPitchFactor(&ps->sola, factor);
14681416
1469− i = p->i;
1470− iLast = p->iLast;
1471−
1472− out = tHighpass_tick(&ps->hp, ps->outBuffer[iLast]);
1473−
1474− if (p->indexstore >= ps->frameSize)
1475− {
1476−// period = tPeriodDetection_getPeriod(&p);
1477− float f = tDualPitchDetector_getFrequency(&ps->dp);
1478− if (f > 0) period = 1.0f / f;
1479−
1480− if(pitchshift_attackdetect(ps) == 1)
1481− {
1482− p->fba = 5;
1483− tSOLAD_setReadLag(&ps->sola, p->windowSize);
1484− }
1485−
1486− tSOLAD_setPeriod(&ps->sola, period * leaf->sampleRate);
1487−
1488− if (period != 0) ps->pitchFactor = freq * period;
1489− else ps->pitchFactor = 1.0f;
1490−
1491− tSOLAD_setPitchFactor(&ps->sola, ps->pitchFactor);
1492−
1493− tSOLAD_ioSamples(&ps->sola, &(p->inBuffer[i]), &(ps->outBuffer[i]), ps->frameSize);
1494− }
1495− return out;
1417+ tSOLAD_ioSamples(&ps->sola, in, out, bufSize);
14961418 }
14971419
1498−float tPitchShift_shiftToFunc (tPitchShift* psr, float (*fun)(float))
1420+void tPitchShift_shiftTo (tPitchShift* const psr, float freq, float* in, float* out, int bufSize)
14991421 {
15001422 _tPitchShift* ps = *psr;
1501− _tPeriodDetection* p = *ps->p;
1502−
1503− float period, out;
1504− int i, iLast;
1505−
1506− i = p->i;
1507− iLast = p->iLast;
1508−
1509− out = tHighpass_tick(&ps->hp, ps->outBuffer[iLast]);
1423+ LEAF* leaf = ps->mempool->leaf;
15101424
1511− if (p->indexstore >= ps->frameSize)
1512− {
1513−// period = tPeriodDetection_getPeriod(&p);
1514− period = 1.0f / tDualPitchDetector_getFrequency(&ps->dp);
1515−
1516− if(pitchshift_attackdetect(ps) == 1)
1517− {
1518− p->fba = 5;
1519− tSOLAD_setReadLag(&ps->sola, p->windowSize);
1520− }
1521−
1522− tSOLAD_setPeriod(&ps->sola, period);
1523−
1524− ps->pitchFactor = period/fun(period);
1525− tSOLAD_setPitchFactor(&ps->sola, ps->pitchFactor);
1526−
1527− tSOLAD_ioSamples(&ps->sola, &(p->inBuffer[i]), &(ps->outBuffer[i]), ps->frameSize);
1528−
1529− ps->curBlock++;
1530− if (ps->curBlock >= p->framesPerBuffer) ps->curBlock = 0;
1531− ps->lastBlock++;
1532− if (ps->lastBlock >= ps->framesPerBuffer) ps->lastBlock = 0;
1533− }
1425+ float period = 1.0f / tDualPitchDetector_getFrequency(&ps->pd);
1426+ float factor = freq * period;
1427+ tSOLAD_setPeriod(&ps->sola, leaf->sampleRate * period);
1428+ tSOLAD_setPitchFactor(&ps->sola, factor);
15341429
1535− return out;
1430+ tSOLAD_ioSamples(&ps->sola, in, out, bufSize);
15361431 }
15371432
15381433 //============================================================================================================
15391434 // RETUNE
15401435 //============================================================================================================
15411436
1542−void tRetune_init(tRetune* const rt, int numVoices, int bufSize, int frameSize, LEAF* const leaf)
1437+void tRetune_init(tRetune* const rt, int numVoices, int bufSize, LEAF* const leaf)
15431438 {
1544− tRetune_initToPool(rt, numVoices, bufSize, frameSize, &leaf->mempool);
1439+ tRetune_initToPool(rt, numVoices, bufSize, &leaf->mempool);
15451440 }
15461441
1547−void tRetune_initToPool (tRetune* const rt, int numVoices, int bufSize, int frameSize, tMempool* const mp)
1442+void tRetune_initToPool (tRetune* const rt, int numVoices, int bufSize, tMempool* const mp)
15481443 {
15491444 _tMempool* m = *mp;
15501445 _tRetune* r = *rt = (_tRetune*) mpool_alloc(sizeof(_tRetune), m);
15511446 r->mempool = *mp;
15521447
15531448 r->bufSize = bufSize;
1554− r->frameSize = frameSize;
15551449 r->numVoices = numVoices;
15561450
15571451 r->inBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
1558− r->outBuffers = (float**) mpool_calloc(sizeof(float*) * r->numVoices, m);
1559−
1560− r->hopSize = DEFHOPSIZE;
1561− r->windowSize = DEFWINDOWSIZE;
1562− r->fba = FBA;
1563− tRetune_setTimeConstant(rt, DEFTIMECONSTANT);
1452+ r->outBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
1453+ r->lastOutBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
15641454
1565− r->inputPeriod = 0.0f;
1455+ r->index = 0;
15661456
15671457 r->ps = (tPitchShift*) mpool_calloc(sizeof(tPitchShift) * r->numVoices, m);
1568− r->pitchFactor = (float*) mpool_calloc(sizeof(float) * r->numVoices, m);
1569− r->tickOutput = (float*) mpool_calloc(sizeof(float) * r->numVoices, m);
1570− for (int i = 0; i < r->numVoices; ++i)
1571− {
1572− r->outBuffers[i] = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
1573− }
1458+ r->pitchFactors = (float*) mpool_calloc(sizeof(float) * r->numVoices, m);
15741459
15751460 tDualPitchDetector_initToPool(&r->dp, mtof(48), mtof(72), mp);
1576− tPeriodDetection_initToPool(&r->pd, r->inBuffer, r->outBuffers[0], r->bufSize, r->frameSize, mp);
15771461
15781462 for (int i = 0; i < r->numVoices; ++i)
15791463 {
1580− tPitchShift_initToPool(&r->ps[i], &r->pd, &r->dp, r->outBuffers[i], r->bufSize, mp);
1464+ tPitchShift_initToPool(&r->ps[i], &r->dp, mp);
15811465 }
15821466 }
15831467
@@ -1590,30 +1474,34 @@ void tRetune_free (tRetune* const rt) · 6 unmodified lines
15901474 for (int i = 0; i < r->numVoices; ++i)
15911475 {
15921476 tPitchShift_free(&r->ps[i]);
1593− mpool_free((char*)r->outBuffers[i], r->mempool);
15941477 }
1595− mpool_free((char*)r->tickOutput, r->mempool);
1596− mpool_free((char*)r->pitchFactor, r->mempool);
1478+ mpool_free((char*)r->pitchFactors, r->mempool);
15971479 mpool_free((char*)r->ps, r->mempool);
15981480 mpool_free((char*)r->inBuffer, r->mempool);
1599− mpool_free((char*)r->outBuffers, r->mempool);
1481+ mpool_free((char*)r->outBuffer, r->mempool);
16001482 mpool_free((char*)r, r->mempool);
16011483 }
16021484
1603−float* tRetune_tick(tRetune* const rt, float sample)
1485+float tRetune_tick(tRetune* const rt, float sample)
16041486 {
16051487 _tRetune* r = *rt;
16061488
1607− tPeriodDetection_tick(&r->pd, sample);
16081489 tDualPitchDetector_tick(&r->dp, sample);
1609− r->inputPeriod = 1.0f / tDualPitchDetector_getFrequency(&r->dp);
16101490
1611− for (int v = 0; v < r->numVoices; ++v)
1491+ r->inBuffer[r->index] = sample;
1492+ float out = r->outBuffer[r->index];
1493+ r->outBuffer[r->index++] = 0.0f;
1494+
1495+ if (r->index >= r->bufSize)
16121496 {
1613− r->tickOutput[v] = tPitchShift_shift(&r->ps[v]);
1497+ for (int i = 0; i < r->numVoices; ++i)
1498+ {
1499+ tPitchShift_shiftBy(&r->ps[i], r->pitchFactors[i], r->inBuffer, r->outBuffer, r->bufSize);
1500+ }
1501+ r->index = 0;
16141502 }
16151503
1616− return r->tickOutput;
1504+ return out;
16171505 }
16181506
16191507 void tRetune_setNumVoices(tRetune* const rt, int numVoices)
@@ -1621,11 +1509,10 @@ void tRetune_setNumVoices(tRetune* const rt, int numVoices) · 1 unmodified line
16211509 _tRetune* r = *rt;
16221510
16231511 int bufSize = r->bufSize;
1624− int frameSize = r->frameSize;
16251512 tMempool mempool = r->mempool;
16261513
16271514 tRetune_free(rt);
1628− tRetune_initToPool(rt, numVoices, bufSize, frameSize, &mempool);
1515+ tRetune_initToPool(rt, numVoices, bufSize, &mempool);
16291516 }
16301517
16311518 void tRetune_setPitchFactors(tRetune* const rt, float pf)
@@ -1634,8 +1521,7 @@ void tRetune_setPitchFactors(tRetune* const rt, float pf) · 2 unmodified lines
16341521
16351522 for (int i = 0; i < r->numVoices; ++i)
16361523 {
1637− r->pitchFactor[i] = pf;
1638− tPitchShift_setPitchFactor(&r->ps[i], r->pitchFactor[i]);
1524+ r->pitchFactors[i] = pf;
16391525 }
16401526 }
16411527
@@ -1643,142 +1529,81 @@ void tRetune_setPitchFactor(tRetune* const rt, float pf, int voice) · 1 unmodified line
16431529 {
16441530 _tRetune* r = *rt;
16451531
1646− r->pitchFactor[voice] = pf;
1647− tPitchShift_setPitchFactor(&r->ps[voice], r->pitchFactor[voice]);
1648−}
1649−
1650−void tRetune_setTimeConstant(tRetune* const rt, float tc)
1651−{
1652− _tRetune* r = *rt;
1653− LEAF* leaf = r->mempool->leaf;
1654−
1655− r->timeConstant = tc;
1656− r->radius = expf(-1000.0f * r->hopSize * leaf->invSampleRate / r->timeConstant);
1657−}
1658−
1659−void tRetune_setHopSize(tRetune* const rt, int hs)
1660−{
1661− _tRetune* r = *rt;
1662−
1663− r->hopSize = hs;
1664− tPeriodDetection_setHopSize(&r->pd, r->hopSize);
1665−}
1666−
1667−void tRetune_setWindowSize(tRetune* const rt, int ws)
1668−{
1669− _tRetune* r = *rt;
1670−
1671− r->windowSize = ws;
1672− tPeriodDetection_setWindowSize(&r->pd, r->windowSize);
1673−}
1674−
1675−void tRetune_setFidelityThreshold(tRetune* const rt, float threshold)
1676−{
1677− _tRetune* r = *rt;
1678−
1679− tPeriodDetection_setFidelityThreshold(&r->pd, threshold);
1680−}
1681−
1682−float tRetune_getInputPeriod(tRetune* const rt)
1683−{
1684− _tRetune* r = *rt;
1685− LEAF* leaf = r->mempool->leaf;
1686−
1687− return (r->inputPeriod * leaf->invSampleRate);
1688−}
1689−
1690−float tRetune_getInputFreq(tRetune* const rt)
1691−{
1692− _tRetune* r = *rt;
1693− LEAF* leaf = r->mempool->leaf;
1694−
1695− return 1.0f/(r->inputPeriod * leaf->invSampleRate);
1532+ r->pitchFactors[voice] = pf;
16961533 }
16971534
16981535 //============================================================================================================
16991536 // AUTOTUNE
17001537 //============================================================================================================
17011538
1702−void tAutotune_init (tAutotune* const rt, int numVoices, int bufSize, int frameSize, LEAF* const leaf)
1539+void tAutotune_init (tAutotune* const rt, int numVoices, int bufSize, LEAF* const leaf)
17031540 {
1704− tAutotune_initToPool(rt, numVoices, bufSize, frameSize, &leaf->mempool);
1541+ tAutotune_initToPool(rt, numVoices, bufSize, &leaf->mempool);
17051542 }
17061543
1707−void tAutotune_initToPool (tAutotune* const rt, int numVoices, int bufSize, int frameSize, tMempool* const mp)
1544+void tAutotune_initToPool (tAutotune* const rt, int numVoices, int bufSize, tMempool* const mp)
17081545 {
17091546 _tMempool* m = *mp;
17101547 _tAutotune* r = *rt = (_tAutotune*) mpool_alloc(sizeof(_tAutotune), m);
17111548 r->mempool = *mp;
17121549
17131550 r->bufSize = bufSize;
1714− r->frameSize = frameSize;
17151551 r->numVoices = numVoices;
17161552
1717− r->inBuffer = (float*) mpool_alloc(sizeof(float) * r->bufSize, m);
1718− r->outBuffers = (float**) mpool_alloc(sizeof(float*) * r->numVoices, m);
1553+ r->inBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
1554+ r->outBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
1555+ r->lastOutBuffer = (float*) mpool_calloc(sizeof(float) * r->bufSize, m);
17191556
1720− r->hopSize = DEFHOPSIZE;
1721− r->windowSize = DEFWINDOWSIZE;
1722− r->fba = FBA;
1723− tAutotune_setTimeConstant(rt, DEFTIMECONSTANT);
1557+ r->index = 0;
17241558
1725− r->ps = (tPitchShift*) mpool_alloc(sizeof(tPitchShift) * r->numVoices, m);
1726− r->freq = (float*) mpool_alloc(sizeof(float) * r->numVoices, m);
1727− r->tickOutput = (float*) mpool_alloc(sizeof(float) * r->numVoices, m);
1728− for (int i = 0; i < r->numVoices; ++i)
1729− {
1730− r->outBuffers[i] = (float*) mpool_alloc(sizeof(float) * r->bufSize, m);
1731− }
1559+ r->ps = (tPitchShift*) mpool_calloc(sizeof(tPitchShift) * r->numVoices, m);
1560+ r->freqs = (float*) mpool_calloc(sizeof(float) * r->numVoices, m);
17321561
17331562 tDualPitchDetector_initToPool(&r->dp, mtof(48), mtof(72), mp);
1734− tPeriodDetection_initToPool(&r->pd, r->inBuffer, r->outBuffers[0], r->bufSize, r->frameSize, mp);
1735−
1563+
17361564 for (int i = 0; i < r->numVoices; ++i)
17371565 {
1738− tPitchShift_initToPool(&r->ps[i], &r->pd, &r->dp, r->outBuffers[i], r->bufSize, mp);
1566+ tPitchShift_initToPool(&r->ps[i], &r->dp, mp);
17391567 }
1740−
1741− r->inputPeriod = 0.0f;
1742− r->shiftOn = 0;
17431568 }
17441569
17451570 void tAutotune_free (tAutotune* const rt)
17461571 {
17471572 _tAutotune* r = *rt;
17481573
1574+ tDualPitchDetector_free(&r->dp);
17491575 tPeriodDetection_free(&r->pd);
17501576 for (int i = 0; i < r->numVoices; ++i)
17511577 {
17521578 tPitchShift_free(&r->ps[i]);
1753− mpool_free((char*)r->outBuffers[i], r->mempool);
17541579 }
1755− mpool_free((char*)r->tickOutput, r->mempool);
1756− mpool_free((char*)r->freq, r->mempool);
1580+ mpool_free((char*)r->freqs, r->mempool);
17571581 mpool_free((char*)r->ps, r->mempool);
17581582 mpool_free((char*)r->inBuffer, r->mempool);
1759− mpool_free((char*)r->outBuffers, r->mempool);
1583+ mpool_free((char*)r->outBuffer, r->mempool);
17601584 mpool_free((char*)r, r->mempool);
17611585 }
17621586
1763−float* tAutotune_tick(tAutotune* const rt, float sample)
1587+float tAutotune_tick(tAutotune* const rt, float sample)
17641588 {
17651589 _tAutotune* r = *rt;
17661590
1767− tPeriodDetection_tick(&r->pd, sample);
17681591 tDualPitchDetector_tick(&r->dp, sample);
1769− r->inputPeriod = 1.0f / tDualPitchDetector_getFrequency(&r->dp);
17701592
1771−// if (tempPeriod < 1000.0f) //to avoid trying to follow consonants JS
1772−// {
1773−// r->inputPeriod = tempPeriod;
1774−// }
1775−
1776− for (int v = 0; v < r->numVoices; ++v)
1593+ r->inBuffer[r->index] = sample;
1594+ float out = r->outBuffer[r->index];
1595+ r->outBuffer[r->index++] = 0.0f;
1596+
1597+ if (r->index >= r->bufSize)
17771598 {
1778− r->tickOutput[v] = tPitchShift_shiftToFreq(&r->ps[v], r->freq[v]);
1599+ for (int i = 0; i < r->numVoices; ++i)
1600+ {
1601+ tPitchShift_shiftTo(&r->ps[i], r->freqs[i], r->inBuffer, r->outBuffer, r->bufSize);
1602+ }
1603+ r->index = 0;
17791604 }
1780−
1781− return r->tickOutput;
1605+
1606+ return out;
17821607 }
17831608
17841609 void tAutotune_setNumVoices(tAutotune* const rt, int numVoices)
@@ -1786,11 +1611,10 @@ void tAutotune_setNumVoices(tAutotune* const rt, int numVoices) · 1 unmodified line
17861611 _tAutotune* r = *rt;
17871612
17881613 int bufSize = r->bufSize;
1789− int frameSize = r->frameSize;
17901614 tMempool mempool = r->mempool;
17911615
17921616 tAutotune_free(rt);
1793− tAutotune_initToPool(rt, numVoices, bufSize, frameSize, &mempool);
1617+ tAutotune_initToPool(rt, numVoices, bufSize, &mempool);
17941618 }
17951619
17961620 void tAutotune_setFreqs(tAutotune* const rt, float f)
@@ -1799,7 +1623,7 @@ void tAutotune_setFreqs(tAutotune* const rt, float f) · 2 unmodified lines
17991623
18001624 for (int i = 0; i < r->numVoices; ++i)
18011625 {
1802− r->freq[i] = f;
1626+ r->freqs[i] = f;
18031627 }
18041628 }
18051629
@@ -1807,65 +1631,7 @@ void tAutotune_setFreq(tAutotune* const rt, float f, int voice) · 1 unmodified line
18071631 {
18081632 _tAutotune* r = *rt;
18091633
1810− r->freq[voice] = f;
1811−}
1812−
1813−void tAutotune_setTimeConstant(tAutotune* const rt, float tc)
1814−{
1815− _tAutotune* r = *rt;
1816− LEAF* leaf = r->mempool->leaf;
1817−
1818− r->timeConstant = tc;
1819− r->radius = expf(-1000.0f * r->hopSize * leaf->invSampleRate / r->timeConstant);
1820−}
1821−
1822−void tAutotune_setHopSize(tAutotune* const rt, int hs)
1823−{
1824− _tAutotune* r = *rt;
1825−
1826− r->hopSize = hs;
1827− tPeriodDetection_setHopSize(&r->pd, r->hopSize);
1828−}
1829−
1830−void tAutotune_setWindowSize(tAutotune* const rt, int ws)
1831−{
1832− _tAutotune* r = *rt;
1833−
1834− r->windowSize = ws;
1835− tPeriodDetection_setWindowSize(&r->pd, r->windowSize);
1836−}
1837−
1838−void tAutotune_setFidelityThreshold(tAutotune* const rt, float threshold)
1839−{
1840− _tAutotune* r = *rt;
1841−
1842− tPeriodDetection_setFidelityThreshold(&r->pd, threshold);
1843−}
1844−
1845−void tAutotune_setAlpha (tAutotune* rt, float alpha)
1846−{
1847− _tAutotune* r = *rt;
1848− tPeriodDetection_setAlpha(&r->pd, alpha);
1849−}
1850−
1851−void tAutotune_setTolerance (tAutotune* rt, float tolerance)
1852−{
1853− _tAutotune* r = *rt;
1854− tPeriodDetection_setTolerance(&r->pd, tolerance);
1855−}
1856−
1857−float tAutotune_getInputPeriod(tAutotune* const rt)
1858−{
1859− _tAutotune* r = *rt;
1860−
1861− return r->inputPeriod;
1862−}
1863−
1864−float tAutotune_getInputFreq(tAutotune* const rt)
1865−{
1866− _tAutotune* r = *rt;
1867−
1868− return 1.0f/r->inputPeriod;
1634+ r->freqs[voice] = f;
18691635 }
18701636
18711637 //============================================================================================================

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