From a73d515abe9a5c20b2c10d6055173365dba6416e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20S=C3=B8rensen?= Date: Thu, 8 Jun 2023 16:00:08 +0200 Subject: [PATCH] Better sanity checking for some sample editor routines --- src/pt2_mouse.c | 102 ++++++++++++++++++++++++---------------------- src/pt2_sampler.c | 10 ++--- 2 files changed, 59 insertions(+), 53 deletions(-) diff --git a/src/pt2_mouse.c b/src/pt2_mouse.c index 8576f5d..c55e888 100644 --- a/src/pt2_mouse.c +++ b/src/pt2_mouse.c @@ -1270,8 +1270,6 @@ static void handleSamplerVolumeBox(void) // NORMALIZE if (mouse.x >= 101 && mouse.x <= 143) { - int32_t sampleLength; - if (editor.sampleZero) { statusNotSampleZero(); @@ -1287,44 +1285,46 @@ static void handleSamplerVolumeBox(void) int8_t *sampleData = &song->sampleData[s->offset]; - if (editor.markStartOfs != -1 && editor.markEndOfs != editor.markStartOfs) - { - sampleData += editor.markStartOfs; - sampleLength = editor.markEndOfs - editor.markStartOfs; - } - else - { - sampleLength = s->length; - } - - int16_t sampleVol = 0; - int32_t sampleIndex = 0; + int32_t from = 0; + int32_t to = s->length; - while (sampleIndex < sampleLength) + if (editor.markStartOfs != -1) { - int16_t sample = *sampleData++; - sample = ABS(sample); + from = editor.markStartOfs; + to = editor.markEndOfs; - if (sampleVol < sample) - sampleVol = sample; + if (to > s->length) + to = s->length; - sampleIndex++; + if (from == to || from >= s->length || to < from) + { + from = 0; + to = s->length; + } + } + + int32_t hi = 0; + for (int32_t i = from; i < to; i++) + { + const int32_t sample = ABS(sampleData[i]); + if (sample > hi) + hi = sample; } - if (sampleVol <= 0 || sampleVol > 127) + if (hi <= 0 || hi > 127) { editor.vol1 = 100; editor.vol2 = 100; } - else if (sampleVol < 64) + else if (hi < 64) { editor.vol1 = 200; editor.vol2 = 200; } else { - editor.vol1 = (uint16_t)((100 * 127) / sampleVol); - editor.vol2 = (uint16_t)((100 * 127) / sampleVol); + editor.vol1 = (uint16_t)((100 * 127) / hi); + editor.vol2 = (uint16_t)((100 * 127) / hi); } ui.updateVolFromText = true; @@ -1382,8 +1382,6 @@ static void handleSamplerVolumeBox(void) // RAMP else if (mouse.x >= 72 && mouse.x <= 100) { - int32_t sampleLength; - if (editor.sampleZero) { statusNotSampleZero(); @@ -1405,34 +1403,42 @@ static void handleSamplerVolumeBox(void) } int8_t *sampleData = &song->sampleData[s->offset]; - if (editor.markStartOfs != -1 && editor.markEndOfs-editor.markStartOfs >= 1) - { - sampleData += editor.markStartOfs; - sampleLength = editor.markEndOfs - editor.markStartOfs; - } - else - { - sampleLength = s->length; - } - if (sampleLength > 0) + int32_t from = 0; + int32_t to = s->length; + + if (editor.markStartOfs != -1) { - double dSampleLengthMul = 1.0 / sampleLength; + from = editor.markStartOfs; + to = editor.markEndOfs; + + if (to > s->length) + to = s->length; - int32_t sampleIndex = 0; - while (sampleIndex < sampleLength) + if (from == to || from >= s->length || to < from) { - double dSmp = (sampleIndex * editor.vol2) * dSampleLengthMul; - dSmp += ((sampleLength - sampleIndex) * editor.vol1) * dSampleLengthMul; - dSmp *= *sampleData; - dSmp *= (1.0 / 100.0); + from = 0; + to = s->length; + } + } - int32_t smp32 = (int32_t)dSmp; - CLAMP8(smp32); + const int32_t markLength = to - from; - *sampleData++ = (int8_t)smp32; - sampleIndex++; - } + const double dSampleLengthMul = 1.0 / markLength; + + int32_t sampleIndex = 0; + for (int32_t i = from; i < to; i++) + { + double dSmp = (sampleIndex * editor.vol2) * dSampleLengthMul; + dSmp += ((markLength - sampleIndex) * editor.vol1) * dSampleLengthMul; + dSmp *= sampleData[i]; + dSmp *= 1.0 / 100.0; + + int32_t smp32 = (int32_t)dSmp; + CLAMP8(smp32); + + sampleData[i] = (int8_t)smp32; + sampleIndex++; } fixSampleBeep(s); diff --git a/src/pt2_sampler.c b/src/pt2_sampler.c index 2cc70f9..c2abf19 100644 --- a/src/pt2_sampler.c +++ b/src/pt2_sampler.c @@ -572,7 +572,7 @@ void highPassSample(int32_t cutOff) if (to > s->length) to = s->length; - if (from == to) + if (from == to || from >= s->length || to < from) { from = 0; to = s->length; @@ -671,7 +671,7 @@ void lowPassSample(int32_t cutOff) if (to > s->length) to = s->length; - if (from == to) + if (from == to || from >= s->length || to < from) { from = 0; to = s->length; @@ -875,7 +875,7 @@ void samplerRemoveDcOffset(void) if (to > s->length) to = s->length; - if (from == to) + if (from == to || from >= s->length || to < from) { from = 0; to = s->length; @@ -1158,7 +1158,7 @@ void boostSample(int32_t sample, bool ignoreMark) if (to > s->length) to = s->length; - if (from == to) + if (from == to || from >= s->length || to < from) { from = 0; to = s->length; @@ -1209,7 +1209,7 @@ void filterSample(int32_t sample, bool ignoreMark) if (to > s->length) to = s->length; - if (from == to) + if (from == to || from >= s->length || to < from) { from = 0; to = s->length;