8#include "metrics/hart_metric_query.hpp"
9#include "metrics/hart_metrics_common.hpp"
33template <
typename SampleType>
36 if ((referenceBuffer.hasSampleRate() || estimatedBuffer.hasSampleRate()) && referenceBuffer.getSampleRateHz() != estimatedBuffer.getSampleRateHz())
39 if (estimatedBuffer.getNumFrames() != referenceBuffer.getNumFrames())
42 typename MetricQuery<
double>::ChannelPairMetricEvaluator evaluator =
43 [&referenceBuffer, &estimatedBuffer]
44 (size_t referenceBufferChannel, size_t estimatedBufferChannel,
Slice slice,
Unit requestedUnit)
47 hassert (estimatedBuffer.getNumFrames() == referenceBuffer.getNumFrames());
49 if (referenceBufferChannel >= referenceBuffer.getNumChannels())
52 if (estimatedBufferChannel >= estimatedBuffer.getNumChannels())
62 const auto sliceFrameIndices = referenceBuffer.getFrameIndices (slice);
63 const size_t sliceStart = sliceFrameIndices.first;
64 const size_t sliceStop = sliceFrameIndices.second;
65 hassert (sliceStop > sliceStart);
66 hassert (sliceStop <= referenceBuffer.getNumFrames());
68 const size_t numFrames = sliceStop - sliceStart;
74 const SampleType* referenceSamples = referenceBuffer[referenceBufferChannel] + sliceStart;
75 const SampleType* estimatedSamples = estimatedBuffer[estimatedBufferChannel] + sliceStart;
77 for (size_t frame = 0; frame < numFrames; ++frame)
79 const double x =
static_cast<
double> (referenceSamples[frame]);
80 const double y =
static_cast<
double> (estimatedSamples[frame]);
81 const double noise = x - y;
84 noisePower
+= noise * noise;
93 const size_t numPairs = std::min (referenceBuffer.getNumChannels(), estimatedBuffer.getNumChannels());
95 std::move (evaluator),
96 referenceBuffer.getNumChannels(),
97 estimatedBuffer.getNumChannels(),
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.
Container for audio data.
Thrown when a container index is out of range.
Manages the metrics calculations.
Thrown when sample rate is mismatched.
Thrown when an unexpected container size is encountered.
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 ...
FloatType nan()
Returns a quiet NaN value for the given floating-point type.
static SampleType floatsEqual(SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
Compares two floating point numbers within a given tolerance.
MetricQuery< double > esr(const AudioBuffer< SampleType > &referenceBuffer, const AudioBuffer< SampleType > &estimatedBuffer)
Calculates error-to-signal ratio (ESR)
Unit
Represents a physical unit.
@ native
Default (native) unit of whatever returns some value.
Helpers to generate common default channel subsets.
static std::vector< std::pair< size_t, size_t > > diagonalChannelPairs(size_t numChannels)
Represents a slice of analysis data.