Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
merge.hpp
Go to the documentation of this file.
1/**
2 * @file merge.hpp
3 * @brief Merge primitives: one parameter, several sources
4 *
5 * The merge step runs once per control tick (Stage::Merge) and is the only
6 * writer of each bridged level. These primitives decide that level when more
7 * than one source can drive a parameter. Plain summation (pitch = knob +
8 * V/Oct + MIDI) needs no type.
9 *
10 * - Priority<N>: the highest-priority active source wins.
11 * - Latest<N>: the last source to move wins.
12 * - Pickup: a takeover source must reach the held value before it captures it.
13 *
14 * All work on signal floats, hold no pointers, allocate nothing, and run in one
15 * context (the control tick).
16 *
17 * Usage:
18 * @code
19 * sbl::control::Pickup fold; // knob vs. MIDI CC
20 * // Stage::Drain, on the CC: fold.hold(cc_value);
21 * // Stage::Merge, every tick: fold_value = fold.process(knob.value());
22 * // UI slot: led = fold.caught();
23 * @endcode
24 */
25
26#ifndef SBL_CONTROL_MERGE_HPP_
27#define SBL_CONTROL_MERGE_HPP_
28
29#include <cstdint>
30
31namespace sbl::control {
32
33// Priority
34
35/**
36 * @brief The highest-priority active source wins
37 *
38 * Source 0 is the highest priority. Each source reports a value and whether
39 * it is active (a MIDI source while its gate is high; a knob always). With
40 * no source active, the last resolved value holds.
41 */
42template<uint8_t N>
43class Priority {
44 static_assert(N > 0, "Priority needs at least one source");
45
46public:
47 void set(uint8_t source, float value, bool active) {
48 if (source >= N) {
49 return;
50 }
51 values_[source] = value;
52 active_[source] = active;
53 resolve();
54 }
55
56 float value() const { return value_; }
57
58 /** Winning source index, or -1 if none is active (value() is holding). */
59 int8_t winner() const { return winner_; }
60
61private:
62 void resolve() {
63 winner_ = -1;
64 for (uint8_t i = 0; i < N; ++i) {
65 if (active_[i]) {
66 winner_ = static_cast<int8_t>(i);
67 value_ = values_[i];
68 return;
69 }
70 }
71 }
72
73 float values_[N]{};
74 bool active_[N]{};
75 float value_ = 0.0f;
76 int8_t winner_ = -1;
77};
78
79// Latest
80
81/**
82 * @brief The last source to move wins
83 *
84 * Continuous sources (a pot polled every tick) report through update(): a
85 * report counts as a write only when the source has moved more than the
86 * threshold since its own last write, so a resting knob's noise cannot take
87 * the parameter from a CC. The first report from a source sets its baseline
88 * without taking ownership. Event sources (a CC) report through write():
89 * every call is a write.
90 */
91template<uint8_t N>
92class Latest {
93 static_assert(N > 0, "Latest needs at least one source");
94
95public:
96 explicit Latest(float threshold = 0.001f) : threshold_(threshold) {}
97
98 void update(uint8_t source, float value) {
99 if (source >= N) {
100 return;
101 }
102 if (!seen_[source]) {
103 seen_[source] = true;
104 last_[source] = value;
105 return;
106 }
107 float moved = value - last_[source];
108 if (moved < 0.0f) {
109 moved = -moved;
110 }
111 if (moved > threshold_) {
112 take(source, value);
113 }
114 }
115
116 void write(uint8_t source, float value) {
117 if (source >= N) {
118 return;
119 }
120 seen_[source] = true;
121 take(source, value);
122 }
123
124 float value() const { return value_; }
125
126 /** Source that wrote last, or -1 before any write. */
127 int8_t owner() const { return owner_; }
128
129private:
130 void take(uint8_t source, float value) {
131 last_[source] = value;
132 value_ = value;
133 owner_ = static_cast<int8_t>(source);
134 }
135
136 float last_[N]{};
137 bool seen_[N]{};
138 float threshold_;
139 float value_ = 0.0f;
140 int8_t owner_ = -1;
141};
142
143// Pickup
144
145/**
146 * @brief A takeover source must reach the held value before it captures it
147 *
148 * When another source sets the parameter (hold()), the takeover source - a
149 * knob - is ignored until it crosses the held value or comes within the
150 * window of it. From then on the parameter follows the knob until the next
151 * hold(). Without a hold the knob drives the parameter directly.
152 */
153class Pickup {
154public:
155 explicit Pickup(float window = 0.01f) : window_(window) {}
156
157 /** Another source set the parameter; the takeover source must now reach it. */
158 void hold(float value) {
159 value_ = value;
160 armed_ = true;
161 side_known_ = false;
162 }
163
164 /** Feed the takeover source (every tick). Returns the parameter value. */
165 float process(float source) {
166 if (armed_) {
167 const float d = source - value_;
168 const bool above = d > 0.0f;
169 const bool within = (d <= window_) && (d >= -window_);
170 const bool crossed = side_known_ && (above != above_);
171 if (!within && !crossed) {
172 above_ = above;
173 side_known_ = true;
174 return value_;
175 }
176 armed_ = false;
177 }
178 value_ = source;
179 return value_;
180 }
181
182 /** True while the takeover source drives the parameter. */
183 bool caught() const { return !armed_; }
184
185 float value() const { return value_; }
186
187 /** Hand the parameter to the takeover source now (e.g. a panel reset). */
188 void release() { armed_ = false; }
189
190private:
191 float window_;
192 float value_ = 0.0f;
193 bool armed_ = false;
194 bool above_ = false;
195 bool side_known_ = false;
196};
197
198} // namespace sbl::control
199
200#endif // SBL_CONTROL_MERGE_HPP_
The last source to move wins.
Definition merge.hpp:92
int8_t owner() const
Definition merge.hpp:127
A takeover source must reach the held value before it captures it.
Definition merge.hpp:153
float process(float source)
Definition merge.hpp:165
bool caught() const
Definition merge.hpp:183
void hold(float value)
Definition merge.hpp:158
The highest-priority active source wins.
Definition merge.hpp:43
int8_t winner() const
Definition merge.hpp:59
The control stack: scheduler, event queue, merge, CC mapping, calibration.