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
14static void leaf_pool_dump(void);14static void leaf_pool_dump(void);
15static void run_pool_test(void);15static void run_pool_test(void);
16 16
17tNoise noise;
18tSVF bp1;
19tSVF bp2;
20tFormantShifter fs;
21
22tSampler samp;
23tBuffer buff;
24tEnvelope env;
25
26tAutotune at;
27
28tPhasor phasor;
29
30tHighpass hipass;
31
32tMBSaw bsaw;17tMBSaw bsaw;
33tMBTriangle btri;18tMBTriangle btri;
34tMBPulse bpulse;19tMBPulse bpulse;
@@ -42,6 +27,9 @@ tEnvelopeFollower ef; · 7 unmodified lines
42 27
43tCycle sine;28tCycle sine;
44 29
30tBuffer samp;
31tMBSampler sampler;
32
45float gain;33float gain;
46float dtime;34float dtime;
47bool buttonState;35bool buttonState;
@@ -53,7 +41,7 @@ float a, b, c, d; · 5 unmodified lines
53float* bufIn;41float* bufIn;
54float* bufOut;42float* bufOut;
55 43
56#define MSIZE 50000044#define MSIZE 2048000
57char memory[MSIZE];45char memory[MSIZE];
58 46
59void LEAFTest_init (float sampleRate, int blockSize)47void LEAFTest_init (float sampleRate, int blockSize)
@@ -76,6 +64,12 @@ void LEAFTest_init (float sampleRate, int blockSize) · 16 unmodified lines
76 64
77 tCycle_init(&sine);65 tCycle_init(&sine);
78 tCycle_setFreq(&sine, 100);66 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);
79}73}
80 74
81inline double getSawFall(double angle) {75inline double getSawFall(double angle) {
@@ -90,6 +84,11 @@ inline double getSawFall(double angle) { · 8 unmodified lines
90float lastFreq;84float lastFreq;
91float LEAFTest_tick (float input)85float LEAFTest_tick (float input)
92{86{
87 tBuffer_tick(&samp, input);
88
89 return tMBSampler_tick(&sampler);
90
91
93// tMBSaw_setFreq(&bsaw, x);92// tMBSaw_setFreq(&bsaw, x);
94// tMBTriangle_setFreq(&btri, x);93// tMBTriangle_setFreq(&btri, x);
95// tMBPulse_setFreq(&bpulse, x);94// tMBPulse_setFreq(&bpulse, x);
@@ -102,21 +101,21 @@ float LEAFTest_tick (float input) · 6 unmodified lines
102// return tMBPulse_tick(&bpulse);101// return tMBPulse_tick(&bpulse);
103 102
104 103
105 float freq = 1.0f/tPeriodDetection_tick(&pd, input) * leaf.sampleRate;104// float freq = 1.0f/tPeriodDetection_tick(&pd, input) * leaf.sampleRate;
106 tPitchDetector_tick(&detector, input);105// tPitchDetector_tick(&detector, input);
107 float altFreq = tPitchDetector_getFrequency(&detector);106// float altFreq = tPitchDetector_getFrequency(&detector);
108 107//
109 if (fabsf(1.0f - (freq / altFreq)) < 0.05f)108// if (fabsf(1.0f - (freq / altFreq)) < 0.05f)
110// if (tZeroCrossingCounter_tick(&zc, input) < 0.05 && freq > 0.0f)109//// if (tZeroCrossingCounter_tick(&zc, input) < 0.05 && freq > 0.0f)
111 {110// {
112 tCycle_setFreq(&sine, altFreq);111// tCycle_setFreq(&sine, altFreq);
113 }112// }
114 113//
115 float g = tEnvelopeFollower_tick(&ef, input);114// float g = tEnvelopeFollower_tick(&ef, input);
116 115//
117 lastFreq = freq;116// lastFreq = freq;
118 117//
119 return tCycle_tick(&sine) * g * 2.0f + input;118// return tCycle_tick(&sine) * g * 2.0f + input;
120}119}
121 120
122int firstFrame = 1;121int firstFrame = 1;
@@ -129,37 +128,27 @@ void LEAFTest_block (void) · 6 unmodified lines
129 DBG(tPitchDetector_getFrequency(&detector));128 DBG(tPitchDetector_getFrequency(&detector));
130 DBG(tPitchDetector_getPeriodicity(&detector));129 DBG(tPitchDetector_getPeriodicity(&detector));
131 }130 }
132 // if (firstFrame == 1)
133 // {
134 // tBuffer_record(&buff); // starts recording
135 // tSampler_play(&samp); // start spitting samples out
136 // firstFrame = 0;
137 // }
138 131
139 float val = getSliderValue("mod freq");132 float val = getSliderValue("on/off");
140 133
141 x = val;134 if (val > 0.5f && !sampler->active)
142 135 {
143 136 tBuffer_record(&samp);
137 tMBSampler_play(&sampler);
138 }
139 else if (val < 0.5f && sampler->active)
140 {
141 tMBSampler_stop(&sampler);
142 }
144 143
145 // a = val * tBuffer_getBufferLength(&buff);144 val = getSliderValue("mod freq");
146 145
147// DBG("start: " + String(a));146 tMBSampler_setStart(&sampler, val * 5.0f * leaf.sampleRate);
147
148 148
149 val = getSliderValue("mod depth");149 val = getSliderValue("mod depth");
150 150
151 y = val * 2.0f - 1.0f;151 tMBSampler_setRate(&sampler, val * 8.0f - 4.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);
163 152
164}153}
165 154
leaf/Inc/leaf-math.h+4−0
@@ -225,6 +225,10 @@ extern "C" { · 224 unmodified lines
225 225
226 float median3f(float a, float b, float c);226 float median3f(float a, float b, float c);
227 227
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
228 /*! @} */232 /*! @} */
229 233
230 //==============================================================================234 //==============================================================================
leaf/Inc/leaf-sampling.h+102−0
@@ -352,6 +352,108 @@ extern "C" { · 351 unmodified lines
352 void tAutoSampler_setWindowSize (tAutoSampler* const, uint32_t size);352 void tAutoSampler_setWindowSize (tAutoSampler* const, uint32_t size);
353 void tAutoSampler_setCrossfadeLength (tAutoSampler* const, uint32_t length);353 void tAutoSampler_setCrossfadeLength (tAutoSampler* const, uint32_t length);
354 void tAutoSampler_setRate (tAutoSampler* const, float rate);354 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);
355 457
356#ifdef __cplusplus458#ifdef __cplusplus
357}459}
leaf/Src/leaf-math.c+48−0
@@ -786,3 +786,51 @@ float median3f(float a, float b, float c) · 785 unmodified lines
786{786{
787 return fmax(fmin(a, b), fmin(fmax(a, b), c));787 return fmax(fmin(a, b), fmin(fmax(a, b), c));
788}788}
789
790/// MINBLEPS
791
792// https://github.com/MrBlueXav/Dekrispator_v2 blepvco.c
793
794
795void 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
819void 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
868}868}
869 869
870 870
871/// MINBLEPS
872 871
873// https://github.com/MrBlueXav/Dekrispator_v2 blepvco.c
874
875
876static 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
900static 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}
918//----------------------------------------------------------------------------------------------------------872//----------------------------------------------------------------------------------------------------------
919 873
920void tMBPulse_init(tMBPulse* const osc)874void 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
875{875{
876 ;876 ;
877}877}
878
879
880
881void tMBSampler_init(tMBSampler* const sp, tBuffer* const b)
882{
883 tMBSampler_initToPool(sp, b, &leaf.mempool);
884}
885
886void 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
911void 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
920void 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
934float 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
1099void tMBSampler_setMode (tMBSampler* const sp, PlayMode mode)
1100{
1101 _tMBSampler* p = *sp;
1102 p->mode = mode;
1103}
1104
1105void 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
1120void tMBSampler_stop (tMBSampler* const sp)
1121{
1122 _tMBSampler* p = *sp;
1123
1124 tExpSmooth_setDest(&p->gain, 0.0f);
1125 p->active = 0;
1126}
1127
1128void 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
1137void 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
1147void tMBSampler_setLength (tMBSampler* const sp, int32_t length)
1148{
1149 _tMBSampler* p = *sp;
1150 tMBSampler_setEnd(sp, p->start + length);
1151}
1152
1153void 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