Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
envelope.hpp
Go to the documentation of this file.
1// sbl/widgets/mod/envelope.hpp — Envelope widget (Audio Stack — Widgets)
2//
3// Pre-composed envelope shapes built on SegmentGenerator. Factory functions
4// create common configurations (ADSR, AR, AHR, AD) with a single call.
5// For custom multi-segment envelopes, use custom().
6//
7// The curve engine is selected at compile time. Default is FloatRationalWarp.
8//
9// Output is float [0.0, 1.0] — SegmentGenerator operates natively in float.
10//
11// See FDP-014 and FDP-016 for design rationale.
12
13#pragma once
14
15#include <cstdint>
16
17#include <sbl/dsp/segment.hpp>
18
20
21template<typename CurveEngine = dsp::FloatDefaultWarp>
22class Envelope {
23public:
24 /// @note All public methods are ISR-safe — bounded computation, no I/O.
25
26 // ── Factory functions ─────────────────────────────────────────
27 //
28 // Each factory returns a fully configured Envelope ready for gate_on().
29 // The curve parameter applies to all segments (use custom() for per-segment curves).
30 // Curve: -1.0 = concave (fast start), 0.0 = linear, 1.0 = convex (slow start).
31
32 /// Attack-Decay-Sustain-Release envelope.
33 /// @param sustain Sustain level [0.0, 1.0]
34 /// @param curve Shape [-1.0, 1.0]: -1 = concave, 0 = linear, 1 = convex
35 static Envelope adsr(float attack_ms, float decay_ms,
36 float sustain, float release_ms,
37 float curve = 0.0f) {
38 Envelope env;
39 dsp::Segment segs[] = {
40 {1.0f, curve, attack_ms},
41 {sustain, curve, decay_ms},
42 {0.0f, curve, release_ms},
43 };
44 env.gen_.configure(segs, 3, 1);
45 return env;
46 }
47
48 /// Attack-Release envelope (sustain at peak until gate_off).
49 static Envelope ar(float attack_ms, float release_ms,
50 float curve = 0.0f) {
51 Envelope env;
52 dsp::Segment segs[] = {
53 {1.0f, curve, attack_ms},
54 {0.0f, curve, release_ms},
55 };
56 env.gen_.configure(segs, 2, 0);
57 return env;
58 }
59
60 /// Attack-Hold-Release envelope (timed hold, no gate sustain).
61 static Envelope ahr(float attack_ms, float hold_ms,
62 float release_ms, float curve = 0.0f) {
63 Envelope env;
64 dsp::Segment segs[] = {
65 {1.0f, curve, attack_ms},
66 {1.0f, 0.0f, hold_ms},
67 {0.0f, curve, release_ms},
68 };
69 env.gen_.configure(segs, 3, -1);
70 return env;
71 }
72
73 /// Attack-Decay envelope (one-shot, no sustain or release).
74 static Envelope ad(float attack_ms, float decay_ms,
75 float curve = 0.0f) {
76 Envelope env;
77 dsp::Segment segs[] = {
78 {1.0f, curve, attack_ms},
79 {0.0f, curve, decay_ms},
80 };
81 env.gen_.configure(segs, 2, -1);
82 return env;
83 }
84
85 /// Custom multi-segment envelope.
86 static Envelope custom(const dsp::Segment* segments, uint8_t count,
87 int8_t sustain_index = -1) {
88 Envelope env;
89 env.gen_.configure(segments, count, sustain_index);
90 return env;
91 }
92
93 // ── Control ───────────────────────────────────────────────────
94
95 void set_sample_rate(uint32_t sr) { gen_.set_sample_rate(sr); }
96
97 void gate_on() { gen_.gate_on(); }
98 void gate_off() { gen_.gate_off(); }
99 void retrigger() { gen_.retrigger(); }
100
101 // ── Processing ────────────────────────────────────────────────
102
103 /// Generate envelope output (float [0.0, 1.0]).
104 void process(float* out, uint16_t frames) {
105 gen_.process(out, frames);
106 }
107
108 // ── Queries ───────────────────────────────────────────────────
109
110 float level() const { return gen_.level(); }
111 bool active() const { return gen_.active(); }
112 dsp::SegmentState state() const { return gen_.state(); }
113
114private:
116};
117
118} // namespace sbl::widgets::mod
static Envelope ad(float attack_ms, float decay_ms, float curve=0.0f)
Attack-Decay envelope (one-shot, no sustain or release).
Definition envelope.hpp:74
static Envelope ar(float attack_ms, float release_ms, float curve=0.0f)
Attack-Release envelope (sustain at peak until gate_off).
Definition envelope.hpp:49
static Envelope custom(const dsp::Segment *segments, uint8_t count, int8_t sustain_index=-1)
Custom multi-segment envelope.
Definition envelope.hpp:86
static Envelope adsr(float attack_ms, float decay_ms, float sustain, float release_ms, float curve=0.0f)
Definition envelope.hpp:35
void set_sample_rate(uint32_t sr)
Definition envelope.hpp:95
static Envelope ahr(float attack_ms, float hold_ms, float release_ms, float curve=0.0f)
Attack-Hold-Release envelope (timed hold, no gate sustain).
Definition envelope.hpp:61
dsp::SegmentState state() const
Definition envelope.hpp:112
void process(float *out, uint16_t frames)
Generate envelope output (float [0.0, 1.0]).
Definition envelope.hpp:104
Modulation source widgets.
Definition envelope.hpp:19