8#include "metrics/hart_metric_query.hpp"
9#include "metrics/hart_metrics_common.hpp"
55template <
typename SampleType>
58 typename MetricQuery<
double>::SingleChannelMetricEvaluator evaluator =
60 (size_t channel,
Slice slice,
Unit requestedUnit)
63 hassert (channel < buffer.getNumChannels());
68 const auto sliceFrameIndices = buffer.getFrameIndices (slice);
69 const size_t sliceStart = sliceFrameIndices.first;
70 const size_t sliceStop = sliceFrameIndices.second;
71 const size_t numFrames = sliceStop - sliceStart;
73 hassert (sliceStart < sliceStop);
74 hassert (sliceStop <= buffer.getNumFrames());
76 const SampleType* channelData = buffer[channel];
78 double peakLinear = 0.0;
81 for (size_t frame = sliceStart; frame < sliceStop; ++frame)
83 const double x =
static_cast<
double> (channelData[frame]);
84 const double absX = std::abs (x);
86 if (absX > peakLinear)
92 const double meanSquare = sumSquares.get<
double>() / numFrames;
97 const double rms = std::sqrt (meanSquare);
98 const double crestFactorLinear = peakLinear / rms;
100 switch (requestedUnit)
111 const size_t numChannels = buffer.getNumChannels();
113 std::move (evaluator)
,
Implements Kahan algorithm for floating point accumulations.
AccurateSum & operator+=(SampleType value)
Adds a value to a sum, tracking the potential floating point error.
Container for audio data.
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.
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 > crestFactor(const AudioBuffer< SampleType > &buffer)
Calculates linear crest factor for a single channel of an audio buffer.
FloatType nan()
Returns a quiet NaN value for the given floating-point type.
constexpr double inf
Infinity.
static SampleType ratioToDecibels(SampleType valueLinear)
Converts linear value (ratio) 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.