2026-11-13 02:02:50 +06:00
|
|
|
#include "../include/envelope.hpp"
|
2026-11-12 22:37:00 +06:00
|
|
|
|
2026-11-13 02:02:50 +06:00
|
|
|
void Envelope::note_on(){
|
|
|
|
|
condition = Condition::Attack;
|
|
|
|
|
if (attack_time <= 0.0){
|
|
|
|
|
attack_time = 0.0001;
|
|
|
|
|
}
|
|
|
|
|
rate = (1.0 - currnet_amplitude)/(attack_time*sample_rate);
|
2026-11-12 22:37:00 +06:00
|
|
|
|
2026-11-13 02:02:50 +06:00
|
|
|
}
|
|
|
|
|
void Envelope::note_off(){
|
|
|
|
|
if (condition != Condition::Idle){
|
|
|
|
|
condition = Condition::Release;
|
|
|
|
|
if (release_time <= 0.0){
|
|
|
|
|
release_time = 0.0001;
|
|
|
|
|
}
|
|
|
|
|
rate = (0.0 - currnet_amplitude)/(release_time*sample_rate);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-12 22:37:00 +06:00
|
|
|
|
2026-11-13 02:02:50 +06:00
|
|
|
double Envelope::process(){
|
2026-11-12 22:37:00 +06:00
|
|
|
|
2026-11-13 02:02:50 +06:00
|
|
|
switch(condition){
|
|
|
|
|
case Condition::Attack:{
|
|
|
|
|
currnet_amplitude += rate;
|
|
|
|
|
if (currnet_amplitude >= 1.0){
|
|
|
|
|
currnet_amplitude = 1.0;
|
|
|
|
|
if (decay_time <= 0.0){
|
|
|
|
|
condition = Condition::Sustain;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
condition = Condition::Decay;
|
|
|
|
|
rate = (sustain_level - 1.0)/(decay_time*sample_rate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return currnet_amplitude;
|
|
|
|
|
}
|
2026-11-12 22:37:00 +06:00
|
|
|
|
2026-11-13 02:02:50 +06:00
|
|
|
case Condition::Decay:{
|
|
|
|
|
currnet_amplitude += rate;
|
|
|
|
|
if (currnet_amplitude <= sustain_level){
|
|
|
|
|
currnet_amplitude = sustain_level;
|
|
|
|
|
condition = Condition::Sustain;
|
|
|
|
|
}
|
|
|
|
|
return currnet_amplitude;
|
|
|
|
|
}
|
2026-11-12 22:37:00 +06:00
|
|
|
|
2026-11-13 02:02:50 +06:00
|
|
|
case Condition::Sustain:{
|
|
|
|
|
return currnet_amplitude;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case Condition::Release:{
|
|
|
|
|
currnet_amplitude += rate;
|
|
|
|
|
if (currnet_amplitude <= 0.0){
|
|
|
|
|
currnet_amplitude = 0.0;
|
|
|
|
|
condition = Condition::Idle;
|
|
|
|
|
}
|
|
|
|
|
return currnet_amplitude;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case Condition::Idle:{
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default: return 0.0;
|
|
|
|
|
|
|
|
|
|
}
|
2026-11-12 22:37:00 +06:00
|
|
|
}
|
2026-11-14 20:37:48 +06:00
|
|
|
|
|
|
|
|
bool Envelope::is_idle() const{
|
|
|
|
|
return condition == Condition::Idle;
|
|
|
|
|
}
|