Loading...
Searching...
No Matches
juce_LookupTable.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce::dsp
{
template <typename FloatType>
class LookupTable
{
public:
LookupTable();
LookupTable (const std::function<FloatType (size_t)>& functionToApproximate, size_t numPointsToUse);
void initialise (const std::function<FloatType (size_t)>& functionToApproximate, size_t numPointsToUse);
//==============================================================================
FloatType getUnchecked (FloatType index) const noexcept
{
jassert (isInitialised()); // Use the non-default constructor or call initialise() before first use
jassert (isPositiveAndBelow (index, FloatType (getNumPoints())));
auto i = truncatePositiveToUnsignedInt (index);
auto f = index - FloatType (i);
jassert (isPositiveAndBelow (f, FloatType (1)));
auto x0 = data.getUnchecked (static_cast<int> (i));
auto x1 = data.getUnchecked (static_cast<int> (i + 1));
return jmap (f, x0, x1);
}
//==============================================================================
FloatType get (FloatType index) const noexcept
{
if (index >= (FloatType) getNumPoints())
index = static_cast<FloatType> (getGuardIndex());
else if (index < 0)
index = {};
return getUnchecked (index);
}
//==============================================================================
FloatType operator[] (FloatType index) const noexcept { return getUnchecked (index); }
size_t getNumPoints() const noexcept { return static_cast<size_t> (data.size()) - 1; }
bool isInitialised() const noexcept { return data.size() > 1; }
private:
//==============================================================================
Array<FloatType> data;
void prepare() noexcept;
static size_t getRequiredBufferSize (size_t numPointsToUse) noexcept { return numPointsToUse + 1; }
size_t getGuardIndex() const noexcept { return getRequiredBufferSize (getNumPoints()) - 1; }
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LookupTable)
};
//==============================================================================
template <typename FloatType>
class LookupTableTransform
{
public:
//==============================================================================
LookupTableTransform() = default;
//==============================================================================
LookupTableTransform (const std::function<FloatType (FloatType)>& functionToApproximate,
FloatType minInputValueToUse,
FloatType maxInputValueToUse,
size_t numPoints)
{
initialise (functionToApproximate, minInputValueToUse, maxInputValueToUse, numPoints);
}
//==============================================================================
void initialise (const std::function<FloatType (FloatType)>& functionToApproximate,
FloatType minInputValueToUse,
FloatType maxInputValueToUse,
size_t numPoints);
//==============================================================================
FloatType processSampleUnchecked (FloatType value) const noexcept
{
jassert (value >= minInputValue && value <= maxInputValue);
return lookupTable[scaler * value + offset];
}
//==============================================================================
FloatType processSample (FloatType value) const noexcept
{
auto index = scaler * jlimit (minInputValue, maxInputValue, value) + offset;
jassert (isPositiveAndBelow (index, FloatType (lookupTable.getNumPoints())));
return lookupTable[index];
}
//==============================================================================
FloatType operator[] (FloatType index) const noexcept { return processSampleUnchecked (index); }
FloatType operator() (FloatType index) const noexcept { return processSample (index); }
//==============================================================================
void processUnchecked (const FloatType* input, FloatType* output, size_t numSamples) const noexcept
{
for (size_t i = 0; i < numSamples; ++i)
output[i] = processSampleUnchecked (input[i]);
}
//==============================================================================
void process (const FloatType* input, FloatType* output, size_t numSamples) const noexcept
{
for (size_t i = 0; i < numSamples; ++i)
output[i] = processSample (input[i]);
}
//==============================================================================
static double calculateMaxRelativeError (const std::function<FloatType (FloatType)>& functionToApproximate,
FloatType minInputValue,
FloatType maxInputValue,
size_t numPoints,
size_t numTestPoints = 0);
private:
//==============================================================================
static double calculateRelativeDifference (double, double) noexcept;
//==============================================================================
LookupTable<FloatType> lookupTable;
FloatType minInputValue, maxInputValue;
FloatType scaler, offset;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LookupTableTransform)
};
} // namespace juce::dsp
juce::Array
Definition juce_Array.h:56
juce::dsp::LookupTableTransform
Definition juce_LookupTable.h:173
juce::dsp::LookupTableTransform::LookupTableTransform
LookupTableTransform(const std::function< FloatType(FloatType)> &functionToApproximate, FloatType minInputValueToUse, FloatType maxInputValueToUse, size_t numPoints)
Definition juce_LookupTable.h:196
juce::dsp::LookupTableTransform::operator[]
FloatType operator[](FloatType index) const noexcept
Definition juce_LookupTable.h:261
juce::dsp::LookupTableTransform::processSampleUnchecked
FloatType processSampleUnchecked(FloatType value) const noexcept
Definition juce_LookupTable.h:231
juce::dsp::LookupTableTransform::initialise
void initialise(const std::function< FloatType(FloatType)> &functionToApproximate, FloatType minInputValueToUse, FloatType maxInputValueToUse, size_t numPoints)
Definition juce_LookupTable.cpp:72
juce::dsp::LookupTableTransform::process
void process(const FloatType *input, FloatType *output, size_t numSamples) const noexcept
Definition juce_LookupTable.h:280
juce::dsp::LookupTableTransform::processUnchecked
void processUnchecked(const FloatType *input, FloatType *output, size_t numSamples) const noexcept
Definition juce_LookupTable.h:270
juce::dsp::LookupTableTransform::processSample
FloatType processSample(FloatType value) const noexcept
Definition juce_LookupTable.h:251
juce::dsp::LookupTableTransform::operator()
FloatType operator()(FloatType index) const noexcept
Definition juce_LookupTable.h:264
juce::dsp::LookupTableTransform::LookupTableTransform
LookupTableTransform()=default
juce::dsp::LookupTableTransform::calculateMaxRelativeError
static double calculateMaxRelativeError(const std::function< FloatType(FloatType)> &functionToApproximate, FloatType minInputValue, FloatType maxInputValue, size_t numPoints, size_t numTestPoints=0)
Definition juce_LookupTable.cpp:98
juce::dsp::LookupTable
Definition juce_LookupTable.h:51
juce::dsp::LookupTable::get
FloatType get(FloatType index) const noexcept
Definition juce_LookupTable.h:121
juce::dsp::LookupTable::operator[]
FloatType operator[](FloatType index) const noexcept
Definition juce_LookupTable.h:133
juce::dsp::LookupTable::getUnchecked
FloatType getUnchecked(FloatType index) const noexcept
Definition juce_LookupTable.h:93
juce::dsp::LookupTable::isInitialised
bool isInitialised() const noexcept
Definition juce_LookupTable.h:139
juce::dsp::LookupTable::getNumPoints
size_t getNumPoints() const noexcept
Definition juce_LookupTable.h:136
juce::dsp::LookupTable::initialise
void initialise(const std::function< FloatType(size_t)> &functionToApproximate, size_t numPointsToUse)
Definition juce_LookupTable.cpp:44
juce::dsp::LookupTable::LookupTable
LookupTable()
Definition juce_LookupTable.cpp:30