implement envelopes

This commit is contained in:
2025-12-24 22:25:42 -06:00
parent 544e1d1849
commit 37b3c6ed66
6 changed files with 127 additions and 10 deletions

54
src/synth/Envelope.h Normal file
View File

@@ -0,0 +1,54 @@
#pragma once
#include <cstdint>
#include <algorithm>
enum class State {
Idle,
Attack,
Sustain,
Decay,
Release
};
class Envelope {
public:
Envelope();
~Envelope() = default;
// setters
void setSampleRate(float sampleRate) { sampleRate_ = sampleRate; }
void setAttack(float seconds) { attack_ = std::max(seconds, 0.0001f); }
void setDecay(float seconds) { decay_ = std::max(seconds, 0.0001f); }
void setSustain(float level) { sustain_ = level; }
void setRelease(float seconds) { release_ = std::max(seconds, 0.0001f); }
// values close to zero introduce that popping sound on noteOn/noteOffs
// note events
void noteOn();
void noteOff();
// return current level
float process();
// determine if a note is playing or not
bool isActive() const { return state_ != State::Idle; }
private:
State state_ = State::Idle;
float sampleRate_ = 44100.0f;
float attack_ = 0.05f; // seconds
float decay_ = 0.2f; // seconds
float sustain_ = 0.7f; // level
float release_ = 0.2f; // seconds
float value_ = 0.0f;
};