12#include "dsp/hart_dsp_all.hpp"
14#include "matchers/hart_matcher.hpp"
18#include "signals/hart_signals_all.hpp"
37template <
typename SampleType>
44 template <
typename DSPType>
46 typename std::enable_if<
47 std::is_lvalue_reference<DSPType&&>::value &&
48 std::is_base_of<
DSPBase<SampleType>,
typename std::decay<DSPType>::type>::value
50 : m_processor (dsp.copy())
57 template <
typename DSPType>
59 typename std::enable_if<
60 ! std::is_lvalue_reference<DSPType&&>::value &&
61 std::is_base_of<
DSPBase<SampleType>,
typename std::decay<DSPType>::type>::value
63 : m_processor (std::forward<DSPType> (dsp).move())
72 : m_processor (std::move (dsp))
81 if (sampleRateHz <= 0)
84 if (! m_processor->supportsSampleRate (sampleRateHz))
87 m_sampleRateHz = sampleRateHz;
95 if (blockSizeFrames == 0)
98 m_blockSizeFrames = blockSizeFrames;
109 paramValues.emplace_back (ParamValue { id, value });
117 if (durationSeconds < 0)
120 m_durationSeconds = durationSeconds;
129 m_inputSignal = std::move (signal.copy());
139 if (numInputChannels == 0)
142 if (numInputChannels > 128)
145 m_numInputChannels = numInputChannels;
155 if (numOutputChannels == 0)
158 if (numOutputChannels > 128)
161 m_numOutputChannels = numOutputChannels;
168 return this->withInputChannels (2);
174 return this->withOutputChannels (2);
180 return this->withInputChannels (1);
186 return this->withOutputChannels (1);
192 return this->withMonoInput().withMonoOutput();
198 return this->withStereoInput().withStereoOutput();
203 template<
typename MatcherType>
206 addCheck (std::forward<MatcherType> (matcher), SignalAssertionLevel::expect,
true);
212 template<
typename MatcherType>
215 addCheck (std::forward<MatcherType> (matcher), SignalAssertionLevel::expect,
false);
221 template<
typename MatcherType>
224 addCheck (std::forward<MatcherType> (matcher), SignalAssertionLevel::assert,
true);
230 template<
typename MatcherType>
233 addCheck (std::forward<MatcherType> (matcher), SignalAssertionLevel::assert,
false);
248 m_saveOutputMode = mode;
249 m_saveOutputWavFormat = wavFormat;
264 m_savePlotMode = mode;
274 m_testLabel = testLabel;
282 m_durationFrames = (size_t) std::round (m_sampleRateHz * m_durationSeconds);
284 if (m_durationFrames == 0)
287 for (
auto& check : perBlockChecks)
289 check.matcher->prepare (m_sampleRateHz, m_numOutputChannels, m_blockSizeFrames);
290 check.shouldSkip =
false;
293 for (
auto& check : fullSignalChecks)
295 check.matcher->prepare (m_sampleRateHz, m_numOutputChannels, m_blockSizeFrames);
296 check.shouldSkip =
false;
300 m_processor->reset();
301 m_processor->prepareWithEnvelopes (m_sampleRateHz, m_numInputChannels, m_numOutputChannels, m_blockSizeFrames);
303 for (
const ParamValue& paramValue : paramValues)
306 m_processor->setValue (paramValue.id, paramValue.value);
309 if (m_inputSignal ==
nullptr)
312 m_inputSignal->resetWithDSPChain();
313 m_inputSignal->prepareWithDSPChain (m_sampleRateHz, m_numInputChannels, m_blockSizeFrames);
316 AudioBuffer<SampleType> fullInputBuffer (m_numInputChannels);
317 AudioBuffer<SampleType> fullOutputBuffer (m_numOutputChannels);
318 bool atLeastOneCheckFailed =
false;
320 while (offsetFrames < m_durationFrames)
324 const size_t blockSizeFrames = std::min (m_blockSizeFrames, m_durationFrames - offsetFrames);
326 hart::
AudioBuffer<SampleType> inputBlock (m_numInputChannels, blockSizeFrames);
327 hart::
AudioBuffer<SampleType> outputBlock (m_numOutputChannels, blockSizeFrames);
328 m_inputSignal->renderNextBlockWithDSPChain (inputBlock);
329 m_processor->processWithEnvelopes (inputBlock, outputBlock);
331 const bool allChecksPassed = processChecks (perBlockChecks, outputBlock);
332 atLeastOneCheckFailed |= ! allChecksPassed;
333 fullInputBuffer.appendFrom (inputBlock);
334 fullOutputBuffer.appendFrom (outputBlock);
336 offsetFrames += blockSizeFrames;
339 const bool allChecksPassed = processChecks (fullSignalChecks, fullOutputBuffer);
340 atLeastOneCheckFailed |= ! allChecksPassed;
343 WavWriter<SampleType>::writeBuffer (fullOutputBuffer, m_saveOutputPath, m_sampleRateHz, m_saveOutputWavFormat);
346 plotData (fullInputBuffer, fullOutputBuffer, m_sampleRateHz, m_savePlotPath);
348 return std::move (m_processor);
358 enum class SignalAssertionLevel
367 SignalAssertionLevel signalAssertionLevel;
372 std::unique_ptr<
DSPBase<SampleType>> m_processor;
373 std::unique_ptr<
SignalBase<SampleType>> m_inputSignal;
374 double m_sampleRateHz = (
double) 44100;
375 size_t m_blockSizeFrames = 1024;
376 size_t m_numInputChannels = 1;
377 size_t m_numOutputChannels = 1;
378 std::vector<ParamValue> paramValues;
379 double m_durationSeconds = 0.1;
380 size_t m_durationFrames =
static_cast<size_t> (m_durationSeconds * m_sampleRateHz);
381 size_t offsetFrames = 0;
382 std::string m_testLabel = {};
384 std::vector<Check> perBlockChecks;
385 std::vector<Check> fullSignalChecks;
387 std::string m_saveOutputPath;
391 std::string m_savePlotPath;
395 typename MatcherType,
396 typename =
typename std::enable_if<
398 typename std::decay<MatcherType>::type,
402 void addCheck (MatcherType&& matcher, SignalAssertionLevel assertionLevel,
bool shouldPass)
404 using Derived =
typename std::decay<MatcherType>::type;
405 static_assert (std::is_base_of<
MatcherBase<SampleType>, Derived>::value,
"matcher argument must derive from hart::Matcher");
407 const bool forceFullSignal = !shouldPass;
408 auto& group = (matcher.canOperatePerBlock() && !forceFullSignal)
414 std::forward<MatcherType>(matcher).move(),
421 void addCheck (
const MatcherBase<SampleType>& matcher, SignalAssertionLevel assertionLevel,
bool shouldPass)
423 const bool forceFullSignal = ! shouldPass;
424 auto& group = (matcher.canOperatePerBlock() && ! forceFullSignal)
429 group.push_back({ matcher.copy(), assertionLevel,
false, shouldPass });
432 bool processChecks (std::vector<Check>& checksGroup,
AudioBuffer<SampleType>& outputBlock)
434 for (
auto& check : checksGroup)
436 if (check.shouldSkip)
439 auto& assertionLevel = check.signalAssertionLevel;
440 auto& matcher = check.matcher;
442 const bool matchPassed = matcher->match (outputBlock);
444 if (matchPassed != check.shouldPass)
446 check.shouldSkip =
true;
448 if (assertionLevel == SignalAssertionLevel::assert)
450 std::stringstream stream;
451 stream << (check.shouldPass ?
"assertTrue() failed" :
"assertFalse() failed");
453 if (! m_testLabel.empty())
454 stream <<
" at \"" << m_testLabel <<
"\"";
456 stream << std::endl <<
"Condition: " << *matcher;
458 if (check.shouldPass)
459 appendFailureDetails (stream, matcher->getFailureDetails(), outputBlock);
465 std::stringstream stream;
466 stream << (check.shouldPass ?
"expectTrue() failed" :
"expectFalse() failed");
468 if (!m_testLabel.empty())
469 stream <<
" at \"" << m_testLabel <<
"\"";
471 stream << std::endl <<
"Condition: " << * matcher;
473 if (check.shouldPass)
474 appendFailureDetails (stream, matcher->getFailureDetails(), outputBlock);
491 const double timestampSeconds =
static_cast<
double> (offsetFrames + details
.frame) / m_sampleRateHz;
492 const SampleType sampleValue = observedAudioBlock[details
.channel][details
.frame];
495 <<
"Channel: " << details
.channel << std::endl
496 <<
"Frame: " << details
.frame << std::endl
497 <<
secPrecision <<
"Timestamp: " << timestampSeconds <<
" seconds" << std::endl
499 <<
dbPrecision <<
" (" << ratioToDecibels (std::abs (sampleValue)) <<
" dB)" << std::endl
509template <
typename DSPType>
512 return AudioTestBuilder<
typename std::decay<DSPType>::type::SampleTypePublicAlias> (std::forward<DSPType>(dsp));
521template <
typename DSPType>
524 using SampleType =
typename DSPType::SampleTypePublicAlias;
530 using hart::processAudioWith;
534 using hart::processAudioWith;
A DSP host used for building and running tests inside a test case.
AudioTestBuilder & inMono()
Sets number of input and output channels to one.
AudioTestBuilder & withLabel(const std::string &testLabel)
Adds a label to the test.
AudioTestBuilder(DSPType &&dsp, typename std::enable_if< std::is_lvalue_reference< DSPType && >::value &&std::is_base_of< DSPBase< SampleType >, typename std::decay< DSPType >::type >::value >::type *=0)
Copies the DSP instance into the host.
AudioTestBuilder & withDuration(double durationSeconds)
Sets the total duration of the input signal to be processed.
AudioTestBuilder & withStereoInput()
Sets number of input channels to two.
AudioTestBuilder & expectFalse(MatcherType &&matcher)
Adds a reversed "expect" check.
AudioTestBuilder & assertFalse(MatcherType &&matcher)
Adds a reversed "assert" check.
AudioTestBuilder & withInputChannels(size_t numInputChannels)
Sets arbitrary number of input channels.
AudioTestBuilder(DSPType &&dsp, typename std::enable_if< ! std::is_lvalue_reference< DSPType && >::value &&std::is_base_of< DSPBase< SampleType >, typename std::decay< DSPType >::type >::value >::type *=0)
Moves the DSP instance into the host.
AudioTestBuilder(std::unique_ptr< DSPBase< SampleType > > dsp)
Transfers the DSP smart pointer into the host.
AudioTestBuilder & expectTrue(MatcherType &&matcher)
Adds an "expect" check.
AudioTestBuilder & withSampleRate(double sampleRateHz)
Sets the sample rate for the test.
AudioTestBuilder & savePlotTo(const std::string &path, Save mode=Save::always)
Enables saving a plot to an SVG file.
AudioTestBuilder & withMonoOutput()
Sets number of output channels to one.
AudioTestBuilder & assertTrue(MatcherType &&matcher)
Adds an "assert" check.
AudioTestBuilder & withValue(int id, double value)
Sets the initial param value for the tested DSP.
AudioTestBuilder & saveOutputTo(const std::string &path, Save mode=Save::always, WavFormat wavFormat=WavFormat::pcm24)
Enables saving output audio to a wav file.
AudioTestBuilder & withInputSignal(const SignalBase< SampleType > &signal)
Sets the input signal for the test.
AudioTestBuilder & withMonoInput()
Sets number of input channels to one.
AudioTestBuilder & withStereoOutput()
Sets number of output channels to two.
AudioTestBuilder & inStereo()
Sets number of input and output channels to two.
AudioTestBuilder & withOutputChannels(size_t numOutputChannels)
Sets arbitrary number of output channels.
std::unique_ptr< DSPBase< SampleType > > process()
Perfoems the test.
AudioTestBuilder & withBlockSize(size_t blockSizeFrames)
Sets the block size for the test.
Polymorphic base for all DSP.
static std::vector< std::string > & get()
Polymorphic base for all matchers.
Polymorphic base for all signals.
std::ostream & linPrecision(std::ostream &stream)
Sets number of decimal places for linear (sample) values.
std::ostream & secPrecision(std::ostream &stream)
Sets number of decimal places for values in seconds.
std::ostream & dbPrecision(std::ostream &stream)
Sets number of decimal places for values in decibels.
Save
Determines when to save a file.
AudioTestBuilder< typename std::decay< DSPType >::type::SampleTypePublicAlias > processAudioWith(DSPType &&dsp)
Call this to start building your test.
AudioTestBuilder< typename DSPType::SampleTypePublicAlias > processAudioWith(std::unique_ptr< DSPType > &&dsp)
Call this to start building your test.
@ whenFails
File will be saved only when the test has failed.
@ never
File will not be saved.
@ always
File will be saved always, after the test is performed.
static std::string toAbsolutePath(const std::string &path)
Converts path to absolute, if it's relative.
#define HART_THROW_OR_RETURN(ExceptionType, message, returnValue)
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.