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
85 85
86 int active;86 int active;
87 87
88 float gain;88 float g1;
89 float g2;
89 } tSamplePlayer;90 } tSamplePlayer;
90 91
91 void tSamplePlayer_init (tSamplePlayer* const, tSample* s);92 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
105}105}
106 106
107//==============================================================================107//==============================================================================
108
109#define CFX 50
108 110
109void tSamplePlayer_init (tSamplePlayer* const p, tSample* s)111void tSamplePlayer_init (tSamplePlayer* const p, tSample* s)
110{112{
@@ -124,7 +126,8 @@ void tSamplePlayer_init (tSamplePlayer* const p, tSample* s) · 13 unmodified lines
124 126
125 p->mode = Normal;127 p->mode = Normal;
126 128
127 p->gain = 1.;129 p->g1 = 1.f;
130 p->g2 = 0.f;
128}131}
129 132
130void tSamplePlayer_free (tSamplePlayer* const p)133void tSamplePlayer_free (tSamplePlayer* const p)
@@ -140,6 +143,7 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 9 unmodified lines
140 if (p->active == 0 || (p->len < 4)) return 0.f;143 if (p->active == 0 || (p->len < 4)) return 0.f;
141 144
142 float sample = 0.f;145 float sample = 0.f;
146 float cfxsample = 0.f;
143 147
144 float* buff = p->samp->buff;148 float* buff = p->samp->buff;
145 149
@@ -148,17 +152,20 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 2 unmodified lines
148 int idx = (int) p->idx;152 int idx = (int) p->idx;
149 float alpha = p->idx - idx;153 float alpha = p->idx - idx;
150 154
151 uint32_t start = p->start, end = p->end;155 int32_t start = p->start, end = p->end;
152 if (p->flip < 0)156 if (p->flip < 0)
153 {157 {
154 start = p->end;158 start = p->end;
155 end = p->start;159 end = p->start;
156 }160 }
157 161
162 p->g1 = 1.f;
163 p->g2 = 0.f;
164
158 // Check dir (direction) to interpolate properly165 // Check dir (direction) to interpolate properly
159 if (dir > 0)166 if (dir > 0)
160 {167 {
161 // FORWARD168 // FORWARD NORMAL SAMPLE
162 int i1 = idx-1;169 int i1 = idx-1;
163 int i3 = idx+1;170 int i3 = idx+1;
164 int i4 = idx+2;171 int i4 = idx+2;
@@ -172,6 +179,24 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 7 unmodified lines
172 buff[i3],179 buff[i3],
173 buff[i4],180 buff[i4],
174 alpha);181 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 }
175 }200 }
176 else201 else
177 {202 {
@@ -188,40 +213,53 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 10 unmodified lines
188 buff[idx],213 buff[idx],
189 buff[i3],214 buff[i3],
190 buff[i4],215 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 }
192 }236 }
193 237
194 p->idx += (dir * p->inc);238 p->idx += (dir * p->inc);
195 239
196 if (p->mode != BackAndForth)240 if (p->mode != BackAndForth ) // == Normal or Loop
197 {241 {
198 // Check flip bit to change loop test
199 if (idx <= start)242 if (idx <= start)
200 {243 {
201 // -inc
202 p->idx += (float)(p->len);244 p->idx += (float)(p->len);
203 p->cnt++;245 p->cnt++;
204 }246 }
205 else if (idx >= end)247 else if (idx >= end)
206 {248 {
207 // +inc
208 p->idx -= (float)(p->len);249 p->idx -= (float)(p->len);
209 p->cnt++;250 p->cnt++;
210 }251 }
211
212 }252 }
213 else // BackAndForth253 else // == BackAndForth
214 {254 {
215 if ((idx < start) || (idx > end))255 if ((idx <= start) || (idx >= end))
216 {256 {
217 p->dir = -p->dir;257 p->dir = -p->dir;
218 p->idx += (2*p->inc);
219 p->cnt++;258 p->cnt++;
220 }259 }
221 }260 }
222 261
223 262 return sample * p->g1 + cfxsample * p->g2;
224 return sample * p->gain;
225}263}
226 264
227void tSamplePlayer_setSample (tSamplePlayer* const p, tSample* s)265void tSamplePlayer_setSample (tSamplePlayer* const p, tSample* s)
@@ -238,6 +276,17 @@ void tSamplePlayer_play (tSamplePlayer* const p) · 10 unmodified lines
238{276{
239 p->active = 1;277 p->active = 1;
240 p->cnt = 0;278 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 }
241}290}
242 291
243void tSamplePlayer_stop (tSamplePlayer* const p)292void tSamplePlayer_stop (tSamplePlayer* const p)
LEAF_JUCEPlugin/Source/MyTest.cpp+3−3
@@ -57,17 +57,17 @@ void LEAFTest_block (void) · 56 unmodified lines
57 57
58 float rate = val * 16.0f - 8.0f;58 float rate = val * 16.0f - 8.0f;
59 tSamplePlayer_setRate(&player, rate);59 tSamplePlayer_setRate(&player, rate);
60 DBG("rate: " + String(rate));60 //DBG("rate: " + String(rate));
61 61
62 val = getSliderValue("start");62 val = getSliderValue("start");
63 float start = val * sample.length;63 float start = val * sample.length;
64 tSamplePlayer_setStart(&player, start);64 tSamplePlayer_setStart(&player, start);
65 DBG("start: " + String(start));65 //DBG("start: " + String(start));
66 66
67 val = getSliderValue("end");67 val = getSliderValue("end");
68 float end = val * sample.length;68 float end = val * sample.length;
69 tSamplePlayer_setEnd(&player, end);69 tSamplePlayer_setEnd(&player, end);
70 DBG("end: " + String(end));70 //DBG("end: " + String(end));
71 71
72 bool state = getButtonState("sample");72 bool state = getButtonState("sample");
73 73

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