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