Fix trailing samples not being cut when using pad end

This commit is contained in:
bnnm 2020-11-09 14:53:34 +01:00
parent 1770ca53b6
commit 1dbf99e994

View File

@ -404,13 +404,21 @@ static int render_fade(VGMSTREAM* vgmstream, sample_t* buf, int samples_done) {
} }
} }
static int render_pad_end(VGMSTREAM* vgmstream, sample_t* buf, int samples_to_do) { static int render_pad_end(VGMSTREAM* vgmstream, sample_t* buf, int samples_done) {
play_state_t* ps = &vgmstream->pstate;
int channels = vgmstream->pstate.output_channels; int channels = vgmstream->pstate.output_channels;
int start = 0;
/* since anything beyond pad end is silence no need to check ranges */ /* since anything beyond pad end is silence no need to check end */
if (ps->play_position < ps->pad_end_start) {
start = samples_done - (ps->play_position + samples_done - ps->pad_end_start);
}
else {
start = 0;
}
memset(buf, 0, samples_to_do * sizeof(sample_t) * channels); memset(buf + (start * channels), 0, (samples_done - start) * sizeof(sample_t) * channels);
return samples_to_do; return samples_done;
} }
@ -445,7 +453,7 @@ int render_vgmstream(sample_t* buf, int32_t sample_count, VGMSTREAM* vgmstream)
tmpbuf += done * vgmstream->pstate.output_channels; /* as if mixed */ tmpbuf += done * vgmstream->pstate.output_channels; /* as if mixed */
} }
/* end padding (done before to avoid decoding if possible) */ /* end padding (done before to avoid decoding if possible, samples_to_do becomes 0) */
if (!vgmstream->config.play_forever /* && ps->pad_end_left */ if (!vgmstream->config.play_forever /* && ps->pad_end_left */
&& ps->play_position + samples_done >= ps->pad_end_start) { && ps->play_position + samples_done >= ps->pad_end_start) {
done = render_pad_end(vgmstream, tmpbuf, samples_to_do); done = render_pad_end(vgmstream, tmpbuf, samples_to_do);
@ -462,12 +470,18 @@ int render_vgmstream(sample_t* buf, int32_t sample_count, VGMSTREAM* vgmstream)
samples_done += done; samples_done += done;
if (!vgmstream->config.play_forever) {
/* simple fadeout */ /* simple fadeout */
if (!vgmstream->config.play_forever && ps->fade_left if (ps->fade_left && ps->play_position + done >= ps->fade_start) {
&& ps->play_position + done >= ps->fade_start) {
render_fade(vgmstream, tmpbuf, done); render_fade(vgmstream, tmpbuf, done);
} }
/* silence leftover buf samples (rarely used when no fade is set) */
if (ps->play_position + done >= ps->pad_end_start) {
render_pad_end(vgmstream, tmpbuf, done);
}
}
tmpbuf += done * vgmstream->pstate.output_channels; tmpbuf += done * vgmstream->pstate.output_channels;
} }