OpenShot Audio Library | OpenShotAudio: juce_Bias.h Source File

OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
juce_Bias.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
27namespace juce
28{
29namespace dsp
30{
31
43template <typename FloatType>
44 class Bias
45{
46public:
47 Bias() noexcept = default;
48
49 //==============================================================================
53 void setBias (FloatType newBias) noexcept
54 {
55 jassert (newBias >= static_cast<FloatType> (-1) && newBias <= static_cast<FloatType> (1));
56 bias.setTargetValue (newBias);
57 }
58
59 //==============================================================================
63 FloatType getBias() const noexcept { return bias.getTargetValue(); }
64
66 void setRampDurationSeconds (double newDurationSeconds) noexcept
67 {
68 if (rampDurationSeconds != newDurationSeconds)
69 {
70 rampDurationSeconds = newDurationSeconds;
71 updateRamp();
72 }
73 }
74
75 double getRampDurationSeconds() const noexcept { return rampDurationSeconds; }
76
77 //==============================================================================
79 void prepare (const ProcessSpec& spec) noexcept
80 {
81 sampleRate = spec.sampleRate;
82 updateRamp();
83 }
84
85 void reset() noexcept
86 {
87 bias.reset (sampleRate, rampDurationSeconds);
88 }
89
90 //==============================================================================
92 template <typename SampleType>
93 SampleType processSample (SampleType inputSample) const noexcept
94 {
95 return inputSample + bias.getNextValue();
96 }
97
98 //==============================================================================
100 template<typename ProcessContext>
101 void process (const ProcessContext& context) noexcept
102 {
103 auto&& inBlock = context.getInputBlock();
104 auto&& outBlock = context.getOutputBlock();
105
106 jassert (inBlock.getNumChannels() == outBlock.getNumChannels());
107 jassert (inBlock.getNumSamples() == outBlock.getNumSamples());
108
109 auto len = inBlock.getNumSamples();
110 auto numChannels = inBlock.getNumChannels();
111
112 if (context.isBypassed)
113 {
114 bias.skip (static_cast<int> (len));
115
116 if (context.usesSeparateInputAndOutputBlocks())
117 outBlock.copyFrom (inBlock);
118
119 return;
120 }
121
122 if (numChannels == 1)
123 {
124 auto* src = inBlock.getChannelPointer (0);
125 auto* dst = outBlock.getChannelPointer (0);
126
127 for (size_t i = 0; i < len; ++i)
128 dst[i] = src[i] + bias.getNextValue();
129 }
130 else
131 {
132 auto* biases = static_cast<FloatType*> (alloca (sizeof (FloatType) * len));
133
134 for (size_t i = 0; i < len; ++i)
135 biases[i] = bias.getNextValue();
136
137 for (size_t chan = 0; chan < numChannels; ++chan)
138 FloatVectorOperations::add (outBlock.getChannelPointer (chan),
139 inBlock.getChannelPointer (chan),
140 biases, static_cast<int> (len));
141 }
142 }
143
144
145private:
146 //==============================================================================
147 SmoothedValue<FloatType> bias;
148 double sampleRate = 0, rampDurationSeconds = 0;
149
150 void updateRamp() noexcept
151 {
152 if (sampleRate > 0)
153 bias.reset (sampleRate, rampDurationSeconds);
154 }
155};
156
157} // namespace dsp
158} // namespace juce
juce::FloatVectorOperations::add
static void JUCE_CALLTYPE add(float *dest, float amountToAdd, int numValues) noexcept
Definition: juce_FloatVectorOperations.cpp:572
juce::SmoothedValue
Definition: juce_SmoothedValue.h:229
juce::SmoothedValue::reset
void reset(double sampleRate, double rampLengthInSeconds) noexcept
Definition: juce_SmoothedValue.h:254
juce::dsp::Bias
Definition: juce_Bias.h:45
juce::dsp::Bias::getBias
FloatType getBias() const noexcept
Definition: juce_Bias.h:63
juce::dsp::Bias::prepare
void prepare(const ProcessSpec &spec) noexcept
Definition: juce_Bias.h:79
juce::dsp::Bias::processSample
SampleType processSample(SampleType inputSample) const noexcept
Definition: juce_Bias.h:93
juce::dsp::Bias::setRampDurationSeconds
void setRampDurationSeconds(double newDurationSeconds) noexcept
Definition: juce_Bias.h:66
juce::dsp::Bias::setBias
void setBias(FloatType newBias) noexcept
Definition: juce_Bias.h:53
juce::dsp::Bias::process
void process(const ProcessContext &context) noexcept
Definition: juce_Bias.h:101
juce::dsp::ProcessSpec
Definition: juce_ProcessContext.h:39