History

Sampler crossfade gone.

mulshine committed 7 years ago

Commit 3f1b8b21 parent 54f35b14 files · +116 −120Browse files

Unified · Split

4 files
  1. LEAF/Inc/leaf-sample.h +29 −28
  2. LEAF/Src/leaf-sample.c +65 −68
  3. LEAF_JUCEPlugin/Source/LEAFLink.cpp +2 −1
  4. LEAF_JUCEPlugin/Source/MyTest.cpp +20 −23
LEAF/Inc/leaf-sample.h+29−28
@@ -28,7 +28,7 @@ extern "C" { · 27 unmodified lines
28 RecordModeNil28 RecordModeNil
29 } RecordMode;29 } RecordMode;
30 30
31 typedef struct _tSample31 typedef struct _tBuffer
32 {32 {
33 float* buff;33 float* buff;
34 34
@@ -39,23 +39,23 @@ extern "C" { · 4 unmodified lines
39 39
40 int active;40 int active;
41 41
42 } tSample;42 } tBuffer;
43 43
44 void tSample_init (tSample* const, uint32_t length);44 void tBuffer_init (tBuffer* const, uint32_t length);
45 void tSample_free (tSample* const);45 void tBuffer_free (tBuffer* const);
46 46
47 void tSample_tick (tSample* const, float sample);47 void tBuffer_tick (tBuffer* const, float sample);
48 48
49 void tSample_read(tSample* const, float* buff, uint32_t len);49 void tBuffer_read(tBuffer* const, float* buff, uint32_t len);
50 50
51 float tSample_get (tSample* const, int idx);51 float tBuffer_get (tBuffer* const, int idx);
52 52
53 void tSample_start (tSample* const);53 void tBuffer_record (tBuffer* const);
54 void tSample_stop (tSample* const);54 void tBuffer_stop (tBuffer* const);
55 55
56 void tSample_setRecordMode (tSample* const, RecordMode mode);56 void tBuffer_setRecordMode (tBuffer* const, RecordMode mode);
57 57
58 void tSample_clear (tSample* const);58 void tBuffer_clear (tBuffer* const);
59 59
60//==============================================================================60//==============================================================================
61 61
@@ -67,44 +67,45 @@ extern "C" { · 5 unmodified lines
67 SampleModeNil67 SampleModeNil
68 } Mode;68 } Mode;
69 69
70 typedef struct _tSamplePlayer70 typedef struct _tSampler
71 {71 {
72 tSample* samp;72 tBuffer* samp;
73 73
74 float idx;74 float idx;
75 float inc;75 float inc;
76 float iinc;
76 int8_t dir;77 int8_t dir;
77 int8_t flip;78 int8_t flip;
78 79
79 int32_t start;80 int32_t start;
80 int32_t end;81 int32_t end;
81 uint32_t len;82 uint32_t len;
82 uint32_t cnt;83 uint32_t cfxlen;
83 84
84 Mode mode;85 Mode mode;
85 86
86 int active;87 int active;
87 88
88 float g1;89 } tSampler;
89 float g2;
90 } tSamplePlayer;
91 90
92 void tSamplePlayer_init (tSamplePlayer* const, tSample* s);91 void tSampler_init (tSampler* const, tBuffer* s);
93 void tSamplePlayer_free (tSamplePlayer* const);92 void tSampler_free (tSampler* const);
94 93
95 float tSamplePlayer_tick (tSamplePlayer* const);94 float tSampler_tick (tSampler* const);
96 95
97 void tSamplePlayer_setSample (tSamplePlayer* const, tSample* s);96 void tSampler_setSample (tSampler* const, tBuffer* s);
97
98 void tSampler_setMode (tSampler* const, Mode mode);
98 99
99 void tSamplePlayer_setMode (tSamplePlayer* const, Mode mode);100 void tSampler_play (tSampler* const);
101 void tSampler_stop (tSampler* const);
100 102
101 void tSamplePlayer_play (tSamplePlayer* const);103 void tSampler_setStart (tSampler* const, int32_t start);
102 void tSamplePlayer_stop (tSamplePlayer* const);104 void tSampler_setEnd (tSampler* const, int32_t end);
103 105
104 void tSamplePlayer_setStart (tSamplePlayer* const, int32_t start);106 void tSampler_setCrossfadeLength (tSampler* const p, uint32_t length);
105 void tSamplePlayer_setEnd (tSamplePlayer* const, int32_t end);
106 107
107 void tSamplePlayer_setRate (tSamplePlayer* const, float rate);108 void tSampler_setRate (tSampler* const, float rate);
108 109
109//==============================================================================110//==============================================================================
110 111
LEAF/Src/leaf-sample.c+65−68
@@ -23,7 +23,7 @@ · 22 unmodified lines
23 23
24//==============================================================================24//==============================================================================
25 25
26void tSample_init (tSample* const s, uint32_t length)26void tBuffer_init (tBuffer* const s, uint32_t length)
27{27{
28 s->buff = (float*) leaf_alloc( sizeof(float) * length);28 s->buff = (float*) leaf_alloc( sizeof(float) * length);
29 29
@@ -33,16 +33,16 @@ void tSample_init (tSample* const s, uint32_t length) · 3 unmodified lines
33 s->idx = 0;33 s->idx = 0;
34 s->mode = RecordOneShot;34 s->mode = RecordOneShot;
35 35
36 tSample_clear(s);36 tBuffer_clear(s);
37}37}
38 38
39void tSample_free (tSample* const s)39void tBuffer_free (tBuffer* const s)
40{40{
41 leaf_free(s->buff);41 leaf_free(s->buff);
42 leaf_free(s);42 leaf_free(s);
43}43}
44 44
45void tSample_tick (tSample* const s, float sample)45void tBuffer_tick (tBuffer* const s, float sample)
46{46{
47 if (s->active == 1)47 if (s->active == 1)
48 {48 {
@@ -54,7 +54,7 @@ void tSample_tick (tSample* const s, float sample) · 5 unmodified lines
54 {54 {
55 if (s->mode == RecordOneShot)55 if (s->mode == RecordOneShot)
56 {56 {
57 tSample_stop(s);57 tBuffer_stop(s);
58 }58 }
59 else if (s->mode == RecordLoop)59 else if (s->mode == RecordLoop)
60 {60 {
@@ -64,7 +64,7 @@ void tSample_tick (tSample* const s, float sample) · 3 unmodified lines
64 }64 }
65}65}
66 66
67void tSample_read(tSample* const s, float* buff, uint32_t len)67void tBuffer_read(tBuffer* const s, float* buff, uint32_t len)
68{68{
69 for (int i = 0; i < s->length; i++)69 for (int i = 0; i < s->length; i++)
70 {70 {
@@ -73,30 +73,30 @@ void tSample_read(tSample* const s, float* buff, uint32_t len) · 2 unmodified lines
73 }73 }
74}74}
75 75
76float tSample_get (tSample* const s, int idx)76float tBuffer_get (tBuffer* const s, int idx)
77{77{
78 if ((idx < 0) || (idx >= s->length)) return 0.f;78 if ((idx < 0) || (idx >= s->length)) return 0.f;
79 79
80 return s->buff[idx];80 return s->buff[idx];
81}81}
82 82
83void tSample_start(tSample* const s)83void tBuffer_record(tBuffer* const s)
84{84{
85 s->active = 1;85 s->active = 1;
86 s->idx = 0;86 s->idx = 0;
87}87}
88 88
89void tSample_stop(tSample* const s)89void tBuffer_stop(tBuffer* const s)
90{90{
91 s->active = 0;91 s->active = 0;
92}92}
93 93
94void tSample_setRecordMode (tSample* const s, RecordMode mode)94void tBuffer_setRecordMode (tBuffer* const s, RecordMode mode)
95{95{
96 s->mode = mode;96 s->mode = mode;
97}97}
98 98
99void tSample_clear (tSample* const s)99void tBuffer_clear (tBuffer* const s)
100{100{
101 for (int i = 0; i < s->length; i++)101 for (int i = 0; i < s->length; i++)
102 {102 {
@@ -104,11 +104,9 @@ void tSample_clear (tSample* const s) · 1 unmodified line
104 }104 }
105}105}
106 106
107//==============================================================================107//================================tSampler=====================================
108
109#define CFX 300
110 108
111void tSamplePlayer_init (tSamplePlayer* const p, tSample* s)109void tSampler_init (tSampler* const p, tBuffer* s)
112{110{
113 p->samp = s;111 p->samp = s;
114 112
@@ -121,30 +119,29 @@ void tSamplePlayer_init (tSamplePlayer* const p, tSample* s) · 6 unmodified lines
121 119
122 p->idx = 0.f;120 p->idx = 0.f;
123 p->inc = 1.f;121 p->inc = 1.f;
122 p->iinc = 1.f;
124 p->dir = 1;123 p->dir = 1;
125 p->flip = 1;124 p->flip = 1;
126 125
127 p->mode = Normal;126 p->mode = Normal;
128 127
129 p->g1 = 1.f;128 p->cfxlen = 300; // default 300 sample crossfade
130 p->g2 = 0.f;
131}129}
132 130
133void tSamplePlayer_free (tSamplePlayer* const p)131void tSampler_free (tSampler* const p)
134{132{
135 leaf_free(p->samp);133 leaf_free(p->samp);
136 leaf_free(p);134 leaf_free(p);
137}135}
138 136
139float tSamplePlayer_tick (tSamplePlayer* const p)137float tSampler_tick (tSampler* const p)
140{138{
141 if ((p->mode == Normal) && (p->cnt > 0)) p->active = 0;
142
143 if (p->active == 0 || (p->len < 4)) return 0.f;139 if (p->active == 0 || (p->len < 4)) return 0.f;
144 140
145 float sample = 0.f;141 float sample = 0.f;
146 float cfxsample = 0.f;142 float cfxsample = 0.f;
147 int cfx = CFX; // make this part of class143 int numsamps;
144 float g1 = 1.f, g2 = 0.f;
148 145
149 float* buff = p->samp->buff;146 float* buff = p->samp->buff;
150 147
@@ -152,16 +149,16 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line
152 149
153 int idx;150 int idx;
154 float alpha;151 float alpha;
155 152
156 if (dir > 0)153 if (dir > 0)
157 {154 {
158 idx = (int) p->idx;155 idx = (int) p->idx;
159 alpha = p->idx - idx;156 alpha = p->idx - idx;
160 }157 }
161 else158 else
162 {159 {
163 idx = (int) (p->idx + 1.f); // we think this is because flooring on int works different when reading backwards160 idx = (int) (p->idx + 1.f); // we think this is because flooring on int works different when reading backwards
164 alpha = (p->idx+1.f) - idx;161 alpha = (p->idx+1.f) - idx;
165 }162 }
166 163
167 int32_t start = p->start, end = p->end;164 int32_t start = p->start, end = p->end;
@@ -178,10 +175,6 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 10 unmodified lines
178 int i1 = idx-1;175 int i1 = idx-1;
179 int i3 = idx+1;176 int i3 = idx+1;
180 int i4 = idx+2;177 int i4 = idx+2;
181
182 if (i1 < start) i1 += p->len;
183 if (i3 > end) i3 -= p->len;
184 if (i4 > end) i4 -= p->len;
185 178
186 sample = LEAF_interpolate_hermite (buff[i1],179 sample = LEAF_interpolate_hermite (buff[i1],
187 buff[idx],180 buff[idx],
@@ -189,9 +182,11 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line
189 buff[i4],182 buff[i4],
190 alpha);183 alpha);
191 184
192 cfx = (end - idx) / p->inc; // num samples to end of loop, use inverse increment185 // num samples to end of loop
193 186 numsamps = (dir > 0) ? (end - idx) : (idx - start);
194 if (cfx <= CFX)187 numsamps *= p->iinc;
188
189 if (numsamps <= p->cfxlen)
195 {190 {
196 // CROSSFADE SAMPLE191 // CROSSFADE SAMPLE
197 float idxx = p->idx - p->len;192 float idxx = p->idx - p->len;
@@ -207,9 +202,9 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 9 unmodified lines
207 buff[i4],202 buff[i4],
208 alpha);203 alpha);
209 204
210 //rprintf("cidx: %d\n", cdx);205 g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen;
211 }206 }
212 else cfx = CFX;207 else g2 = 0.f;
213 }208 }
214 else209 else
215 {210 {
@@ -217,20 +212,16 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line
217 int i1 = idx+1;212 int i1 = idx+1;
218 int i3 = idx-1;213 int i3 = idx-1;
219 int i4 = idx-2;214 int i4 = idx-2;
220 215
221 if (i1 > end) i1 -= p->len;
222 if (i3 < start) i3 += p->len;
223 if (i4 < start) i4 += p->len;
224
225 sample = LEAF_interpolate_hermite (buff[i1],216 sample = LEAF_interpolate_hermite (buff[i1],
226 buff[idx],217 buff[idx],
227 buff[i3],218 buff[i3],
228 buff[i4],219 buff[i4],
229 1.0f-alpha);220 1.0f-alpha);
230 221
231 cfx = (idx - start) / p->inc;222 numsamps = (idx - start) / p->inc;
232 223
233 if (cfx <= CFX)224 if (numsamps <= p->cfxlen)
234 {225 {
235 // CROSSFADE SAMPLE226 // CROSSFADE SAMPLE
236 float idxx = p->idx + p->len + 1.f;227 float idxx = p->idx + p->len + 1.f;
@@ -246,8 +237,10 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 9 unmodified lines
246 buff[i3],237 buff[i3],
247 buff[i4],238 buff[i4],
248 1.f-alpha);239 1.f-alpha);
240
241 g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen;
249 }242 }
250 else cfx = CFX;243 else g2 = 0.f;
251 }244 }
252 245
253 p->idx += (dir * p->inc);246 p->idx += (dir * p->inc);
@@ -257,12 +250,10 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 3 unmodified lines
257 if (idx <= start)250 if (idx <= start)
258 {251 {
259 p->idx += (float)(p->len);252 p->idx += (float)(p->len);
260 p->cnt++;
261 }253 }
262 else if (idx >= end)254 else if (idx >= end)
263 {255 {
264 p->idx -= (float)(p->len);256 p->idx -= (float)(p->len);
265 p->cnt++;
266 }257 }
267 }258 }
268 else // == BackAndForth259 else // == BackAndForth
@@ -270,32 +261,37 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line
270 if ((idx <= start) || (idx >= end))261 if ((idx <= start) || (idx >= end))
271 {262 {
272 p->dir = -p->dir;263 p->dir = -p->dir;
273 p->cnt++;
274 }264 }
275 }265 }
276 266
277 p->g2 = (float) (CFX - cfx) / (float) CFX;267 g1 = 1.f - g2;
278 p->g1 = 1.f - p->g2;268 sample = sample * g1 + cfxsample * g2;
279
280 sample = sample * p->g1 + cfxsample * p->g2;
281 269
282 return sample;270 return sample;
283}271}
284 272
285void tSamplePlayer_setSample (tSamplePlayer* const p, tSample* s)273void tSampler_setSample (tSampler* const p, tBuffer* s)
286{274{
287 p->samp = s;275 p->samp = s;
288}276}
289 277
290void tSamplePlayer_setMode (tSamplePlayer* const p, Mode mode)278void tSampler_setMode (tSampler* const p, Mode mode)
291{279{
292 p->mode = mode;280 p->mode = mode;
293}281}
294 282
295void tSamplePlayer_play (tSamplePlayer* const p)283void tSampler_setCrossfadeLength (tSampler* const p, uint32_t length)
284{
285 uint32_t cfxlen = LEAF_clip(0, length, 1000);
286
287 //if (cfxlen > p->len) cfxlen = p->len * 0.25f;
288
289 p->cfxlen = cfxlen;
290}
291
292void tSampler_play (tSampler* const p)
296{293{
297 p->active = 1;294 p->active = 1;
298 p->cnt = 0;
299 295
300 if (p->dir > 0)296 if (p->dir > 0)
301 {297 {
@@ -309,13 +305,17 @@ void tSamplePlayer_play (tSamplePlayer* const p) · 7 unmodified lines
309 }305 }
310}306}
311 307
312void tSamplePlayer_stop (tSamplePlayer* const p)308void tSampler_stop (tSampler* const p)
313{309{
314 p->active = 0;310 p->active = 0;
315}311}
316 312
317static void handleStartEndChange(tSamplePlayer* const p)313static void handleStartEndChange(tSampler* const p)
318{314{
315 p->len = abs(p->end - p->start);
316
317 //if (p->len < p->cfxlen) p->cfxlen = p->len * 0.9f;
318
319 if (p->start > p->end)319 if (p->start > p->end)
320 {320 {
321 p->flip = -1;321 p->flip = -1;
@@ -326,25 +326,21 @@ static void handleStartEndChange(tSamplePlayer* const p) · 4 unmodified lines
326 }326 }
327}327}
328 328
329void tSamplePlayer_setStart (tSamplePlayer* const p, int32_t start)329void tSampler_setStart (tSampler* const p, int32_t start)
330{330{
331 p->start = LEAF_clipInt(0, start, (p->samp->length - 1));331 p->start = LEAF_clipInt(0, start, (p->samp->length - 1));
332 332
333 p->len = abs(p->end - p->start);
334
335 handleStartEndChange(p);333 handleStartEndChange(p);
336}334}
337 335
338void tSamplePlayer_setEnd (tSamplePlayer* const p, int32_t end)336void tSampler_setEnd (tSampler* const p, int32_t end)
339{337{
340 p->end = LEAF_clipInt(0, end, (p->samp->length - 1));338 p->end = LEAF_clipInt(0, end, (p->samp->length - 1));
341
342 p->len = abs(p->end - p->start);
343 339
344 handleStartEndChange(p);340 handleStartEndChange(p);
345}341}
346 342
347void tSamplePlayer_setRate (tSamplePlayer* const p, float rate)343void tSampler_setRate (tSampler* const p, float rate)
348{344{
349 if (rate < 0.f)345 if (rate < 0.f)
350 {346 {
@@ -357,6 +353,7 @@ void tSamplePlayer_setRate (tSamplePlayer* const p, float rate) · 6 unmodified lines
357 }353 }
358 354
359 p->inc = LEAF_clip(0.f, rate, 8.0f);355 p->inc = LEAF_clip(0.f, rate, 8.0f);
356 p->iinc = 1.f / p->inc;
360}357}
361 358
362 359
LEAF_JUCEPlugin/Source/LEAFLink.cpp+2−1
@@ -23,7 +23,8 @@ std::vector<juce::String> cSliderNames = std::vector<juce::String> · 22 unmodified lines
23{23{
24 "rate",24 "rate",
25 "start",25 "start",
26 "end"26 "end",
27 "cfxlen"
27};28};
28 29
29std::vector<juce::String> cComboBoxNames = std::vector<juce::String>30std::vector<juce::String> cComboBoxNames = std::vector<juce::String>
LEAF_JUCEPlugin/Source/MyTest.cpp+20−23
@@ -16,38 +16,33 @@ static void leaf_pool_report(void); · 15 unmodified lines
16static void leaf_pool_dump(void);16static void leaf_pool_dump(void);
17static void run_pool_test(void);17static void run_pool_test(void);
18 18
19tSample sample;19tBuffer sample;
20 20
21tSamplePlayer player;21tSampler player;
22 22
23void LEAFTest_init (float sampleRate, int blockSize)23void LEAFTest_init (float sampleRate, int blockSize)
24{24{
25 LEAF_init(sampleRate, blockSize, &randomNumberGenerator);25 LEAF_init(sampleRate, blockSize, &randomNumberGenerator);
26 26
27 tSample_init(&sample, leaf.sampleRate * 4);27 tBuffer_init(&sample, leaf.sampleRate * 2);
28 28
29 tSamplePlayer_init(&player, &sample);29 tSampler_init(&player, &sample);
30 tSamplePlayer_setMode(&player, Loop);30 tSampler_setMode(&player, Loop);
31 31
32 leaf_pool_report();32 leaf_pool_report();
33 33
34 tSample_start(&sample);34 tBuffer_record(&sample);
35
36 tSampler_play(&player);
35}37}
36 38
37int timer = 0;39int timer = 0;
38 40
39float LEAFTest_tick (float input)41float LEAFTest_tick (float input)
40{42{
41 tSample_tick(&sample, input);43 tBuffer_tick(&sample, input);
42
43 timer++;
44
45 if (player.active == 0 && timer >= leaf.sampleRate * 4)
46 {
47 tSamplePlayer_play(&player);
48 }
49 44
50 return tSamplePlayer_tick(&player);45 return tSampler_tick(&player);
51}46}
52 47
53bool lastState = false;48bool lastState = false;
@@ -56,26 +51,30 @@ void LEAFTest_block (void) · 2 unmodified lines
56 float val = getSliderValue("rate");51 float val = getSliderValue("rate");
57 52
58 float rate = val * 16.0f - 8.0f;53 float rate = val * 16.0f - 8.0f;
59 tSamplePlayer_setRate(&player, rate);54 tSampler_setRate(&player, rate);
60 //DBG("rate: " + String(rate));55 //DBG("rate: " + String(rate));
61 56
62 val = getSliderValue("start");57 val = getSliderValue("start");
63 float start = val * sample.length;58 float start = val * sample.length;
64 tSamplePlayer_setStart(&player, start);59 tSampler_setStart(&player, start);
65 //DBG("start: " + String(start));60 //DBG("start: " + String(start));
66 61
67 val = getSliderValue("end");62 val = getSliderValue("end");
68 float end = val * sample.length;63 float end = val * sample.length;
69 tSamplePlayer_setEnd(&player, end);64 tSampler_setEnd(&player, end);
70 //DBG("end: " + String(end));65 //DBG("end: " + String(end));
71 66
67 val = getSliderValue("cfxlen");
68 uint32_t cfxlen = val * 500;
69 tSampler_setCrossfadeLength(&player, cfxlen);
70 DBG("cfxlen: " + String(cfxlen));
71
72 bool state = getButtonState("sample");72 bool state = getButtonState("sample");
73 73
74 if (state && !lastState)74 if (state && !lastState)
75 {75 {
76 timer = 0;76 timer = 0;
77 tSamplePlayer_stop(&player);77 tBuffer_record(&sample);
78 tSample_start(&sample);
79 }78 }
80 79
81 lastState = state;80 lastState = state;
@@ -142,7 +141,7 @@ static void run_pool_test(void) · 60 unmodified lines
142 }141 }
143 142
144 leaf_pool_report();143 leaf_pool_report();
145 144
146 DBG("ALLOC BUFFER 2");145 DBG("ALLOC BUFFER 2");
147 size = 25;146 size = 25;
148 147
@@ -154,8 +153,6 @@ static void run_pool_test(void) · 5 unmodified lines
154 {153 {
155 buffer[i] = (float)(i*2);154 buffer[i] = (float)(i*2);
156 }155 }
157
158 DBG("FREE BUFFER 2");
159 leaf_free(buffer);156 leaf_free(buffer);
160 157
161 leaf_pool_report();158 leaf_pool_report();

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