History

Added custom tTapeDelay object.

mulshine committed 7 years ago

Commit e28e39c1 parent 9d82b878 files · +332 −50Browse files

Showing the changes to LEAF/Src_cpp/leaf-delay.cpp (1 of 8 files). Show all

Unified · Split

LEAF/Src_cpp/leaf-delay.cpp+169−22
@@ -63,8 +63,8 @@ float tDelay_tick (tDelay* const d, float input) · 62 unmodified lines
63 63
64int tDelay_setDelay (tDelay* const d, uint32_t delay)64int tDelay_setDelay (tDelay* const d, uint32_t delay)
65{65{
66 if (delay >= d->maxDelay) d->delay = d->maxDelay;66 d->delay = LEAF_clip(0.0f, delay, d->maxDelay);
67 else d->delay = delay;67
68 68
69 // read chases write69 // read chases write
70 if ( d->inPoint >= delay ) d->outPoint = d->inPoint - d->delay;70 if ( d->inPoint >= delay ) d->outPoint = d->inPoint - d->delay;
@@ -163,29 +163,26 @@ float tDelayL_tick (tDelayL* const d, float input) · 92 unmodified lines
163 d->buff[d->inPoint] = input * d->gain;163 d->buff[d->inPoint] = input * d->gain;
164 164
165 // Increment input pointer modulo length.165 // Increment input pointer modulo length.
166 if (++(d->inPoint) == d->maxDelay ) d->inPoint = 0;166 if (++(d->inPoint) == d->maxDelay ) d->inPoint = 0;
167 167
168 168
169 // First 1/2 of interpolation169 uint32_t idx = (uint32_t) d->outPoint;
170 d->lastOut = d->buff[d->outPoint] * d->omAlpha;170
171 171 d->lastOut = LEAF_interpolate_hermite (d->buff[((idx - 1) + d->maxDelay) % d->maxDelay],
172 // Second 1/2 of interpolation172 d->buff[idx],
173 if (d->outPoint + 1 < d->maxDelay)173 d->buff[(idx + 1) % d->maxDelay],
174 d->lastOut += d->buff[d->outPoint+1] * d->alpha;174 d->buff[(idx + 2) % d->maxDelay],
175 else175 d->alpha);
176 d->lastOut += d->buff[0] * d->alpha;
177 176
178 // Increment output pointer modulo length.177 // Increment output pointer modulo length
179 if ( ++(d->outPoint) == d->maxDelay ) d->outPoint = 0;178 if ( (++d->outPoint) >= d->maxDelay ) d->outPoint = 0;
180 179
181 return d->lastOut;180 return d->lastOut;
182}181}
183 182
184int tDelayL_setDelay (tDelayL* const d, float delay)183int tDelayL_setDelay (tDelayL* const d, float delay)
185{184{
186 if (delay < 0.0f) d->delay = 0.0f;185 d->delay = LEAF_clip(0.0f, delay, d->maxDelay);
187 else if (delay <= d->maxDelay) d->delay = delay;
188 else d->delay = d->maxDelay;
189 186
190 float outPointer = d->inPoint - d->delay;187 float outPointer = d->inPoint - d->delay;
191 188
@@ -326,9 +323,8 @@ float tDelayA_tick (tDelayA* const d, float input) · 134 unmodified lines
326 323
327int tDelayA_setDelay (tDelayA* const d, float delay)324int tDelayA_setDelay (tDelayA* const d, float delay)
328{325{
329 if (delay < 0.5f) d->delay = 0.5f;326 d->delay = LEAF_clip(0.5f, delay, d->maxDelay);
330 else if (delay <= d->maxDelay) d->delay = delay;327
331 else d->delay = d->maxDelay;
332 328
333 // outPoint chases inPoint329 // outPoint chases inPoint
334 float outPointer = (float)d->inPoint - d->delay + 1.0f;330 float outPointer = (float)d->inPoint - d->delay + 1.0f;
@@ -413,5 +409,156 @@ void tDelayA_setGain (tDelayA* const d, float gain) · 78 unmodified lines
413float tDelayA_getGain (tDelayA* const d)409float tDelayA_getGain (tDelayA* const d)
414{410{
415 return d->gain;411 return d->gain;
412}
413
414// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ TapeDelay ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
415void tTapeDelay_init (tTapeDelay* const d, float delay, uint32_t maxDelay)
416{
417 d->maxDelay = maxDelay;
418
419 d->delay = LEAF_clip(0.0f, delay, d->maxDelay);
420
421 d->buff = (float*) leaf_alloc(sizeof(float) * maxDelay);
422
423 d->gain = 1.0f;
424
425 d->lastIn = 0.0f;
426 d->lastOut = 0.0f;
427
428 d->idx = 0.0f;
429 d->inc = 1.0f;
430 d->inPoint = 0;
431 d->outPoint = 0;
432
433 tTapeDelay_setDelay(d, 1);
434}
435
436void tTapeDelay_free(tTapeDelay* const d)
437{
438 leaf_free(d->buff);
439 leaf_free(d);
440}
441
442int count = 0;
443
444#define SMOOTH_FACTOR 10.f
445
446float tTapeDelay_tick (tTapeDelay* const d, float input)
447{
448 d->buff[d->inPoint] = input * d->gain;
449
450 // Increment input pointer modulo length.
451 if (++(d->inPoint) == d->maxDelay ) d->inPoint = 0;
452
453 int idx = (int) d->idx;
454 d->alpha = d->idx - idx;
455
456 d->lastOut = LEAF_interpolate_hermite (d->buff[((idx - 1) + d->maxDelay) % d->maxDelay],
457 d->buff[idx],
458 d->buff[(idx + 1) % d->maxDelay],
459 d->buff[(idx + 2) % d->maxDelay],
460 d->alpha);
461
462 float diff = (d->inPoint - d->idx);
463 while (diff < 0.f) diff += d->maxDelay;
464
465 d->inc = 1.0f + (diff - d->delay) / d->delay * SMOOTH_FACTOR;
466
467 d->idx += d->inc;
468
469 if (d->idx >= d->maxDelay) d->idx = 0.f;
470
471 if ( ++(d->outPoint) >= d->maxDelay ) d->outPoint -= d->maxDelay;
472
473 return d->lastOut;
474}
475
476
477void tTapeDelay_setRate(tTapeDelay* const d, float rate)
478{
479 d->inc = rate;
480}
481
482int tTapeDelay_setDelay (tTapeDelay* const d, float delay)
483{
484 d->delay = LEAF_clip(0.0f, delay, d->maxDelay);
485
486
487 float outPointer = d->inPoint - d->delay;
488
489 while ( outPointer < 0 )
490 outPointer += d->maxDelay; // modulo maximum length
491
492 d->outPoint = (uint32_t) outPointer; // integer part
493
494
495 return 0;
496}
497
498float tTapeDelay_tapOut (tTapeDelay* const d, float tapDelay)
499{
500 float tap = (float) d->inPoint - tapDelay - 1.f;
501
502 // Check for wraparound.
503 while ( tap < 0.f ) tap += (float)d->maxDelay;
504
505 int idx = (int) tap;
506
507 float alpha = tap - idx;
508
509 float samp = LEAF_interpolate_hermite (d->buff[((idx - 1) + d->maxDelay) % d->maxDelay],
510 d->buff[idx],
511 d->buff[(idx + 1) % d->maxDelay],
512 d->buff[(idx + 2) % d->maxDelay],
513 d->alpha);
514
515 return samp;
516
517}
518
519void tTapeDelay_tapIn (tTapeDelay* const d, float value, uint32_t tapDelay)
520{
521 int32_t tap = d->inPoint - tapDelay - 1;
522
523 // Check for wraparound.
524 while ( tap < 0 ) tap += d->maxDelay;
525
526 d->buff[tap] = value;
527}
528
529float tTapeDelay_addTo (tTapeDelay* const d, float value, uint32_t tapDelay)
530{
531 int32_t tap = d->inPoint - tapDelay - 1;
532
533 // Check for wraparound.
534 while ( tap < 0 ) tap += d->maxDelay;
535
536 return (d->buff[tap] += value);
537}
538
539float tTapeDelay_getDelay (tTapeDelay *d)
540{
541 return d->delay;
542}
543
544float tTapeDelay_getLastOut (tTapeDelay* const d)
545{
546 return d->lastOut;
547}
548
549float tTapeDelay_getLastIn (tTapeDelay* const d)
550{
551 return d->lastIn;
552}
553
554void tTapeDelay_setGain (tTapeDelay* const d, float gain)
555{
556 if (gain < 0.0f) d->gain = 0.0f;
557 else d->gain = gain;
558}
559
560float tTapeDelay_getGain (tTapeDelay* const d)
561{
562 return d->gain;
416}563}
417 564

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