bw_env_gen: reintroduce BW_ENV_GEN_V_MAX
This commit is contained in:
parent
750c807db3
commit
adbd1ab61c
@ -465,6 +465,8 @@ struct bw_env_gen_state {
|
||||
#define BW_ENV_GEN_PARAM_SUSTAIN (1<<2)
|
||||
#define BW_ENV_GEN_PARAM_RELEASE (1<<3)
|
||||
|
||||
#define BW_ENV_GEN_V_MAX 4294967040 // instead of UINT32_MAX to avoid big float precision errors (e.g., sustain = 1)
|
||||
|
||||
static inline void bw_env_gen_init(
|
||||
bw_env_gen_coeffs * BW_RESTRICT coeffs) {
|
||||
BW_ASSERT(coeffs != BW_NULL);
|
||||
@ -513,13 +515,13 @@ static inline void bw_env_gen_do_update_coeffs_ctrl(
|
||||
if (coeffs->param_changed) {
|
||||
// coeffs->T = actual minimum duration
|
||||
if (coeffs->param_changed & BW_ENV_GEN_PARAM_ATTACK)
|
||||
coeffs->attack_inc = coeffs->attack > coeffs->T ? (float)UINT32_MAX * (coeffs->T * bw_rcpf(coeffs->attack)) : UINT32_MAX;
|
||||
coeffs->attack_inc = coeffs->attack > coeffs->T ? (float)BW_ENV_GEN_V_MAX * (coeffs->T * bw_rcpf(coeffs->attack)) : BW_ENV_GEN_V_MAX;
|
||||
if (coeffs->param_changed & (BW_ENV_GEN_PARAM_DECAY | BW_ENV_GEN_PARAM_SUSTAIN))
|
||||
coeffs->decay_dec = (1.f - coeffs->sustain) * (coeffs->decay > coeffs->T ? ((float)UINT32_MAX * (coeffs->T * bw_rcpf(coeffs->decay))) : UINT32_MAX);
|
||||
coeffs->decay_dec = (1.f - coeffs->sustain) * (coeffs->decay > coeffs->T ? ((float)BW_ENV_GEN_V_MAX * (coeffs->T * bw_rcpf(coeffs->decay))) : BW_ENV_GEN_V_MAX);
|
||||
if (coeffs->param_changed & BW_ENV_GEN_PARAM_SUSTAIN)
|
||||
coeffs->sustain_v = (float)UINT32_MAX * coeffs->sustain;
|
||||
coeffs->sustain_v = (float)BW_ENV_GEN_V_MAX * coeffs->sustain;
|
||||
if (coeffs->param_changed & (BW_ENV_GEN_PARAM_SUSTAIN | BW_ENV_GEN_PARAM_RELEASE))
|
||||
coeffs->release_dec = coeffs->sustain * (coeffs->release > coeffs->T ? ((float)UINT32_MAX * (coeffs->T * bw_rcpf(coeffs->release))) : UINT32_MAX);
|
||||
coeffs->release_dec = coeffs->sustain * (coeffs->release > coeffs->T ? ((float)BW_ENV_GEN_V_MAX * (coeffs->T * bw_rcpf(coeffs->release))) : BW_ENV_GEN_V_MAX);
|
||||
coeffs->param_changed = 0;
|
||||
}
|
||||
}
|
||||
@ -558,7 +560,7 @@ static inline float bw_env_gen_reset_state(
|
||||
state->phase = bw_env_gen_phase_off;
|
||||
state->v = 0;
|
||||
}
|
||||
const float y = (1.f / (float)UINT32_MAX) * state->v;
|
||||
const float y = (1.f / (float)BW_ENV_GEN_V_MAX) * state->v;
|
||||
|
||||
#ifdef BW_DEBUG_DEEP
|
||||
state->hash = bw_hash_sdbm("bw_env_gen_state");
|
||||
@ -658,8 +660,8 @@ static inline float bw_env_gen_process1(
|
||||
switch (state->phase) {
|
||||
case bw_env_gen_phase_attack:
|
||||
v = state->v + coeffs->attack_inc;
|
||||
if (v == UINT32_MAX || v <= state->v) {
|
||||
v = UINT32_MAX;
|
||||
if (v >= BW_ENV_GEN_V_MAX || v <= state->v) {
|
||||
v = BW_ENV_GEN_V_MAX;
|
||||
state->phase = bw_env_gen_phase_decay;
|
||||
}
|
||||
break;
|
||||
@ -671,7 +673,7 @@ static inline float bw_env_gen_process1(
|
||||
}
|
||||
break;
|
||||
case bw_env_gen_phase_sustain:
|
||||
v = (uint32_t)((float)UINT32_MAX * bw_clipf(bw_one_pole_process1(&coeffs->smooth_coeffs, &state->smooth_state, coeffs->sustain), 0.f, 1.f));
|
||||
v = (uint32_t)((float)BW_ENV_GEN_V_MAX * bw_clipf(bw_one_pole_process1(&coeffs->smooth_coeffs, &state->smooth_state, coeffs->sustain), 0.f, 1.f));
|
||||
if (coeffs->skip_sustain)
|
||||
state->phase = bw_env_gen_phase_release;
|
||||
break;
|
||||
@ -687,7 +689,7 @@ static inline float bw_env_gen_process1(
|
||||
break;
|
||||
}
|
||||
state->v = v;
|
||||
const float y = (1.f / (float)UINT32_MAX) * v;
|
||||
const float y = (1.f / (float)BW_ENV_GEN_V_MAX) * v;
|
||||
|
||||
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
||||
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
||||
@ -889,7 +891,7 @@ static inline float bw_env_gen_get_y_z1(
|
||||
BW_ASSERT(state != BW_NULL);
|
||||
BW_ASSERT_DEEP(bw_env_gen_state_is_valid(BW_NULL, state));
|
||||
|
||||
const float y = (1.f / (float)UINT32_MAX) * state->v;
|
||||
const float y = (1.f / (float)BW_ENV_GEN_V_MAX) * state->v;
|
||||
|
||||
BW_ASSERT(bw_is_finite(y));
|
||||
|
||||
@ -945,6 +947,7 @@ static inline char bw_env_gen_state_is_valid(
|
||||
#undef BW_ENV_GEN_PARAM_DECAY
|
||||
#undef BW_ENV_GEN_PARAM_SUSTAIN
|
||||
#undef BW_ENV_GEN_PARAM_RELEASE
|
||||
#undef BW_ENV_GEN_V_MAX
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user