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
2828 RecordModeNil
2929 } RecordMode;
3030
31− typedef struct _tSample
31+ typedef struct _tBuffer
3232 {
3333 float* buff;
3434
@@ -39,23 +39,23 @@ extern "C" { · 4 unmodified lines
3939
4040 int active;
4141
42− } tSample;
42+ } tBuffer;
4343
44− void tSample_init (tSample* const, uint32_t length);
45− void tSample_free (tSample* const);
44+ void tBuffer_init (tBuffer* const, uint32_t length);
45+ void tBuffer_free (tBuffer* const);
4646
47− void tSample_tick (tSample* const, float sample);
47+ void tBuffer_tick (tBuffer* const, float sample);
4848
49− void tSample_read(tSample* const, float* buff, uint32_t len);
49+ void tBuffer_read(tBuffer* const, float* buff, uint32_t len);
5050
51− float tSample_get (tSample* const, int idx);
51+ float tBuffer_get (tBuffer* const, int idx);
5252
53− void tSample_start (tSample* const);
54− void tSample_stop (tSample* const);
53+ void tBuffer_record (tBuffer* const);
54+ void tBuffer_stop (tBuffer* const);
5555
56− void tSample_setRecordMode (tSample* const, RecordMode mode);
56+ void tBuffer_setRecordMode (tBuffer* const, RecordMode mode);
5757
58− void tSample_clear (tSample* const);
58+ void tBuffer_clear (tBuffer* const);
5959
6060 //==============================================================================
6161
@@ -67,44 +67,45 @@ extern "C" { · 5 unmodified lines
6767 SampleModeNil
6868 } Mode;
6969
70− typedef struct _tSamplePlayer
70+ typedef struct _tSampler
7171 {
72− tSample* samp;
72+ tBuffer* samp;
7373
7474 float idx;
7575 float inc;
76+ float iinc;
7677 int8_t dir;
7778 int8_t flip;
7879
7980 int32_t start;
8081 int32_t end;
8182 uint32_t len;
82− uint32_t cnt;
83+ uint32_t cfxlen;
8384
8485 Mode mode;
8586
8687 int active;
87−
88− float g1;
89− float g2;
90− } tSamplePlayer;
88+
89+ } tSampler;
9190
92− void tSamplePlayer_init (tSamplePlayer* const, tSample* s);
93− void tSamplePlayer_free (tSamplePlayer* const);
91+ void tSampler_init (tSampler* const, tBuffer* s);
92+ void tSampler_free (tSampler* const);
9493
95− float tSamplePlayer_tick (tSamplePlayer* const);
94+ float tSampler_tick (tSampler* const);
9695
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);
9899
99− void tSamplePlayer_setMode (tSamplePlayer* const, Mode mode);
100+ void tSampler_play (tSampler* const);
101+ void tSampler_stop (tSampler* const);
100102
101− void tSamplePlayer_play (tSamplePlayer* const);
102− void tSamplePlayer_stop (tSamplePlayer* const);
103+ void tSampler_setStart (tSampler* const, int32_t start);
104+ void tSampler_setEnd (tSampler* const, int32_t end);
103105
104− void tSamplePlayer_setStart (tSamplePlayer* const, int32_t start);
105− void tSamplePlayer_setEnd (tSamplePlayer* const, int32_t end);
106+ void tSampler_setCrossfadeLength (tSampler* const p, uint32_t length);
106107
107− void tSamplePlayer_setRate (tSamplePlayer* const, float rate);
108+ void tSampler_setRate (tSampler* const, float rate);
108109
109110 //==============================================================================
110111
LEAF/Src/leaf-sample.c+65−68
@@ -23,7 +23,7 @@ · 22 unmodified lines
2323
2424 //==============================================================================
2525
26−void tSample_init (tSample* const s, uint32_t length)
26+void tBuffer_init (tBuffer* const s, uint32_t length)
2727 {
2828 s->buff = (float*) leaf_alloc( sizeof(float) * length);
2929
@@ -33,16 +33,16 @@ void tSample_init (tSample* const s, uint32_t length) · 3 unmodified lines
3333 s->idx = 0;
3434 s->mode = RecordOneShot;
3535
36− tSample_clear(s);
36+ tBuffer_clear(s);
3737 }
3838
39−void tSample_free (tSample* const s)
39+void tBuffer_free (tBuffer* const s)
4040 {
4141 leaf_free(s->buff);
4242 leaf_free(s);
4343 }
4444
45−void tSample_tick (tSample* const s, float sample)
45+void tBuffer_tick (tBuffer* const s, float sample)
4646 {
4747 if (s->active == 1)
4848 {
@@ -54,7 +54,7 @@ void tSample_tick (tSample* const s, float sample) · 5 unmodified lines
5454 {
5555 if (s->mode == RecordOneShot)
5656 {
57− tSample_stop(s);
57+ tBuffer_stop(s);
5858 }
5959 else if (s->mode == RecordLoop)
6060 {
@@ -64,7 +64,7 @@ void tSample_tick (tSample* const s, float sample) · 3 unmodified lines
6464 }
6565 }
6666
67−void tSample_read(tSample* const s, float* buff, uint32_t len)
67+void tBuffer_read(tBuffer* const s, float* buff, uint32_t len)
6868 {
6969 for (int i = 0; i < s->length; i++)
7070 {
@@ -73,30 +73,30 @@ void tSample_read(tSample* const s, float* buff, uint32_t len) · 2 unmodified lines
7373 }
7474 }
7575
76−float tSample_get (tSample* const s, int idx)
76+float tBuffer_get (tBuffer* const s, int idx)
7777 {
7878 if ((idx < 0) || (idx >= s->length)) return 0.f;
7979
8080 return s->buff[idx];
8181 }
8282
83−void tSample_start(tSample* const s)
83+void tBuffer_record(tBuffer* const s)
8484 {
8585 s->active = 1;
8686 s->idx = 0;
8787 }
8888
89−void tSample_stop(tSample* const s)
89+void tBuffer_stop(tBuffer* const s)
9090 {
9191 s->active = 0;
9292 }
9393
94−void tSample_setRecordMode (tSample* const s, RecordMode mode)
94+void tBuffer_setRecordMode (tBuffer* const s, RecordMode mode)
9595 {
9696 s->mode = mode;
9797 }
9898
99−void tSample_clear (tSample* const s)
99+void tBuffer_clear (tBuffer* const s)
100100 {
101101 for (int i = 0; i < s->length; i++)
102102 {
@@ -104,11 +104,9 @@ void tSample_clear (tSample* const s) · 1 unmodified line
104104 }
105105 }
106106
107−//==============================================================================
108−
109−#define CFX 300
107+//================================tSampler=====================================
110108
111−void tSamplePlayer_init (tSamplePlayer* const p, tSample* s)
109+void tSampler_init (tSampler* const p, tBuffer* s)
112110 {
113111 p->samp = s;
114112
@@ -121,30 +119,29 @@ void tSamplePlayer_init (tSamplePlayer* const p, tSample* s) · 6 unmodified lines
121119
122120 p->idx = 0.f;
123121 p->inc = 1.f;
122+ p->iinc = 1.f;
124123 p->dir = 1;
125124 p->flip = 1;
126125
127126 p->mode = Normal;
128−
129− p->g1 = 1.f;
130− p->g2 = 0.f;
127+
128+ p->cfxlen = 300; // default 300 sample crossfade
131129 }
132130
133−void tSamplePlayer_free (tSamplePlayer* const p)
131+void tSampler_free (tSampler* const p)
134132 {
135133 leaf_free(p->samp);
136134 leaf_free(p);
137135 }
138136
139−float tSamplePlayer_tick (tSamplePlayer* const p)
137+float tSampler_tick (tSampler* const p)
140138 {
141− if ((p->mode == Normal) && (p->cnt > 0)) p->active = 0;
142−
143139 if (p->active == 0 || (p->len < 4)) return 0.f;
144140
145141 float sample = 0.f;
146142 float cfxsample = 0.f;
147− int cfx = CFX; // make this part of class
143+ int numsamps;
144+ float g1 = 1.f, g2 = 0.f;
148145
149146 float* buff = p->samp->buff;
150147
@@ -152,16 +149,16 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line
152149
153150 int idx;
154151 float alpha;
155−
156− if (dir > 0)
157− {
152+
153+ if (dir > 0)
154+ {
158155 idx = (int) p->idx;
159156 alpha = p->idx - idx;
160− }
161− else
162− {
163− 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;
157+ }
158+ else
159+ {
160+ idx = (int) (p->idx + 1.f); // we think this is because flooring on int works different when reading backwards
161+ alpha = (p->idx+1.f) - idx;
165162 }
166163
167164 int32_t start = p->start, end = p->end;
@@ -178,10 +175,6 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 10 unmodified lines
178175 int i1 = idx-1;
179176 int i3 = idx+1;
180177 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;
185178
186179 sample = LEAF_interpolate_hermite (buff[i1],
187180 buff[idx],
@@ -189,9 +182,11 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line
189182 buff[i4],
190183 alpha);
191184
192− cfx = (end - idx) / p->inc; // num samples to end of loop, use inverse increment
193−
194− if (cfx <= CFX)
185+ // num samples to end of loop
186+ numsamps = (dir > 0) ? (end - idx) : (idx - start);
187+ numsamps *= p->iinc;
188+
189+ if (numsamps <= p->cfxlen)
195190 {
196191 // CROSSFADE SAMPLE
197192 float idxx = p->idx - p->len;
@@ -207,9 +202,9 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 9 unmodified lines
207202 buff[i4],
208203 alpha);
209204
210− //rprintf("cidx: %d\n", cdx);
205+ g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen;
211206 }
212− else cfx = CFX;
207+ else g2 = 0.f;
213208 }
214209 else
215210 {
@@ -217,20 +212,16 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line
217212 int i1 = idx+1;
218213 int i3 = idx-1;
219214 int i4 = idx-2;
220−
221− if (i1 > end) i1 -= p->len;
222− if (i3 < start) i3 += p->len;
223− if (i4 < start) i4 += p->len;
224−
215+
225216 sample = LEAF_interpolate_hermite (buff[i1],
226217 buff[idx],
227218 buff[i3],
228219 buff[i4],
229220 1.0f-alpha);
230221
231− cfx = (idx - start) / p->inc;
222+ numsamps = (idx - start) / p->inc;
232223
233− if (cfx <= CFX)
224+ if (numsamps <= p->cfxlen)
234225 {
235226 // CROSSFADE SAMPLE
236227 float idxx = p->idx + p->len + 1.f;
@@ -246,8 +237,10 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 9 unmodified lines
246237 buff[i3],
247238 buff[i4],
248239 1.f-alpha);
240+
241+ g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen;
249242 }
250− else cfx = CFX;
243+ else g2 = 0.f;
251244 }
252245
253246 p->idx += (dir * p->inc);
@@ -257,12 +250,10 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 3 unmodified lines
257250 if (idx <= start)
258251 {
259252 p->idx += (float)(p->len);
260− p->cnt++;
261253 }
262254 else if (idx >= end)
263255 {
264256 p->idx -= (float)(p->len);
265− p->cnt++;
266257 }
267258 }
268259 else // == BackAndForth
@@ -270,32 +261,37 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line
270261 if ((idx <= start) || (idx >= end))
271262 {
272263 p->dir = -p->dir;
273− p->cnt++;
274264 }
275265 }
276266
277− p->g2 = (float) (CFX - cfx) / (float) CFX;
278− p->g1 = 1.f - p->g2;
279−
280− sample = sample * p->g1 + cfxsample * p->g2;
267+ g1 = 1.f - g2;
268+ sample = sample * g1 + cfxsample * g2;
281269
282270 return sample;
283271 }
284272
285−void tSamplePlayer_setSample (tSamplePlayer* const p, tSample* s)
273+void tSampler_setSample (tSampler* const p, tBuffer* s)
286274 {
287275 p->samp = s;
288276 }
289277
290−void tSamplePlayer_setMode (tSamplePlayer* const p, Mode mode)
278+void tSampler_setMode (tSampler* const p, Mode mode)
291279 {
292280 p->mode = mode;
293281 }
294282
295−void tSamplePlayer_play (tSamplePlayer* const p)
283+void 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+
292+void tSampler_play (tSampler* const p)
296293 {
297294 p->active = 1;
298− p->cnt = 0;
299295
300296 if (p->dir > 0)
301297 {
@@ -309,13 +305,17 @@ void tSamplePlayer_play (tSamplePlayer* const p) · 7 unmodified lines
309305 }
310306 }
311307
312−void tSamplePlayer_stop (tSamplePlayer* const p)
308+void tSampler_stop (tSampler* const p)
313309 {
314310 p->active = 0;
315311 }
316312
317−static void handleStartEndChange(tSamplePlayer* const p)
313+static void handleStartEndChange(tSampler* const p)
318314 {
315+ p->len = abs(p->end - p->start);
316+
317+ //if (p->len < p->cfxlen) p->cfxlen = p->len * 0.9f;
318+
319319 if (p->start > p->end)
320320 {
321321 p->flip = -1;
@@ -326,25 +326,21 @@ static void handleStartEndChange(tSamplePlayer* const p) · 4 unmodified lines
326326 }
327327 }
328328
329−void tSamplePlayer_setStart (tSamplePlayer* const p, int32_t start)
329+void tSampler_setStart (tSampler* const p, int32_t start)
330330 {
331331 p->start = LEAF_clipInt(0, start, (p->samp->length - 1));
332332
333− p->len = abs(p->end - p->start);
334−
335333 handleStartEndChange(p);
336334 }
337335
338−void tSamplePlayer_setEnd (tSamplePlayer* const p, int32_t end)
336+void tSampler_setEnd (tSampler* const p, int32_t end)
339337 {
340338 p->end = LEAF_clipInt(0, end, (p->samp->length - 1));
341−
342− p->len = abs(p->end - p->start);
343339
344340 handleStartEndChange(p);
345341 }
346342
347−void tSamplePlayer_setRate (tSamplePlayer* const p, float rate)
343+void tSampler_setRate (tSampler* const p, float rate)
348344 {
349345 if (rate < 0.f)
350346 {
@@ -357,6 +353,7 @@ void tSamplePlayer_setRate (tSamplePlayer* const p, float rate) · 6 unmodified lines
357353 }
358354
359355 p->inc = LEAF_clip(0.f, rate, 8.0f);
356+ p->iinc = 1.f / p->inc;
360357 }
361358
362359
LEAF_JUCEPlugin/Source/LEAFLink.cpp+2−1
@@ -23,7 +23,8 @@ std::vector<juce::String> cSliderNames = std::vector<juce::String> · 22 unmodified lines
2323 {
2424 "rate",
2525 "start",
26− "end"
26+ "end",
27+ "cfxlen"
2728 };
2829
2930 std::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
1616 static void leaf_pool_dump(void);
1717 static void run_pool_test(void);
1818
19−tSample sample;
19+tBuffer sample;
2020
21−tSamplePlayer player;
21+tSampler player;
2222
2323 void LEAFTest_init (float sampleRate, int blockSize)
2424 {
2525 LEAF_init(sampleRate, blockSize, &randomNumberGenerator);
2626
27− tSample_init(&sample, leaf.sampleRate * 4);
27+ tBuffer_init(&sample, leaf.sampleRate * 2);
2828
29− tSamplePlayer_init(&player, &sample);
30− tSamplePlayer_setMode(&player, Loop);
29+ tSampler_init(&player, &sample);
30+ tSampler_setMode(&player, Loop);
3131
3232 leaf_pool_report();
3333
34− tSample_start(&sample);
34+ tBuffer_record(&sample);
35+
36+ tSampler_play(&player);
3537 }
3638
3739 int timer = 0;
3840
3941 float LEAFTest_tick (float input)
4042 {
41− tSample_tick(&sample, input);
42−
43− timer++;
44−
45− if (player.active == 0 && timer >= leaf.sampleRate * 4)
46− {
47− tSamplePlayer_play(&player);
48− }
43+ tBuffer_tick(&sample, input);
4944
50− return tSamplePlayer_tick(&player);
45+ return tSampler_tick(&player);
5146 }
5247
5348 bool lastState = false;
@@ -56,26 +51,30 @@ void LEAFTest_block (void) · 2 unmodified lines
5651 float val = getSliderValue("rate");
5752
5853 float rate = val * 16.0f - 8.0f;
59− tSamplePlayer_setRate(&player, rate);
54+ tSampler_setRate(&player, rate);
6055 //DBG("rate: " + String(rate));
6156
6257 val = getSliderValue("start");
6358 float start = val * sample.length;
64− tSamplePlayer_setStart(&player, start);
59+ tSampler_setStart(&player, start);
6560 //DBG("start: " + String(start));
6661
6762 val = getSliderValue("end");
6863 float end = val * sample.length;
69− tSamplePlayer_setEnd(&player, end);
64+ tSampler_setEnd(&player, end);
7065 //DBG("end: " + String(end));
7166
67+ val = getSliderValue("cfxlen");
68+ uint32_t cfxlen = val * 500;
69+ tSampler_setCrossfadeLength(&player, cfxlen);
70+ DBG("cfxlen: " + String(cfxlen));
71+
7272 bool state = getButtonState("sample");
7373
7474 if (state && !lastState)
7575 {
7676 timer = 0;
77− tSamplePlayer_stop(&player);
78− tSample_start(&sample);
77+ tBuffer_record(&sample);
7978 }
8079
8180 lastState = state;
@@ -142,7 +141,7 @@ static void run_pool_test(void) · 60 unmodified lines
142141 }
143142
144143 leaf_pool_report();
145−
144+
146145 DBG("ALLOC BUFFER 2");
147146 size = 25;
148147
@@ -154,8 +153,6 @@ static void run_pool_test(void) · 5 unmodified lines
154153 {
155154 buffer[i] = (float)(i*2);
156155 }
157−
158− DBG("FREE BUFFER 2");
159156 leaf_free(buffer);
160157
161158 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