13#include "dsp/hart_dsp_all.hpp"
14#include "dsp/hart_dsp_function.hpp"
16#include "matchers/hart_matcher.hpp"
17#include "matchers/hart_matcher_function.hpp"
21#include "signals/hart_signals_all.hpp"
48template <
typename SampleType>
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_testDurationSeconds = durationSeconds;
138 if (warmUpDurationSeconds < 0)
141 m_warmUpDurationSeconds = warmUpDurationSeconds;
152 m_inputSignal = std::move (signal.copy());
153 m_resetSignalBeforeProcessing = resetSignalBeforeProcessing ==
ResetSignal::yes;
163 m_inputSignal = std::move (signal.move());
164 m_resetSignalBeforeProcessing = resetSignalBeforeProcessing ==
ResetSignal::yes;
175 m_inputSignal = std::move (signal);
176 m_resetSignalBeforeProcessing = resetSignalBeforeProcessing ==
ResetSignal::yes;
198 std::function<
void (
AudioBuffer<SampleType>&)> signalFunction,
199 const std::string& label = {},
203 std::move (signalFunction),
208 m_resetSignalBeforeProcessing =
false;
218 if (numInputChannels == 0)
221 if (numInputChannels > 128)
224 m_numInputChannels = numInputChannels;
234 if (numOutputChannels == 0)
237 if (numOutputChannels > 128)
240 m_numOutputChannels = numOutputChannels;
247 return this->withInputChannels (2);
253 return this->withOutputChannels (2);
259 return this->withInputChannels (1);
265 return this->withOutputChannels (1);
271 return this->withMonoInput().withMonoOutput();
277 return this->withStereoInput().withStereoOutput();
282 template<
typename MatcherType>
285 addCheck (std::forward<MatcherType> (matcher), SignalAssertionLevel::expect,
true);
291 template<
typename MatcherType>
294 addCheck (std::forward<MatcherType> (matcher), SignalAssertionLevel::expect,
false);
300 template<
typename MatcherType>
303 addCheck (std::forward<MatcherType> (matcher), SignalAssertionLevel::assert,
true);
309 template<
typename MatcherType>
312 addCheck (std::forward<MatcherType> (matcher), SignalAssertionLevel::assert,
false);
329 return expectTrue (
MatcherFunction<SampleType> (std::move (matcherFunction), label));
346 return expectTrue (
MatcherFunction<SampleType> (std::move (matcherFunction), label));
360 return expectFalse (
MatcherFunction<SampleType> (std::move (matcherFunction), label));
377 return expectFalse (
MatcherFunction<SampleType> (std::move (matcherFunction), label));
391 return assertTrue (
MatcherFunction<SampleType> (std::move (matcherFunction), label));
408 return assertTrue (
MatcherFunction<SampleType> (std::move (matcherFunction), label));
422 return assertFalse (
MatcherFunction<SampleType> (std::move (matcherFunction), label));
439 return assertFalse (
MatcherFunction<SampleType> (std::move (matcherFunction), label));
454 m_saveOutputMode = mode;
455 m_saveOutputWavFormat = wavFormat;
466 m_outputBufferSink = [&receivingBuffer] (
AudioBuffer<SampleType>&& outputBuffer)
468 receivingBuffer = std::move (outputBuffer);
481 m_outputBufferSink = std::move (outputBufferSink);
498 m_savePlotMode = mode;
508 m_inputSignalSink = [&receivingSignal] (std::unique_ptr<
SignalBase<SampleType>>&& usedSignal)
510 receivingSignal = std::move (usedSignal);
522 m_inputSignalSink = std::move (inputSignalSink);
533 m_testLabel = testLabel;
541 const size_t totalDurationFrames = (size_t) std::round (m_sampleRateHz * (m_testDurationSeconds + m_warmUpDurationSeconds));
542 const size_t warmUpDurationFrames = (size_t) std::round (m_sampleRateHz * m_warmUpDurationSeconds);
543 const size_t testDurationFrames = totalDurationFrames - warmUpDurationFrames;
545 if (totalDurationFrames == 0)
548 for (
auto& check : perBlockChecks)
550 check.matcher->prepare (m_sampleRateHz, m_numOutputChannels, m_blockSizeFrames);
551 check.shouldSkip =
false;
554 for (
auto& check : fullSignalChecks)
556 check.matcher->prepare (m_sampleRateHz, m_numOutputChannels, m_blockSizeFrames);
557 check.shouldSkip =
false;
561 m_processor->reset();
562 m_processor->prepareWithEnvelopes (m_sampleRateHz, m_numInputChannels, m_numOutputChannels, m_blockSizeFrames);
564 for (
const ParamValue& paramValue : paramValues)
567 m_processor->setValue (paramValue.id, paramValue.value);
570 if (m_inputSignal ==
nullptr)
573 if (m_resetSignalBeforeProcessing)
574 m_inputSignal->resetWithDSPChain();
576 m_inputSignal->prepareWithDSPChain (m_sampleRateHz, m_numInputChannels, m_blockSizeFrames);
580 AudioBuffer<SampleType> fullInputBuffer (m_numInputChannels, 0, m_sampleRateHz);
581 AudioBuffer<SampleType> fullOutputBuffer (m_numOutputChannels, 0, m_sampleRateHz);
582 bool atLeastOneCheckFailed =
false;
585 while (offsetFrames < warmUpDurationFrames)
587 const size_t blockSizeFrames = std::min (m_blockSizeFrames, warmUpDurationFrames - offsetFrames);
589 hart::
AudioBuffer<SampleType> inputBlock (m_numInputChannels, blockSizeFrames, m_sampleRateHz);
590 hart::
AudioBuffer<SampleType> outputBlock (m_numOutputChannels, blockSizeFrames, m_sampleRateHz);
591 m_inputSignal->renderNextBlockWithDSPChain (inputBlock);
592 m_processor->processWithEnvelopes (inputBlock, outputBlock);
594 fullInputBuffer.appendFrom (inputBlock);
595 fullOutputBuffer.appendFrom (outputBlock);
597 offsetFrames += blockSizeFrames;
600 if (m_resetSignalAfterWarmUp)
601 m_inputSignal->resetWithDSPChain();
604 while (offsetFrames < totalDurationFrames)
608 const size_t blockSizeFrames = std::min (m_blockSizeFrames, totalDurationFrames - offsetFrames);
610 hart::
AudioBuffer<SampleType> inputBlock (m_numInputChannels, blockSizeFrames, m_sampleRateHz);
611 hart::
AudioBuffer<SampleType> outputBlock (m_numOutputChannels, blockSizeFrames, m_sampleRateHz);
612 m_inputSignal->renderNextBlockWithDSPChain (inputBlock);
613 m_processor->processWithEnvelopes (inputBlock, outputBlock);
615 const bool allChecksPassed = processChecks (perBlockChecks, inputBlock, outputBlock, offsetFrames);
616 atLeastOneCheckFailed |= ! allChecksPassed;
617 fullInputBuffer.appendFrom (inputBlock);
618 fullOutputBuffer.appendFrom (outputBlock);
620 offsetFrames += blockSizeFrames;
623 if (testDurationFrames != 0 && ! fullSignalChecks.empty())
627 AudioBuffer<SampleType> fullInputNoWarmUpBuffer (m_numInputChannels, testDurationFrames, m_sampleRateHz);
628 AudioBuffer<SampleType> fullOutputNoWarmUpBuffer (m_numOutputChannels, testDurationFrames, m_sampleRateHz);
630 for (size_t channel = 0; channel < m_numInputChannels; ++channel)
631 fullInputNoWarmUpBuffer.copyFrom (channel, 0, fullInputBuffer, channel, warmUpDurationFrames, testDurationFrames);
633 for (size_t channel = 0; channel < m_numOutputChannels; ++channel)
634 fullOutputNoWarmUpBuffer.copyFrom (channel, 0, fullOutputBuffer, channel, warmUpDurationFrames, testDurationFrames);
636 const bool allChecksPassed = processChecks (fullSignalChecks, fullInputNoWarmUpBuffer, fullOutputNoWarmUpBuffer, warmUpDurationFrames);
637 atLeastOneCheckFailed |= ! allChecksPassed;
641 WavWriter<SampleType>::writeBuffer (fullOutputBuffer, m_saveOutputPath, m_saveOutputWavFormat);
644 plotData (fullInputBuffer, fullOutputBuffer, m_savePlotPath);
646 if (m_outputBufferSink !=
nullptr)
647 m_outputBufferSink (std::move (fullOutputBuffer));
649 if (m_inputSignalSink !=
nullptr)
650 m_inputSignalSink (std::move (m_inputSignal));
652 return std::move (m_processor);
662 enum class SignalAssertionLevel
671 SignalAssertionLevel signalAssertionLevel;
676 std::unique_ptr<
DSPBase<SampleType>> m_processor;
677 std::unique_ptr<
SignalBase<SampleType>> m_inputSignal;
678 double m_sampleRateHz = (
double) 44100;
679 size_t m_blockSizeFrames = 1024;
680 size_t m_numInputChannels = 1;
681 size_t m_numOutputChannels = 1;
682 std::vector<ParamValue> paramValues;
683 double m_testDurationSeconds = 0.1;
684 double m_warmUpDurationSeconds = 0.0;
685 bool m_resetSignalAfterWarmUp =
false;
686 bool m_resetSignalBeforeProcessing =
false;
687 size_t offsetFrames = 0;
688 std::string m_testLabel = {};
690 std::vector<Check> perBlockChecks;
691 std::vector<Check> fullSignalChecks;
693 std::string m_saveOutputPath;
697 std::string m_savePlotPath;
700 std::function<
void (
AudioBuffer<SampleType>&&)> m_outputBufferSink =
nullptr;
701 std::function<
void (std::unique_ptr<
SignalBase<SampleType>>&&)> m_inputSignalSink =
nullptr;
704 typename MatcherType,
705 typename =
typename std::enable_if<
707 typename std::decay<MatcherType>::type,
711 void addCheck (MatcherType&& matcher, SignalAssertionLevel assertionLevel,
bool shouldPass)
713 using Derived =
typename std::decay<MatcherType>::type;
714 static_assert (std::is_base_of<
MatcherBase<SampleType>, Derived>::value,
"matcher argument must derive from hart::Matcher");
716 const bool forceFullSignal = !shouldPass;
717 auto& group = (matcher.canOperatePerBlock() && !forceFullSignal)
723 std::forward<MatcherType>(matcher).move(),
730 void addCheck (
const MatcherBase<SampleType>& matcher, SignalAssertionLevel assertionLevel,
bool shouldPass)
732 const bool forceFullSignal = ! shouldPass;
733 auto& group = (matcher.canOperatePerBlock() && ! forceFullSignal)
738 group.push_back({ matcher.copy(), assertionLevel,
false, shouldPass });
741 bool processChecks (std::vector<Check>& checksGroup,
const AudioBuffer<SampleType>& inputAudio,
const AudioBuffer<SampleType>& outputAudio, size_t baseFrameOffset)
743 for (
auto& check : checksGroup)
745 if (check.shouldSkip)
748 auto& assertionLevel = check.signalAssertionLevel;
749 auto& matcher = check.matcher;
751 const bool matchPassed = matcher->match (inputAudio, outputAudio);
753 if (matchPassed != check.shouldPass)
755 check.shouldSkip =
true;
757 if (assertionLevel == SignalAssertionLevel::assert)
759 std::stringstream stream;
760 stream << (check.shouldPass ?
"assertTrue() failed" :
"assertFalse() failed");
762 if (! m_testLabel.empty())
763 stream <<
" at \"" << m_testLabel <<
"\"";
765 stream << std::endl <<
"Condition: " << *matcher;
767 if (check.shouldPass)
768 appendFailureDetails (stream, matcher->getFailureDetails(), inputAudio, outputAudio, baseFrameOffset);
774 std::stringstream stream;
775 stream << (check.shouldPass ?
"expectTrue() failed" :
"expectFalse() failed");
777 if (!m_testLabel.empty())
778 stream <<
" at \"" << m_testLabel <<
"\"";
780 stream << std::endl <<
"Condition: " << * matcher;
782 if (check.shouldPass)
783 appendFailureDetails (stream, matcher->getFailureDetails(), inputAudio, outputAudio, baseFrameOffset);
785 hart::ExpectationFailureMessages::get().emplace_back (stream.str());
802 const size_t frameOverall = baseFrameOffset + details
.frame;
803 const double timestampOverall =
static_cast<
double> (frameOverall) / m_sampleRateHz;
804 const size_t warmUpDurationFrames = (size_t) std::round (m_sampleRateHz * m_warmUpDurationSeconds);
805 const SampleType inputSampleValue = inputAudio[details
.channel][details
.frame];
806 const SampleType outputSampleValue = observedOutputAudio[details
.channel][details
.frame];
809 <<
"Input signal: " << *m_inputSignal << std::endl
810 <<
"Channel: " << details
.channel << std::endl;
812 if (warmUpDurationFrames == 0)
815 <<
"Frame: " << frameOverall << std::endl
816 <<
secPrecision <<
"Timestamp: " << timestampOverall <<
" seconds";
820 const size_t framePostWarmUp = frameOverall - warmUpDurationFrames;
821 const double timestampPostWarmUp =
static_cast<
double> (framePostWarmUp) / m_sampleRateHz;
823 <<
"Frame (overall): " << frameOverall << std::endl
824 <<
"Frame (post warm-up): " << framePostWarmUp << std::endl
826 <<
"Timestamp (overall): " << timestampOverall <<
" seconds" << std::endl
827 <<
"Timestamp (post warm-up): " << timestampPostWarmUp <<
" seconds";
831 <<
linPrecision <<
"Input sample value: " << inputSampleValue
832 <<
dbPrecision <<
" (" << ratioToDecibels (std::abs (inputSampleValue)) <<
" dB)" << std::endl
833 <<
linPrecision <<
"Output sample value: " << outputSampleValue
834 <<
dbPrecision <<
" (" << ratioToDecibels (std::abs (outputSampleValue)) <<
" dB)" << std::endl
843template <
typename DSPType>
846 return AudioTestBuilder<
typename std::decay<DSPType>::type::SampleTypePublicAlias> (std::forward<DSPType>(dsp));
854template <
typename DSPType>
857 using SampleType =
typename DSPType::SampleTypePublicAlias;
946using hart::processAudioWith;
974using hart::processAudioWith;
Container for audio data.
A DSP host used for building and running tests inside a test case.
AudioTestBuilder & withInputSignal(std::function< void(AudioBuffer< SampleType > &)> signalFunction, const std::string &label={}, Loop loop=Loop::yes)
Sets the input signal using a function-based signal definition.
AudioTestBuilder & withInputSignal(SignalBase< SampleType > &&signal, ResetSignal resetSignalBeforeProcessing=ResetSignal::no)
Sets the input signal for the test by moving it.
AudioTestBuilder & inMono()
Sets number of input and output channels to one.
AudioTestBuilder & assertFalse(std::function< bool(const AudioBuffer< SampleType > &, const AudioBuffer< SampleType > &)> matcherFunction, const std::string &label={})
Adds a reversed "assert" check using a function matcher.
AudioTestBuilder & withLabel(const std::string &testLabel)
Adds a label to the test.
AudioTestBuilder & expectTrue(std::function< bool(const AudioBuffer< SampleType > &)> matcherFunction, const std::string &label={})
Adds an "expect" check using a function matcher.
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 using a Matcher object.
AudioTestBuilder & assertFalse(MatcherType &&matcher)
Adds a reversed "assert" check using a Matcher object.
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 & assertFalse(std::function< bool(const AudioBuffer< SampleType > &)> matcherFunction, const std::string &label={})
Adds a reversed "assert" check using a function matcher.
AudioTestBuilder(std::unique_ptr< DSPBase< SampleType > > dsp)
Transfers the DSP smart pointer into the host.
AudioTestBuilder & expectTrue(MatcherType &&matcher)
Adds an "expect" check using a Matcher object.
AudioTestBuilder & withSampleRate(double sampleRateHz)
Sets the sample rate for the test.
AudioTestBuilder & assertTrue(std::function< bool(const AudioBuffer< SampleType > &)> matcherFunction, const std::string &label={})
Adds an "assert" check using a function matcher.
AudioTestBuilder & saveOutputTo(AudioBuffer< SampleType > &receivingBuffer)
Enables saving output audio to a provided buffer.
AudioTestBuilder & savePlotTo(const std::string &path, Save mode=Save::always)
Enables saving a plot to an SVG file.
AudioTestBuilder & withInputSignal(const SignalBase< SampleType > &signal, ResetSignal resetSignalBeforeProcessing=ResetSignal::no)
Sets the input signal for the test by copying it.
AudioTestBuilder & saveInputSignalTo(std::unique_ptr< SignalBase< SampleType > > &receivingSignal)
Moves the input signal after the processing into the provided smart pointer.
AudioTestBuilder & withMonoOutput()
Sets number of output channels to one.
AudioTestBuilder & expectFalse(std::function< bool(const AudioBuffer< SampleType > &)> matcherFunction, const std::string &label={})
Adds a reversed "expect" check using a function matcher.
AudioTestBuilder & expectFalse(std::function< bool(const AudioBuffer< SampleType > &, const AudioBuffer< SampleType > &)> matcherFunction, const std::string &label={})
Adds a reversed "expect" check using a function matcher.
AudioTestBuilder & assertTrue(MatcherType &&matcher)
Adds an "assert" check using a Matcher object.
AudioTestBuilder & withValue(int id, double value)
Sets the initial param value for the tested DSP.
AudioTestBuilder & expectTrue(std::function< bool(const AudioBuffer< SampleType > &, const AudioBuffer< SampleType > &)> matcherFunction, const std::string &label={})
Adds an "expect" check using a function matcher.
AudioTestBuilder & saveOutputTo(const std::string &path, Save mode=Save::always, WavFormat wavFormat=WavFormat::pcm24)
Enables saving output audio to a wav file.
AudioTestBuilder & withMonoInput()
Sets number of input channels to one.
AudioTestBuilder & withWarmUp(double warmUpDurationSeconds, ResetSignal resetSignalAfterWarmUp=ResetSignal::no)
Adds a warm‑up period before the main test.
AudioTestBuilder & saveOutputTo(std::function< void(AudioBuffer< SampleType > &&)> outputBufferSink)
Enables saving output audio via provided callback.
AudioTestBuilder & withStereoOutput()
Sets number of output channels to two.
AudioTestBuilder & inStereo()
Sets number of input and output channels to two.
AudioTestBuilder & assertTrue(std::function< bool(const AudioBuffer< SampleType > &, const AudioBuffer< SampleType > &)> matcherFunction, const std::string &label={})
Adds an "assert" check using a function matcher.
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.
AudioTestBuilder & saveInputSignalTo(std::function< void(std::unique_ptr< SignalBase< SampleType > > &&)> inputSignalSink)
Moves the input signal after the processing via provided callback.
AudioTestBuilder & withInputSignal(std::unique_ptr< SignalBase< SampleType > > signal, ResetSignal resetSignalBeforeProcessing=ResetSignal::no)
Sets the input signal for the test by transfering its smart pointer.
Polymorphic base for all DSP.
A DSP processor defined by a user-provided function.
Polymorphic base for all matchers.
Matcher defined by a user-provided function.
Thrown when sample rate is mismatched.
Polymorphic base for all signals.
Signal defined by a user-provided function.
Thrown when an unexpected container size is encountered.
Thrown when some unexpected state is encountered.
Thrown by test asserts like HART_ASSERT_TRUE() and AudioTestBuilder::assertFalse()
Thrown when an inappropriate value is encountered.
Helper class for writing audio buffers to wav files.
#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 ...
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.
AudioTestBuilder< double > processAudioWith(std::function< void(const AudioBuffer< double > &, AudioBuffer< double > &)> dspFunction, const std::string &label={})
See the description of the float version of this function.
ResetSignal
Determines whether to reset the Signal in a given context.
AudioTestBuilder< float > processAudioWith(std::function< void(const AudioBuffer< float > &, AudioBuffer< float > &)> dspFunction, const std::string &label={})
Call this to start building your test using a block-wise non-replacing function.
Save
Determines when to save a file.
AudioTestBuilder< double > processAudioWith(std::function< double(double)> dspFunction, const std::string &label={})
See the description of the float version of this function.
AudioTestBuilder< typename std::decay< DSPType >::type::SampleTypePublicAlias > processAudioWith(DSPType &&dsp)
Call this to start building your test using a DSP object.
AudioTestBuilder< typename DSPType::SampleTypePublicAlias > processAudioWith(std::unique_ptr< DSPType > &&dsp)
Call this to start building your test using a smart pointer to a DSP object.
AudioTestBuilder< float > processAudioWith(std::function< float(float)> dspFunction, const std::string &label={})
Call this to start building your test using a sample-wise function.
AudioTestBuilder< float > processAudioWith(std::function< void(AudioBuffer< float > &)> dspFunction, const std::string &label={})
Call this to start building your test using a block-wise in-place function.
AudioTestBuilder< double > processAudioWith(std::function< void(AudioBuffer< double > &)> dspFunction, const std::string &label={})
See the description of the float version of this function.
@ no
The signal will continue from whatever state it was in.
@ yes
The signal's state will be reset.
@ 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.
std::unique_ptr< ObjectType > make_unique(Args &&... args)
std::make_unique() replacement for C++11
static std::string toAbsolutePath(const std::string &path)
Converts path to absolute, if it's relative.
Loop
Helper values for something that could loop, like a Signal.
WavFormat
Audio data storage format for the wav files.
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.