OpenShot Audio Library | OpenShotAudio  0.6.0
juce_Bias.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 
40 template <typename FloatType>
41 class Bias
42 {
43 public:
44  Bias() noexcept = default;
45 
46  //==============================================================================
50  void setBias (FloatType newBias) noexcept
51  {
52  jassert (newBias >= static_cast<FloatType> (-1) && newBias <= static_cast<FloatType> (1));
53  bias.setTargetValue (newBias);
54  }
55 
56  //==============================================================================
60  FloatType getBias() const noexcept { return bias.getTargetValue(); }
61 
63  void setRampDurationSeconds (double newDurationSeconds) noexcept
64  {
65  if (! approximatelyEqual (rampDurationSeconds, newDurationSeconds))
66  {
67  rampDurationSeconds = newDurationSeconds;
68  updateRamp();
69  }
70  }
71 
72  double getRampDurationSeconds() const noexcept { return rampDurationSeconds; }
73 
74  //==============================================================================
76  void prepare (const ProcessSpec& spec) noexcept
77  {
78  sampleRate = spec.sampleRate;
79  updateRamp();
80  }
81 
82  void reset() noexcept
83  {
84  bias.reset (sampleRate, rampDurationSeconds);
85  }
86 
87  //==============================================================================
89  template <typename SampleType>
90  SampleType processSample (SampleType inputSample) noexcept
91  {
92  return inputSample + bias.getNextValue();
93  }
94 
95  //==============================================================================
97  template <typename ProcessContext>
98  void process (const ProcessContext& context) noexcept
99  {
100  auto&& inBlock = context.getInputBlock();
101  auto&& outBlock = context.getOutputBlock();
102 
103  jassert (inBlock.getNumChannels() == outBlock.getNumChannels());
104  jassert (inBlock.getNumSamples() == outBlock.getNumSamples());
105 
106  auto len = inBlock.getNumSamples();
107  auto numChannels = inBlock.getNumChannels();
108 
109  if (context.isBypassed)
110  {
111  bias.skip (static_cast<int> (len));
112 
113  if (context.usesSeparateInputAndOutputBlocks())
114  outBlock.copyFrom (inBlock);
115 
116  return;
117  }
118 
119  if (numChannels == 1)
120  {
121  auto* src = inBlock.getChannelPointer (0);
122  auto* dst = outBlock.getChannelPointer (0);
123 
124  for (size_t i = 0; i < len; ++i)
125  dst[i] = src[i] + bias.getNextValue();
126  }
127  else
128  {
129  JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6255 6386)
130  auto* biases = static_cast<FloatType*> (alloca (sizeof (FloatType) * len));
131 
132  for (size_t i = 0; i < len; ++i)
133  biases[i] = bias.getNextValue();
134 
135  for (size_t chan = 0; chan < numChannels; ++chan)
136  FloatVectorOperations::add (outBlock.getChannelPointer (chan),
137  inBlock.getChannelPointer (chan),
138  biases, static_cast<int> (len));
139  JUCE_END_IGNORE_WARNINGS_MSVC
140  }
141  }
142 
143 
144 private:
145  //==============================================================================
147  double sampleRate = 0, rampDurationSeconds = 0;
148 
149  void updateRamp() noexcept
150  {
151  if (sampleRate > 0)
152  bias.reset (sampleRate, rampDurationSeconds);
153  }
154 };
155 
156 } // namespace juce::dsp
void reset(double sampleRate, double rampLengthInSeconds) noexcept
SampleType processSample(SampleType inputSample) noexcept
Definition: juce_Bias.h:90
FloatType getBias() const noexcept
Definition: juce_Bias.h:60
void prepare(const ProcessSpec &spec) noexcept
Definition: juce_Bias.h:76
void setRampDurationSeconds(double newDurationSeconds) noexcept
Definition: juce_Bias.h:63
void setBias(FloatType newBias) noexcept
Definition: juce_Bias.h:50
void process(const ProcessContext &context) noexcept
Definition: juce_Bias.h:98