17#ifndef SBL_DSP_COMP_ENVELOPE_FOLLOWER_HPP_
18#define SBL_DSP_COMP_ENVELOPE_FOLLOWER_HPP_
39 void set_times(
float attack_ms,
float release_ms, uint32_t sample_rate) {
40 float sr =
static_cast<float>(sample_rate);
44 float attack_samples = (attack_ms / 1000.0f) * sr;
45 float release_samples = (release_ms / 1000.0f) * sr;
47 if (attack_samples > 0.0f) {
48 float x = 1.0f / attack_samples;
49 attack_coeff_ = x / (1.0f + x);
54 if (release_samples > 0.0f) {
55 float x = 1.0f / release_samples;
56 release_coeff_ = x / (1.0f + x);
58 release_coeff_ = 1.0f;
72 attack_coeff_ = attack;
73 release_coeff_ = release;
88 float rectified = sample < 0.0f ? -sample : sample;
91 float coeff = rectified > envelope_ ? attack_coeff_ : release_coeff_;
92 envelope_ += coeff * (rectified - envelope_);
106 void process(
const float* in,
float* out, uint16_t frames) {
107 for (uint16_t i = 0; i < frames; ++i) {
113 float value()
const {
return envelope_; }
119 void reset(
float initial) { envelope_ = initial; }
122 float envelope_ = 0.0f;
123 float attack_coeff_ = 0.0f;
124 float release_coeff_ = 0.0f;
void process(const float *in, float *out, uint16_t frames)
Process a block, write envelope to output buffer.
void reset()
Reset envelope state to zero.
void set_coefficients(float attack, float release)
Set attack and release coefficients directly.
void set_times(float attack_ms, float release_ms, uint32_t sample_rate)
Set attack and release times in milliseconds.
float process(float sample)
Process a single sample, return envelope value.
float value() const
Current envelope value (can be read without processing)
void reset(float initial)
Reset envelope state to a specific value.
Compositions: subcircuits of primitives.