Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
indexed_control.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include <algorithm>
9#include <cmath>
10#include <concepts>
11#include <cstddef>
12#include <span>
13
14namespace pulp::signal {
15
18template <typename SampleType = float> class MapClipT {
19 public:
20 static_assert(std::floating_point<SampleType>);
21 bool configure(SampleType input_min, SampleType input_max, SampleType output_min,
22 SampleType output_max) noexcept {
23 if (!std::isfinite(input_min) || !std::isfinite(input_max) || !std::isfinite(output_min) ||
24 !std::isfinite(output_max) || !(input_max > input_min))
25 return false;
26 input_min_ = input_min;
27 input_max_ = input_max;
28 output_min_ = output_min;
29 output_max_ = output_max;
30 return true;
31 }
32
33 SampleType process(SampleType input) const noexcept {
34 if (!std::isfinite(input))
35 return output_min_;
36 if (input <= input_min_)
37 return output_min_;
38 if (input >= input_max_)
39 return output_max_;
40 const auto span = input_max_ - input_min_;
41 // Halve only overflowing spans: scaling tiny intervals can erase them.
42 const auto unit = std::isfinite(span)
43 ? (input - input_min_) / span
44 : (input * SampleType{0.5} - input_min_ * SampleType{0.5}) /
45 (input_max_ * SampleType{0.5} - input_min_ * SampleType{0.5});
46 return std::lerp(output_min_, output_max_, unit);
47 }
48
49 private:
50 SampleType input_min_ = 0;
51 SampleType input_max_ = 1;
52 SampleType output_min_ = 0;
53 SampleType output_max_ = 1;
54};
55
58
63template <typename SampleType = float> class IndexedControlT {
64 public:
65 static_assert(std::floating_point<SampleType>);
66 void set_table(std::span<const SampleType> table) noexcept {
67 table_ = table;
68 }
69
70 SampleType process(double index) const noexcept {
71 if (table_.empty() || !std::isfinite(index))
72 return SampleType{};
73 if (index <= 0.0)
74 return table_.front();
75 const auto last = table_.size() - 1;
76 if (index >= static_cast<double>(last))
77 return table_.back();
78 const auto left = static_cast<std::size_t>(index);
79 const auto fraction = static_cast<SampleType>(index - static_cast<double>(left));
80 return std::lerp(table_[left], table_[left + 1], fraction);
81 }
82
83 private:
84 std::span<const SampleType> table_;
85};
86
89
90} // namespace pulp::signal
Read an externally indexed control waveform.
Definition indexed_control.hpp:63
void set_table(std::span< const SampleType > table) noexcept
Definition indexed_control.hpp:66
SampleType process(double index) const noexcept
Definition indexed_control.hpp:70
Map a finite control interval to another interval and clip outside it.
Definition indexed_control.hpp:18
bool configure(SampleType input_min, SampleType input_max, SampleType output_min, SampleType output_max) noexcept
Definition indexed_control.hpp:21
SampleType process(SampleType input) const noexcept
Definition indexed_control.hpp:33
Definition additive_bank.hpp:218
MapClipT< double > MapClip64
Definition indexed_control.hpp:57
@ left
Definition multi_channel_meter.hpp:66
IndexedControlT< double > IndexedControl64
Definition indexed_control.hpp:88
IndexedControlT< float > IndexedControl
Definition indexed_control.hpp:87
MapClipT< float > MapClip
Definition indexed_control.hpp:56