HART  0.2.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
Utilities

Handy functions and constants. More...

Classes

class  AccurateSum< SampleType >
 Implements Kahan algorithm for floating point accumulations. More...
 
class  TimeIt
 Helper class for measuring duration of some block of code over multiple runs. More...
 
class  WavWriter< SampleType >
 Helper class for writing audio buffers to wav files. More...
 

Macros

#define HART_STR(...)   (hart::Str() << __VA_ARGS__).toStdString()
 A helper to construct strings using the "<<" syntax.
 
#define HART_TIME_IT(timeItInstanceName, numRuns)
 Measures execution time of a code block over multiple iterations.
 
#define HART_DEFINE_GENERIC_REPRESENT(ClassName)
 Defines a basic string representation of your class.
 
#define HART_DEPRECATED(msg)
 

Enumerations

enum class  SilencePolicy { strict , relaxed }
 Defines how silence in various algorithms. More...
 
enum  Channel { left = 0 , right = 1 }
 Helper values for channel indices. More...
 
enum  MidSideChannel { mid = 0 , side = 1 }
 Helper values for mid-side channel indices. More...
 
enum class  Loop { no , yes }
 Helper values for something that could loop, like a Signal. More...
 
enum  Oversampling { x4 = 4 , x8 = 8 , x16 = 16 }
 Oversampling ratio. More...
 
enum class  Interpolation { nearest , linear }
 Interpolation method. More...
 

Functions

std::ostream & operator<< (std::ostream &os, Oversampling oversampling)
 
template<typename FloatType >
FloatType nan ()
 Returns a quiet NaN value for the given floating-point type.
 
template<typename NumericType >
NumericType clamp (const NumericType &value, const NumericType &low, const NumericType &high)
 std::clamp() replacement for C++11
 
template<typename SampleType >
static SampleType decibelsToRatio (SampleType valueDb)
 Converts dB to linear value (ratio)
 
template<typename SampleType >
static SampleType ratioToDecibels (SampleType valueLinear)
 Converts linear value (ratio) to dB.
 
template<typename SampleType >
static SampleType decibelsToPower (SampleType valueDb)
 Converts dB to linear value (power)
 
template<typename SampleType >
static SampleType powerToDecibels (SampleType valueLinear)
 Converts linear value (power) to dB.
 
