Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
knob_gate.hpp
Go to the documentation of this file.
1// sbl/components/control/knob_gate.hpp — Anti-jump knob gate
2//
3// After a mode switch, a knob's position may map to a completely different
4// parameter value. KnobGate suppresses the knob's output until the user
5// physically moves it past a threshold from its entry position. This
6// prevents parameter jumps on mode transitions.
7//
8// Pair one KnobGate with each knob that has multi-mode mappings.
9//
10// Usage:
11// sbl::components::control::KnobGate gate;
12// // On mode switch:
13// gate.arm(knob.value());
14// // In your control loop:
15// gate.update(knob.value());
16// if (gate.live()) {
17// apply_parameter(knob.scaled(...));
18// }
19
20#pragma once
21
22#include <cstdint>
23
24namespace sbl {
25namespace components {
26namespace control {
27
28class KnobGate {
29public:
30 /// @param threshold Movement required to unlock (0-65535). Default ~3%.
31 explicit KnobGate(uint16_t threshold = 2000)
32 : threshold_(threshold) {}
33
34 /// Lock the gate and record current knob position.
35 void arm(uint16_t entry_value) {
36 entry_ = entry_value;
37 live_ = false;
38 }
39
40 /// Check if knob has moved enough from entry position to unlock.
41 void update(uint16_t current_value) {
42 if (live_) return;
43 int32_t d = static_cast<int32_t>(current_value) - static_cast<int32_t>(entry_);
44 if (d < 0) d = -d;
45 if (d > static_cast<int32_t>(threshold_)) {
46 live_ = true;
47 }
48 }
49
50 /// True when the gate is open (output allowed).
51 bool live() const { return live_; }
52
53 /// Force the gate open (e.g., for the initial mode that needs no gating).
54 void force_live() { live_ = true; }
55
56 /// Set threshold (0-65535).
57 void set_threshold(uint16_t t) { threshold_ = t; }
58
59private:
60 uint16_t threshold_;
61 uint16_t entry_ = 0;
62 bool live_ = true; // Start live (not armed)
63};
64
65} // namespace control
66} // namespace components
67} // namespace sbl
void arm(uint16_t entry_value)
Lock the gate and record current knob position.
Definition knob_gate.hpp:35
void update(uint16_t current_value)
Check if knob has moved enough from entry position to unlock.
Definition knob_gate.hpp:41
void force_live()
Force the gate open (e.g., for the initial mode that needs no gating).
Definition knob_gate.hpp:54
void set_threshold(uint16_t t)
Set threshold (0-65535).
Definition knob_gate.hpp:57
KnobGate(uint16_t threshold=2000)
Definition knob_gate.hpp:31
bool live() const
True when the gate is open (output allowed).
Definition knob_gate.hpp:51
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24