Skip to content

Commit

Permalink
Merge pull request #1067 from FluidSynth/issue1059
Browse files Browse the repository at this point in the history
Prevent ModEnv from being stuck in decay phase
  • Loading branch information
derselbst authored Mar 15, 2022
2 parents 22144a4 + 344796a commit c037dde
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
15 changes: 7 additions & 8 deletions src/rvoice/fluid_adsr_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct _fluid_env_data_t
};

/* Indices for envelope tables */
enum fluid_voice_envelope_index_t
enum fluid_voice_envelope_index
{
FLUID_VOICE_ENVDELAY,
FLUID_VOICE_ENVATTACK,
Expand All @@ -49,22 +49,22 @@ enum fluid_voice_envelope_index_t
FLUID_VOICE_ENVLAST
};

typedef enum fluid_voice_envelope_index_t fluid_adsr_env_section_t;
typedef enum fluid_voice_envelope_index fluid_adsr_env_section_t;

typedef struct _fluid_adsr_env_t fluid_adsr_env_t;

struct _fluid_adsr_env_t
{
fluid_env_data_t data[FLUID_VOICE_ENVLAST];
unsigned int count;
int section;
fluid_real_t val; /* the current value of the envelope */
fluid_adsr_env_section_t section;
};

/* For performance, all functions are inlined */

static FLUID_INLINE void
fluid_adsr_env_calc(fluid_adsr_env_t *env, int is_volenv)
fluid_adsr_env_calc(fluid_adsr_env_t *env)
{
fluid_env_data_t *env_data;
fluid_real_t x;
Expand All @@ -76,7 +76,8 @@ fluid_adsr_env_calc(fluid_adsr_env_t *env, int is_volenv)
{
// If we're switching envelope stages from decay to sustain, force the value to be the end value of the previous stage
// Hmm, should this only apply to volenv? It was so before refactoring, so keep it for now. [DH]
if(env->section == FLUID_VOICE_ENVDECAY && is_volenv)
// No, must apply to both, otherwise some voices may sound detuned. [TM] (https://github.com/FluidSynth/fluidsynth/issues/1059)
if(env->section == FLUID_VOICE_ENVDECAY)
{
env->val = env_data->min * env_data->coeff;
}
Expand Down Expand Up @@ -106,8 +107,6 @@ fluid_adsr_env_calc(fluid_adsr_env_t *env, int is_volenv)
}

env->val = x;


}

/* This one cannot be inlined since it is referenced in
Expand All @@ -118,7 +117,7 @@ static FLUID_INLINE void
fluid_adsr_env_reset(fluid_adsr_env_t *env)
{
env->count = 0;
env->section = 0;
env->section = FLUID_VOICE_ENVDELAY;
env->val = 0.0f;
}

Expand Down
4 changes: 2 additions & 2 deletions src/rvoice/fluid_rvoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ fluid_rvoice_write(fluid_rvoice_t *voice, fluid_real_t *dsp_buf)

/******************* vol env **********************/

fluid_adsr_env_calc(&voice->envlfo.volenv, 1);
fluid_adsr_env_calc(&voice->envlfo.volenv);
fluid_check_fpe("voice_write vol env");

if(fluid_adsr_env_get_section(&voice->envlfo.volenv) == FLUID_VOICE_ENVFINISHED)
Expand All @@ -341,7 +341,7 @@ fluid_rvoice_write(fluid_rvoice_t *voice, fluid_real_t *dsp_buf)

/******************* mod env **********************/

fluid_adsr_env_calc(&voice->envlfo.modenv, 0);
fluid_adsr_env_calc(&voice->envlfo.modenv);
fluid_check_fpe("voice_write mod env");

/******************* lfo **********************/
Expand Down
3 changes: 2 additions & 1 deletion src/synth/fluid_voice.c
Original file line number Diff line number Diff line change
Expand Up @@ -1110,8 +1110,9 @@ fluid_voice_update_param(fluid_voice_t *voice, int gen)
/* Modulation envelope */
case GEN_MODENVDELAY: /* SF2.01 section 8.1.3 # 25 */
fluid_clip(x, -12000.0f, 5000.0f);
count = NUM_BUFFERS_DELAY(x);
fluid_voice_update_modenv(voice, TRUE, FLUID_VOICE_ENVDELAY,
NUM_BUFFERS_DELAY(x), 0.0f, 0.0f, -1.0f, 1.0f);
count, 0.0f, 0.0f, -1.0f, 1.0f);
break;

case GEN_MODENVATTACK: /* SF2.01 section 8.1.3 # 26 */
Expand Down

0 comments on commit c037dde

Please sign in to comment.