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
cv_input.hpp
Go to the documentation of this file.
1/**
2 * @file cv_input.hpp
3 * @brief CV Input component - smoothed ADC reading with a signal output
4 *
5 * CvInput is a source adapter: it takes raw ADC counts, applies EWMA
6 * smoothing in the raw domain, and emits normalized signal units at its
7 * output. The jack's musical role (unipolar / bipolar / V/Oct) is a
8 * structural parameter that defaults to unipolar; the conditioning
9 * constants live in CvConditioning. The mapping can be changed at runtime, for example per mode.
10 *
11 * Acquisition is owned by the component: feed values with update(), or read
12 * straight from a DMA scan buffer with update_dma() (ISR-safe).
13 *
14 * Usage:
15 * @code
16 * sbl::control::input::CvInput jack{sbl::control::input::Smoothing::Light};
17 * jack.set_electrical(sbl::hw::electrical::cv_5); // Board::init() does this for you
18 * jack.set_mapping(CvMapping::VOct);
19 * jack.update_dma(adc_buf, 4);
20 * float pitch_offset = jack.value(); // octaves under VOct
21 * @endcode
22 */
23
24#ifndef SBL_CONTROL_INPUT_CV_INPUT_HPP_
25#define SBL_CONTROL_INPUT_CV_INPUT_HPP_
26
27#include <cstdint>
30
31namespace sbl::control::input {
32
33/**
34 * @brief Smoothing presets for the raw-domain EWMA filter
35 *
36 * Values are the EWMA shift exponent (alpha = 1/2^shift).
37 */
38enum class Smoothing : uint8_t {
39 None = 0, /**< Raw passthrough (no filtering) */
40 Light = 1, /**< alpha=1/2, 95% of a step in 5 samples (pitch, fast CV) */
41 Medium = 3, /**< alpha=1/8, 95% of a step in 23 samples (pots, knobs) */
42 Heavy = 5, /**< alpha=1/32, 95% of a step in 95 samples (noisy sources) */
43};
44
45/**
46 * @brief Musical interpretation of the jack - a structural parameter
47 *
48 * Runtime-settable; remapping per mode is supported. Mapping applies at the
49 * output stage, so remaps are glitch-free with respect to filter state.
50 * Mapped state derived from the old mapping (pickup targets, stored
51 * positions) must be re-armed by the app on remap.
52 */
53enum class CvMapping : uint8_t {
54 Unipolar, /**< [0, 1] - full ADC span (default) */
55 Bipolar, /**< [-1, 1] - around the conditioned midpoint */
56 VOct, /**< pitch offset in octaves - 1 V per octave, unclamped */
57};
58
59/**
60 * @brief Conditioning constants for the analog front end
61 *
62 * Describes the analog front end between the jack and the ADC. The
63 * defaults describe a plain 16-bit unipolar input. from() derives the
64 * constants from a port's electrical description; other hardware sets
65 * its own values once at init.
66 */
68 float counts_max = 65535.0f; /**< Full-scale ADC count */
69 float midpoint = 32768.0f; /**< Count at 0 V (bipolar conditioning) */
70 bool inverted = false; /**< Conditioning op-amp inverts (+V -> lower count) */
71 float counts_per_volt = 6553.5f; /**< Counts per volt (16-bit over +/-5 V) */
72
73 /**
74 * @brief Derive the constants from a port's electrical description
75 *
76 * The description is generated by sloth into sbl::hw::electrical from
77 * the hardware manifest.
78 *
79 * @param e The port's electrical description
80 * @return The conditioning constants
81 */
82 static constexpr CvConditioning from(const sbl::hal::adc::Electrical& e) {
83 return {e.counts_max(), static_cast<float>(e.midpoint()), e.inverted, e.counts_per_volt()};
84 }
85};
86
87/** @brief CvInput construction settings */
89 Smoothing smoothing = Smoothing::None; /**< CV carries signal: smoothing is opt-in */
90};
91
92/**
93 * @brief Smoothed CV input emitting normalized signal units
94 *
95 * @context update() and update_dma() are ISR-safe. Call them from exactly
96 * one context; the getters may be called from another.
97 */
98class CvInput {
99public:
100 /**
101 * @brief Construct from settings
102 * @param config Settings; the default is no smoothing
103 */
104 explicit CvInput(const CvInputConfig& config = {}) : filter_(static_cast<uint8_t>(config.smoothing)), raw_(0) {}
105
106 /**
107 * @brief Construct with a smoothing strength
108 * @param smoothing EWMA strength
109 */
110 explicit CvInput(Smoothing smoothing) : filter_(static_cast<uint8_t>(smoothing)), raw_(0) {}
111
112 /**
113 * @brief Feed a new raw ADC sample
114 * @param raw ADC sample, 0 to 65535
115 */
116 void update(uint16_t raw) {
117 raw_ = raw;
118 filter_.update(raw);
119 }
120
121 /**
122 * @brief Read one channel from a DMA scan buffer and update
123 *
124 * The volatile cast forces a real memory read: DMA updates the buffer
125 * asynchronously.
126 *
127 * @param buffer DMA scan buffer
128 * @param channel_index Index of this input's channel in the buffer
129 * @context ISR-safe, non-blocking.
130 */
131 void update_dma(const uint16_t* buffer, uint8_t channel_index) {
132 update(static_cast<const volatile uint16_t*>(buffer)[channel_index]);
133 }
134
135 /** @brief Last raw (unfiltered) ADC count - for calibration routines */
136 uint16_t raw() const { return raw_; }
137
138 /** @brief Smoothed output in signal units, per the configured mapping */
139 float value() const { return map(filter_.value()); }
140
141 /** @brief Set the jack's musical role (structural; runtime-remappable) */
142 void set_mapping(CvMapping mapping) { mapping_ = mapping; }
143
144 CvMapping mapping() const { return mapping_; }
145
146 /** @brief Set the analog front-end description (once at init, typically) */
147 void set_conditioning(const CvConditioning& cond) { cond_ = cond; }
148
149 /** Condition from a port's electrical description (the board role's init hook). */
151
152 const CvConditioning& conditioning() const { return cond_; }
153
154 /** @brief Change smoothing level without resetting filter state */
155 void set_smoothing(Smoothing smoothing) { filter_.set_shift(static_cast<uint8_t>(smoothing)); }
156
157 /** @brief Reset filter state */
158 void reset() {
159 filter_.reset();
160 raw_ = 0;
161 }
162
163private:
164 float map(uint16_t counts) const {
165 float c = static_cast<float>(counts);
166 switch (mapping_) {
167 case CvMapping::Unipolar: {
168 float t = c / cond_.counts_max;
169 if (cond_.inverted) {
170 t = 1.0f - t;
171 }
172 return (t < 0.0f) ? 0.0f : (t > 1.0f) ? 1.0f : t;
173 }
174 case CvMapping::Bipolar: {
175 float b = (c - cond_.midpoint) / cond_.midpoint;
176 if (cond_.inverted) {
177 b = -b;
178 }
179 return (b < -1.0f) ? -1.0f : (b > 1.0f) ? 1.0f : b;
180 }
181 case CvMapping::VOct: {
182 // Offset semantics: unclamped - the receiving port clamps.
183 float volts = (c - cond_.midpoint) / cond_.counts_per_volt;
184 if (cond_.inverted) {
185 volts = -volts;
186 }
187 return volts; // octaves: 1 V per octave
188 }
189 }
190 return 0.0f;
191 }
192
193 Ewma filter_;
194 uint16_t raw_;
196 CvConditioning cond_{};
197};
198
199} // namespace sbl::control::input
200
201#endif // SBL_CONTROL_INPUT_CV_INPUT_HPP_
Smoothed CV input emitting normalized signal units.
Definition cv_input.hpp:98
float value() const
Smoothed output in signal units, per the configured mapping.
Definition cv_input.hpp:139
void set_smoothing(Smoothing smoothing)
Change smoothing level without resetting filter state.
Definition cv_input.hpp:155
void reset()
Reset filter state.
Definition cv_input.hpp:158
CvInput(Smoothing smoothing)
Construct with a smoothing strength.
Definition cv_input.hpp:110
void update_dma(const uint16_t *buffer, uint8_t channel_index)
Read one channel from a DMA scan buffer and update.
Definition cv_input.hpp:131
void update(uint16_t raw)
Feed a new raw ADC sample.
Definition cv_input.hpp:116
CvInput(const CvInputConfig &config={})
Construct from settings.
Definition cv_input.hpp:104
void set_electrical(const sbl::hal::adc::Electrical &e)
Definition cv_input.hpp:150
void set_mapping(CvMapping mapping)
Set the jack's musical role (structural; runtime-remappable)
Definition cv_input.hpp:142
void set_conditioning(const CvConditioning &cond)
Set the analog front-end description (once at init, typically)
Definition cv_input.hpp:147
uint16_t raw() const
Last raw (unfiltered) ADC count - for calibration routines.
Definition cv_input.hpp:136
uint16_t update(uint16_t raw)
Feed a new sample and return the smoothed value.
Definition ewma.hpp:56
void set_shift(uint8_t shift)
Change smoothing exponent without resetting filter state.
Definition ewma.hpp:76
void reset()
Reset filter state (next update will re-seed)
Definition ewma.hpp:79
uint16_t value() const
Return current smoothed value without updating.
Definition ewma.hpp:73
Electrical description of an analog port - the jack-to-converter circuit.
Exponentially Weighted Moving Average filter.
Inputs: debounce, decoding, smoothing and scaling of raw samples.
Definition button.hpp:29
CvMapping
Musical interpretation of the jack - a structural parameter.
Definition cv_input.hpp:53
Smoothing
Smoothing presets for the raw-domain EWMA filter.
Definition cv_input.hpp:38
Conditioning constants for the analog front end.
Definition cv_input.hpp:67
static constexpr CvConditioning from(const sbl::hal::adc::Electrical &e)
Derive the constants from a port's electrical description.
Definition cv_input.hpp:82
CvInput construction settings.
Definition cv_input.hpp:88
constexpr uint16_t midpoint() const