OpenShot Audio Library | OpenShotAudio  0.6.0
juce_Gain.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11  Agreement and JUCE Privacy Policy.
12 
13  End User License Agreement: www.juce.com/juce-7-licence
14  Privacy Policy: www.juce.com/juce-privacy-policy
15 
16  Or: You may also use this code under the terms of the GPL v3 (see
17  www.gnu.org/licenses).
18 
19  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21  DISCLAIMED.
22 
23  ==============================================================================
24 */
25 
26 namespace juce::dsp
27 {
28 
34 template <typename FloatType>
35 class Gain
36 {
37 public:
38  Gain() noexcept = default;
39 
40  //==============================================================================
42  void setGainLinear (FloatType newGain) noexcept { gain.setTargetValue (newGain); }
43 
45  void setGainDecibels (FloatType newGainDecibels) noexcept { setGainLinear (Decibels::decibelsToGain<FloatType> (newGainDecibels)); }
46 
48  FloatType getGainLinear() const noexcept { return gain.getTargetValue(); }
49 
51  FloatType getGainDecibels() const noexcept { return Decibels::gainToDecibels<FloatType> (getGainLinear()); }
52 
54  void setRampDurationSeconds (double newDurationSeconds) noexcept
55  {
56  if (! approximatelyEqual (rampDurationSeconds, newDurationSeconds))
57  {
58  rampDurationSeconds = newDurationSeconds;
59  reset();
60  }
61  }
62 
64  double getRampDurationSeconds() const noexcept { return rampDurationSeconds; }
65 
67  bool isSmoothing() const noexcept { return gain.isSmoothing(); }
68 
69  //==============================================================================
71  void prepare (const ProcessSpec& spec) noexcept
72  {
73  sampleRate = spec.sampleRate;
74  reset();
75  }
76 
78  void reset() noexcept
79  {
80  if (sampleRate > 0)
81  gain.reset (sampleRate, rampDurationSeconds);
82  }
83 
84  //==============================================================================
86  template <typename SampleType>
87  SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType s) noexcept
88  {
89  return s * gain.getNextValue();
90  }
91 
93  template <typename ProcessContext>
94  void process (const ProcessContext& context) noexcept
95  {
96  auto&& inBlock = context.getInputBlock();
97  auto&& outBlock = context.getOutputBlock();
98 
99  jassert (inBlock.getNumChannels() == outBlock.getNumChannels());
100  jassert (inBlock.getNumSamples() == outBlock.getNumSamples());
101 
102  auto len = inBlock.getNumSamples();
103  auto numChannels = inBlock.getNumChannels();
104 
105  if (context.isBypassed)
106  {
107  gain.skip (static_cast<int> (len));
108 
109  if (context.usesSeparateInputAndOutputBlocks())
110  outBlock.copyFrom (inBlock);
111 
112  return;
113  }
114 
115  if (numChannels == 1)
116  {
117  auto* src = inBlock.getChannelPointer (0);
118  auto* dst = outBlock.getChannelPointer (0);
119 
120  for (size_t i = 0; i < len; ++i)
121  dst[i] = src[i] * gain.getNextValue();
122  }
123  else
124  {
125  JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6255 6386)
126  auto* gains = static_cast<FloatType*> (alloca (sizeof (FloatType) * len));
127 
128  for (size_t i = 0; i < len; ++i)
129  gains[i] = gain.getNextValue();
130  JUCE_END_IGNORE_WARNINGS_MSVC
131 
132  for (size_t chan = 0; chan < numChannels; ++chan)
133  FloatVectorOperations::multiply (outBlock.getChannelPointer (chan),
134  inBlock.getChannelPointer (chan),
135  gains, static_cast<int> (len));
136  }
137  }
138 
139 private:
140  //==============================================================================
142  double sampleRate = 0, rampDurationSeconds = 0;
143 };
144 
145 } // namespace juce::dsp
FloatType getGainDecibels() const noexcept
Definition: juce_Gain.h:51
FloatType getGainLinear() const noexcept
Definition: juce_Gain.h:48
void prepare(const ProcessSpec &spec) noexcept
Definition: juce_Gain.h:71
double getRampDurationSeconds() const noexcept
Definition: juce_Gain.h:64
bool isSmoothing() const noexcept
Definition: juce_Gain.h:67
void setGainLinear(FloatType newGain) noexcept
Definition: juce_Gain.h:42
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType s) noexcept
Definition: juce_Gain.h:87
void reset() noexcept
Definition: juce_Gain.h:78
void process(const ProcessContext &context) noexcept
Definition: juce_Gain.h:94
void setRampDurationSeconds(double newDurationSeconds) noexcept
Definition: juce_Gain.h:54
void setGainDecibels(FloatType newGainDecibels) noexcept
Definition: juce_Gain.h:45