OpenShot Library | OpenShotAudio  0.2.2
juce_Bias.h
1 
2 /** @weakgroup juce_dsp-processors
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  By using JUCE, you agree to the terms of both the JUCE 5 End-User License
15  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
16  27th April 2017).
17 
18  End User License Agreement: www.juce.com/juce-5-licence
19  Privacy Policy: www.juce.com/juce-5-privacy-policy
20 
21  Or: You may also use this code under the terms of the GPL v3 (see
22  www.gnu.org/licenses).
23 
24  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
25  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
26  DISCLAIMED.
27 
28  ==============================================================================
29 */
30 
31 namespace juce
32 {
33 namespace dsp
34 {
35 
36 /**
37  Adds a DC offset (voltage bias) to the audio samples.
38 
39  This is a useful preprocessor for asymmetric waveshaping when a waveshaper is
40  bookended by a bias on input and a DC-offset removing high pass filter on output.
41 
42  This is an extremely simple bias implementation that simply adds a value to a signal.
43  More complicated bias behaviours exist in real circuits - for your homework ;).
44 
45  @tags{DSP}
46 */
47 template <typename FloatType>
48 class Bias
49 {
50 public:
51  Bias() noexcept = default;
52 
53  //==============================================================================
54  /** Sets the DC bias
55  @param newBias DC offset in range [-1, 1]
56  */
57  void setBias (FloatType newBias) noexcept
58  {
59  jassert (newBias >= static_cast<FloatType> (-1) && newBias <= static_cast<FloatType> (1));
60  bias.setTargetValue (newBias);
61  }
62 
63  //==============================================================================
64  /** Returns the DC bias
65  @return DC bias, which should be in the range [-1, 1]
66  */
67  FloatType getBias() const noexcept { return bias.getTargetValue(); }
68 
69  /** Sets the length of the ramp used for smoothing gain changes. */
70  void setRampDurationSeconds (double newDurationSeconds) noexcept
71  {
72  if (rampDurationSeconds != newDurationSeconds)
73  {
74  rampDurationSeconds = newDurationSeconds;
75  updateRamp();
76  }
77  }
78 
79  double getRampDurationSeconds() const noexcept { return rampDurationSeconds; }
80 
81  //==============================================================================
82  /** Called before processing starts */
83  void prepare (const ProcessSpec& spec) noexcept
84  {
85  sampleRate = spec.sampleRate;
86  updateRamp();
87  }
88 
89  void reset() noexcept
90  {
91  bias.reset (sampleRate, rampDurationSeconds);
92  }
93 
94  //==============================================================================
95  /** Returns the result of processing a single sample. */
96  template <typename SampleType>
97  SampleType processSample (SampleType inputSample) const noexcept
98  {
99  return inputSample + bias.getNextValue();
100  }
101 
102  //==============================================================================
103  /** Processes the input and output buffers supplied in the processing context. */
104  template<typename ProcessContext>
105  void process (const ProcessContext& context) noexcept
106  {
107  auto&& inBlock = context.getInputBlock();
108  auto&& outBlock = context.getOutputBlock();
109 
110  jassert (inBlock.getNumChannels() == outBlock.getNumChannels());
111  jassert (inBlock.getNumSamples() == outBlock.getNumSamples());
112 
113  auto len = inBlock.getNumSamples();
114  auto numChannels = inBlock.getNumChannels();
115 
116  if (context.isBypassed)
117  {
118  bias.skip (static_cast<int> (len));
119 
120  if (context.usesSeparateInputAndOutputBlocks())
121  outBlock.copyFrom (inBlock);
122 
123  return;
124  }
125 
126  if (numChannels == 1)
127  {
128  auto* src = inBlock.getChannelPointer (0);
129  auto* dst = outBlock.getChannelPointer (0);
130 
131  for (size_t i = 0; i < len; ++i)
132  dst[i] = src[i] + bias.getNextValue();
133  }
134  else
135  {
136  auto* biases = static_cast<FloatType*> (alloca (sizeof (FloatType) * len));
137 
138  for (size_t i = 0; i < len; ++i)
139  biases[i] = bias.getNextValue();
140 
141  for (size_t chan = 0; chan < numChannels; ++chan)
142  FloatVectorOperations::add (outBlock.getChannelPointer (chan),
143  inBlock.getChannelPointer (chan),
144  biases, static_cast<int> (len));
145  }
146  }
147 
148 
149 private:
150  //==============================================================================
152  double sampleRate = 0, rampDurationSeconds = 0;
153 
154  void updateRamp() noexcept
155  {
156  if (sampleRate > 0)
157  bias.reset (sampleRate, rampDurationSeconds);
158  }
159 };
160 
161 } // namespace dsp
162 } // namespace juce
163 
164 /** @}*/
static void JUCE_CALLTYPE add(float *dest, float amountToAdd, int numValues) noexcept
Adds a fixed value to the destination values.
A utility class for values that need smoothing to avoid audio glitches.
void reset(double sampleRate, double rampLengthInSeconds) noexcept
Reset to a new sample rate and ramp length.
Adds a DC offset (voltage bias) to the audio samples.
Definition: juce_Bias.h:49
FloatType getBias() const noexcept
Returns the DC bias.
Definition: juce_Bias.h:67
void prepare(const ProcessSpec &spec) noexcept
Called before processing starts.
Definition: juce_Bias.h:83
SampleType processSample(SampleType inputSample) const noexcept
Returns the result of processing a single sample.
Definition: juce_Bias.h:97
void setRampDurationSeconds(double newDurationSeconds) noexcept
Sets the length of the ramp used for smoothing gain changes.
Definition: juce_Bias.h:70
void setBias(FloatType newBias) noexcept
Sets the DC bias.
Definition: juce_Bias.h:57
void process(const ProcessContext &context) noexcept
Processes the input and output buffers supplied in the processing context.
Definition: juce_Bias.h:105
This structure is passed into a DSP algorithm's prepare() method, and contains information about vari...