31template <
typename SampleType =
float>
42 std::function<SampleType(SampleType)> generator)
43 : min_(min_input), max_(max_input)
45 if (
size <= 0)
return;
47 table_.resize(
static_cast<size_t>(
size));
49 if (
size == 1 || max_ == min_) {
50 std::fill(table_.begin(), table_.end(), generator(min_));
51 inv_range_ = SampleType{0.0f};
56 std::swap(min_, max_);
58 inv_range_ =
static_cast<SampleType
>(
size - 1) / (max_ - min_);
60 for (
int i = 0;
i <
size; ++
i) {
61 SampleType x = min_ + (max_ - min_) *
static_cast<SampleType
>(
i) /
62 static_cast<SampleType
>(
size - 1);
63 table_[
static_cast<size_t>(
i)] = generator(x);
68 SampleType
process(SampleType input)
const {
69 if (table_.empty())
return input;
71 SampleType index = (std::clamp(input, min_, max_) - min_) * inv_range_;
72 int i0 =
static_cast<int>(index);
73 int i1 = std::min(i0 + 1,
static_cast<int>(table_.size()) - 1);
74 SampleType frac = index -
static_cast<SampleType
>(i0);
76 return table_[
static_cast<size_t>(i0)] * (SampleType{1.0f} - frac)
77 + table_[
static_cast<size_t>(i1)] * frac;
81 void process(SampleType* data,
int num_samples)
const {
82 for (
int i = 0;
i < num_samples; ++
i) {
89 if (table_.empty())
return SampleType{0.0f};
91 return table_[
static_cast<size_t>(std::clamp(index, 0,
92 static_cast<int>(table_.size()) - 1))];
95 int size()
const {
return static_cast<int>(table_.size()); }
96 bool empty()
const {
return table_.empty(); }
99 std::vector<SampleType> table_;
100 SampleType min_ = SampleType{0.0f};
101 SampleType max_ = SampleType{1.0f};
102 SampleType inv_range_ = SampleType{1.0f};
Pre-computed function lookup table with linear interpolation.
Definition lookup_table.hpp:32
int size() const
Definition lookup_table.hpp:95
SampleType process(SampleType input) const
Look up a value with linear interpolation.
Definition lookup_table.hpp:68
bool empty() const
Definition lookup_table.hpp:96
LookupTableT(int size, SampleType min_input, SampleType max_input, std::function< SampleType(SampleType)> generator)
Construct with a generator function.
Definition lookup_table.hpp:41
void process(SampleType *data, int num_samples) const
Process a buffer in-place.
Definition lookup_table.hpp:81
SampleType operator[](int index) const
Direct table access (no interpolation).
Definition lookup_table.hpp:88
Definition additive_bank.hpp:218
LookupTableT< double > LookupTable64
Definition lookup_table.hpp:106
LookupTableT< float > LookupTable
Definition lookup_table.hpp:105
@ i
Definition formant_filter_bank.hpp:20