5#include "metrics/hart_metric_query.hpp"
6#include "metrics/hart_metrics_common.hpp"
21template <
typename SampleType>
24 if (! buffer.hasSampleRate())
27 if (floatsEqual (buffer.getSampleRateHz(), 0.0))
30 typename MetricQuery<
double>::SingleChannelMetricEvaluator evaluator =
32 (size_t channel,
Slice slice,
Unit requestedUnit)
35 hassert (channel < buffer.getNumChannels());
43 const auto sliceFrameIndices = buffer.getFrameIndices (slice);
44 const size_t sliceStart = sliceFrameIndices.first;
45 const size_t sliceStop = sliceFrameIndices.second;
46 const size_t numFrames = sliceStop - sliceStart;
48 hassert (sliceStart < sliceStop);
49 hassert (sliceStop <= buffer.getNumFrames());
54 const SampleType* channelData = buffer[channel];
55 size_t zeroCrossings = 0;
56 double previous = channelData[sliceStart];
58 for (size_t frame = sliceStart + 1; frame < sliceStop; ++frame)
60 const double current = channelData[frame];
61 zeroCrossings += (previous >= 0.0) ^ (current >= 0.0);
65 const double sliceDurationSeconds =
static_cast<
double> (numFrames) / buffer.getSampleRateHz();
66 return static_cast<
double> (zeroCrossings) / sliceDurationSeconds;
69 const size_t numChannels = buffer.getNumChannels();
71 std::move (evaluator)
,
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 sample rate is mismatched.
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.
Unit
Represents a physical unit.
@ native
Default (native) unit of whatever returns some value.
MetricQuery< double > zcr(const AudioBuffer< SampleType > &buffer)
Calculates zero-crossing rate (ZCR) of a signal.
Helpers to generate common default channel subsets.
static std::vector< size_t > allChannels(size_t numChannels)
Represents a slice of analysis data.