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_echo.hpp
Go to the documentation of this file.
1/**
2 * @file param_echo.hpp
3 * @brief Send every mapped parameter's value back out as its Control Change
4 *
5 * For a controller that takes its values back: after a reset, or at
6 * startup, each row of a CcMapper's table is sent as a Control Change with
7 * the target's value, so the controller's encoders show where the
8 * parameters are. A 14-bit row sends its MSB only.
9 *
10 * request() may come from the control tick; poll() runs in the main loop
11 * and sends as many rows as the sender takes, resuming on the next call. A
12 * request during a walk starts it again from the first row.
13 *
14 * @code
15 * sbl::control::ParamEcho<Param> echo{mapper};
16 * if (seeded) echo.request(); // the tick
17 *
18 * echo.poll(CHANNEL, // the main loop
19 * [](Param p) { return params.value(p); },
20 * [](const sbl::midi::MidiEvent& e) { return sbl::usb::MidiPort::send(e); });
21 * @endcode
22 */
23
24#ifndef SBL_CONTROL_PARAM_ECHO_HPP_
25#define SBL_CONTROL_PARAM_ECHO_HPP_
26
27#include <atomic>
28#include <cstddef>
29#include <cstdint>
30
32#include <sbl/math/clamp.hpp>
34
35namespace sbl::control {
36
37template<typename Target>
38class ParamEcho {
39public:
40 explicit ParamEcho(const CcMapper<Target>& mapper) : begin_(mapper.begin()), count_(mapper.size()), next_(count_) {}
41
42 /** Ask for a walk of the table. From any one context other than poll()'s. */
43 void request() { requested_.store(true, std::memory_order_relaxed); }
44
45 /** Whether a walk is asked for or under way. */
46 bool pending() const { return requested_.load(std::memory_order_relaxed) || next_ < count_; }
47
48 /**
49 * Send rows until the walk ends or `send` refuses one.
50 * @param channel the MIDI channel to send on, 0 to 15
51 * @param value_of `float(Target)`: the target's value in [0, 1]
52 * @param send `bool(const sbl::midi::MidiEvent&)`: false when the port is full
53 * @return the rows sent by this call
54 */
55 template<typename ValueOf, typename Send>
56 size_t poll(uint8_t channel, ValueOf&& value_of, Send&& send) {
57 // A load and a store, not an exchange: a request between them is one this walk already covers.
58 if (requested_.load(std::memory_order_relaxed)) {
59 requested_.store(false, std::memory_order_relaxed);
60 next_ = 0;
61 }
62 size_t sent = 0;
63 while (next_ < count_) {
64 const CcEntry<Target>& row = begin_[next_];
65 const float value = sbl::math::clamp01(static_cast<float>(value_of(row.target)));
66 if (!send(sbl::midi::MidiEvent::control_change(channel, row.cc, sbl::midi::to_7bit(value)))) {
67 break;
68 }
69 ++next_;
70 ++sent;
71 }
72 return sent;
73 }
74
75private:
76 const CcEntry<Target>* begin_;
77 size_t count_;
78 size_t next_; // poll()'s context only: the next row to send, count_ when none
79 std::atomic<bool> requested_{false};
80};
81
82} // namespace sbl::control
83
84#endif // SBL_CONTROL_PARAM_ECHO_HPP_
Declarative MIDI CC routing.
Clamping.
Routes MIDI CCs to app targets through a constexpr table.
Definition cc_mapper.hpp:85
size_t size() const
Rows in the map.
const CcEntry< Target > * begin() const
The first row, for a walk over them (an echo of the targets' values, a listing)
size_t poll(uint8_t channel, ValueOf &&value_of, Send &&send)
The control stack: scheduler, event queue, merge, CC mapping, calibration.
constexpr T clamp01(T x)
x held to [0, 1]; a NaN goes to 0.
Definition clamp.hpp:35
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 constexpr MidiEvent control_change(uint8_t channel, uint8_t number, uint8_t value)
A Control Change event: channel 0-15, controller number and value 0-127.
Definition types.hpp:68