template<typename SampleType >
static SampleType floatsEqual (SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
 Compares two floating point numbers within a given tolerance.
 
template<typename SampleType >
static SampleType floatsNotEqual (SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
 Compares two floating point numbers within a given tolerance.
 
template<typename SampleType >
static size_t roundToSizeT (SampleType x)
 Rounds a floating point value to a size_t value.
 
double addCents (double baseFrequencyHz, double cents)
 Anns an offset in cents to a frequency in Hz.
 
template<typename SampleType >
SampleType wrapPhase (const SampleType phaseRadians)
 Keeps phase in 0..twoPi range.
 
static bool isAbsolutePath (const std::string &path)
 Checks if the provided file path is absolute.
 
static std::string toAbsolutePath (const std::string &path)
 Converts path to absolute, if it's relative.
 
template<typename KeyType , typename ValueType >
static bool contains (const std::unordered_map< KeyType, ValueType > &map, const KeyType &key)
 std::unordered_map::contains() replacement for C++11
 
template<typename ObjectType , typename... Args>
std::unique_ptr< ObjectType > make_unique (Args &&... args)
 std::make_unique() replacement for C++11
 
static bool isExceptionUnwinding ()
 Returns true if an exception is currently being unwound.
 

Variables

constexpr double inf = std::numeric_limits<double>::infinity()
 Infinity.
 
constexpr double oo = inf
 Infinity.
 
constexpr double pi = 3.14159265358979323846
 pi
 
constexpr double twoPi = 2.0 * pi
 2 * pi
 
constexpr double halfPi = pi / 2.0
 pi / 2
 

Detailed Description

Handy functions and constants.

Macro Definition Documentation

◆ HART_STR

#define HART_STR (   ...)    (hart::Str() << __VA_ARGS__).toStdString()

A helper to construct strings using the "<<" syntax.

Usage: HART_STR ("Some text: " << someValue << "...")

Definition at line 35 of file hart_str.hpp.

◆ HART_TIME_IT

#define HART_TIME_IT (   timeItInstanceName,
  numRuns 
)
Value:
::hart::TimeIt timeItInstanceName (numRuns); \
for (size_t HART_timeItCurrentRun = 0; HART_timeItCurrentRun < (numRuns); ++HART_timeItCurrentRun) \
for (bool HART_timeItRunOnce = true; HART_timeItRunOnce; HART_timeItRunOnce = false) \
for (::hart::TimeItSample HART_sample (timeItInstanceName); HART_timeItRunOnce; HART_timeItRunOnce = false)
Helper class for measuring duration of some block of code over multiple runs.

Measures execution time of a code block over multiple iterations.

This macro runs the provided code block numRuns times and records the duration of each iteration into a local hart::TimeIt instance.

The recorder object is declared with the name provided in timeItInstanceName and can be queried after the block using reducers such as hart::mean(), hart::max(), hart::percentile() etc (see Reducers).

To query the result, call hart::TimeIt::result() on the created TimeIt instance.

Example
HART_TIME_IT (myTimeIt, 100)
{
doSomething();
};
const double t = myTimeIt.result (hart::mean());
#define HART_TIME_IT(timeItInstanceName, numRuns)
Measures execution time of a code block over multiple iterations.
Returns the arithmetic mean of all elements in the range.
Parameters
timeItInstanceNameName of the hart::TimeIt instance to create. Pick any valid name, and the object with such name will be created by the macro.
numRunsNumber of iterations to run the block.
Warning
This macro expands to multiple statements and behaves like a control statement. It must always be followed by a block. If nested, it must always be surrounded by braces. In particular, do NOT use it like this:
if (someCondition)
HART_TIME_IT (myTimeIt, 100) { doSomething(); } // Incorrect! It's not a single statement.
else
doSomethingElse();
Instead, always use braces:
if (cond)
{
HART_TIME_IT (myTimeIt, 100)
{
doSomething();
};
}
else
{
doSomethingElse();
}
Note
If the code inside the block throws an exception, the current iteration will still be recorded (via RAII), but may include stack unwinding time. Subsequent iterations will not run.

Definition at line 64 of file hart_time_it.hpp.

◆ HART_DEFINE_GENERIC_REPRESENT

#define HART_DEFINE_GENERIC_REPRESENT (   ClassName)
Value:
virtual void represent(std::ostream& stream) const override \
{ \
stream << #ClassName "()"; \
}

Defines a basic string representation of your class.

If your class takes ctor arguments, it's strongly encouraged to make a proper implementation of represent(), so that you get more detailed test failure reports. See hart::DSP::represent(), hart::Matcher::represent(), hart::Signal::represent() for the description.

Definition at line 236 of file hart_utils.hpp.

◆ HART_DEPRECATED

#define HART_DEPRECATED (   msg)

Definition at line 248 of file hart_utils.hpp.

Enumeration Type Documentation

◆ SilencePolicy

enum class SilencePolicy
strong

Defines how silence in various algorithms.

Exact meaning of each option depends on the algorithm, so refer to the docs of each specific algorithm or class for details.

Enumerator
strict 
relaxed 

Definition at line 10 of file hart_silence_policy.hpp.

◆ Channel

enum Channel

Helper values for channel indices.

Enumerator
left 
right 

Definition at line 38 of file hart_utils.hpp.

◆ MidSideChannel

Helper values for mid-side channel indices.

Enumerator
mid 
side 

Definition at line 45 of file hart_utils.hpp.

◆ Loop

enum class Loop
strong

Helper values for something that could loop, like a Signal.

Enumerator
no 
yes 

Definition at line 52 of file hart_utils.hpp.

◆ Oversampling

Oversampling ratio.

Enumerator
x4 
x8 
x16 

Definition at line 59 of file hart_utils.hpp.

◆ Interpolation

enum class Interpolation
strong

Interpolation method.

Enumerator
nearest 
linear 

Definition at line 67 of file hart_utils.hpp.

Function Documentation

◆ operator<<()

std::ostream & operator<< ( std::ostream &  os,
Oversampling  oversampling 
)
inline

Definition at line 73 of file hart_utils.hpp.

◆ nan()

template<typename FloatType >
FloatType nan ( )
inline

Returns a quiet NaN value for the given floating-point type.

Definition at line 80 of file hart_utils.hpp.

◆ clamp()

template<typename NumericType >
NumericType clamp ( const NumericType &  value,
const NumericType &  low,
const NumericType &  high 
)

std::clamp() replacement for C++11

Definition at line 87 of file hart_utils.hpp.

◆ decibelsToRatio()

template<typename SampleType >
static SampleType decibelsToRatio ( SampleType  valueDb)
inlinestatic

Converts dB to linear value (ratio)

Parameters
valueDbValue in decibels
Returns
Value in linear domain

Definition at line 96 of file hart_utils.hpp.

◆ ratioToDecibels()

template<typename SampleType >
static SampleType ratioToDecibels ( SampleType  valueLinear)
inlinestatic

Converts linear value (ratio) to dB.

Parameters
valueLinearValue in linear domain
Returns
Value in decibels

Definition at line 108 of file hart_utils.hpp.

◆ decibelsToPower()

template<typename SampleType >
static SampleType decibelsToPower ( SampleType  valueDb)
inlinestatic

Converts dB to linear value (power)

Parameters
valueDbValue in decibels
Returns
Value in linear domain

Definition at line 120 of file hart_utils.hpp.

◆ powerToDecibels()

template<typename SampleType >
static SampleType powerToDecibels ( SampleType  valueLinear)
inlinestatic

Converts linear value (power) to dB.

Parameters
valueLinearValue in linear domain
Returns
Value in decibels

Definition at line 132 of file hart_utils.hpp.

◆ floatsEqual()

template<typename SampleType >
static SampleType floatsEqual ( SampleType  a,
SampleType  b,
SampleType  epsilon = (SampleType) 1e-8 
)
inlinestatic

Compares two floating point numbers within a given tolerance.

Definition at line 142 of file hart_utils.hpp.

◆ floatsNotEqual()

template<typename SampleType >
static SampleType floatsNotEqual ( SampleType  a,
SampleType  b,
SampleType  epsilon = (SampleType) 1e-8 
)
inlinestatic

Compares two floating point numbers within a given tolerance.

Definition at line 149 of file hart_utils.hpp.

◆ roundToSizeT()

template<typename SampleType >
static size_t roundToSizeT ( SampleType  x)
inlinestatic

Rounds a floating point value to a size_t value.

Definition at line 156 of file hart_utils.hpp.

◆ addCents()

double addCents ( double  baseFrequencyHz,
double  cents 
)
inline

Anns an offset in cents to a frequency in Hz.

Definition at line 162 of file hart_utils.hpp.

◆ wrapPhase()

template<typename SampleType >
SampleType wrapPhase ( const SampleType  phaseRadians)

Keeps phase in 0..twoPi range.

Definition at line 169 of file hart_utils.hpp.

◆ isAbsolutePath()

static bool isAbsolutePath ( const std::string &  path)
inlinestatic

Checks if the provided file path is absolute.

Definition at line 180 of file hart_utils.hpp.

◆ toAbsolutePath()

static std::string toAbsolutePath ( const std::string &  path)
inlinestatic

Converts path to absolute, if it's relative.

Relative paths are resolved based on a provided --data-root-path CLI argument

Definition at line 198 of file hart_utils.hpp.

◆ contains()

template<typename KeyType , typename ValueType >
static bool contains ( const std::unordered_map< KeyType, ValueType > &  map,
const KeyType &  key 
)
inlinestatic

std::unordered_map::contains() replacement for C++11

Definition at line 208 of file hart_utils.hpp.

◆ make_unique()

template<typename ObjectType , typename... Args>
std::unique_ptr< ObjectType > make_unique ( Args &&...  args)

std::make_unique() replacement for C++11

For C++11 compatibility only. If you're one C++14 or later, just use STL version.

Definition at line 216 of file hart_utils.hpp.

◆ isExceptionUnwinding()

static bool isExceptionUnwinding ( )
inlinestatic

Returns true if an exception is currently being unwound.

Definition at line 222 of file hart_utils.hpp.

Variable Documentation

◆ inf

constexpr double inf = std::numeric_limits<double>::infinity()
constexpr

Infinity.

Definition at line 23 of file hart_utils.hpp.

◆ oo

constexpr double oo = inf
constexpr

Infinity.

Definition at line 26 of file hart_utils.hpp.

◆ pi

constexpr double pi = 3.14159265358979323846
constexpr

pi

Definition at line 29 of file hart_utils.hpp.

◆ twoPi

constexpr double twoPi = 2.0 * pi
constexpr

2 * pi

Definition at line 32 of file hart_utils.hpp.

◆ halfPi

constexpr double halfPi = pi / 2.0
constexpr

pi / 2

Definition at line 35 of file hart_utils.hpp.