JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Bias.h
Go to the documentation of this file.
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
26namespace juce::dsp
27{
28
40template <typename FloatType>
41class Bias
42{
43public:
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
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
144private:
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
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:42
SampleType processSample(SampleType inputSample) noexcept
Returns the result of processing a single sample.
Definition juce_Bias.h:90
FloatType getBias() const noexcept
Returns the DC bias.
Definition juce_Bias.h:60
void prepare(const ProcessSpec &spec) noexcept
Called before processing starts.
Definition juce_Bias.h:76
void setRampDurationSeconds(double newDurationSeconds) noexcept
Sets the length of the ramp used for smoothing gain changes.
Definition juce_Bias.h:63
void setBias(FloatType newBias) noexcept
Sets the DC bias.
Definition juce_Bias.h:50
void process(const ProcessContext &context) noexcept
Processes the input and output buffers supplied in the processing context.
Definition juce_Bias.h:98
#define jassert(expression)
Platform-independent assertion macro.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
This structure is passed into a DSP algorithm's prepare() method, and contains information about vari...