6#include "metrics/hart_metric_query.hpp"
7#include "metrics/hart_metrics_common.hpp"
69template <
typename SampleType>
72 typename MetricQuery<
double>::ChannelPairMetricEvaluator evaluator =
74 (size_t channelA, size_t channelB,
Slice slice,
Unit requestedUnit)
79 if (channelA >= buffer.getNumChannels())
82 if (channelB >= buffer.getNumChannels())
91 const auto sliceFrameIndices = buffer.getFrameIndices (slice);
92 const size_t sliceStart = sliceFrameIndices.first;
93 const size_t sliceStop = sliceFrameIndices.second;
94 hassert (sliceStart < sliceStop);
95 hassert (sliceStop - sliceStart != 0);
96 hassert (sliceStop <= buffer.getNumFrames());
101 const SampleType* channelAData = buffer[channelA];
102 const SampleType* channelBData = buffer[channelB];
108 for (size_t frame = sliceStart; frame < sliceStop; ++frame)
110 const double channelAValue =
static_cast<
double> (channelAData[frame]);
111 const double channelBValue =
static_cast<
double> (channelBData[frame]);
113 dotProduct
+= channelAValue * channelBValue;
114 sumSqChannelA
+= channelAValue * channelAValue;
115 sumSqChannelB
+= channelBValue * channelBValue;
121 return dotProduct / std::sqrt (sumSqChannelA * sumSqChannelB);
124 const size_t numChannels = buffer.getNumChannels();
126 std::move (evaluator)
,
Implements Kahan algorithm for floating point accumulations.
AccurateSum(SampleType initialSum=(SampleType) 0)
Inits AccurateSum with a specific value.
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.
MetricQuery(ChannelPairMetricEvaluator evaluator, size_t totalNumChannelsA, size_t totalNumChannelsB, std::vector< std::pair< size_t, size_t > > &&defaultChannelPairsToProcess)
Create a metric query object for a metric that operates on pair of channels at a time.
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 > channelCorrelation(const AudioBuffer< SampleType > &buffer)
Calculates zero-lag normalized cross-correlation between two channels of an audio buffer.
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.
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 > > upperTriangleChannelPairs(size_t numChannels)
Represents a slice of analysis data.