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
param_bank.hpp
Go to the documentation of this file.
1/**
2 * @file param_bank.hpp
3 * @brief An app's parameters: one merge each, their defaults, and a reset
4 *
5 * The merge layer of the control tick for a parameter list
6 * declared in the app's params manifest. Each parameter has one merge, by
7 * default Latest: the last source to move it holds it. Polled sources, a
8 * knob read every tick, report through poll(); event sources, a CC, through
9 * write().
10 *
11 * Seeding. Until its first update, and again after reset(), a bank is due a
12 * seed: begin_update() writes every parameter's default, and the polls of
13 * that update take their parameters outright, so a parameter with a knob
14 * starts on its knob and the rest on their defaults. reset() is what MIDI's
15 * Reset All Controllers asks for.
16 *
17 * @context The control tick only. Stage order: events write in Drain; the
18 * polls and begin_update() run in Update; the app reads value() in Merge.
19 *
20 * @code
21 * using Param = sbl::app::Param;
22 * sbl::control::ParamBank<Param, sbl::app::PARAM_COUNT, 2> params;
23 * enum : uint8_t { SRC_KNOB, SRC_CC };
24 *
25 * params.write(SRC_CC, Param::Mass, 0.25f); // Drain: a CC
26 *
27 * const bool seeded = params.begin_update(sbl::app::PARAM_DEFAULTS, SRC_CC); // Update
28 * sbl::app::for_each_control(board, [](Param p, const auto& control) {
29 * params.poll(SRC_KNOB, p, control.smoothed());
30 * });
31 *
32 * controls.mass = params.value(Param::Mass); // Merge
33 * @endcode
34 */
35
36#ifndef SBL_CONTROL_PARAM_BANK_HPP_
37#define SBL_CONTROL_PARAM_BANK_HPP_
38
39#include <cstdint>
40
41#include <sbl/control/merge.hpp>
42
43namespace sbl::control {
44
45/**
46 * @tparam Param the app's parameter enum, its values 0 to Count - 1
47 * @tparam Count the number of parameters
48 * @tparam Sources the number of sources that can hold a parameter
49 * @tparam Merge the merge each parameter gets: write(source, value), update(source, value) and value()
50 */
51template<typename Param, uint8_t Count, uint8_t Sources, typename Merge = Latest<Sources>>
52class ParamBank {
53 static_assert(Count > 0, "ParamBank needs at least one parameter");
54
55public:
56 static constexpr uint8_t COUNT = Count;
57
58 /**
59 * Start an update. If a seed is due, write every parameter's default as
60 * `source` and let this update's polls take their parameters. Returns
61 * whether this update seeds.
62 */
63 bool begin_update(const float (&defaults)[Count], uint8_t source) {
64 seeding_ = seed_due_;
65 if (seed_due_) {
66 for (uint8_t i = 0; i < Count; ++i) {
67 merges_[i].write(source, defaults[i]);
68 }
69 seed_due_ = false;
70 }
71 return seeding_;
72 }
73
74 /** A polled source's reading: it takes the parameter once it has moved, or at once in a seeding update. */
75 void poll(uint8_t source, Param param, float value) {
76 Merge& merge = merges_[index(param)];
77 if (seeding_) {
78 merge.write(source, value);
79 } else {
80 merge.update(source, value);
81 }
82 }
83
84 /** An event source's value: it takes the parameter. */
85 void write(uint8_t source, Param param, float value) { merges_[index(param)].write(source, value); }
86
87 /** The parameter's merged value. */
88 float value(Param param) const { return merges_[index(param)].value(); }
89
90 /** Make a seed due: the next begin_update() puts every parameter back as at startup. */
91 void reset() { seed_due_ = true; }
92
93 bool seed_due() const { return seed_due_; }
94
95private:
96 static constexpr uint8_t index(Param param) {
97 const auto i = static_cast<uint8_t>(param);
98 return i < Count ? i : uint8_t{0};
99 }
100
101 Merge merges_[Count];
102 bool seed_due_ = true;
103 bool seeding_ = false;
104};
105
106} // namespace sbl::control
107
108#endif // SBL_CONTROL_PARAM_BANK_HPP_
bool begin_update(const float(&defaults)[Count], uint8_t source)
void write(uint8_t source, Param param, float value)
void poll(uint8_t source, Param param, float value)
float value(Param param) const
Merge primitives: one parameter, several sources.
The control stack: scheduler, event queue, merge, CC mapping, calibration.