HART  0.2.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_gainlinear.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
10namespace hart
11{
12
13/// @brief Applies linear gain (not decibels) to the signal
14/// @details To set gain in decibels, consider using @ref GainDb class instead
15/// @ingroup DSP
16template <typename SampleType>
18 public hart::DSP<SampleType, GainLinear<SampleType>>
19{
20public:
21 enum Params
22 {
23 gainLinear ///< Linear gain
24 };
25
26 /// @brief Constructor
27 /// @param initialGainLinear Linear gain
28 GainLinear (double initialGainLinear = 1.0):
29 m_initialGainLinear (initialGainLinear),
30 m_gainLinear (initialGainLinear)
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 const size_t numInputChannels = input.getNumChannels();
39 const size_t numOutputChannels = output.getNumChannels();
40 hassert (output.getNumFrames() == input.getNumFrames());
41
42 if (! supportsChannelLayout (numInputChannels, numOutputChannels))
43 HART_THROW_OR_RETURN_VOID (hart::ChannelLayoutError, "Unsupported channel configuration");
44
45 const bool hasGainEnvelope = ! envelopeBuffers.empty() && contains (envelopeBuffers, (int) Params::gainLinear);
46
47 if (hasGainEnvelope)
48 processEnvelopedGain (input, output, envelopeBuffers.at (GainLinear::gainLinear), channelsToProcess);
49 else
50 processConstantGain (input, output, channelsToProcess);
51 }
52
53 void reset() override {}
54
55 /// @param id Only @ref GainLinear::gainLinear is accepted
56 /// @param value linear gain
57 void setValue (int id, double value) override
58 {
59 if (id == Params::gainLinear)
60 m_gainLinear = value;
61 }
62
63 /// @param id Only @ref GainLinear::gainLinear is accepted
64 /// @return linear gain
65 double getValue (int id) const override
66 {
67 if (id == Params::gainLinear)
68 return m_gainLinear;
69
70 return 0.0;
71 }
72
73 /// @details Supports n-to-n configurations
74 virtual bool supportsChannelLayout (size_t numInputChannels, size_t numOutputChannels) const override
75 {
76 return numInputChannels == numOutputChannels;
77 }
78
79 virtual void represent (std::ostream& stream) const override
80 {
81 stream << dbPrecision << "GainLinear (" << m_initialGainLinear << ")";
82 }
83
84 /// @param id Only @ref GainLinear::gainLinear is accepted
85 /// return true for GainLinear::gainLinear, else otherwise
86 bool supportsEnvelopeFor (int id) const override
87 {
88 return id == Params::gainLinear;
89 }
90
91private:
92 double m_initialGainLinear;
93 double m_gainLinear;
94
95 void processConstantGain (const AudioBuffer<SampleType>& input, AudioBuffer<SampleType>& output, ChannelFlags channelsToProcess)
96 {
97 for (size_t channel = 0; channel < input.getNumChannels(); ++channel)
98 {
99 if (channelsToProcess[channel] == true)
100 {
101 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
102 output[channel][frame] = input[channel][frame] * (SampleType) m_gainLinear;
103 }
104 else
105 {
106 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
107 output[channel][frame] = input[channel][frame];
108 }
109 }
110 }
111
112 void processEnvelopedGain (const AudioBuffer<SampleType>& input, AudioBuffer<SampleType>& output, const std::vector<double>& gainEnvelopeValues, ChannelFlags channelsToProcess)
113 {
114 for (size_t channel = 0; channel < input.getNumChannels(); ++channel)
115 {
116 if (channelsToProcess[channel] == true)
117 {
118 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
119 output[channel][frame] = input[channel][frame] * (SampleType) gainEnvelopeValues[frame];
120 }
121 else
122 {
123 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
124 output[channel][frame] = input[channel][frame];
125 }
126
127 }
128 }
129};
130
132
133} // 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 linear gain (not decibels) to the signal.
virtual bool supportsChannelLayout(size_t numInputChannels, size_t numOutputChannels) const override
void prepare(double, size_t, size_t, size_t) override
Prepare for processing.
double getValue(int id) const override
@ gainLinear
Linear gain.
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.
GainLinear(double initialGainLinear=1.0)
Constructor.
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
#define HART_DSP_DECLARE_ALIASES_FOR(ClassName)
Definition hart_dsp.hpp:487
#define HART_THROW_OR_RETURN_VOID(ExceptionType, message)
#define hassert(condition)