5#include "metrics/hart_metric_query.hpp"
6#include "metrics/hart_metrics_common.hpp"
22template <
typename SampleType>
25 if (! buffer.hasSampleRate())
28 if (floatsEqual (buffer.getSampleRateHz(), 0.0))
31 typename MetricQuery<
double>::SingleChannelMetricEvaluator evaluator =
33 (size_t channel,
Slice slice,
Unit requestedUnit)
36 hassert (channel < buffer.getNumChannels());
44 const auto sliceFrameIndices = buffer.getFrameIndices (slice);
45 const size_t sliceStart = sliceFrameIndices.first;
46 const size_t sliceStop = sliceFrameIndices.second;
47 const size_t numFrames = sliceStop - sliceStart;
49 hassert (sliceStart < sliceStop);
50 hassert (sliceStop <= buffer.getNumFrames());
55 const SampleType* channelData = buffer[channel];
56 size_t zeroCrossings = 0;
57 double previous = channelData[sliceStart];
59 for (size_t frame = sliceStart + 1; frame < sliceStop; ++frame)
61 const double current = channelData[frame];
62 zeroCrossings += (previous >= 0.0) ^ (current >= 0.0);
66 const double sliceDurationSeconds =
static_cast<
double> (numFrames) / buffer.getSampleRateHz();
67 return static_cast<
double> (zeroCrossings) / sliceDurationSeconds;
70 const size_t numChannels = buffer.getNumChannels();
72 std::move (evaluator)
,
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 sample rate is mismatched or invalid.
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 > zcr(const AudioBuffer< SampleType > &buffer)
Calculates zero-crossing rate (ZCR) of a signal.
FloatType nan()
Returns a quiet NaN value for the given floating-point type.
Unit
Represents a physical unit.
@ native
Default (native) unit of whatever returns some value.
Helpers to generate common default channel subsets.
static std::vector< size_t > allChannels(size_t numChannels)
Represents a slice of analysis data.