HART  0.1.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_hardclip.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm> // min(), max()
4#include <iomanip>
5
7#include "hart_dsp.hpp"
8
9namespace hart
10{
11
12/// @brief Applies symmetrical hard clipping (no knee) to the signal.
13/// @details Signal will never go above the threshold. Signal that doesn't peak above
14/// the threshold is guaranteed to be identical to the input signal.
15/// @ingroup DSP
16template <typename SampleType>
18 public hart::DSP<SampleType>
19{
20public:
21 enum Params
22 {
23 thresholdDb ///< Threshold in decibels
24 };
25
26 /// @brief Contructor
27 /// @param thresholdDb Fixed threshold in decibels, above which the signal will be symmetrically hard-clipped
28 HardClip (double thresholdDb = 0.0):
29 m_initialThresholdDb (thresholdDb),
30 m_thresholdLinear (decibelsToRatio (thresholdDb))
31 {
32 }
33
34 void prepare (double /*sampleRateHz*/, size_t /*numInputChannels*/, size_t /*numOutputChannels*/, size_t /*maxBlockSizeFrames*/) override {}
35
36 void process (const AudioBuffer<SampleType>& input, AudioBuffer<SampleType>& output, const EnvelopeBuffers& /* envelopeBuffers */) override
37 {
38 hassert (output.getNumFrames() == input.getNumFrames());
39 const size_t numChannels = input.getNumChannels();
40 const size_t numFrames = input.getNumFrames();
41
42 if (input.getNumChannels() != output.getNumChannels())
43 HART_THROW_OR_RETURN_VOID (hart::ChannelLayoutError, "Unsupported channel configuration");
44
45 for (size_t channel = 0; channel < numChannels; ++channel)
46 for (size_t frame = 0; frame < numFrames; ++frame)
47 output[channel][frame] = std::min (std::max (input[channel][frame], (SampleType) -m_thresholdLinear), (SampleType) m_thresholdLinear);
48 }
49
50 void reset() override {}
51
52 /// @param id Only @ref HardClip::thresholdDb is accepted
53 /// @param value Threshold in decibels
54 void setValue(int id, double value) override
55 {
56 if (id == Params::thresholdDb)
57 m_thresholdLinear = decibelsToRatio (value);
58 }
59
60 /// @param id Only @ref HardClip::thresholdDb is accepted
61 /// @return Threshold in decibels
62 double getValue (int id) const override
63 {
64 if (id == Params::thresholdDb)
65 return ratioToDecibels (m_thresholdLinear);
66
67 return 0.0;
68 }
69
70 /// @details Supports only n-to-n channel configurations
71 bool supportsChannelLayout (size_t numInputChannels, size_t numOutputChannels) const override
72 {
73 return numInputChannels == numOutputChannels;
74 }
75
76 void represent (std::ostream& stream) const override
77 {
78 stream << dbPrecision << "HardClip (" << m_initialThresholdDb << "_dB)";
79 }
80
81 bool supportsEnvelopeFor (int /* id */) const override
82 {
83 return false;
84 }
85
87
88private:
89 double m_initialThresholdDb;
90 double m_thresholdLinear;
91};
92
94
95} // namespace hart
Base for DSP effects.
Definition hart_dsp.hpp:34
Applies symmetrical hard clipping (no knee) to the signal.
void represent(std::ostream &stream) const override
Makes a text representation of this DSP effect for test failure outputs.
void process(const AudioBuffer< SampleType > &input, AudioBuffer< SampleType > &output, const EnvelopeBuffers &) override
Processes the audio.
void prepare(double, size_t, size_t, size_t) override
Prepare for processing.
HardClip(double thresholdDb=0.0)
Contructor.
double getValue(int id) const override
@ thresholdDb
Threshold in decibels.
bool supportsEnvelopeFor(int) const override
Tells whether this effect accepts automation envelopes for a particular parameter.
void setValue(int id, double value) override
void reset() override
Resets to initial state.
bool supportsChannelLayout(size_t numInputChannels, size_t numOutputChannels) const override
#define HART_DSP_DEFINE_COPY_AND_MOVE(ClassName)
Defines hart::DSP::copy() and hart::DSP::move() methods.
Definition hart_dsp.hpp:334
static SampleType ratioToDecibels(SampleType valueLinear)
Converts linear value (ratio) to dB.
static SampleType decibelsToRatio(SampleType valueDb)
Converts dB to linear value (ratio)
#define HART_DSP_DECLARE_ALIASES_FOR(ClassName)
Definition hart_dsp.hpp:370
#define HART_THROW_OR_RETURN_VOID(ExceptionType, message)
#define hassert(condition)