LMMS
Loading...
Searching...
No Matches
juce_LookupTable.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28namespace dsp
29{
30
51template <typename FloatType>
53{
54public:
63
70 LookupTable (const std::function<FloatType (size_t)>& functionToApproximate, size_t numPointsToUse);
71
82 void initialise (const std::function<FloatType (size_t)>& functionToApproximate, size_t numPointsToUse);
83
84 //==============================================================================
95 FloatType getUnchecked (FloatType index) const noexcept
96 {
97 jassert (isInitialised()); // Use the non-default constructor or call initialise() before first use
98 jassert (isPositiveAndBelow (index, FloatType (getNumPoints())));
99
100 auto i = truncatePositiveToUnsignedInt (index);
101 auto f = index - FloatType (i);
102 jassert (isPositiveAndBelow (f, FloatType (1)));
103
104 auto x0 = data.getUnchecked (static_cast<int> (i));
105 auto x1 = data.getUnchecked (static_cast<int> (i + 1));
106
107 return jmap (f, x0, x1);
108 }
109
110 //==============================================================================
123 FloatType get (FloatType index) const noexcept
124 {
125 if (index >= (FloatType) getNumPoints())
126 index = static_cast<FloatType> (getGuardIndex());
127 else if (index < 0)
128 index = {};
129
130 return getUnchecked (index);
131 }
132
133 //==============================================================================
135 FloatType operator[] (FloatType index) const noexcept { return getUnchecked (index); }
136
138 size_t getNumPoints() const noexcept { return static_cast<size_t> (data.size()) - 1; }
139
141 bool isInitialised() const noexcept { return data.size() > 1; }
142
143private:
144 //==============================================================================
146
147 void prepare() noexcept;
148 static size_t getRequiredBufferSize (size_t numPointsToUse) noexcept { return numPointsToUse + 1; }
150
152};
153
154
155//==============================================================================
173template <typename FloatType>
175{
176public:
177 //==============================================================================
186
187 //==============================================================================
198 LookupTableTransform (const std::function<FloatType (FloatType)>& functionToApproximate,
199 FloatType minInputValueToUse,
200 FloatType maxInputValueToUse,
201 size_t numPoints)
202 {
203 initialise (functionToApproximate, minInputValueToUse, maxInputValueToUse, numPoints);
204 }
205
206 //==============================================================================
217 void initialise (const std::function<FloatType (FloatType)>& functionToApproximate,
218 FloatType minInputValueToUse,
219 FloatType maxInputValueToUse,
220 size_t numPoints);
221
222 //==============================================================================
233 FloatType processSampleUnchecked (FloatType value) const noexcept
234 {
236 return lookupTable[scaler * value + offset];
237 }
238
239 //==============================================================================
253 FloatType processSample (FloatType value) const noexcept
254 {
256 jassert (isPositiveAndBelow (index, FloatType (lookupTable.getNumPoints())));
257
258 return lookupTable[index];
259 }
260
261 //==============================================================================
263 FloatType operator[] (FloatType index) const noexcept { return processSampleUnchecked (index); }
264
266 FloatType operator() (FloatType index) const noexcept { return processSample (index); }
267
268 //==============================================================================
272 void processUnchecked (const FloatType* input, FloatType* output, size_t numSamples) const noexcept
273 {
274 for (size_t i = 0; i < numSamples; ++i)
275 output[i] = processSampleUnchecked (input[i]);
276 }
277
278 //==============================================================================
282 void process (const FloatType* input, FloatType* output, size_t numSamples) const noexcept
283 {
284 for (size_t i = 0; i < numSamples; ++i)
285 output[i] = processSample (input[i]);
286 }
287
288 //==============================================================================
311 static double calculateMaxRelativeError (const std::function<FloatType (FloatType)>& functionToApproximate,
312 FloatType minInputValue,
313 FloatType maxInputValue,
314 size_t numPoints,
315 size_t numTestPoints = 0);
316private:
317 //==============================================================================
318 static double calculateRelativeDifference (double, double) noexcept;
319
320 //==============================================================================
322
324 FloatType scaler, offset;
325
327};
328
329} // namespace dsp
330} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
Definition juce_Array.h:56
Definition juce_LookupTable.h:53
FloatType get(FloatType index) const noexcept
Definition juce_LookupTable.h:123
FloatType getUnchecked(FloatType index) const noexcept
Definition juce_LookupTable.h:95
size_t getGuardIndex() const noexcept
Definition juce_LookupTable.h:149
static size_t getRequiredBufferSize(size_t numPointsToUse) noexcept
Definition juce_LookupTable.h:148
void prepare() noexcept
Definition juce_LookupTable.cpp:67
bool isInitialised() const noexcept
Definition juce_LookupTable.h:141
size_t getNumPoints() const noexcept
Definition juce_LookupTable.h:138
void initialise(const std::function< FloatType(size_t)> &functionToApproximate, size_t numPointsToUse)
Definition juce_LookupTable.cpp:46
Array< FloatType > data
Definition juce_LookupTable.h:145
LookupTable()
Definition juce_LookupTable.cpp:32
LookupTableTransform(const std::function< FloatType(FloatType)> &functionToApproximate, FloatType minInputValueToUse, FloatType maxInputValueToUse, size_t numPoints)
Definition juce_LookupTable.h:198
LookupTable< FloatType > lookupTable
Definition juce_LookupTable.h:321
FloatType processSampleUnchecked(FloatType value) const noexcept
Definition juce_LookupTable.h:233
FloatType minInputValue
Definition juce_LookupTable.h:323
FloatType scaler
Definition juce_LookupTable.h:324
void initialise(const std::function< FloatType(FloatType)> &functionToApproximate, FloatType minInputValueToUse, FloatType maxInputValueToUse, size_t numPoints)
Definition juce_LookupTable.cpp:74
void process(const FloatType *input, FloatType *output, size_t numSamples) const noexcept
Definition juce_LookupTable.h:282
void processUnchecked(const FloatType *input, FloatType *output, size_t numSamples) const noexcept
Definition juce_LookupTable.h:272
FloatType processSample(FloatType value) const noexcept
Definition juce_LookupTable.h:253
FloatType maxInputValue
Definition juce_LookupTable.h:323
FloatType offset
Definition juce_LookupTable.h:324
register unsigned i
Definition inflate.c:1575
unsigned f
Definition inflate.c:1572
static PuglViewHint int value
Definition pugl.h:1708
#define jassert(expression)
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
constexpr Type jmap(Type value0To1, Type targetRangeMin, Type targetRangeMax)
Definition juce_MathsFunctions.h:120
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Definition juce_MathsFunctions.h:262
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Definition juce_MathsFunctions.h:279
unsigned int truncatePositiveToUnsignedInt(FloatType value) noexcept
Definition juce_MathsFunctions.h:515
#define const
Definition zconf.h:137