Moved the content of Curve.hpp into Curve.h

This commit is contained in:
Paul Fd 2020-03-15 00:20:22 +01:00
parent 784d1a3971
commit c572bebb18
3 changed files with 39 additions and 51 deletions

View file

@ -6,6 +6,8 @@
#pragma once
#include "absl/types/span.h"
#include "MathHelpers.h"
#include "SfzHelpers.h"
#include <array>
#include <vector>
#include <memory>
@ -22,17 +24,29 @@ public:
/**
* @brief Compute the curve for integral x in domain [0:127]
*/
float evalCC7(int value7) const;
float evalCC7(int value7) const
{
return _points[clamp(value7, 0, 127)];
}
/**
* @brief Compute the curve for real x in domain [0:127]
*/
float evalCC7(float value7) const;
float evalCC7(float value7) const
{
value7 = clamp(value7, 0.0f, 127.0f);
int i1 = static_cast<int>(value7);
int i2 = std::min(127, i1 + 1);
float mu = value7 - i1;
return _points[i1] + mu * (_points[i2] - _points[i1]);
}
/**
* @brief Compute the curve for real x in domain [0:1]
*/
float evalNormalized(float value) const;
float evalNormalized(float value) const
{
return evalCC7(denormalize7Bits<int>(value));
}
/**
* @brief Kind of curve interpolator
@ -118,7 +132,10 @@ public:
/**
* @brief Get the number of slots.
*/
unsigned getNumCurves() const;
unsigned getNumCurves() const
{
return static_cast<unsigned>(_curves.size());
}
private:
std::vector<std::unique_ptr<Curve>> _curves;
@ -126,5 +143,3 @@ private:
};
} // namespace sfz
#include "Curve.hpp"

View file

@ -1,39 +0,0 @@
// SPDX-License-Identifier: BSD-2-Clause
// This code is part of the sfizz library and is licensed under a BSD 2-clause
// license. You should have receive a LICENSE.md file along with the code.
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#pragma once
#include "Curve.h"
#include "MathHelpers.h"
namespace sfz
{
inline float Curve::evalCC7(int value7) const
{
return _points[clamp(value7, 0, 127)];
}
inline float Curve::evalCC7(float value7) const
{
value7 = clamp(value7, 0.0f, 127.0f);
int i1 = static_cast<int>(value7);
int i2 = std::min(127, i1 + 1);
float mu = value7 - i1;
return _points[i1] + mu * (_points[i2] - _points[i1]);
}
inline float Curve::evalNormalized(float value) const
{
return evalCC7(127 * value);
}
///
inline unsigned CurveSet::getNumCurves() const
{
return static_cast<unsigned>(_curves.size());
}
} // namespace sfz

View file

@ -13,6 +13,7 @@
#include "Macros.h"
#include "Config.h"
#include "MathHelpers.h"
#include "absl/meta/type_traits.h"
namespace sfz
{
@ -76,8 +77,20 @@ constexpr float centsFactor(T cents, T centsPerOctave = 1200)
return std::pow(2.0f, static_cast<float>(cents) / centsPerOctave);
}
template<class T, absl::enable_if_t<std::is_integral<T>::value, int> = 0>
constexpr T denormalize7Bits(float value)
{
return static_cast<T>(value * 127.0f);
}
template<class T>
constexpr float normalize7Bits(T value)
{
return static_cast<float>(min(max(value, T{ 0 }), T{ 127 })) / 127.0f;
}
/**
* @brief Normalize a CC value between (T)0.0 and (T)1.0
* @brief Normalize a CC value between 0.0 and 1.0
*
* @tparam T
* @param ccValue
@ -86,12 +99,11 @@ constexpr float centsFactor(T cents, T centsPerOctave = 1200)
template<class T>
constexpr float normalizeCC(T ccValue)
{
static_assert(std::is_integral<T>::value, "Requires an integral T");
return static_cast<float>(min(max(ccValue, static_cast<T>(0)), static_cast<T>(127))) / 127.0f;
return normalize7Bits(ccValue);
}
/**
* @brief Normalize a velocity between (T)0.0 and (T)1.0
* @brief Normalize a velocity between 0.0 and 1.0
*
* @tparam T
* @param ccValue
@ -100,7 +112,7 @@ constexpr float normalizeCC(T ccValue)
template<class T>
constexpr float normalizeVelocity(T velocity)
{
return normalizeCC(velocity);
return normalize7Bits(velocity);
}