7#include "metrics/hart_metric_query.hpp"
8#include "metrics/hart_metrics_common.hpp"
55 typename MetricQuery<
double>::SingleChannelMetricEvaluator evaluator =
56 [&spectrum, floorLinear]
57 (size_t channel,
const Slice& slice,
Unit requestedUnit)
64 const size_t startBin = binIndices.first;
65 const size_t stopBin = binIndices.second;
66 const size_t numBinsInSlice = stopBin - startBin;
77 for (size_t bin = startBin; bin < stopBin; ++bin)
79 const double magnitudeLinear = std::max (spectrum
.getBinMagnitude (channel
, bin
), floorLinear);
80 logSum
+= std::log (magnitudeLinear);
81 magnitudeSum
+= magnitudeLinear;
87 const double oneOverN = 1.0 /
static_cast<
double> (numBinsInSlice);
88 const double spectralFlatnessLinear = std::exp (oneOverN * logSum
.getValue()) / (oneOverN * magnitudeSum
.getValue());
91 switch (requestedUnit)
104 std::move (evaluator)
,
Implements Kahan algorithm for floating point accumulations.
SampleType getValue() const
AccurateSum & operator+=(SampleType value)
Adds a value to a sum, tracking the potential floating point error.
Manages the metrics calculations.
MetricQuery(SingleChannelMetricEvaluator evaluator, size_t totalNumChannels, std::vector< size_t > &&defaultChannelsToProcess)
Create a metric query object for a metric that operates on one channel at a time.
Frequency-domain representation of a multi-channel audio signal.
double getSampleRateHz() const
Returns sample rate in Hz.
std::pair< size_t, size_t > getBinIndices(const Slice &slice) const
Returns a pair of indices representing a provided slice.
double getBinMagnitude(size_t channel, size_t binIndex) const
Returns magnitude of a frequency bin, by bin index.
size_t getNumBins() const
Returns number of frequency bins per channel.
size_t getNumChannels() const
Returns number of channels.
Thrown when some metric is requested to return a value in an unsupported unit.
#define hassert(condition)
Triggers a HartAssertException if the condition is false
#define HART_THROW_OR_RETURN(ExceptionType, message, returnValue)
Throws an exception if HART_DO_NOT_THROW_EXCEPTIONS is set, prints a message and returns a specified ...
MetricQuery< double > spectralFlatness(const Spectrum &spectrum, double floorLinear=1e-16)
Calculates spectral flatness, also known as Wiener entropy, or tonality coefficient.
FloatType nan()
Returns a quiet NaN value for the given floating-point type.
static SampleType powerToDecibels(SampleType valueLinear)
Converts linear value (power) to dB.
static SampleType floatsEqual(SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
Compares two floating point numbers within a given tolerance.
Unit
Represents a physical unit.
@ dB
Value of something in decibels. Can represent voltage, power, or a domain-specific unit like "LUFS" o...
@ native
Default (native) unit of whatever returns some value.
@ linear
Value of a sample (voltage) in a linear domain.
Helpers to generate common default channel subsets.
static std::vector< size_t > allChannels(size_t numChannels)
Represents a slice of analysis data.