| @@ -126,6 +126,9 @@ void tSampler_init (tSampler* const p, tBuffer* s) · 125 unmodified lines |
| 126 | 126 | p->mode = Normal; |
| 127 | 127 | |
| 128 | 128 | p->cfxlen = 300; // default 300 sample crossfade |
| 129 | + |
| 130 | + tRamp_init(&p->gain, 7.0f, 1); |
| 131 | + tRamp_setVal(&p->gain, 0.f); |
| 129 | 132 | } |
| 130 | 133 | |
| 131 | 134 | void tSampler_free (tSampler* const p) |
| @@ -186,25 +189,27 @@ float tSampler_tick (tSampler* const p) · 54 unmodified lines |
| 186 | 189 | numsamps = (dir > 0) ? (end - idx) : (idx - start); |
| 187 | 190 | numsamps *= p->iinc; |
| 188 | 191 | |
| 189 | | − if (numsamps <= p->cfxlen) |
| 192 | + if (p->mode == Loop) |
| 190 | 193 | { |
| 191 | | − // CROSSFADE SAMPLE |
| 192 | | − float idxx = p->idx - p->len; |
| 193 | | − int cdx = (int)(idxx); |
| 194 | | − |
| 195 | | − i1 = cdx-1; |
| 196 | | − i3 = cdx+1; |
| 197 | | − i4 = cdx+2; |
| 198 | | − |
| 199 | | − cfxsample = LEAF_interpolate_hermite (buff[i1], |
| 200 | | − buff[cdx], |
| 201 | | − buff[i3], |
| 202 | | − buff[i4], |
| 203 | | − alpha); |
| 204 | | − |
| 205 | | − g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen; |
| 194 | + if (numsamps <= p->cfxlen) |
| 195 | + { |
| 196 | + // CROSSFADE SAMPLE |
| 197 | + float idxx = p->idx - p->len; |
| 198 | + int cdx = (int)(idxx); |
| 199 | + |
| 200 | + i1 = cdx-1; |
| 201 | + i3 = cdx+1; |
| 202 | + i4 = cdx+2; |
| 203 | + |
| 204 | + cfxsample = LEAF_interpolate_hermite (buff[i1], |
| 205 | + buff[cdx], |
| 206 | + buff[i3], |
| 207 | + buff[i4], |
| 208 | + alpha); |
| 209 | + |
| 210 | + g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen; |
| 211 | + } |
| 206 | 212 | } |
| 207 | | − else g2 = 0.f; |
| 208 | 213 | } |
| 209 | 214 | else |
| 210 | 215 | { |
| @@ -221,31 +226,37 @@ float tSampler_tick (tSampler* const p) · 10 unmodified lines |
| 221 | 226 | |
| 222 | 227 | numsamps = (idx - start) / p->inc; |
| 223 | 228 | |
| 224 | | − if (numsamps <= p->cfxlen) |
| 229 | + if (p->mode == Loop) |
| 225 | 230 | { |
| 226 | | − // CROSSFADE SAMPLE |
| 227 | | − float idxx = p->idx + p->len + 1.f; |
| 228 | | − int cdx = (int)(idxx); |
| 229 | | − alpha = idxx - cdx; |
| 230 | | − |
| 231 | | − i1 = cdx+1; |
| 232 | | − i3 = cdx-1; |
| 233 | | − i4 = cdx-2; |
| 234 | | − |
| 235 | | − cfxsample = LEAF_interpolate_hermite (buff[i1], |
| 236 | | − buff[cdx], |
| 237 | | − buff[i3], |
| 238 | | − buff[i4], |
| 239 | | − 1.f-alpha); |
| 240 | | − |
| 241 | | − g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen; |
| 231 | + if (numsamps <= p->cfxlen) |
| 232 | + { |
| 233 | + // CROSSFADE SAMPLE |
| 234 | + float idxx = p->idx + p->len + 1.f; |
| 235 | + int cdx = (int)(idxx); |
| 236 | + alpha = idxx - cdx; |
| 237 | + |
| 238 | + i1 = cdx+1; |
| 239 | + i3 = cdx-1; |
| 240 | + i4 = cdx-2; |
| 241 | + |
| 242 | + cfxsample = LEAF_interpolate_hermite (buff[i1], |
| 243 | + buff[cdx], |
| 244 | + buff[i3], |
| 245 | + buff[i4], |
| 246 | + 1.f-alpha); |
| 247 | + |
| 248 | + g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen; |
| 249 | + } |
| 242 | 250 | } |
| 243 | | − else g2 = 0.f; |
| 244 | 251 | } |
| 245 | 252 | |
| 246 | 253 | p->idx += (dir * p->inc); |
| 247 | 254 | |
| 248 | | − if (p->mode != BackAndForth ) // == Normal or Loop |
| 255 | + if (p->mode == Normal) |
| 256 | + { |
| 257 | + if (numsamps == 0) p->active = 0; |
| 258 | + } |
| 259 | + else if (p->mode == Loop ) // == Normal or Loop |
| 249 | 260 | { |
| 250 | 261 | if (idx <= start) |
| 251 | 262 | { |
| @@ -261,12 +272,24 @@ float tSampler_tick (tSampler* const p) · 9 unmodified lines |
| 261 | 272 | if ((idx <= start) || (idx >= end)) |
| 262 | 273 | { |
| 263 | 274 | p->dir = -p->dir; |
| 275 | + p->idx += (p->dir * p->flip * p->inc); |
| 264 | 276 | } |
| 265 | 277 | } |
| 266 | 278 | |
| 267 | 279 | g1 = 1.f - g2; |
| 280 | + |
| 268 | 281 | sample = sample * g1 + cfxsample * g2; |
| 269 | 282 | |
| 283 | + sample = sample * tRamp_tick(&p->gain); |
| 284 | + |
| 285 | + if (p->active < 0) |
| 286 | + { |
| 287 | + if (tRamp_sample(&p->gain) <= 0.00001f) |
| 288 | + { |
| 289 | + p->active = 0; |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 270 | 293 | return sample; |
| 271 | 294 | } |
| 272 | 295 | |
| @@ -303,11 +326,16 @@ void tSampler_play (tSampler* const p) · 30 unmodified lines |
| 303 | 326 | if (p->flip > 0) p->idx = p->end; |
| 304 | 327 | else p->idx = p->start; |
| 305 | 328 | } |
| 329 | + |
| 330 | + tRamp_setVal(&p->gain, 0.f); |
| 331 | + tRamp_setDest(&p->gain, 1.f); |
| 306 | 332 | } |
| 307 | 333 | |
| 308 | 334 | void tSampler_stop (tSampler* const p) |
| 309 | 335 | { |
| 310 | | − p->active = 0; |
| 336 | + p->active = -1; |
| 337 | + |
| 338 | + tRamp_setDest(&p->gain, 0.f); |
| 311 | 339 | } |
| 312 | 340 | |
| 313 | 341 | static void handleStartEndChange(tSampler* const p) |