From c572bebb188ce0b2e8f1848b6c0572d2d341f25e Mon Sep 17 00:00:00 2001 From: Paul Fd Date: Sun, 15 Mar 2020 00:20:22 +0100 Subject: [PATCH] Moved the content of Curve.hpp into Curve.h --- src/sfizz/Curve.h | 29 ++++++++++++++++++++++------- src/sfizz/Curve.hpp | 39 --------------------------------------- src/sfizz/SfzHelpers.h | 22 +++++++++++++++++----- 3 files changed, 39 insertions(+), 51 deletions(-) delete mode 100644 src/sfizz/Curve.hpp diff --git a/src/sfizz/Curve.h b/src/sfizz/Curve.h index dff2f86e..c9a724f7 100644 --- a/src/sfizz/Curve.h +++ b/src/sfizz/Curve.h @@ -6,6 +6,8 @@ #pragma once #include "absl/types/span.h" +#include "MathHelpers.h" +#include "SfzHelpers.h" #include #include #include @@ -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(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(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(_curves.size()); + } private: std::vector> _curves; @@ -126,5 +143,3 @@ private: }; } // namespace sfz - -#include "Curve.hpp" diff --git a/src/sfizz/Curve.hpp b/src/sfizz/Curve.hpp deleted file mode 100644 index 1ebead18..00000000 --- a/src/sfizz/Curve.hpp +++ /dev/null @@ -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(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(_curves.size()); -} - -} // namespace sfz diff --git a/src/sfizz/SfzHelpers.h b/src/sfizz/SfzHelpers.h index a50c3428..4ff80155 100644 --- a/src/sfizz/SfzHelpers.h +++ b/src/sfizz/SfzHelpers.h @@ -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(cents) / centsPerOctave); } +template::value, int> = 0> +constexpr T denormalize7Bits(float value) +{ + return static_cast(value * 127.0f); +} + +template +constexpr float normalize7Bits(T value) +{ + return static_cast(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 constexpr float normalizeCC(T ccValue) { - static_assert(std::is_integral::value, "Requires an integral T"); - return static_cast(min(max(ccValue, static_cast(0)), static_cast(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 constexpr float normalizeVelocity(T velocity) { - return normalizeCC(velocity); + return normalize7Bits(velocity); }