You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
psyn/env.c

39 lines
1.1 KiB

#include <stdint.h>
#include "env.h"
#include "psyn.h"
void env_init(struct envelope_t *env, double attack, double attack_hold, double decay, double sustain, double release)
{
env->attack = attack;
env->attack_hold = attack_hold;
env->decay = decay;
env->sustain = sustain;
env->release = release;
env->attack_s = env->attack * _sample_rate;
env->attack_hold_s = env->attack_s + env->attack_hold * _sample_rate;
env->decay_s = env->attack_hold_s + env->decay * _sample_rate;
env->release_s = env->release * _sample_rate;
}
double env_getamplitude(const struct envelope_t *env, uint32_t sample, uint32_t released)
{
double amplitude;
if (sample < env->attack_s) {
amplitude = sample / env->attack_s;
} else if (sample < env->attack_hold_s) {
amplitude = 1.0;
} else if (sample < env->decay_s) {
amplitude = 1.0 - (sample - env->attack_hold_s) / (env->decay_s - env->attack_hold_s) * (1.0 - env->sustain);
} else {
amplitude = env->sustain;
}
if (released > 0) {
amplitude *= (1.0 - (sample - released) / env->release_s);
}
return amplitude;
}