- fluid_env_data_t* env_data;
- fluid_real_t x;
-
- env_data = &env->data[env->section];
-
- /* skip to the next section of the envelope if necessary */
- while (env->count >= env_data->count)
- {
- // 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)
- env->val = env_data->min * env_data->coeff;
-
- env_data = &env->data[++env->section];
- env->count = 0;
- }
+ fluid_env_data_t *env_data;
+ fluid_real_t x;
+
+ env_data = &env->data[env->section];
+
+ /* skip to the next section of the envelope if necessary */
+ while(env->count >= env_data->count)
+ {
+ // 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)
+ {
+ env->val = env_data->min * env_data->coeff;
+ }
+
+ env_data = &env->data[++env->section];
+ env->count = 0;
+ }
+
+ /* calculate the envelope value and check for valid range */
+ x = env_data->coeff * env->val + env_data->increment;
+
+ if(x < env_data->min)
+ {
+ x = env_data->min;
+ env->section++;
+ env->count = 0;
+ }
+ else if(x > env_data->max)
+ {
+ x = env_data->max;
+ env->section++;
+ env->count = 0;
+ }
+ else
+ {
+ env->count++;
+ }
+
+ env->val = x;