HART  0.2.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, HardClip<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 */, ChannelFlags channelsToProcess) 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 {
47 if (channelsToProcess[channel] == true)
48 {
49 for (size_t frame = 0; frame < numFrames; ++frame)
50 output[channel][frame] = std::min (std::max (input[channel][frame], (SampleType) -m_thresholdLinear), (SampleType) m_thresholdLinear);
51 }
52 else
53 {
54 for (size_t frame = 0; frame < numFrames; ++frame)
55 output[channel][frame] = input[channel][frame];
56 }
57 }
58 }
59
60 void reset() override {}
61
62 /// @param id Only @ref HardClip::thresholdDb is accepted
63 /// @param value Threshold in decibels
64 void setValue(int id, double value) override
65 {
66 if (id == Params::thresholdDb)
67 m_thresholdLinear = decibelsToRatio (value);
68 }
69
70 /// @param id Only @ref HardClip::thresholdDb is accepted
71 /// @return Threshold in decibels
72 double getValue (int id) const override
73 {
74 if (id == Params::thresholdDb)
75 return ratioToDecibels (m_thresholdLinear);
76
77 return 0.0;
78 }
79
80 /// @details Supports only n-to-n channel configurations
81 bool supportsChannelLayout (size_t numInputChannels, size_t numOutputChannels) const override
82 {
83 return numInputChannels == numOutputChannels;
84 }
85
86 void represent (std::ostream& stream) const override
87 {
88 stream << dbPrecision << "HardClip (" << m_initialThresholdDb << "_dB)";
89 }
90
91 bool supportsEnvelopeFor (int /* id */) const override
92 {
93 return false;
94 }
95
96private:
97 double m_initialThresholdDb;
98 double m_thresholdLinear;
99};
100
102
103} // namespace hart
A set of boolean flags mapped to each audio channel.
std::bitset< m_maxChannels >::reference operator[](size_t channel)
Access the flag value for a specific channel.
Base for DSP effects.
Definition hart_dsp.hpp:322
Applies symmetrical hard clipping (no knee) to the signal.
void process(const AudioBuffer< SampleType > &input, AudioBuffer< SampleType > &output, const EnvelopeBuffers &, ChannelFlags channelsToProcess) override
Processes the audio.
void represent(std::ostream &stream) const override
Makes a text representation of this DSP effect for test failure outputs.
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
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:487
#define HART_THROW_OR_RETURN_VOID(ExceptionType, message)
#define hassert(condition)