7#include "matchers/hart_matcher.hpp"
9#include "signals/hart_signal.hpp"
21template<
typename SampleType>
32 template <
typename SignalType>
33 EqualsTo (
const SignalType& referenceSignal,
double toleranceLinear = (SampleType) 1e-5):
34 m_referenceSignal (referenceSignal.copy()),
35 m_toleranceLinear ((SampleType) toleranceLinear)
37 using DecayedType =
typename std::decay<SignalType>::type;
39 std::is_base_of<
Signal<SampleType>, DecayedType>::value,
40 "SignalType must be a hart::Signal subclass"
45 m_referenceSignal (std::move (other.m_referenceSignal)),
46 m_toleranceLinear (other.m_toleranceLinear)
51 m_referenceSignal (other.m_referenceSignal !=
nullptr ? other.m_referenceSignal->copy() :
nullptr),
52 m_toleranceLinear (other.m_toleranceLinear)
56 void prepare (
double sampleRateHz, size_t numChannels, size_t maxBlockSizeFrames)
override
58 m_referenceSignal->prepareWithDSPChain (sampleRateHz, numChannels, maxBlockSizeFrames);
63 auto referenceAudio =
AudioBuffer<SampleType>::emptyLike (observedAudio);
64 m_referenceSignal->renderNextBlockWithDSPChain (referenceAudio);
66 for (size_t channel = 0; channel < referenceAudio.getNumChannels(); ++channel)
68 for (size_t frame = 0; frame < referenceAudio.getNumFrames(); ++frame)
70 if (notEqual (observedAudio[channel][frame], referenceAudio[channel][frame]))
72 m_failedFrame = frame;
73 m_failedChannel = (
int) channel;
74 m_failedObservedValue = observedAudio[channel][frame];
75 m_failedExpectedValue = referenceAudio[channel][frame];
91 m_referenceSignal->resetWithDSPChain();
96 const SampleType m_differenceLinear = std::abs (m_failedExpectedValue - m_failedObservedValue);
97 std::stringstream stream;
98 stream <<
linPrecision <<
"Expected sample value: " << m_failedExpectedValue
99 <<
dbPrecision <<
" (" << ratioToDecibels (m_failedExpectedValue) <<
" dB)"
100 <<
linPrecision <<
", difference: " << m_differenceLinear
101 <<
dbPrecision <<
" (" << ratioToDecibels (m_differenceLinear) <<
" dB)";
104 details
.frame = m_failedFrame;
112 stream <<
"EqualsTo (" << *m_referenceSignal
119 std::unique_ptr<
Signal<SampleType>> m_referenceSignal;
120 const SampleType m_toleranceLinear;
122 size_t m_failedFrame = 0;
123 size_t m_failedChannel = 0;
124 SampleType m_failedObservedValue = (SampleType) 0;
125 SampleType m_failedExpectedValue = (SampleType) 0;
127 inline bool notEqual (SampleType x, SampleType y)
129 return std::abs (x - y) > m_toleranceLinear;
Checks whether the audio is identical to some signal.
bool match(const AudioBuffer< SampleType > &observedAudio) override
Tells the host if the piece of audio satisfies Matcher's condition or not.
void represent(std::ostream &stream) const override
Makes a text representation of this Macther for test failure outputs.
EqualsTo(EqualsTo &&other) noexcept
bool canOperatePerBlock() override
Tells the host is if can operate on a block-by-block basis.
virtual MatcherFailureDetails getFailureDetails() const override
Returns a description of why the match has failed.
EqualsTo(const EqualsTo &other)
void reset() override
Resets the matcher to its initial state.
EqualsTo(const SignalType &referenceSignal, double toleranceLinear=(SampleType) 1e-5)
Creates a matcher for a specific signal details The reference signal can be something simple like a S...
void prepare(double sampleRateHz, size_t numChannels, size_t maxBlockSizeFrames) override
Prepare for processing It is guaranteed that all subsequent process() calls will be in line with the ...
#define HART_MATCHER_DEFINE_COPY_AND_MOVE(ClassName)
Defines hart::Matcher::copy() and hart::Matcher::move() methods.
std::ostream & linPrecision(std::ostream &stream)
Sets number of decimal places for linear (sample) values.
std::ostream & dbPrecision(std::ostream &stream)
Sets number of decimal places for values in decibels.
#define HART_MATCHER_DECLARE_ALIASES_FOR(ClassName)
Details about matcher failure.
size_t channel
Index of channel at which the failure was detected.
std::string description
Readable description of why the match has failed.
size_t frame
Index of frame at which the match has failed.