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
92
93private:
94 double m_initialGainLinear;
95 double m_gainLinear;
96
97 void processConstantGain (const AudioBuffer<SampleType>& input, AudioBuffer<SampleType>& output, ChannelFlags channelsToProcess)
98 {
99 for (size_t channel = 0; channel < input.getNumChannels(); ++channel)
100 {
101 if (channelsToProcess[channel] == true)
102 {
103 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
104 output[channel][frame] = input[channel][frame] * (SampleType) m_gainLinear;
105 }
106 else
107 {
108 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
109 output[channel][frame] = input[channel][frame];
110 }
111 }
112 }
113
114 void processEnvelopedGain (const AudioBuffer<SampleType>& input, AudioBuffer<SampleType>& output, const std::vector<double>& gainEnvelopeValues, ChannelFlags channelsToProcess)
115 {
116 for (size_t channel = 0; channel < input.getNumChannels(); ++channel)
117 {
118 if (channelsToProcess[channel] == true)
119 {
120 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
121 output[channel][frame] = input[channel][frame] * (SampleType) gainEnvelopeValues[frame];
122 }
123 else
124 {
125 for (size_t frame = 0; frame < input.getNumFrames(); ++frame)
126 output[channel][frame] = input[channel][frame];
127 }
128
129 }
130 }
131};
132
134
135} // namespace hart
Container for audio data.
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.
Thrown when a numbers of channels is mismatched.
Base for DSP effects.
Definition hart_dsp.hpp:342
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.
#define HART_DSP_COPYABLE(ClassName)
Implements a generic hart::DSP::copy() method.
Definition hart_dsp.hpp:599
#define HART_THROW_OR_RETURN_VOID(ExceptionType, message)
Throws an exception if HART_DO_NOT_THROW_EXCEPTIONS is set, prints a message and returns otherwise.
#define hassert(condition)
Triggers a HartAssertException if the condition is false
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:616