HART  0.2.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_gaindb.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm> // fill()
4#include <iomanip>
5#include <vector>
6
7#include "hart_dsp.hpp"
9#include "hart_utils.hpp"
10
11namespace hart
12{
13
14/// @brief Applies gain in decibels to the signal
15/// @note For automation, consider using @ref GainLinear instead, because the two classes produce different curve shapes.
16/// @ingroup DSP
17template <typename SampleType>
18class GainDb:
19 public hart::DSP<SampleType, GainDb<SampleType>>
20{
21public:
22 enum Params
23 {
24 gainDb ///< Gain in decibels
25 };
26
27 /// @brief Constructor
28 /// @param gainDb Initial gain in decibels
29 GainDb (double gainDb = 0.0):
30 m_initialGainDb (gainDb),
31 m_gainLinear (decibelsToRatio (gainDb))
32 {
33 }
34
35 void prepare (double /* sampleRateHz */, size_t /* numInputChannels */, size_t /* numOutputChannels */, size_t maxBlockSizeFrames) override
36 {
37 m_gainEnvelopeValuesLinear.resize (this->hasEnvelopeFor (Params::gainDb) ? maxBlockSizeFrames : 0);
38 }
39
40 void process (const AudioBuffer<SampleType>& input, AudioBuffer<SampleType>& output, const EnvelopeBuffers& envelopeBuffers, ChannelFlags channelsToProcess) override
41 {
42 const size_t numInputChannels = input.getNumChannels();
43 const size_t numOutputChannels = output.getNumChannels();
44 hassert (output.getNumFrames() == input.getNumFrames());
45
46 if (! supportsChannelLayout (numInputChannels, numOutputChannels))
47 HART_THROW_OR_RETURN_VOID (hart::ChannelLayoutError, "Unsupported channel configuration");
48
49 const bool hasGainEnvelope = ! envelopeBuffers.empty() && contains (envelopeBuffers, (int) Params::gainDb);
50
51 if (hasGainEnvelope)
52 {
53 auto& gainEnvelopeValuesDb = envelopeBuffers. at(Params::gainDb);
54 hassert (gainEnvelopeValuesDb.size() == m_gainEnvelopeValuesLinear.size());
55
56 for (size_t i = 0; i < m_gainEnvelopeValuesLinear.size(); ++i)
57 m_gainEnvelopeValuesLinear[i] = decibelsToRatio (gainEnvelopeValuesDb[i]);
58
59 processEnvelopedGain (input, output, channelsToProcess);
60 return;
61 }
62
63 // No gain envelope
64 processConstantGain (input, output, channelsToProcess);
65 }
66
67 void reset() override {}
68
69 /// @param id Only @ref GainDb::gainDb is accepted
70 /// @param value Gain in decibels
71 void setValue (int id, double value) override
72 {
73 if (id == Params::gainDb)
74 m_gainLinear = decibelsToRatio (value);
75 }
76
77 /// @param id Only @ref GainDb::gainDb is accepted
78 /// @retval (value) Gain in decibels
79 double getValue (int id) const override
80 {
81 if (id == Params::gainDb)
82 return ratioToDecibels (m_gainLinear);
83
84 return 0.0;
85 }
86
87 /// @brief Checks if the DSP supports given i/o configuration
88 /// @details Supports only n-to-n configurations
89 virtual bool supportsChannelLayout (size_t numInputChannels, size_t numOutputChannels) const override
90 {
91 return numInputChannels == numOutputChannels;
92 }
93
94 virtual void represent (std::ostream& stream) const override
95 {
96 stream << dbPrecision << "GainDb (" << m_initialGainDb << "_dB)";
97 }
98
99 /// @param id Only @ref GainDb::gainDb is accepted
100 /// retval (bool) true for GainDb::gainDb, else otherwise
101 bool supportsEnvelopeFor (int id) const override
102 {
103 return id == Params::gainDb;
104 }
105
106private:
107 double m_initialGainDb;
108 double m_gainLinear;
109 std::vector<double> m_gainEnvelopeValuesLinear;
110
111 void processConstantGain (const AudioBuffer<SampleType>& input, AudioBuffer<SampleType>& output, ChannelFlags channelsToProcess)
112 {
113 for (size_t channel = 0; channel < input.getNumChannels(); ++channel)
114 {
115 if (channelsToProcess[channel] == true)
116 {
117 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
118 output[channel][frame] = input[channel][frame] * (SampleType) m_gainLinear;
119 }
120 else
121 {
122 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
123 output[channel][frame] = input[channel][frame];
124 }
125 }
126 }
127
128 void processEnvelopedGain (const AudioBuffer<SampleType>& input, AudioBuffer<SampleType>& output, ChannelFlags channelsToProcess)
129 {
130 for (size_t channel = 0; channel < input.getNumChannels(); ++channel)
131 {
132 if (channelsToProcess[channel] == true)
133 {
134 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
135 output[channel][frame] = input[channel][frame] * (SampleType) m_gainEnvelopeValuesLinear[frame];
136 }
137 else
138 {
139 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
140 output[channel][frame] = input[channel][frame];
141 }
142
143 }
144 }
145};
146
148
149} // 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 gain in decibels to the signal.
virtual bool supportsChannelLayout(size_t numInputChannels, size_t numOutputChannels) const override
Checks if the DSP supports given i/o configuration.
void prepare(double, size_t, size_t, size_t maxBlockSizeFrames) override
Prepare for processing.
GainDb(double gainDb=0.0)
Constructor.
double getValue(int id) const override
@ gainDb
Gain in decibels.
virtual 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 &envelopeBuffers, ChannelFlags channelsToProcess) override
Processes the audio.
void setValue(int id, double value) override
bool supportsEnvelopeFor(int id) const override
void reset() override
Resets to initial state.
std::ostream & dbPrecision(std::ostream &stream)
Sets number of decimal places for values in decibels.
static bool contains(const std::unordered_map< KeyType, ValueType > &map, const KeyType &key)
std::unordered_map::contains() replacement for C++11
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)