HART  0.1.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm> // min(), max()
4#include <cctype> // isalpha()
5#include <cmath> // pow()
6#include <limits> // infinity()
7#include <memory>
8#include <string>
9#include <unordered_map>
10
12
13namespace hart
14{
15
16/// @defgroup Utilities Utilities
17/// @brief Handy functions and constants
18/// @{
19
20/// @brief Infinity
21constexpr double inf = std::numeric_limits<double>::infinity();
22
23/// @brief Infinity
24constexpr double oo = inf;
25
26/// @brief pi
27constexpr double pi = 3.14159265358979323846;
28
29/// @brief 2 * pi
30constexpr double twoPi = 2.0 * pi;
31
32/// @brief pi / 2
33constexpr double halfPi = pi / 2.0;
34
35/// @brief `std::clamp()` replacement for C++11
36template <typename NumericType>
37NumericType clamp (const NumericType& value, const NumericType& low, const NumericType& high)
38{
39 return std::min<NumericType> (std::max<NumericType> (value, low), high);
40}
41
42/// @brief Converts dB to linear value (ratio)
43/// @param valueDb Value in decibels
44/// @return Value in linear domain
45template <typename SampleType>
46inline static SampleType decibelsToRatio (SampleType valueDb)
47{
48 if (valueDb < -120)
49 return 0;
50
51 return std::pow (static_cast<SampleType> (10), valueDb / static_cast<SampleType> (20));
52}
53
54/// @brief Converts linear value (ratio) to dB
55/// @param valueLinear Value in linear domain
56/// @return Value in decibels
57template <typename SampleType>
58inline static SampleType ratioToDecibels (SampleType valueLinear)
59{
60 if (valueLinear < 1e-6)
61 return -120;
62
63 return static_cast<SampleType> (20 * std::log10 (valueLinear));
64}
65
66/// @brief Compares two floating point numbers within a given tolerance
67template <typename SampleType>
68inline static SampleType floatsEqual (SampleType a, SampleType b, SampleType epsilon = (SampleType) 1e-8)
69{
70 return std::abs (a - b) < epsilon;
71}
72
73/// @brief Compares two floating point numbers within a given tolerance
74template <typename SampleType>
75inline static SampleType floatsNotEqual (SampleType a, SampleType b, SampleType epsilon = (SampleType) 1e-8)
76{
77 return std::abs (a - b) >= epsilon;
78}
79
80/// @brief Rounds a floating point value to a `size_t` value
81template <typename SampleType>
82inline static size_t roundToSizeT (SampleType x)
83{
84 return static_cast<size_t> (x + (SampleType) 0.5);
85}
86
87/// @brief Keeps phase in 0..twoPi range
88template <typename SampleType>
89SampleType wrapPhase (const SampleType phaseRadians)
90{
91 SampleType wrappedPhaseRadians = std::remainder (phaseRadians, (SampleType) hart::twoPi);
92
93 if (wrappedPhaseRadians < 0.0)
94 wrappedPhaseRadians += hart::twoPi;
95
96 return wrappedPhaseRadians;
97}
98
99/// @brief Checks if the provided file path is absolute
100inline static bool isAbsolutePath (const std::string& path)
101{
102 if (path.empty())
103 return false;
104
105 if (path[0] == '/' || path[0] == '\\')
106 return true;
107
108 #ifdef _WIN32
109 if (path.size() > 1 && std::isalpha (path[0]) && path[1] == ':')
110 return true;
111 #endif
112
113 return false;
114}
115
116/// @brief Converts path to absolute, if it's relative
117/// @deials Relative paths are resolved based on a provided `--data-root-path` CLI argument
118inline static std::string toAbsolutePath (const std::string& path)
119{
120 if (isAbsolutePath(path))
121 return path;
122
124}
125
126/// @brief `std::unordered_map::contains()` replacement for C++11
127template <typename KeyType, typename ValueType>
128inline static bool contains (const std::unordered_map<KeyType, ValueType>& map, const KeyType& key)
129{
130 return map.find (key) != map.end();
131}
132
133/// @brief `std::make_unique()` replacement for C++11
134/// @details For C++11 compatibility only. If you're one C++14 or later, just use STL version.
135template<typename ObjectType, typename... Args>
136std::unique_ptr<ObjectType> make_unique (Args&&... args)
137{
138 return std::unique_ptr<ObjectType> (new ObjectType (std::forward<Args> (args)...));
139}
140
141/// @brief Defines a basic string representation of your class
142/// @details If your class takes ctor arguments, it's strongly encouraged to make a proper
143/// implementation of represent(), so that you get more detailed test failure reports.
144/// See @ref DSP::represent(), @ref Macthers::represent(), @ref Signal::represent() for the description.
145#define HART_DEFINE_GENERIC_REPRESENT(ClassName)
146 virtual void represent(std::ostream& stream) const override
147 {
148 stream << #ClassName "()";
149 }
150
151/// @}
152
153} // namespace hart
constexpr double twoPi
2 * pi
static bool isAbsolutePath(const std::string &path)
Checks if the provided file path is absolute.
SampleType wrapPhase(const SampleType phaseRadians)
Keeps phase in 0..twoPi range.
static bool contains(const std::unordered_map< KeyType, ValueType > &map, const KeyType &key)
std::unordered_map::contains() replacement for C++11
constexpr double halfPi
pi / 2
static size_t roundToSizeT(SampleType x)
Rounds a floating point value to a size_t value.
constexpr double inf
Infinity.
static SampleType ratioToDecibels(SampleType valueLinear)
Converts linear value (ratio) to dB.
static SampleType floatsNotEqual(SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
Compares two floating point numbers within a given tolerance.
std::unique_ptr< ObjectType > make_unique(Args &&... args)
std::make_unique() replacement for C++11
NumericType clamp(const NumericType &value, const NumericType &low, const NumericType &high)
std::clamp() replacement for C++11
constexpr double oo
Infinity.
static std::string toAbsolutePath(const std::string &path)
Converts path to absolute, if it's relative @deials Relative paths are resolved based on a provided -...
static SampleType decibelsToRatio(SampleType valueDb)
Converts dB to linear value (ratio)
constexpr double pi
pi
static SampleType floatsEqual(SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
Compares two floating point numbers within a given tolerance.
std::string getDataRootPath()
static CLIConfig & getInstance()