History

Decent crossfade implemented.

mulshine committed 7 years ago

Commit 6c87e501 parent 58869ea3 files · +68 −18Browse files

Unified · Split

3 files
  1. LEAF/Inc/leaf-sample.h +2 −1
  2. LEAF/Src/leaf-sample.c +63 −14
  3. LEAF_JUCEPlugin/Source/MyTest.cpp +3 −3
LEAF/Inc/leaf-sample.h+2−1
@@ -85,7 +85,8 @@ extern "C" { · 84 unmodified lines
8585
8686 int active;
8787
88− float gain;
88+ float g1;
89+ float g2;
8990 } tSamplePlayer;
9091
9192 void tSamplePlayer_init (tSamplePlayer* const, tSample* s);
LEAF/Src/leaf-sample.c+63−14
@@ -105,6 +105,8 @@ void tSample_clear (tSample* const s) · 104 unmodified lines
105105 }
106106
107107 //==============================================================================
108+
109+#define CFX 50
108110
109111 void tSamplePlayer_init (tSamplePlayer* const p, tSample* s)
110112 {
@@ -124,7 +126,8 @@ void tSamplePlayer_init (tSamplePlayer* const p, tSample* s) · 13 unmodified lines
124126
125127 p->mode = Normal;
126128
127− p->gain = 1.;
129+ p->g1 = 1.f;
130+ p->g2 = 0.f;
128131 }
129132
130133 void tSamplePlayer_free (tSamplePlayer* const p)
@@ -140,6 +143,7 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 9 unmodified lines
140143 if (p->active == 0 || (p->len < 4)) return 0.f;
141144
142145 float sample = 0.f;
146+ float cfxsample = 0.f;
143147
144148 float* buff = p->samp->buff;
145149
@@ -148,17 +152,20 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 2 unmodified lines
148152 int idx = (int) p->idx;
149153 float alpha = p->idx - idx;
150154
151− uint32_t start = p->start, end = p->end;
155+ int32_t start = p->start, end = p->end;
152156 if (p->flip < 0)
153157 {
154158 start = p->end;
155159 end = p->start;
156160 }
157161
162+ p->g1 = 1.f;
163+ p->g2 = 0.f;
164+
158165 // Check dir (direction) to interpolate properly
159166 if (dir > 0)
160167 {
161− // FORWARD
168+ // FORWARD NORMAL SAMPLE
162169 int i1 = idx-1;
163170 int i3 = idx+1;
164171 int i4 = idx+2;
@@ -172,6 +179,24 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 7 unmodified lines
172179 buff[i3],
173180 buff[i4],
174181 alpha);
182+ if ((end - idx) <= CFX)
183+ {
184+ // CROSSFADE SAMPLE
185+ int cdx = idx - p->len;
186+
187+ i1 = cdx-1;
188+ i3 = cdx+1;
189+ i4 = cdx+2;
190+
191+ cfxsample = LEAF_interpolate_hermite (buff[i1],
192+ buff[idx],
193+ buff[i3],
194+ buff[i4],
195+ alpha);
196+
197+ p->g2 = (float) (CFX - (end - idx - 1)) / (float) CFX;
198+ p->g1 = 1.f - p->g2;
199+ }
175200 }
176201 else
177202 {
@@ -188,40 +213,53 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 10 unmodified lines
188213 buff[idx],
189214 buff[i3],
190215 buff[i4],
191− alpha);
216+ 1.0f-alpha);
217+
218+ if ((idx - start) <= CFX)
219+ {
220+ // CROSSFADE SAMPLE
221+ int cdx = idx + p->len;
222+
223+ i1 = cdx+1;
224+ i3 = cdx-1;
225+ i4 = cdx-2;
226+
227+ cfxsample = LEAF_interpolate_hermite (buff[i1],
228+ buff[idx],
229+ buff[i3],
230+ buff[i4],
231+ alpha);
232+
233+ p->g2 = (float) (CFX - (idx - start - 1)) / (float) CFX;
234+ p->g1 = 1.f - p->g2;
235+ }
192236 }
193237
194238 p->idx += (dir * p->inc);
195239
196− if (p->mode != BackAndForth)
240+ if (p->mode != BackAndForth ) // == Normal or Loop
197241 {
198− // Check flip bit to change loop test
199242 if (idx <= start)
200243 {
201− // -inc
202244 p->idx += (float)(p->len);
203245 p->cnt++;
204246 }
205247 else if (idx >= end)
206248 {
207− // +inc
208249 p->idx -= (float)(p->len);
209250 p->cnt++;
210251 }
211−
212252 }
213− else // BackAndForth
253+ else // == BackAndForth
214254 {
215− if ((idx < start) || (idx > end))
255+ if ((idx <= start) || (idx >= end))
216256 {
217257 p->dir = -p->dir;
218− p->idx += (2*p->inc);
219258 p->cnt++;
220259 }
221260 }
222261
223−
224− return sample * p->gain;
262+ return sample * p->g1 + cfxsample * p->g2;
225263 }
226264
227265 void tSamplePlayer_setSample (tSamplePlayer* const p, tSample* s)
@@ -238,6 +276,17 @@ void tSamplePlayer_play (tSamplePlayer* const p) · 10 unmodified lines
238276 {
239277 p->active = 1;
240278 p->cnt = 0;
279+
280+ if (p->dir > 0)
281+ {
282+ if (p->flip > 0) p->idx = p->start;
283+ else p->idx = p->end;
284+ }
285+ else
286+ {
287+ if (p->flip > 0) p->idx = p->end;
288+ else p->idx = p->start;
289+ }
241290 }
242291
243292 void tSamplePlayer_stop (tSamplePlayer* const p)
LEAF_JUCEPlugin/Source/MyTest.cpp+3−3
@@ -57,17 +57,17 @@ void LEAFTest_block (void) · 56 unmodified lines
5757
5858 float rate = val * 16.0f - 8.0f;
5959 tSamplePlayer_setRate(&player, rate);
60− DBG("rate: " + String(rate));
60+ //DBG("rate: " + String(rate));
6161
6262 val = getSliderValue("start");
6363 float start = val * sample.length;
6464 tSamplePlayer_setStart(&player, start);
65− DBG("start: " + String(start));
65+ //DBG("start: " + String(start));
6666
6767 val = getSliderValue("end");
6868 float end = val * sample.length;
6969 tSamplePlayer_setEnd(&player, end);
70− DBG("end: " + String(end));
70+ //DBG("end: " + String(end));
7171
7272 bool state = getButtonState("sample");
7373

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