Skip to content

Commit

Permalink
Better sanity checking for some sample editor routines
Browse files Browse the repository at this point in the history
  • Loading branch information
8bitbubsy committed Jun 8, 2023
1 parent 4779a03 commit a73d515
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 53 deletions.
102 changes: 54 additions & 48 deletions src/pt2_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1270,8 +1270,6 @@ static void handleSamplerVolumeBox(void)
// NORMALIZE
if (mouse.x >= 101 && mouse.x <= 143)
{
int32_t sampleLength;

if (editor.sampleZero)
{
statusNotSampleZero();
Expand All @@ -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;
Expand Down Expand Up @@ -1382,8 +1382,6 @@ static void handleSamplerVolumeBox(void)
// RAMP
else if (mouse.x >= 72 && mouse.x <= 100)
{
int32_t sampleLength;

if (editor.sampleZero)
{
statusNotSampleZero();
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/pt2_sampler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a73d515

Please sign in to comment.