HART  0.1.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_peaksbelow.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cmath> // abs()
4#include <iomanip>
5#include <sstream>
6
8#include "matchers/hart_matcher.hpp"
9#include "hart_utils.hpp" // decibelsToRatio()
10
11namespace hart
12{
13
14/// @brief Checks whether the audio peaks below specific level
15/// @note Tip: To check if the audio peaks @em above some level, just flip your assertion, e.g.
16/// @code expectFalse (PeaksBelow (-3_Db)) @endcode or @code assertFalse (PeaksBelow (-3_Db)) @endcode
17/// @attention It checks the sample peaks, not the inter-sample peaks (a popular metric in audio
18/// mastering community). Fon inter-sample peak checking, you can make your own custom Matcher..
19/// @ingroup Matchers
20template<typename SampleType>
22 public Matcher<SampleType>
23{
24public:
25 /// @brief Creates a matcher for a specific peak level
26 /// @param thresholdDb Expected sample peak threshold in decibels
27 /// @param toleranceLinear Absolute tolerance for comparing frames, in linear domain (not decibels)
28 PeaksBelow (double thresholdDb, double toleranceLinear = 1e-3):
29 m_thresholdDb ((SampleType) thresholdDb),
30 m_thresholdLinear (static_cast<SampleType> (decibelsToRatio (thresholdDb) + toleranceLinear))
31 {
32 }
33
34 void prepare (double /*sampleRateHz*/, size_t /* numChannels */, size_t /* maxBlockSizeFrames */) override {}
35
36 bool match (const AudioBuffer<SampleType>& observedAudio) override
37 {
38 for (size_t channel = 0; channel < observedAudio.getNumChannels(); ++channel)
39 {
40 for (size_t frame = 0; frame < observedAudio.getNumFrames(); ++frame)
41 {
42 const SampleType observedPeakLinear = std::abs (observedAudio[channel][frame]);
43
44 if (observedPeakLinear > m_thresholdLinear)
45 {
46 m_failedFrame = frame;
47 m_failedChannel = channel;
48 m_observedPeakDb = ratioToDecibels (observedPeakLinear);
49 return false;
50 }
51 }
52 }
53
54 return true;
55 }
56
57 bool canOperatePerBlock() override
58 {
59 return true;
60 }
61
62 void reset() override {}
63
65 {
66 std::stringstream stream;
67 stream << "Observed audio peaks at at least "
68 << dbPrecision << m_observedPeakDb << " dB";
69
71 details.frame = m_failedFrame;
72 details.channel = m_failedChannel;
73 details.description = std::move (stream.str());
74 return details;
75 }
76
77 void represent (std::ostream& stream) const override
78 {
79 stream << "PeaksBelow ("
80 << dbPrecision << m_thresholdDb << "_dB, "
81 << linPrecision << m_thresholdLinear << ')';
82 }
83
85
86private:
87 const SampleType m_thresholdDb;
88 const SampleType m_thresholdLinear;
89
90 size_t m_failedFrame = 0;
91 size_t m_failedChannel = 0;
92 SampleType m_observedPeakDb = (SampleType) 0;
93};
94
96
97} // namespace hart
Base for audio matchers.
Checks whether the audio peaks below specific level.
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.
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.
PeaksBelow(double thresholdDb, double toleranceLinear=1e-3)
Creates a matcher for a specific peak level.
void prepare(double, size_t, size_t) override
Prepare for processing It is guaranteed that all subsequent process() calls will be in line with the ...
void reset() override
Resets the matcher to its initial state.
#define HART_MATCHER_DEFINE_COPY_AND_MOVE(ClassName)
Defines hart::Matcher::copy() and hart::Matcher::move() methods.
static SampleType decibelsToRatio(SampleType valueDb)
Converts dB to linear value (ratio)
#define HART_MATCHER_DECLARE_ALIASES_FOR(ClassName)
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.