History

mostly working minblep sampler

Matthew Wang committed 6 years ago

Commit 382ddd11 parent d634fb36 files · +480 −101Browse files

Unified · Split

6 files
  1. TestPlugin/Source/MyTest.cpp +44 −55
  2. leaf/Inc/leaf-math.h +4 −0
  3. leaf/Inc/leaf-sampling.h +102 −0
  4. leaf/Src/leaf-math.c +48 −0
  5. leaf/Src/leaf-oscillators.c +0 −46
  6. leaf/Src/leaf-sampling.c +282 −0
TestPlugin/Source/MyTest.cpp+44−55
@@ -14,21 +14,6 @@ static void leaf_pool_report(void); · 13 unmodified lines
1414 static void leaf_pool_dump(void);
1515 static void run_pool_test(void);
1616
17−tNoise noise;
18−tSVF bp1;
19−tSVF bp2;
20−tFormantShifter fs;
21−
22−tSampler samp;
23−tBuffer buff;
24−tEnvelope env;
25−
26−tAutotune at;
27−
28−tPhasor phasor;
29−
30−tHighpass hipass;
31−
3217 tMBSaw bsaw;
3318 tMBTriangle btri;
3419 tMBPulse bpulse;
@@ -42,6 +27,9 @@ tEnvelopeFollower ef; · 7 unmodified lines
4227
4328 tCycle sine;
4429
30+tBuffer samp;
31+tMBSampler sampler;
32+
4533 float gain;
4634 float dtime;
4735 bool buttonState;
@@ -53,7 +41,7 @@ float a, b, c, d; · 5 unmodified lines
5341 float* bufIn;
5442 float* bufOut;
5543
56−#define MSIZE 500000
44+#define MSIZE 2048000
5745 char memory[MSIZE];
5846
5947 void LEAFTest_init (float sampleRate, int blockSize)
@@ -76,6 +64,12 @@ void LEAFTest_init (float sampleRate, int blockSize) · 16 unmodified lines
7664
7765 tCycle_init(&sine);
7866 tCycle_setFreq(&sine, 100);
67+
68+ tBuffer_init(&samp, 5.0f * leaf.sampleRate);
69+ tMBSampler_init(&sampler, &samp);
70+
71+ tMBSampler_setMode(&sampler, PlayLoop);
72+ tMBSampler_setEnd(&sampler, samp->bufferLength);
7973 }
8074
8175 inline double getSawFall(double angle) {
@@ -90,6 +84,11 @@ inline double getSawFall(double angle) { · 8 unmodified lines
9084 float lastFreq;
9185 float LEAFTest_tick (float input)
9286 {
87+ tBuffer_tick(&samp, input);
88+
89+ return tMBSampler_tick(&sampler);
90+
91+
9392 // tMBSaw_setFreq(&bsaw, x);
9493 // tMBTriangle_setFreq(&btri, x);
9594 // tMBPulse_setFreq(&bpulse, x);
@@ -102,21 +101,21 @@ float LEAFTest_tick (float input) · 6 unmodified lines
102101 // return tMBPulse_tick(&bpulse);
103102
104103
105− float freq = 1.0f/tPeriodDetection_tick(&pd, input) * leaf.sampleRate;
106− tPitchDetector_tick(&detector, input);
107− float altFreq = tPitchDetector_getFrequency(&detector);
108−
109− if (fabsf(1.0f - (freq / altFreq)) < 0.05f)
110−// if (tZeroCrossingCounter_tick(&zc, input) < 0.05 && freq > 0.0f)
111− {
112− tCycle_setFreq(&sine, altFreq);
113− }
114−
115− float g = tEnvelopeFollower_tick(&ef, input);
116−
117− lastFreq = freq;
118−
119− return tCycle_tick(&sine) * g * 2.0f + input;
104+// float freq = 1.0f/tPeriodDetection_tick(&pd, input) * leaf.sampleRate;
105+// tPitchDetector_tick(&detector, input);
106+// float altFreq = tPitchDetector_getFrequency(&detector);
107+//
108+// if (fabsf(1.0f - (freq / altFreq)) < 0.05f)
109+//// if (tZeroCrossingCounter_tick(&zc, input) < 0.05 && freq > 0.0f)
110+// {
111+// tCycle_setFreq(&sine, altFreq);
112+// }
113+//
114+// float g = tEnvelopeFollower_tick(&ef, input);
115+//
116+// lastFreq = freq;
117+//
118+// return tCycle_tick(&sine) * g * 2.0f + input;
120119 }
121120
122121 int firstFrame = 1;
@@ -129,37 +128,27 @@ void LEAFTest_block (void) · 6 unmodified lines
129128 DBG(tPitchDetector_getFrequency(&detector));
130129 DBG(tPitchDetector_getPeriodicity(&detector));
131130 }
132− // if (firstFrame == 1)
133− // {
134− // tBuffer_record(&buff); // starts recording
135− // tSampler_play(&samp); // start spitting samples out
136− // firstFrame = 0;
137− // }
138131
139− float val = getSliderValue("mod freq");
132+ float val = getSliderValue("on/off");
140133
141− x = val;
142−
143−
134+ if (val > 0.5f && !sampler->active)
135+ {
136+ tBuffer_record(&samp);
137+ tMBSampler_play(&sampler);
138+ }
139+ else if (val < 0.5f && sampler->active)
140+ {
141+ tMBSampler_stop(&sampler);
142+ }
144143
145− // a = val * tBuffer_getBufferLength(&buff);
144+ val = getSliderValue("mod freq");
146145
147−// DBG("start: " + String(a));
146+ tMBSampler_setStart(&sampler, val * 5.0f * leaf.sampleRate);
147+
148148
149149 val = getSliderValue("mod depth");
150150
151− y = val * 2.0f - 1.0f;
152−// DBG(String(y));
153− // b = val * tBuffer_getBufferLength(&buff);
154−
155−// DBG("rate: " + String(b));
156− //
157− // tSampler_setStart(&samp, a);
158− // tSampler_setEnd(&samp, b);
159− // tSampler_setRate(&samp, b);
160−
161− // tFormantShifter_setShiftFactor(&fs, x);
162− // tFormantShifter_setIntensity(&fs, y);
151+ tMBSampler_setRate(&sampler, val * 8.0f - 4.0f);
163152
164153 }
165154
leaf/Inc/leaf-math.h+4−0
@@ -225,6 +225,10 @@ extern "C" { · 224 unmodified lines
225225
226226 float median3f(float a, float b, float c);
227227
228+ void place_step_dd(float *buffer, int index, float phase, float w, float scale);
229+
230+ void place_slope_dd(float *buffer, int index, float phase, float w, float slope_delta);
231+
228232 /*! @} */
229233
230234 //==============================================================================
leaf/Inc/leaf-sampling.h+102−0
@@ -352,6 +352,108 @@ extern "C" { · 351 unmodified lines
352352 void tAutoSampler_setWindowSize (tAutoSampler* const, uint32_t size);
353353 void tAutoSampler_setCrossfadeLength (tAutoSampler* const, uint32_t length);
354354 void tAutoSampler_setRate (tAutoSampler* const, float rate);
355+
356+
357+/*!
358+ @defgroup tMBSampler tMBSampler
359+ @ingroup sampling
360+ @brief
361+ @{
362+
363+ @fn void tMBSampler_init (tMBSampler* const, tBuffer* const)
364+ @brief
365+ @param
366+
367+ @fn void tMBSampler_initToPool (tMBSampler* const, tBuffer* const, tMempool* const)
368+ @brief
369+ @param
370+
371+ @fn void tMBSampler_free (tMBSampler* const)
372+ @brief
373+ @param
374+
375+ @fn float tMBSampler_tick (tMBSampler* const)
376+ @brief
377+ @param
378+
379+ @fn void tMBSampler_setSample (tMBSampler* const, tBuffer* const)
380+ @brief
381+ @param
382+
383+ @fn void tMBSampler_setMode (tMBSampler* const, PlayMode mode)
384+ @brief
385+ @param
386+
387+ @fn void tMBSampler_play (tMBSampler* const)
388+ @brief
389+ @param
390+
391+ @fn void tMBSampler_stop (tMBSampler* const)
392+ @brief
393+ @param
394+
395+ @fn void tMBSampler_setStart (tMBSampler* const, int32_t start)
396+ @brief
397+ @param
398+
399+ @fn void tMBSampler_setEnd (tMBSampler* const, int32_t end)
400+ @brief
401+ @param
402+
403+ @fn void tMBSampler_setLength (tMBSampler* const, int32_t length)
404+ @brief
405+ @param
406+
407+ @fn void tMBSampler_setCrossfadeLength (tMBSampler* const sp, uint32_t length)
408+ @brief
409+ @param
410+
411+ @fn void tMBSampler_setRate (tMBSampler* const, float rate)
412+ @brief
413+ @param
414+
415+ @} */
416+
417+#define FILLEN 256
418+
419+ typedef struct _tMBSampler
420+ {
421+ tMempool mempool;
422+
423+ tBuffer samp;
424+ PlayMode mode;
425+ int active;
426+
427+ tExpSmooth gain;
428+
429+ float out;
430+ float last_delta;
431+ float amp;
432+ float last_amp;
433+ float syncin;
434+ float _p, _w, _z;
435+ float _last_w;
436+ float _f [FILLEN + STEP_DD_PULSE_LENGTH];
437+ int _j;
438+
439+ int start, end;
440+ } _tMBSampler;
441+
442+ typedef _tMBSampler* tMBSampler;
443+
444+ void tMBSampler_init (tMBSampler* const, tBuffer* const);
445+ void tMBSampler_initToPool (tMBSampler* const, tBuffer* const, tMempool* const);
446+ void tMBSampler_free (tMBSampler* const);
447+
448+ float tMBSampler_tick (tMBSampler* const);
449+ void tMBSampler_setSample (tMBSampler* const, tBuffer* const);
450+ void tMBSampler_setMode (tMBSampler* const, PlayMode mode);
451+ void tMBSampler_play (tMBSampler* const);
452+ void tMBSampler_stop (tMBSampler* const);
453+ void tMBSampler_setStart (tMBSampler* const, int32_t start);
454+ void tMBSampler_setEnd (tMBSampler* const, int32_t end);
455+ void tMBSampler_setLength (tMBSampler* const, int32_t length);
456+ void tMBSampler_setRate (tMBSampler* const, float rate);
355457
356458 #ifdef __cplusplus
357459 }
leaf/Src/leaf-math.c+48−0
@@ -786,3 +786,51 @@ float median3f(float a, float b, float c) · 785 unmodified lines
786786 {
787787 return fmax(fmin(a, b), fmin(fmax(a, b), c));
788788 }
789+
790+/// MINBLEPS
791+
792+// https://github.com/MrBlueXav/Dekrispator_v2 blepvco.c
793+
794+
795+void place_step_dd(float *buffer, int index, float phase, float w, float scale)
796+{
797+ float r;
798+ int i;
799+
800+ r = MINBLEP_PHASES * phase / w;
801+ i = floorf(r); //rintf(r - 0.5f); is there a reason to use rintf here
802+ r -= (float)i;
803+ i &= MINBLEP_PHASE_MASK; /* extreme modulation can cause i to be out-of-range */
804+ /* this would be better than the above, but more expensive:
805+ * while (i < 0) {
806+ * i += MINBLEP_PHASES;
807+ * index++;
808+ * }
809+ */
810+
811+ while (i < MINBLEP_PHASES * STEP_DD_PULSE_LENGTH) {
812+ buffer[index] += scale * (step_dd_table[i].value + r * step_dd_table[i].delta);
813+ i += MINBLEP_PHASES;
814+ index++;
815+ }
816+}
817+//----------------------------------------------------------------------------------------------------------
818+
819+void place_slope_dd(float *buffer, int index, float phase, float w, float slope_delta)
820+{
821+ float r;
822+ int i;
823+
824+ r = MINBLEP_PHASES * phase / w;
825+ i = rintf(r - 0.5f);
826+ r -= (float)i;
827+ i &= MINBLEP_PHASE_MASK; /* extreme modulation can cause i to be out-of-range */
828+
829+ slope_delta *= w;
830+
831+ while (i < MINBLEP_PHASES * SLOPE_DD_PULSE_LENGTH) {
832+ buffer[index] += slope_delta * (slope_dd_table[i] + r * (slope_dd_table[i + 1] - slope_dd_table[i]));
833+ i += MINBLEP_PHASES;
834+ index++;
835+ }
836+}
leaf/Src/leaf-oscillators.c+0−46
@@ -868,53 +868,7 @@ void tNeuron_setCurrent (tNeuron* const nr, float current) · 867 unmodified lines
868868 }
869869
870870
871−/// MINBLEPS
872871
873−// https://github.com/MrBlueXav/Dekrispator_v2 blepvco.c
874−
875−
876−static void place_step_dd(float *buffer, int index, float phase, float w, float scale)
877−{
878− float r;
879− int i;
880−
881− r = MINBLEP_PHASES * phase / w;
882− i = rintf(r - 0.5f);
883− r -= (float)i;
884− i &= MINBLEP_PHASE_MASK; /* extreme modulation can cause i to be out-of-range */
885− /* this would be better than the above, but more expensive:
886− * while (i < 0) {
887− * i += MINBLEP_PHASES;
888− * index++;
889− * }
890− */
891−
892− while (i < MINBLEP_PHASES * STEP_DD_PULSE_LENGTH) {
893− buffer[index] += scale * (step_dd_table[i].value + r * step_dd_table[i].delta);
894− i += MINBLEP_PHASES;
895− index++;
896− }
897−}
898−//----------------------------------------------------------------------------------------------------------
899−
900−static void place_slope_dd(float *buffer, int index, float phase, float w, float slope_delta)
901−{
902− float r;
903− int i;
904−
905− r = MINBLEP_PHASES * phase / w;
906− i = rintf(r - 0.5f);
907− r -= (float)i;
908− i &= MINBLEP_PHASE_MASK; /* extreme modulation can cause i to be out-of-range */
909−
910− slope_delta *= w;
911−
912− while (i < MINBLEP_PHASES * SLOPE_DD_PULSE_LENGTH) {
913− buffer[index] += slope_delta * (slope_dd_table[i] + r * (slope_dd_table[i + 1] - slope_dd_table[i]));
914− i += MINBLEP_PHASES;
915− index++;
916− }
917−}
918872 //----------------------------------------------------------------------------------------------------------
919873
920874 void tMBPulse_init(tMBPulse* const osc)
leaf/Src/leaf-sampling.c+282−0
@@ -875,3 +875,285 @@ void tAutoSampler_setRate (tAutoSampler* const as, float rate) · 874 unmodified lines
875875 {
876876 ;
877877 }
878+
879+
880+
881+void tMBSampler_init(tMBSampler* const sp, tBuffer* const b)
882+{
883+ tMBSampler_initToPool(sp, b, &leaf.mempool);
884+}
885+
886+void tMBSampler_initToPool(tMBSampler* const sp, tBuffer* const b, tMempool* const mp)
887+{
888+ _tMempool* m = *mp;
889+ _tMBSampler* c = *sp = (_tMBSampler*) mpool_alloc(sizeof(_tMBSampler), m);
890+ c->mempool = m;
891+
892+ c->samp = *b;
893+
894+ c->mode = PlayLoop;
895+ c->active = 0;
896+
897+ tExpSmooth_initToPool(&c->gain, 0.0f, 0.01f, mp);
898+
899+ c->amp = 1.0f;
900+ c->_p = 0.0f;
901+ c->_w = 1.0f;
902+ c->syncin = 0.0f;
903+ c->_z = 0.0f;
904+ c->_j = 0;
905+ memset (c->_f, 0, (FILLEN + STEP_DD_PULSE_LENGTH) * sizeof (float));
906+
907+ c->start = 0;
908+ tMBSampler_setEnd(sp, c->samp->bufferLength);
909+}
910+
911+void tMBSampler_free (tMBSampler* const sp)
912+{
913+ _tMBSampler* p = *sp;
914+
915+ tExpSmooth_free(&p->gain);
916+
917+ mpool_free((char*)p, p->mempool);
918+}
919+
920+void tMBSampler_setSample (tMBSampler* const sp, tBuffer* const b)
921+{
922+ _tMBSampler* p = *sp;
923+
924+ p->samp = *b;;
925+
926+ p->start = 0;
927+ tMBSampler_setEnd(sp, p->samp->bufferLength);
928+
929+ p->_p = 0.0f;
930+}
931+
932+
933+
934+float tMBSampler_tick (tMBSampler* const sp)
935+{
936+ _tMBSampler* c = *sp;
937+
938+ if (c->gain->curr == 0.0f && !c->active) return 0.0f;
939+ if (c->_w == 0.0f) return c->out;
940+
941+ int start, end, length;
942+ float* buff;
943+ int j;
944+ float syncin;
945+ float a, p, w, z;
946+ syncin = c->syncin;
947+
948+ start = c->start;
949+ end = c->end;
950+
951+ buff = c->samp->buff;
952+
953+ p = c->_p; /* position */
954+ w = c->_w; /* rate */
955+ z = c->_z; /* low pass filter state */
956+ j = c->_j; /* index into buffer _f */
957+
958+ length = end - start;
959+
960+ //a = 0.2 + 0.8 * vco->_port [FILT];
961+ a = 0.5f; // when a = 1, LPfilter is disabled
962+
963+ p += w;
964+
965+ float next;
966+
967+// if (syncin >= 1e-20f) { /* sync to master */
968+//
969+// float eof_offset = (syncin - 1e-20f) * w;
970+// float p_at_reset = p - eof_offset;
971+// p = eof_offset;
972+//
973+// /* place any DD that may have occurred in subsample before reset */
974+// if (p_at_reset >= end) {
975+// while (p_at_reset >= (float) end) p_at_reset -= (float) length;
976+//
977+// float f = p_at_reset + eof_offset;
978+// int i = (int) f;
979+// f -= i;
980+// float n = buff[i] * (1.0f - f) + buff[i+1] * f;
981+//
982+// place_step_dd(c->_f, j, p_at_reset + eof_offset, w,
983+// n - c->out);
984+// place_slope_dd(c->_f, j, p_at_reset + eof_offset, w, (n - c->out) - c->last_delta);
985+// }
986+//
987+// float f = p_at_reset;
988+// int i = (int) f;
989+// f -= i;
990+// next = buff[i] * (1.0f - f) + buff[i+1] * f;
991+//
992+// /* now place reset DD */
993+// place_step_dd(c->_f, j, p, w, next - c->out);
994+// place_slope_dd(c->_f, j, p, w, (next - c->out) - c->last_delta);
995+//
996+// } else
997+ if (w > 0.0f) {
998+
999+ if (p >= (float) end) { /* normal phase reset */
1000+
1001+ // start and end are never negative and end must also be greater than start
1002+ // so this loop is fine
1003+ while (p >= (float) end) p -= (float) length;
1004+
1005+ float f = p;
1006+ int i = (int) f;
1007+ f -= i;
1008+ next = buff[i] * (1.0f - f) + buff[i+1] * f;
1009+
1010+ place_step_dd(c->_f, j, p - start, w, next - c->out);
1011+ place_slope_dd(c->_f, j, p - start, w, (next - c->out) - c->last_delta);
1012+
1013+ if (c->mode == PlayNormal) c->active = 0;
1014+ else if (c->mode == PlayBackAndForth) w = -w;
1015+ }
1016+// else if (p < (float) start) { /* start has been set ahead of the current phase */
1017+//
1018+// p = (float) start;
1019+// next = buff[start];
1020+//
1021+// place_step_dd(c->_f, j, p, w, next - c->last);
1022+// place_slope_dd(c->_f, j, p, w, (next - c->last) - c->last_delta);
1023+// }
1024+ else {
1025+
1026+ float f = p;
1027+ int i = (int) f;
1028+ f -= i;
1029+ next = buff[i] * (1.0f - f) + buff[i+1] * f;
1030+ }
1031+
1032+ if (c->_last_w < 0.0f)
1033+ {
1034+ place_slope_dd(c->_f, j, p - start, w, (next - c->out) - c->last_delta);
1035+ }
1036+
1037+ } else { // if (w < 0.0f) {
1038+
1039+ if (p < (float) start) {
1040+
1041+ while (p < (float) start) p += (float) length;
1042+
1043+ float f = p;
1044+ int i = (int) f;
1045+ f -= i;
1046+ next = buff[i] * (1.0f - f) + buff[i+1] * f;
1047+
1048+ place_step_dd(c->_f, j, end - p, w, next - c->out);
1049+ place_slope_dd(c->_f, j, end - p, w, (next - c->out) - c->last_delta);
1050+
1051+ if (c->mode == PlayNormal) c->active = 0;
1052+ else if (c->mode == PlayBackAndForth) w = -w;
1053+ }
1054+// else if (p >= (float) end) {
1055+//
1056+// p = (float) end - 1;
1057+// next = buff[end - 1];
1058+// place_step_dd(c->_f, j, p, w, next - c->last);
1059+// place_slope_dd(c->_f, j, p, w, (next - c->last) - c->last_delta);
1060+// }
1061+ else {
1062+
1063+ float f = p;
1064+ int i = (int) f;
1065+ f -= i;
1066+ next = buff[i] * (1.0f - f) + buff[i+1] * f;
1067+ }
1068+
1069+ if (c->_last_w > 0.0f)
1070+ {
1071+ place_slope_dd(c->_f, j, end - p, w, (next - c->out) - c->last_delta);
1072+ }
1073+ }
1074+
1075+ c->_f[j + DD_SAMPLE_DELAY] += next;
1076+
1077+ z += a * (c->_f[j] - z); // LP filtering
1078+ next = c->amp * z;
1079+
1080+ c->last_delta = next - c->out;
1081+
1082+ c->out = next;
1083+
1084+ if (++j == FILLEN)
1085+ {
1086+ j = 0;
1087+ memcpy (c->_f, c->_f + FILLEN, STEP_DD_PULSE_LENGTH * sizeof (float));
1088+ memset (c->_f + STEP_DD_PULSE_LENGTH, 0, FILLEN * sizeof (float));
1089+ }
1090+
1091+ c->_p = p;
1092+ c->_w = c->_last_w = w;
1093+ c->_z = z;
1094+ c->_j = j;
1095+
1096+ return c->out * tExpSmooth_tick(&c->gain);
1097+}
1098+
1099+void tMBSampler_setMode (tMBSampler* const sp, PlayMode mode)
1100+{
1101+ _tMBSampler* p = *sp;
1102+ p->mode = mode;
1103+}
1104+
1105+void tMBSampler_play (tMBSampler* const sp)
1106+{
1107+ _tMBSampler* p = *sp;
1108+
1109+ if (p->active > 0)
1110+ {
1111+ p->syncin = 1e-20f;
1112+ }
1113+ tExpSmooth_setDest(&p->gain, 1.0f);
1114+ p->active = 1;
1115+ p->_p = p->start;
1116+ p->_z = 0.0f;
1117+ p->_j = 0;
1118+}
1119+
1120+void tMBSampler_stop (tMBSampler* const sp)
1121+{
1122+ _tMBSampler* p = *sp;
1123+
1124+ tExpSmooth_setDest(&p->gain, 0.0f);
1125+ p->active = 0;
1126+}
1127+
1128+void tMBSampler_setStart (tMBSampler* const sp, int32_t start)
1129+{
1130+ _tMBSampler* p = *sp;
1131+
1132+ if (start > p->end - 1) p->start = p->end - 1;
1133+ else if (start < 0) p->start = 0;
1134+ else p->start = start;
1135+}
1136+
1137+void tMBSampler_setEnd (tMBSampler* const sp, int32_t end)
1138+{
1139+ _tMBSampler* p = *sp;
1140+
1141+ if (end < p->start + 1) p->end = p->start + 1;
1142+ // always leave a trailing sample after the end
1143+ else if (end >= p->samp->bufferLength) p->end = p->samp->bufferLength - 1;
1144+ else p->end = end;
1145+}
1146+
1147+void tMBSampler_setLength (tMBSampler* const sp, int32_t length)
1148+{
1149+ _tMBSampler* p = *sp;
1150+ tMBSampler_setEnd(sp, p->start + length);
1151+}
1152+
1153+void tMBSampler_setRate (tMBSampler* const sp, float rate)
1154+{
1155+ _tMBSampler* p = *sp;
1156+
1157+ p->_w = rate;
1158+}
1159+

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