HART  0.2.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/// @details This matcher will calculate peak value on a block-by-block basis.
16/// It checks where all selected channels peak collectively, not for each individual channel.
17/// To do more strict per channel checks, use multiple matchers with @ref atChannel(), so that
18/// each matcher only targets one specific channel.
19/// @note Tip: To check if the audio peaks @em above some level, just flip your assertion, e.g.
20/// @code expectFalse (PeaksBelow (-3_Db)) @endcode or @code assertFalse (PeaksBelow (-3_Db)) @endcode
21/// @attention It checks the sample peaks, not the inter-sample peaks (a popular metric in audio
22/// mastering community). For inter-sample peak checking, see `TruePeaksBelow` and `truePeak()`.
23/// For more versatile expressions, check the `samplePeak()` metric.
24/// @ingroup Matchers
25template<typename SampleType>
27 public Matcher<SampleType, PeaksBelow<SampleType>>
28{
29public:
30 /// @brief Creates a matcher for a specific peak level
31 /// @param thresholdDb Expected sample peak threshold in decibels
32 /// @param toleranceLinear Absolute tolerance for comparing frames, in linear domain (not decibels)
33 PeaksBelow (double thresholdDb, double toleranceLinear = 1e-3):
34 m_thresholdDb ((SampleType) thresholdDb),
35 m_thresholdLinear (static_cast<SampleType> (decibelsToRatio (thresholdDb) + toleranceLinear)),
36 m_toleranceLinear (static_cast<SampleType> (toleranceLinear))
37 {
38 }
39
40 void prepare (double /*sampleRateHz*/, size_t /* numInputChannels */, size_t /* numOutputChannels */, size_t /* maxBlockSizeFrames */) override {}
41
42 bool match (AnalysisContext<SampleType> context) override
43 {
44 const AudioBuffer<SampleType>& observedOutputAudio = context.outputAudio();
45
46 for (size_t channel = 0; channel < observedOutputAudio.getNumChannels(); ++channel)
47 {
48 if (! this->appliesToChannel (channel))
49 continue;
50
51 for (size_t frame = 0; frame < observedOutputAudio.getNumFrames(); ++frame)
52 {
53 const SampleType observedPeakLinear = std::abs (observedOutputAudio[channel][frame]);
54
55 if (observedPeakLinear > m_thresholdLinear)
56 {
57 m_failedFrame = frame;
58 m_failedChannel = channel;
59 m_observedPeakDb = ratioToDecibels (observedPeakLinear);
60 return false;
61 }
62 }
63 }
64
65 return true;
66 }
67
68 bool canOperatePerBlock() const override
69 {
70 return true;
71 }
72
74 {
75 std::stringstream stream;
76 stream << "Observed audio peaks at at least "
77 << dbPrecision << m_observedPeakDb << " dB";
78
80 details.frame = m_failedFrame;
81 details.channel = m_failedChannel;
82 details.description = stream.str();
83 return details;
84 }
85
86 void represent (std::ostream& stream) const override
87 {
88 stream << "PeaksBelow ("
89 << dbPrecision << m_thresholdDb << "_dB, "
90 << linPrecision << m_toleranceLinear << ')';
91 }
92
93private:
94 const SampleType m_thresholdDb;
95 const SampleType m_thresholdLinear;
96 const SampleType m_toleranceLinear;
97
98 size_t m_failedFrame = 0;
99 size_t m_failedChannel = 0;
100 SampleType m_observedPeakDb = (SampleType) 0;
101};
102
104
105} // namespace hart
Contains audio-related artefacts useful for analysis by matchers.
Container for audio data.
Base for audio matchers.
Checks whether the audio peaks below specific level.
void represent(std::ostream &stream) const override
Makes a text representation of this Matcher for test failure outputs.
void prepare(double, size_t, size_t, size_t) override
Prepare for processing It is guaranteed that all subsequent process() calls will be in line with the ...
bool match(AnalysisContext< SampleType > context) override
Tells the host if the piece of audio satisfies Matcher's condition or not.
bool canOperatePerBlock() const override
Tells the host if it 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.
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.
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.