Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
input.hpp
Go to the documentation of this file.
1/**
2 * @file input.hpp
3 * @brief CV Input component — smoothed ADC reading with range scaling
4 * @ingroup components
5 *
6 * CvInput is a pure signal-processing component (I/O Stack). It takes raw
7 * ADC values, applies EWMA smoothing, and provides scaled output. It does
8 * NOT call the ADC driver — the application feeds it values, making it
9 * agnostic to the acquisition strategy (polling, DMA, etc.).
10 *
11 * Usage:
12 * sbl::components::cv::CvInput knob;
13 * knob.update(raw_adc_value);
14 * uint16_t smooth = knob.value(); // 0–65535
15 * int32_t param = knob.scaled(50, 500); // map to parameter range
16 */
17
18#ifndef SBL_COMPONENTS_CV_INPUT_HPP_
19#define SBL_COMPONENTS_CV_INPUT_HPP_
20
21#include <cstdint>
23#include <sbl/dsp/warp.hpp>
24
25namespace sbl {
26namespace components {
27namespace cv {
28
29/**
30 * @brief Smoothing presets for CvInput EWMA filter
31 *
32 * Values are the EWMA shift exponent (alpha = 1/2^shift).
33 * Higher values = more smoothing, slower response.
34 */
35enum class Smoothing : uint8_t {
36 None = 0, ///< Raw passthrough (no filtering)
37 Light = 1, ///< alpha=1/2, settles in ~4 samples (pitch, fast CV)
38 Medium = 3, ///< alpha=1/8, settles in ~24 samples (pots, knobs)
39 Heavy = 5, ///< alpha=1/32, settles in ~96 samples (noisy sources)
40};
41
45
46/**
47 * @brief Smoothed CV input with EWMA filtering and range scaling
48 *
49 * @note All public methods are ISR-safe — pure state machine with no hardware I/O.
50 */
51class CvInput {
52public:
53 explicit CvInput(const CvInputConfig& config = {})
54 : filter_(static_cast<uint8_t>(config.smoothing)), raw_(0) {}
55
56 explicit CvInput(Smoothing smoothing)
57 : filter_(static_cast<uint8_t>(smoothing)), raw_(0) {}
58
59 /** @brief Feed a new raw ADC sample */
60 void update(uint16_t raw) {
61 raw_ = raw;
62 filter_.update(raw);
63 }
64
65 /** @brief Last raw (unfiltered) value */
66 uint16_t raw() const { return raw_; }
67
68 /** @brief Current smoothed value (0–65535) */
69 uint16_t value() const { return filter_.value(); }
70
71 /**
72 * @brief Map smoothed value to an output range (linear)
73 *
74 * Linear interpolation from [0, 65535] → [min, max].
75 * Uses int64_t intermediate to avoid overflow.
76 * For non-linear mapping (frequency, filter cutoff), see scaled_curved().
77 *
78 * @param min Output minimum (inclusive)
79 * @param max Output maximum (inclusive)
80 * @return Scaled value in [min, max]
81 */
82 int32_t scaled(int32_t min, int32_t max) const {
83 int64_t v = filter_.value();
84 return static_cast<int32_t>(min + (v * (max - min)) / 65535);
85 }
86
87 /**
88 * @brief Map smoothed value with curve shaping
89 *
90 * Applies ExpCurveWarp::warp() before scaling, useful for knob-friendly
91 * non-linear mapping (frequency, filter cutoff, gain).
92 *
93 * @param min Output minimum (inclusive)
94 * @param max Output maximum (inclusive)
95 * @param curve Shape preset or arbitrary value (see sbl::dsp::curve)
96 * @return Scaled value in [min, max]
97 */
98 int32_t scaled_curved(int32_t min, int32_t max,
99 uint16_t curve = dsp::curve::Log) const {
100 uint16_t warped = dsp::ExpCurveWarp::warp(filter_.value(), curve);
101 int64_t v = warped;
102 return static_cast<int32_t>(min + (v * (max - min)) / 65535);
103 }
104
105 /** @brief Reset filter state */
106 void reset() {
107 filter_.reset();
108 raw_ = 0;
109 }
110
111private:
113 uint16_t raw_;
114};
115
116} // namespace cv
117} // namespace components
118} // namespace sbl
119
120#endif // SBL_COMPONENTS_CV_INPUT_HPP_
Smoothed CV input with EWMA filtering and range scaling.
Definition input.hpp:51
uint16_t value() const
Current smoothed value (0–65535)
Definition input.hpp:69
int32_t scaled(int32_t min, int32_t max) const
Map smoothed value to an output range (linear)
Definition input.hpp:82
CvInput(Smoothing smoothing)
Definition input.hpp:56
uint16_t raw() const
Last raw (unfiltered) value.
Definition input.hpp:66
CvInput(const CvInputConfig &config={})
Definition input.hpp:53
void update(uint16_t raw)
Feed a new raw ADC sample.
Definition input.hpp:60
int32_t scaled_curved(int32_t min, int32_t max, uint16_t curve=dsp::curve::Log) const
Map smoothed value with curve shaping.
Definition input.hpp:98
void reset()
Reset filter state.
Definition input.hpp:106
uint16_t update(uint16_t raw)
Feed a new sample and return the smoothed value.
Definition ewma.hpp:46
uint16_t value() const
Return current smoothed value without updating.
Definition ewma.hpp:63
void reset()
Reset filter state (next update will re-seed)
Definition ewma.hpp:68
Exponentially Weighted Moving Average filter.
Smoothing
Smoothing presets for CvInput EWMA filter.
Definition input.hpp:35
@ None
Raw passthrough (no filtering)
@ Medium
alpha=1/8, settles in ~24 samples (pots, knobs)
@ Light
alpha=1/2, settles in ~4 samples (pitch, fast CV)
@ Heavy
alpha=1/32, settles in ~96 samples (noisy sources)
static constexpr uint16_t Log
Definition warp.hpp:38
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24
static uint16_t warp(uint16_t phase, uint16_t curve)
Definition warp.hpp:90