From b0128e30840dff1e7db7bcfd69e6fa4f09ed5fbe Mon Sep 17 00:00:00 2001 From: gramanas Date: Mon, 27 Nov 2023 01:03:38 +0200 Subject: wavetables --- src/osc.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/osc.c') diff --git a/src/osc.c b/src/osc.c index 384f002..8ce915c 100644 --- a/src/osc.c +++ b/src/osc.c @@ -21,17 +21,19 @@ osc_interpolate(float offset, float start, float end) { int osc_next_index(osc_t * osc, float offset) { - if (offset >= osc->len) offset = 0; + if (offset >= osc->len) offset = fmodf(osc->start + offset, osc->len - osc->start); + if (offset < osc->start) offset = osc->start + fmodf(osc->start + offset, osc->len - osc->start); int current_index = (int)offset; - return current_index + 1 >= osc->len ? 0 : current_index + 1; + return current_index + 1 >= osc->len ? osc->start : current_index + 1; } float osc_next_offset(osc_t * osc, float f, float offset) { - if (offset >= osc->len) offset = 0; + if (offset >= osc->len) offset = fmodf(osc->start + offset, osc->len - osc->start); + if (offset < osc->start) offset = osc->start + fmodf(osc->start + offset, osc->len - osc->start); if (osc->type == SAMPLE) { // this makes A2 be the base ??? @@ -40,7 +42,7 @@ osc_next_offset(osc_t * osc, float f, float offset) f = f / (16.35160 * pow(2, (2 + 5 / 12.0))); } - float step = f * ((float)osc->len / SAMPLE_RATE); + float step = f * ((osc->len - osc->start) / (float)SAMPLE_RATE); offset += step; if (offset >= osc->len) { -- cgit v1.2.3