Skip to content

Commit f993638

Browse files
committed
LFOProcessor: allowed configuration as function generator or LFO.
Added some docs for clarity.
1 parent 770a71a commit f993638

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

modules/squarepine_audio/effects/LFOProcessor.cpp

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class LFOProcessor::TypeParameter final : public AudioParameterChoice
33
{
44
public:
55
TypeParameter() :
6-
AudioParameterChoice ("type", TRANS ("Type"), getChoices(),
6+
AudioParameterChoice (ParameterID ("type", 1), TRANS ("Type"), getChoices(),
77
static_cast<int> (LFOProcessor::LFOType::sine))
88
{
99
}
@@ -26,20 +26,24 @@ class LFOProcessor::TypeParameter final : public AudioParameterChoice
2626
};
2727

2828
//==============================================================================
29-
LFOProcessor::LFOProcessor() :
30-
InternalProcessor (false)
29+
LFOProcessor::LFOProcessor (double minFreqHz, double maxFreqHz,
30+
double defaultFreqHz, bool isMult) :
31+
InternalProcessor (false),
32+
isMultiplying (isMult)
3133
{
3234
auto layout = createDefaultParameterLayout();
3335

3436
auto tp = std::make_unique<TypeParameter>();
3537
type = tp.get();
3638
layout.add (std::move (tp));
3739

38-
auto pf = std::make_unique<AudioParameterFloat> ("frequency", "Frequency", 0.1f, 10.0f, 0.5f);
40+
auto pf = std::make_unique<AudioParameterFloat> (ParameterID ("frequency", 1), "Frequency", minFreqHz, maxFreqHz, defaultFreqHz);
3941
frequency = pf.get();
4042
layout.add (std::move (pf));
4143

4244
resetAPVTSWithLayout (std::move (layout));
45+
46+
setLFOType (LFOType::sine);
4347
}
4448

4549
//==============================================================================
@@ -84,9 +88,6 @@ void LFOProcessor::setLFOType (LFOType lfoType)
8488
void LFOProcessor::setFrequencyHz (const double newFrequency)
8589
{
8690
*frequency = (float) newFrequency;
87-
88-
floatOsc.setFrequency ((float) newFrequency);
89-
doubleOsc.setFrequency (newFrequency);
9091
}
9192

9293
void LFOProcessor::setFrequencyFromMidiNote (const int midiNote)
@@ -119,10 +120,8 @@ void LFOProcessor::prepareToPlay (const double newSampleRate, const int samplesP
119120

120121
if (isFirstRun)
121122
{
122-
isFirstRun = false;
123-
124-
setFrequencyHz (*frequency);
125123
setLFOType (static_cast<LFOType> (type->getIndex()), true);
124+
isFirstRun = false;
126125
}
127126
}
128127

@@ -132,23 +131,35 @@ void LFOProcessor::process (dsp::Oscillator<FloatType>& osc,
132131
juce::AudioBuffer<FloatType>& multer,
133132
juce::AudioBuffer<FloatType>& buffer)
134133
{
135-
if (isBypassed())
136-
return;
134+
{
135+
auto v = getFrequency();
136+
floatOsc.setFrequency ((float) v);
137+
doubleOsc.setFrequency (v);
138+
}
137139

138-
setFrequencyHz (getFrequency());
140+
if (isMultiplying)
141+
{
142+
multer.setSize (buffer.getNumChannels(), buffer.getNumSamples(), false, true, true);
143+
multer.clear();
139144

140-
multer.setSize (buffer.getNumChannels(), buffer.getNumSamples(), false, true, true);
141-
multer.clear();
145+
dsp::AudioBlock<FloatType> abMulter (multer);
142146

143-
dsp::AudioBlock<FloatType> abMulter (multer);
147+
{
148+
dsp::ProcessContextReplacing<FloatType> context (abMulter);
149+
context.isBypassed = isBypassed();
150+
osc.process (context);
151+
}
144152

153+
dsp::AudioBlock<FloatType> (buffer)
154+
.multiplyBy (abMulter);
155+
}
156+
else
145157
{
146-
dsp::ProcessContextReplacing<FloatType> context (abMulter);
158+
dsp::AudioBlock<FloatType> audioBlock (buffer);
159+
dsp::ProcessContextReplacing<FloatType> context (audioBlock);
160+
context.isBypassed = isBypassed();
147161
osc.process (context);
148162
}
149-
150-
dsp::AudioBlock<FloatType> (buffer)
151-
.multiplyBy (abMulter);
152163
}
153164

154165
void LFOProcessor::processBlock (juce::AudioBuffer<float>& buffer, MidiBuffer&) { process (floatOsc, floatMulter, buffer); }

modules/squarepine_audio/effects/LFOProcessor.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,32 @@ namespace oscillatorFunctions
3131
}
3232
}
3333

34-
/** */
34+
/** A processor that can act as an LFO or a function generator.
35+
36+
This acts as an LFO by default. You may want to use its
37+
identical functionality and properties for generator purposes;
38+
to do so, change the values in the constructor to something suitable
39+
for your needs.
40+
*/
3541
class LFOProcessor final : public InternalProcessor
3642
{
3743
public:
38-
/** Constructor. */
39-
LFOProcessor();
44+
/** Constructor.
45+
46+
@param minFreqHz The minimum frequency, in Hz.
47+
@param maxFreqHz The maximum frequency, in Hz.
48+
@param defaultFreqHz The default frequency, in Hz.
49+
@param isLFO Whether this processor is constructed
50+
to act as an LFO. Flip this to 'off'
51+
if you require a function generator instead.
52+
*/
53+
LFOProcessor (double minFreqHz = 0.1f,
54+
double maxFreqHz = 10.0f,
55+
double defaultFreqHz = 0.5f,
56+
bool isLFO = true);
4057

4158
//==============================================================================
42-
/** */
59+
/** The type of wave generator function you want to use. */
4360
enum class LFOType
4461
{
4562
sine,
@@ -80,6 +97,7 @@ class LFOProcessor final : public InternalProcessor
8097

8198
private:
8299
//==============================================================================
100+
const bool isMultiplying = true;
83101
bool isFirstRun = true;
84102

85103
juce::AudioBuffer<float> floatMulter;

0 commit comments

Comments
 (0)