diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2023-04-29 14:34:48 +0300 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2023-04-29 14:34:48 +0300 |
commit | 0cceb2057e382129ae24565fdefff87e91b076ab (patch) | |
tree | 36676e98fecc6bf0a0ee80dbab21331dddc372d7 /src/adsr.c | |
parent | 402f6791150d503fc29ed75a7b8be9abfdd3867f (diff) | |
download | synth-project-0cceb2057e382129ae24565fdefff87e91b076ab.tar.gz synth-project-0cceb2057e382129ae24565fdefff87e91b076ab.tar.bz2 synth-project-0cceb2057e382129ae24565fdefff87e91b076ab.zip |
self contained adsr
Diffstat (limited to 'src/adsr.c')
-rw-r--r-- | src/adsr.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/adsr.c b/src/adsr.c new file mode 100644 index 0000000..8ef2ba9 --- /dev/null +++ b/src/adsr.c @@ -0,0 +1,47 @@ +#include "adsr.h" +#include "synth_engine.h" +#include "synth_math.h" + +float +adsr_amplitude(adsr_t *adsr, float noteOn, float noteOff, unsigned long long elapsed) +{ + float mod = 0.0; + float release_amplitude = 0.0; + + float lifetime = elapsed / (float)SAMPLE_RATE; + //float time_active = (noteOn + lifetime) - noteOff; + float time_active = noteOn - noteOff + lifetime; + + if (noteOn != 0 && noteOff == 0) { + if (lifetime < adsr->a) + mod = (lifetime / adsr->a)*(lifetime / adsr->a) * adsr->peak; + + if (lifetime >= adsr->a && lifetime <= ( adsr->a + adsr->d)) + mod = ((lifetime - adsr->a) / adsr->d) * (adsr->s - adsr->peak) + adsr->peak; + + if (lifetime > (adsr->a + adsr->d)) + mod = adsr->s; + } + else { // Note is off + if (lifetime < adsr->a) + release_amplitude = (lifetime / adsr->a)*(lifetime / adsr->a) * adsr->peak; + + if (lifetime >= adsr->a && lifetime <= (adsr->a + adsr->d)) + release_amplitude = ((lifetime - adsr->a) / adsr->d) * (adsr->s - adsr->peak) + adsr->peak; + + if (lifetime > (adsr->a + adsr->d)) + release_amplitude = adsr->s; + + mod = (time_active / adsr->r) * (0.0 - release_amplitude) + release_amplitude; + + if (adsr->r < 0) { + mod = adsr->s; + } + + } + // Amplitude should not be negative + if (mod <= 0.000) + mod = 0.0; + + return clamp(mod); +} |