Making Audio Plugins Part 14: LFO

The Low Frequency Oscillator (LFO) is an important part of any classic synthesizer. We’re going to add one to our plugin. As the name implies, an LFO ist just a regular Oscillator! We already have an Oscillator class, so it’s very easy to add another instance with a low frequency. Let’s start in Synthesis.h: Add the following members to the private section:

Oscillator mLFO;
double lfoFilterModAmount;

As you can see, mLFO is just another Oscillator instance. lfoFilterModAmount indicates how much the LFO will modulate the filter’s cutoff frequency. Of course we have to intialize lfoFilterModAmount. In Synthesis.cpp, add the following entry to the constructor’s initializer list:

lfoFilterModAmount(0.1)

Why 0.1? In this post we’re just going to prove the point that it’s very easy to add an LFO. We want to hear that it’s working, so we’re hardcoding a modulation amount here. We will add GUI controls later as part of a nice redesign. At the very end of the constructor, add the following:

mLFO.setMode(OSCILLATOR_MODE_TRIANGLE);
mLFO.setFrequency(6.0);
mLFO.setMuted(false);

This just selects the triangle waveform, sets the oscillator to a frequency of 6 Hz and unmutes it. If we were to add GUI constrols, the first two would probably be set inside OnParamChange according to user input. Muting and unmuting the oscillator would depend on whether lfoFilterModAmount is equal to zero.

Since it’s an Oscillator, we have to inform the LFO about sample rate changes. Add this line to Synthesis::Reset:

mLFO.setSampleRate(GetSampleRate());

Now let’s get the LFO values. We’re doing this in ProcessDoubleReplacing. Change the for loop to the following:

for (int i = 0; i < nFrames; ++i) {
    mMIDIReceiver.advance();
    int velocity = mMIDIReceiver.getLastVelocity();
    double lfoFilterModulation = mLFO.nextSample() * lfoFilterModAmount;
    mOscillator.setFrequency(mMIDIReceiver.getLastFrequency());
    mFilter.setCutoffMod((mFilterEnvelopeGenerator.nextSample() * filterEnvelopeAmount) + lfoFilterModulation);
    leftOutput[i] = rightOutput[i] = mFilter.process(mOscillator.nextSample() * mEnvelopeGenerator.nextSample() * velocity / 127.0);
}

First, we’re calculating lfoFilterModulation: It’s a value between -1 and +1. When we call setCutoffMod, we add the filter envelope and the lfoFilterModulation and pass it to setCutoffMod. This means that our filter`s cutoff will be modulated by both the filter envelope and the LFO.

And that’s it! Run the plugin and play around with the filter (you probably want to set the waveform to something different than sine). You should hear the typical pulsating wobbly sound.

If you are using a real MIDI keyboard to test the plugin, you can still play just one note at the time. You can download the source files for this part here. Next, we’re going to redesign our plugin to make it more beautiful:

If you found this useful, please feel free to
!
comments powered by | Disqus