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

template<typename SampleType >
static void resample (const SampleType *sourceData, size_t sourceSizeFrames, double sourceSampleRateHz, SampleType *destinationData, size_t destinationCapacityFrames, double destinationSampleRateHz)
 Resamples audio buffers.
 
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 size_t nextPowerOfTwo (size_t x)
 Finds next power of 2 after a non-negative number x.
 
static size_t previousPowerOfTwo (size_t x)
 Finds previous power of 2 after a non-negative number x.
 
static bool isPowerOfTwo (size_t x)
 
static bool fileExistsAndReadable (const std::string &path)
 
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 277 of file hart_utils.hpp.

◆ HART_DEPRECATED

#define HART_DEPRECATED (   msg)

Definition at line 289 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 39 of file hart_utils.hpp.

◆ MidSideChannel

Helper values for mid-side channel indices.

Enumerator
mid 
side 

Definition at line 46 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 53 of file hart_utils.hpp.

◆ Oversampling

Oversampling ratio.

Enumerator
x4 
x8 
x16 

Definition at line 60 of file hart_utils.hpp.

◆ Interpolation

enum class Interpolation
strong

Interpolation method.

Enumerator
nearest 
linear 

Definition at line 68 of file hart_utils.hpp.

Function Documentation

◆ resample()

template<typename SampleType >
static void resample ( const SampleType *  sourceData,
size_t  sourceSizeFrames,
double  sourceSampleRateHz,
SampleType *  destinationData,
size_t  destinationCapacityFrames,
double  destinationSampleRateHz 
)
static

Resamples audio buffers.

Uses r8brain under the hood, a sample rate converter designed by Aleksey Vaneev of Voxengo

Note
Intended to be used internally by containers like AudioBuffer or ImpulseResponse, but you can use it too, if you really want to. But it's encouraged to use those containers' methods, whenever appropriate.

Definition at line 23 of file hart_resample.hpp.

◆ operator<<()

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

Definition at line 74 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 81 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 88 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 97 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 109 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 121 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 133 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 143 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 150 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 157 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 163 of file hart_utils.hpp.

◆ wrapPhase()

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

Keeps phase in 0..twoPi range.

Definition at line 170 of file hart_utils.hpp.

◆ nextPowerOfTwo()

static size_t nextPowerOfTwo ( size_t  x)
static

Finds next power of 2 after a non-negative number x.

Definition at line 181 of file hart_utils.hpp.

◆ previousPowerOfTwo()

static size_t previousPowerOfTwo ( size_t  x)
static

Finds previous power of 2 after a non-negative number x.

Note
If x is a power of 2 itself, it will return x

Definition at line 193 of file hart_utils.hpp.

◆ isPowerOfTwo()

static bool isPowerOfTwo ( size_t  x)
static

Definition at line 207 of file hart_utils.hpp.

◆ fileExistsAndReadable()

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

Definition at line 214 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 221 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 239 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 249 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 257 of file hart_utils.hpp.

◆ isExceptionUnwinding()

static bool isExceptionUnwinding ( )
inlinestatic

Returns true if an exception is currently being unwound.

Definition at line 263 of file hart_utils.hpp.

Variable Documentation

◆ inf

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

Infinity.

Definition at line 24 of file hart_utils.hpp.

◆ oo

constexpr double oo = inf
constexpr

Infinity.

Definition at line 27 of file hart_utils.hpp.

◆ pi

constexpr double pi = 3.14159265358979323846
constexpr

pi

Definition at line 30 of file hart_utils.hpp.

◆ twoPi

constexpr double twoPi = 2.0 * pi
constexpr

2 * pi

Definition at line 33 of file hart_utils.hpp.

◆ halfPi

constexpr double halfPi = pi / 2.0
constexpr

pi / 2

Definition at line 36 of file hart_utils.hpp.