Skip to content

Commit

Permalink
Merge pull request #860 from edge-classic/fluidlite-fixes
Browse files Browse the repository at this point in the history
Fluidlite fixes
  • Loading branch information
dashodanger authored Jan 22, 2025
2 parents 03cf2a8 + 8bde5f9 commit 8b8d954
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
53 changes: 20 additions & 33 deletions libraries/fluidlite/src/fluid_defsfont.c
Original file line number Diff line number Diff line change
Expand Up @@ -3167,56 +3167,43 @@ fixup_sample (SFData * sf)
{
fluid_list_t *p;
SFSample *sam;
int invalid_loops=FALSE;
int invalid_loopstart;
int invalid_loopend, loopend_end_mismatch;

p = sf->sample;
while (p)
{
sam = (SFSample *) (p->data);

/* The SoundFont 2.4 spec defines the loop start index as the first sample point of the loop */
invalid_loopstart = (sam->loopstart < sam->start) || (sam->loopstart >= sam->loopend);
/* while loop end is the first point AFTER the last sample of the loop.
* this is as it should be. however we cannot be sure whether any of sam.loopend or sam.end
* is correct. hours of thinking through this have concluded, that it would be best practice
* to mangle with loops as little as necessary by only making sure loopend is within
* sdtachunk_size. incorrect soundfont shall preferably fail loudly. */
invalid_loopend = (sam->loopend > sdtachunk_size) || (sam->loopstart >= sam->loopend);

loopend_end_mismatch = (sam->loopend > sam->end);

/* if sample is not a ROM sample and end is over the sample data chunk
or sam start is greater than 4 less than the end (at least 4 samples) */
if ((!(sam->sampletype & FLUID_SAMPLETYPE_ROM)
&& sam->end > sdtachunk_size) || sam->start > (sam->end - 4)) {
&& sam->end > sdtachunk_size) || sam->start > (sam->end - 4))
{
FLUID_LOG (FLUID_WARN, _("Sample '%s' start/end file positions are invalid,"
" disabling and will not be saved"), sam->name);
" disabling and will not be saved"), sam->name);

/* disable sample by setting all sample markers to 0 */
sam->start = sam->end = sam->loopstart = sam->loopend = 0;

return (OK);
}
/* compressed samples get fixed up after decompression */
else if (sam->sampletype & FLUID_SAMPLETYPE_OGG_VORBIS)
{}
else if (invalid_loopstart || invalid_loopend || loopend_end_mismatch)
{
/* loop is fowled?? (cluck cluck :) */
invalid_loops |= TRUE;

/* force incorrect loop points into the sample range, ignore padding */
if(invalid_loopstart)
{
sam->loopstart = sam->start + 1;
}

if(invalid_loopend)
{
sam->loopend = sam->end - 1;
}
else if (sam->sampletype & FLUID_SAMPLETYPE_OGG_VORBIS) {}
else if (sam->loopend > sam->end || sam->loopstart >= sam->loopend
|| sam->loopstart <= sam->start)
{ /* loop is fowled?? (cluck cluck :) */
/* can pad loop by 8 samples and ensure at least 4 for loop (2*8+4) */
// FLUID_LOG (FLUID_WARN, _("Loop point fixup disabled: %s"), sam->name);
// Disabled - see https://github.com/divideconcept/FluidLite/issues/45
// if ((sam->end - sam->start) >= 20)
// {
// sam->loopstart = sam->start + 8;
// sam->loopend = sam->end - 8;
// }
// else
// { /* loop is fowled, sample is tiny (can't pad 8 samples) */
// sam->loopstart = sam->start + 1;
// sam->loopend = sam->end - 1;
// }
}

/* convert sample end, loopstart, loopend to offsets from sam->start */
Expand Down
2 changes: 1 addition & 1 deletion libraries/fluidlite/src/fluid_synth.c
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,7 @@ fluid_synth_program_change(fluid_synth_t* synth, int chan, int prognum)
subst_prog = prognum;

/* Melodic instrument? */
if (banknum != DRUM_INST_BANK)
if (channel->channum != 9 && banknum != DRUM_INST_BANK)
{
subst_bank = 0;

Expand Down
2 changes: 1 addition & 1 deletion libraries/fluidlite/src/fluid_voice.c
Original file line number Diff line number Diff line change
Expand Up @@ -1954,7 +1954,7 @@ int fluid_voice_optimize_sample(fluid_sample_t* s)

if (!s->amplitude_that_reaches_noise_floor_is_valid){ /* Only once */
/* Scan the loop */
for (i = (int)s->loopstart; i < (int) s->loopend; i ++){
for (i = (int)s->loopstart; i < (int) s->loopend && i < (int) s->end; i ++){
signed short val = s->data[i];
if (val > peak_max) {
peak_max = val;
Expand Down

0 comments on commit 8b8d954

Please sign in to comment.