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
6363
6464 int tDelay_setDelay (tDelay* const d, uint32_t delay)
6565 {
66− if (delay >= d->maxDelay) d->delay = d->maxDelay;
67− else d->delay = delay;
66+ d->delay = LEAF_clip(0.0f, delay, d->maxDelay);
67+
6868
6969 // read chases write
7070 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
163163 d->buff[d->inPoint] = input * d->gain;
164164
165165 // Increment input pointer modulo length.
166− if (++(d->inPoint) == d->maxDelay ) d->inPoint = 0;
167−
168−
169− // First 1/2 of interpolation
170− d->lastOut = d->buff[d->outPoint] * d->omAlpha;
171−
172− // Second 1/2 of interpolation
173− if (d->outPoint + 1 < d->maxDelay)
174− d->lastOut += d->buff[d->outPoint+1] * d->alpha;
175− else
176− d->lastOut += d->buff[0] * d->alpha;
166+ if (++(d->inPoint) == d->maxDelay ) d->inPoint = 0;
167+
168+
169+ uint32_t idx = (uint32_t) d->outPoint;
170+
171+ d->lastOut = LEAF_interpolate_hermite (d->buff[((idx - 1) + d->maxDelay) % d->maxDelay],
172+ d->buff[idx],
173+ d->buff[(idx + 1) % d->maxDelay],
174+ d->buff[(idx + 2) % d->maxDelay],
175+ d->alpha);
177176
178− // Increment output pointer modulo length.
179− if ( ++(d->outPoint) == d->maxDelay ) d->outPoint = 0;
177+ // Increment output pointer modulo length
178+ if ( (++d->outPoint) >= d->maxDelay ) d->outPoint = 0;
180179
181180 return d->lastOut;
182181 }
183182
184183 int tDelayL_setDelay (tDelayL* const d, float delay)
185−{
186− if (delay < 0.0f) d->delay = 0.0f;
187− else if (delay <= d->maxDelay) d->delay = delay;
188− else d->delay = d->maxDelay;
184+{
185+ d->delay = LEAF_clip(0.0f, delay, d->maxDelay);
189186
190187 float outPointer = d->inPoint - d->delay;
191188
@@ -326,9 +323,8 @@ float tDelayA_tick (tDelayA* const d, float input) · 134 unmodified lines
326323
327324 int tDelayA_setDelay (tDelayA* const d, float delay)
328325 {
329− if (delay < 0.5f) d->delay = 0.5f;
330− else if (delay <= d->maxDelay) d->delay = delay;
331− else d->delay = d->maxDelay;
326+ d->delay = LEAF_clip(0.5f, delay, d->maxDelay);
327+
332328
333329 // outPoint chases inPoint
334330 float outPointer = (float)d->inPoint - d->delay + 1.0f;
@@ -413,5 +409,156 @@ void tDelayA_setGain (tDelayA* const d, float gain) · 78 unmodified lines
413409 float tDelayA_getGain (tDelayA* const d)
414410 {
415411 return d->gain;
412+}
413+
414+// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ TapeDelay ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ //
415+void 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+
436+void tTapeDelay_free(tTapeDelay* const d)
437+{
438+ leaf_free(d->buff);
439+ leaf_free(d);
440+}
441+
442+int count = 0;
443+
444+#define SMOOTH_FACTOR 10.f
445+
446+float 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+
477+void tTapeDelay_setRate(tTapeDelay* const d, float rate)
478+{
479+ d->inc = rate;
480+}
481+
482+int 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+
498+float 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+
519+void 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+
529+float 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+
539+float tTapeDelay_getDelay (tTapeDelay *d)
540+{
541+ return d->delay;
542+}
543+
544+float tTapeDelay_getLastOut (tTapeDelay* const d)
545+{
546+ return d->lastOut;
547+}
548+
549+float tTapeDelay_getLastIn (tTapeDelay* const d)
550+{
551+ return d->lastIn;
552+}
553+
554+void tTapeDelay_setGain (tTapeDelay* const d, float gain)
555+{
556+ if (gain < 0.0f) d->gain = 0.0f;
557+ else d->gain = gain;
558+}
559+
560+float tTapeDelay_getGain (tTapeDelay* const d)
561+{
562+ return d->gain;
416563 }
417564

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