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
selector.hpp
Go to the documentation of this file.
1/**
2 * @file selector.hpp
3 * @brief A continuous control read as one of N positions
4 *
5 * For a knob, a fader or a CC standing in for a switch. select() is the
6 * stateless form: N equal shares of the control's travel. Selector adds
7 * hysteresis, so a control resting on a boundary does not chatter between
8 * two positions.
9 *
10 * @code
11 * const uint8_t ratio = sbl::control::select(loop_ratio, 3); // 0, 1 or 2
12 *
13 * sbl::control::Selector<3> selector;
14 * const uint8_t position = selector.update(knob.smoothed());
15 * @endcode
16 */
17
18#ifndef SBL_CONTROL_SELECTOR_HPP_
19#define SBL_CONTROL_SELECTOR_HPP_
20
21#include <cstdint>
22
23namespace sbl::control {
24
25/**
26 * The position, 0 to `positions` - 1, of a control in [0, 1], by equal shares
27 * of its travel. Values outside [0, 1] and NaN land on an end position.
28 */
29inline constexpr uint8_t select(float control, uint8_t positions) {
30 if (positions == 0 || !(control > 0.0f)) {
31 return 0;
32 }
33 const float scaled = control * static_cast<float>(positions);
34 return scaled >= static_cast<float>(positions) ? static_cast<uint8_t>(positions - 1) : static_cast<uint8_t>(scaled);
35}
36
37/**
38 * select() with hysteresis: the position changes only once the control is
39 * past a boundary by `margin` of one position's share of the travel.
40 */
41template<uint8_t Positions>
42class Selector {
43 static_assert(Positions > 0, "Selector needs at least one position");
44
45public:
46 /** @param margin the hysteresis, as a share of one position's width, in [0, 0.5) */
47 explicit constexpr Selector(float margin = 0.1f) : margin_(margin) {}
48
49 /** The position for the control's value now. The first call takes select()'s answer. */
50 uint8_t update(float control) {
51 const uint8_t plain = select(control, Positions);
52 if (!primed_) {
53 primed_ = true;
54 position_ = plain;
55 return position_;
56 }
57 if (plain == position_) {
58 return position_;
59 }
60 const float width = 1.0f / static_cast<float>(Positions);
61 const float low = static_cast<float>(position_) * width - margin_ * width;
62 const float high = static_cast<float>(position_ + 1) * width + margin_ * width;
63 if (control < low || control >= high) {
64 position_ = plain;
65 }
66 return position_;
67 }
68
69 uint8_t position() const { return position_; }
70
71 /** Forget the position; the next update() takes select()'s answer. */
72 void reset() { primed_ = false; }
73
74private:
75 float margin_;
76 uint8_t position_ = 0;
77 bool primed_ = false;
78};
79
80} // namespace sbl::control
81
82#endif // SBL_CONTROL_SELECTOR_HPP_
uint8_t update(float control)
Definition selector.hpp:50
constexpr Selector(float margin=0.1f)
Definition selector.hpp:47
The control stack: scheduler, event queue, merge, CC mapping, calibration.
constexpr uint8_t select(float control, uint8_t positions)
Definition selector.hpp:29