Skip to content

Commit

Permalink
fix(DmPerformance): get rid of broken (out-of-range) curves that can …
Browse files Browse the repository at this point in the history
…destroy the PCM
  • Loading branch information
lmichaelis committed Apr 30, 2024
1 parent dd4db01 commit d2b7079
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Performance.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,14 @@ static void DmPerformance_playPattern(DmPerformance* slf, DmPattern* pttn) {
int16_t start = curve.start_value;
int16_t end = curve.end_value;

// Some MIDI control curves have invalid ranges (e.g. -22000 to 127)
bool start_in_range = start >= 0 && start <= 127;
bool end_in_range = end >= 0 && end <= 127;
if (curve.event_type == DmCurveType_CONTROL_CHANGE && !(start_in_range && end_in_range)) {
Dm_report(DmLogLevel_DEBUG, "DmPerformance: Curve is out-of-range");
continue;
}

float prev_value = start;
// TODO(lmichaelis): Check whether this is actually correct!
for (uint32_t k = 0; k < (duration / DmInt_CURVE_SPACING); ++k) {
Expand Down
11 changes: 9 additions & 2 deletions src/Synth.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,17 @@ size_t DmSynth_render(DmSynth* slf, void* buf, size_t len, DmRenderOptions fmt)
tsf_set_output(slf->channels[i].synth, TSF_MONO, 44100, 0);
}

float vol = slf->channels[i].volume;
if (vol < 0) {
vol = 0;
} else if (vol > 1) {
vol = 1;
}

if (fmt & DmRender_FLOAT) {
tsf_render_float(slf->channels[i].synth, buf, (int) len / channels, true, slf->channels[i].volume);
tsf_render_float(slf->channels[i].synth, buf, (int) len / channels, true, vol);
} else {
tsf_render_short(slf->channels[i].synth, buf, (int) len / channels, true, slf->channels[i].volume);
tsf_render_short(slf->channels[i].synth, buf, (int) len / channels, true, vol);
}
}

Expand Down

0 comments on commit d2b7079

Please sign in to comment.