JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
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::dsp
27{
28
49template <typename FloatType>
51{
52public:
61
68 LookupTable (const std::function<FloatType (size_t)>& functionToApproximate, size_t numPointsToUse);
69
80 void initialise (const std::function<FloatType (size_t)>& functionToApproximate, size_t numPointsToUse);
81
82 //==============================================================================
93 FloatType getUnchecked (FloatType index) const noexcept
94 {
95 jassert (isInitialised()); // Use the non-default constructor or call initialise() before first use
96 jassert (isPositiveAndBelow (index, FloatType (getNumPoints())));
97
98 auto i = truncatePositiveToUnsignedInt (index);
99 auto f = index - FloatType (i);
100 jassert (isPositiveAndBelow (f, FloatType (1)));
101
102 auto x0 = data.getUnchecked (static_cast<int> (i));
103 auto x1 = data.getUnchecked (static_cast<int> (i + 1));
104
105 return jmap (f, x0, x1);
106 }
107
108 //==============================================================================
121 FloatType get (FloatType index) const noexcept
122 {
123 if (index >= (FloatType) getNumPoints())
124 index = static_cast<FloatType> (getGuardIndex());
125 else if (index < 0)
126 index = {};
127
128 return getUnchecked (index);
129 }
130
131 //==============================================================================
133 FloatType operator[] (FloatType index) const noexcept { return getUnchecked (index); }
134
136 size_t getNumPoints() const noexcept { return static_cast<size_t> (data.size()) - 1; }
137
139 bool isInitialised() const noexcept { return data.size() > 1; }
140
141private:
142 //==============================================================================
143 Array<FloatType> data;
144
145 void prepare() noexcept;
146 static size_t getRequiredBufferSize (size_t numPointsToUse) noexcept { return numPointsToUse + 1; }
147 size_t getGuardIndex() const noexcept { return getRequiredBufferSize (getNumPoints()) - 1; }
148
150};
151
152
153//==============================================================================
171template <typename FloatType>
173{
174public:
175 //==============================================================================
184
185 //==============================================================================
203
204 //==============================================================================
215 void initialise (const std::function<FloatType (FloatType)>& functionToApproximate,
216 FloatType minInputValueToUse,
217 FloatType maxInputValueToUse,
218 size_t numPoints);
219
220 //==============================================================================
231 FloatType processSampleUnchecked (FloatType value) const noexcept
232 {
233 jassert (value >= minInputValue && value <= maxInputValue);
234 return lookupTable[scaler * value + offset];
235 }
236
237 //==============================================================================
251 FloatType processSample (FloatType value) const noexcept
252 {
253 auto index = scaler * jlimit (minInputValue, maxInputValue, value) + offset;
254 jassert (isPositiveAndBelow (index, FloatType (lookupTable.getNumPoints())));
255
256 return lookupTable[index];
257 }
258
259 //==============================================================================
261 FloatType operator[] (FloatType index) const noexcept { return processSampleUnchecked (index); }
262
264 FloatType operator() (FloatType index) const noexcept { return processSample (index); }
265
266 //==============================================================================
270 void processUnchecked (const FloatType* input, FloatType* output, size_t numSamples) const noexcept
271 {
272 for (size_t i = 0; i < numSamples; ++i)
273 output[i] = processSampleUnchecked (input[i]);
274 }
275
276 //==============================================================================
280 void process (const FloatType* input, FloatType* output, size_t numSamples) const noexcept
281 {
282 for (size_t i = 0; i < numSamples; ++i)
283 output[i] = processSample (input[i]);
284 }
285
286 //==============================================================================
309 static double calculateMaxRelativeError (const std::function<FloatType (FloatType)>& functionToApproximate,
310 FloatType minInputValue,
311 FloatType maxInputValue,
312 size_t numPoints,
313 size_t numTestPoints = 0);
314private:
315 //==============================================================================
316 static double calculateRelativeDifference (double, double) noexcept;
317
318 //==============================================================================
319 LookupTable<FloatType> lookupTable;
320
321 FloatType minInputValue, maxInputValue;
322 FloatType scaler, offset;
323
325};
326
327} // namespace juce::dsp
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
ElementType getUnchecked(int index) const
Returns one of the elements in the array, without checking the index passed in.
Definition juce_Array.h:252
int size() const noexcept
Returns the current number of elements in the array.
Definition juce_Array.h:215
Class for approximating expensive arithmetic operations.
LookupTableTransform(const std::function< FloatType(FloatType)> &functionToApproximate, FloatType minInputValueToUse, FloatType maxInputValueToUse, size_t numPoints)
Creates and initialises a LookupTableTransform object.
FloatType operator[](FloatType index) const noexcept
FloatType processSampleUnchecked(FloatType value) const noexcept
Calculates the approximated value for the given input value without range checking.
void initialise(const std::function< FloatType(FloatType)> &functionToApproximate, FloatType minInputValueToUse, FloatType maxInputValueToUse, size_t numPoints)
Initialises or changes the parameters of a LookupTableTransform object.
void process(const FloatType *input, FloatType *output, size_t numSamples) const noexcept
Processes an array of input values with range checking.
void processUnchecked(const FloatType *input, FloatType *output, size_t numSamples) const noexcept
Processes an array of input values without range checking.
FloatType processSample(FloatType value) const noexcept
Calculates the approximated value for the given input value with range checking.
FloatType operator()(FloatType index) const noexcept
LookupTableTransform()=default
Creates an uninitialised LookupTableTransform object.
static double calculateMaxRelativeError(const std::function< FloatType(FloatType)> &functionToApproximate, FloatType minInputValue, FloatType maxInputValue, size_t numPoints, size_t numTestPoints=0)
Calculates the maximum relative error of the approximation for the specified parameter set.
Class for efficiently approximating expensive arithmetic operations.
FloatType get(FloatType index) const noexcept
Calculates the approximated value for the given index with range checking.
FloatType operator[](FloatType index) const noexcept
FloatType getUnchecked(FloatType index) const noexcept
Calculates the approximated value for the given index without range checking.
bool isInitialised() const noexcept
Returns true if the LookupTable is initialised and ready to be used.
size_t getNumPoints() const noexcept
Returns the size of the LookupTable, i.e., the number of pre-calculated data points.
void initialise(const std::function< FloatType(size_t)> &functionToApproximate, size_t numPointsToUse)
Initialises or changes the parameters of a LookupTable object.
LookupTable()
Creates an uninitialised LookupTable object.
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
constexpr Type jmap(Type value0To1, Type targetRangeMin, Type targetRangeMax)
Remaps a normalised value (between 0 and 1) to a target range.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Returns true if a value is at least zero, and also below a specified upper limit.
unsigned int truncatePositiveToUnsignedInt(FloatType value) noexcept
Truncates a positive floating-point number to an unsigned int.