HART  0.2.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 Helper values for channel indices
37{
38 left = 0,
39 right = 1
40};
41
42/// @brief `std::clamp()` replacement for C++11
43template <typename NumericType>
44NumericType clamp (const NumericType& value, const NumericType& low, const NumericType& high)
45{
46 return std::min<NumericType> (std::max<NumericType> (value, low), high);
47}
48
49/// @brief Converts dB to linear value (ratio)
50/// @param valueDb Value in decibels
51/// @return Value in linear domain
52template <typename SampleType>
53inline static SampleType decibelsToRatio (SampleType valueDb)
54{
55 if (valueDb < -120)
56 return 0;
57
58 return std::pow (static_cast<SampleType> (10), valueDb / static_cast<SampleType> (20));
59}
60
61/// @brief Converts linear value (ratio) to dB
62/// @param valueLinear Value in linear domain
63/// @return Value in decibels
64template <typename SampleType>
65inline static SampleType ratioToDecibels (SampleType valueLinear)
66{
67 if (valueLinear < 1e-6)
68 return -120;
69
70 return static_cast<SampleType> (20 * std::log10 (valueLinear));
71}
72
73/// @brief Compares two floating point numbers within a given tolerance
74template <typename SampleType>
75inline static SampleType floatsEqual (SampleType a, SampleType b, SampleType epsilon = (SampleType) 1e-8)
76{
77 return std::abs (a - b) < epsilon;
78}
79
80/// @brief Compares two floating point numbers within a given tolerance
81template <typename SampleType>
82inline static SampleType floatsNotEqual (SampleType a, SampleType b, SampleType epsilon = (SampleType) 1e-8)
83{
84 return std::abs (a - b) >= epsilon;
85}
86
87/// @brief Rounds a floating point value to a `size_t` value
88template <typename SampleType>
89inline static size_t roundToSizeT (SampleType x)
90{
91 return static_cast<size_t> (x + (SampleType) 0.5);
92}
93
94/// @brief Keeps phase in 0..twoPi range
95template <typename SampleType>
96SampleType wrapPhase (const SampleType phaseRadians)
97{
98 SampleType wrappedPhaseRadians = std::remainder (phaseRadians, (SampleType) hart::twoPi);
99
100 if (wrappedPhaseRadians < 0.0)
101 wrappedPhaseRadians += hart::twoPi;
102
103 return wrappedPhaseRadians;
104}
105
106/// @brief Checks if the provided file path is absolute
107inline static bool isAbsolutePath (const std::string& path)
108{
109 if (path.empty())
110 return false;
111
112 if (path[0] == '/' || path[0] == '\\')
113 return true;
114
115 #ifdef _WIN32
116 if (path.size() > 1 && std::isalpha (path[0]) && path[1] == ':')
117 return true;
118 #endif
119
120 return false;
121}
122
123/// @brief Converts path to absolute, if it's relative
124/// @details Relative paths are resolved based on a provided `--data-root-path` CLI argument
125inline static std::string toAbsolutePath (const std::string& path)
126{
127 if (isAbsolutePath(path))
128 return path;
129
131}
132
133/// @brief `std::unordered_map::contains()` replacement for C++11
134template <typename KeyType, typename ValueType>
135inline static bool contains (const std::unordered_map<KeyType, ValueType>& map, const KeyType& key)
136{
137 return map.find (key) != map.end();
138}
139
140/// @brief `std::make_unique()` replacement for C++11
141/// @details For C++11 compatibility only. If you're one C++14 or later, just use STL version.
142template<typename ObjectType, typename... Args>
143std::unique_ptr<ObjectType> make_unique (Args&&... args)
144{
145 return std::unique_ptr<ObjectType> (new ObjectType (std::forward<Args> (args)...));
146}
147
148/// @brief Defines a basic string representation of your class
149/// @details If your class takes ctor arguments, it's strongly encouraged to make a proper
150/// implementation of `represent()`, so that you get more detailed test failure reports.
151/// See @ref hart::DSP::represent(), @ref hart::Macthers::represent(),
152/// @ref hart::Signal::represent() for the description.
153#define HART_DEFINE_GENERIC_REPRESENT(ClassName)
154 virtual void represent(std::ostream& stream) const override
155 {
156 stream << #ClassName "()";
157 }
158
159/// @}
160
161} // namespace hart
constexpr double twoPi
2 * pi
static bool isAbsolutePath(const std::string &path)
Checks if the provided file path is absolute.
Channel
Helper values for channel indices.
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.
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()