25namespace deluge::modulation {
29[[gnu::always_inline]]
constexpr float fastExp2(
float x) {
30 int32_t whole =
static_cast<int32_t
>(x);
31 float frac = x -
static_cast<float>(whole);
37 float p = 0.00185888436f;
38 p = p * frac + 0.00903089405f;
39 p = p * frac + 0.0557939352f;
40 p = p * frac + 0.240164314f;
41 p = p * frac + 0.693151594f;
43 return std::bit_cast<float>(std::bit_cast<int32_t>(p) + (whole << 23));
55 constexpr EnvelopeCurve() : EnvelopeCurve(0.f) {}
56 constexpr explicit EnvelopeCurve(
float k)
57 : k_(k), floor_(fastExp2(-k)), scale_((k == 0.f) ? 1.f : 1.f / (1.f - fastExp2(-k))) {}
60 [[gnu::always_inline]]
constexpr float falling(
float x)
const {
64 return (fastExp2(-k_ * x) - floor_) * scale_;
68 [[gnu::always_inline]]
constexpr float rising(
float x)
const {
return 1.f -
falling(x); }
72 value = std::clamp(value, 0.f, 1.f);
76 float x = -std::log2((1.f - value) / scale_ + floor_) / k_;
77 return std::clamp(x, 0.f, 1.f);
80 constexpr float k()
const {
return k_; }
98 static constexpr int32_t kMaxCurve = 50;
99 static constexpr int32_t kClassicCurve = 32;
101 float k =
static_cast<float>(curve) * 0.25f;
109inline constexpr EnvelopeShape kAudioClipEnvelopeShape{.release = EnvelopeCurve{4.f}};
111enum class EnvelopeMode : uint8_t {
126constexpr int32_t kNumEnvelopeModes = 4;
131enum class EnvelopeFollow : uint8_t {
135constexpr int32_t kNumEnvelopeFollows = 2;
139 EnvelopeMode mode{EnvelopeMode::ADSR};
140 EnvelopeFollow follow{EnvelopeFollow::Z};
141 int8_t curve{EnvelopeShape::kClassicCurve};
144 constexpr void setCurve(int32_t newCurve) {
146 shape = EnvelopeShape::fromCurve(curve);
148 constexpr bool isDefault()
const {
149 return mode == EnvelopeMode::ADSR && follow == EnvelopeFollow::Z && curve == EnvelopeShape::kClassicCurve;
153inline constexpr EnvelopeConfig kAudioClipEnvelopeConfig{.shape = kAudioClipEnvelopeShape};
155char const* envelopeModeToString(EnvelopeMode mode);
156EnvelopeMode stringToEnvelopeMode(
char const*
string);
157char const* envelopeFollowToString(EnvelopeFollow follow);
158EnvelopeFollow stringToEnvelopeFollow(
char const*
string);
Definition envelope_curve.h:53
constexpr float falling(float x) const
x in [0, 1]
Definition envelope_curve.h:60
constexpr float rising(float x) const
The rising mirror image: 0 at x = 0, 1 at x = 1. With k > 0 it's fast at the start,...
Definition envelope_curve.h:68
float risingInverse(float value) const
Inverse of rising(): where on the segment the curve reaches value. Not for the render path (uses log2...
Definition envelope_curve.h:71
The shape of each stage of an envelope.
Definition envelope_curve.h:89
A sound's settings for one of its envelopes. Not a param: it doesn't vary per clip and can't be modul...
Definition envelope_curve.h:138
The shape of each stage of an envelope.
Definition envelope_curve.h:89
static constexpr int32_t kMinCurve
Definition envelope_curve.h:97