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
cc_mapper.hpp
Go to the documentation of this file.
1/**
2 * @file cc_mapper.hpp
3 * @brief Declarative MIDI CC routing
4 */
5
6#ifndef SBL_CONTROL_CC_MAPPER_HPP_
7#define SBL_CONTROL_CC_MAPPER_HPP_
8
9#include <cstddef>
10#include <cstdint>
11
12#include <sbl/control/cam.hpp>
14
15namespace sbl::control {
16
17/** A row's curve: a Cam, or linear when it has no table. */
18using CcCurve = Cam;
19
20/** The store for a map with no 14-bit rows: nothing reads it. */
21struct NoCcStore {
22 /** @brief Always 0 */
23 static float cc(uint8_t) { return 0.0f; }
24};
25
26/** A curve with no table: the row's output is its input. */
27inline constexpr CcCurve CC_LINEAR{};
28
29/** Row flags. */
30enum class CcFlags : uint8_t {
31 None = 0, /**< 7-bit: `cc` alone */
32 Paired14 = 1 << 0, /**< 14-bit: `cc` is the MSB (0-31), cc + 32 the LSB */
33};
34
35/** Row channel that matches every channel. */
36inline constexpr uint8_t ANY_CHANNEL = 0xFF;
37
38/**
39 * @brief One row of a CC map
40 * @tparam Target App-defined target type
41 */
42template<typename Target>
43struct CcEntry {
44 uint8_t channel; /**< 0-15, or ANY_CHANNEL */
45 uint8_t cc; /**< controller number (the MSB for Paired14) */
46 Target target; /**< passed to the sink */
47 CcCurve curve; /**< maps the value in [0, 1] to the sink's value */
48 CcFlags flags; /**< CcFlags::Paired14 for a 14-bit pair */
49};
50
51/**
52 * @brief Routes MIDI CCs to app targets through a constexpr table
53 *
54 * A constexpr table routes MIDI CCs: each row routes one
55 * controller on one channel to an app-defined target through a curve. Curves
56 * are LUTE cam tables (the same vocabulary as knob macros) or linear.
57 *
58 * Dispatch is event-driven, inside the control tick's drain loop, after the
59 * app's CC store has taken the event - store before mapper. The store stays
60 * the only CC state: a Paired14 row reads the other half of its pair from it.
61 * Values are absolute; every matching row fires, so a CC with two rows fans
62 * out to two targets.
63 *
64 * The sink is the app's write into the merge layer, e.g.
65 * pickups[param].hold(value). The mapper holds a pointer to the table, so
66 * the table must outlive the mapper.
67 *
68 * Usage:
69 * @code
70 * enum class Param : uint8_t { Pitch, Timbre };
71 * constexpr sbl::control::CcEntry<Param> CC_MAP[] = {
72 * { 0, 86, Param::Pitch, sbl::control::CC_LINEAR, sbl::control::CcFlags::None },
73 * { 0, 87, Param::Timbre, {cam::timbre_256, 256}, sbl::control::CcFlags::None },
74 * };
75 * sbl::control::CcMapper<Param> mapper{CC_MAP, write_param, nullptr};
76 *
77 * // drain loop, per ControlChange event:
78 * converter.cc(e.cc_number(), e.cc_value());
79 * mapper.on_cc(e.channel, e.cc_number(), e.cc_value(), converter);
80 * @endcode
81 *
82 * @tparam Target App-defined target type, usually a parameter enum
83 */
84template<typename Target>
85class CcMapper {
86public:
87 /** @brief The app's write into the merge layer: the context pointer, the row's target and the curve output */
88 using Sink = void (*)(void* context, Target target, float value);
89
90 /**
91 * @brief Construct over a table
92 * @param table The rows; the table must outlive the mapper
93 * @param sink Called once per matching row
94 * @param context Passed to sink unchanged
95 */
96 template<size_t N>
97 CcMapper(const CcEntry<Target> (&table)[N], Sink sink, void* context)
98 : table_(table), count_(N), sink_(sink), context_(context) {}
99
100 /**
101 * @brief Route one ControlChange event
102 *
103 * Call after `store` has taken the event.
104 *
105 * @tparam Store Provides `float cc(uint8_t number) const` in [0, 1]
106 * @param channel MIDI channel, 0 to 15
107 * @param cc Controller number, 0 to 127
108 * @param value Controller value, 0 to 127
109 * @param store The app's CC store; supplies the other half of a 14-bit pair
110 * @return Rows dispatched
111 */
112 template<typename Store>
113 uint8_t on_cc(uint8_t channel, uint8_t cc, uint8_t value, const Store& store) const {
114 uint8_t hits = 0;
115 for (size_t i = 0; i < count_; ++i) {
116 const CcEntry<Target>& e = table_[i];
117 if (e.channel != ANY_CHANNEL && e.channel != channel) {
118 continue;
119 }
120
121 float t;
122 if (e.flags == CcFlags::Paired14) {
123 const uint8_t lsb_cc = static_cast<uint8_t>(e.cc + 32);
124 if (cc != e.cc && cc != lsb_cc) {
125 continue;
126 }
127 const uint8_t msb = (cc == e.cc) ? value : sbl::midi::to_7bit(store.cc(e.cc));
128 const uint8_t lsb = (cc == lsb_cc) ? value : sbl::midi::to_7bit(store.cc(lsb_cc));
129 t = static_cast<float>((static_cast<uint16_t>(msb) << 7) | lsb) * (1.0f / 16383.0f);
130 } else {
131 if (cc != e.cc) {
132 continue;
133 }
134 t = static_cast<float>(value) * (1.0f / 127.0f);
135 }
136
137 const float v = e.curve ? cam_evaluate(e.curve, t) : t;
138 sink_(context_, e.target, v);
139 ++hits;
140 }
141 return hits;
142 }
143
144 /**
145 * @brief Route one ControlChange event through a map with no 14-bit rows
146 * @param channel MIDI channel, 0 to 15
147 * @param cc Controller number, 0 to 127
148 * @param value Controller value, 0 to 127
149 * @return Rows dispatched
150 */
151 uint8_t on_cc(uint8_t channel, uint8_t cc, uint8_t value) const { return on_cc(channel, cc, value, NoCcStore{}); }
152
153 /** @brief Rows in the map */
154 size_t size() const { return count_; }
155
156 /** @brief The first row, for a walk over them (an echo of the targets' values, a listing) */
157 const CcEntry<Target>* begin() const { return table_; }
158 /** @brief One past the last row */
159 const CcEntry<Target>* end() const { return table_ + count_; }
160
161private:
162 const CcEntry<Target>* table_;
163 size_t count_;
164 Sink sink_;
165 void* context_;
166};
167
168} // namespace sbl::control
169
170#endif // SBL_CONTROL_CC_MAPPER_HPP_
Cam profile evaluation.
Routes MIDI CCs to app targets through a constexpr table.
Definition cc_mapper.hpp:85
void(*)(void *context, Target target, float value) Sink
The app's write into the merge layer: the context pointer, the row's target and the curve output.
Definition cc_mapper.hpp:88
size_t size() const
Rows in the map.
CcMapper(const CcEntry< Target >(&table)[N], Sink sink, void *context)
Construct over a table.
Definition cc_mapper.hpp:97
uint8_t on_cc(uint8_t channel, uint8_t cc, uint8_t value) const
Route one ControlChange event through a map with no 14-bit rows.
const CcEntry< Target > * begin() const
The first row, for a walk over them (an echo of the targets' values, a listing)
const CcEntry< Target > * end() const
One past the last row.
uint8_t on_cc(uint8_t channel, uint8_t cc, uint8_t value, const Store &store) const
Route one ControlChange event.
The control stack: scheduler, event queue, merge, CC mapping, calibration.
constexpr CcCurve CC_LINEAR
Definition cc_mapper.hpp:27
float cam_evaluate(const float *table, uint16_t size, float input)
Definition cam.hpp:65
constexpr uint8_t ANY_CHANNEL
Definition cc_mapper.hpp:36
constexpr uint8_t to_7bit(float value)
Definition types.hpp:56
MIDI message types.
One row of a CC map.
Definition cc_mapper.hpp:43
static float cc(uint8_t)
Always 0.
Definition cc_mapper.hpp:23