Simple lfo on amplitude

Hello,

I am wondering if there is a more JUCE way to apply an lfo to my voice, this is how I am doing it now (I implemented lfo myself), is there a way to do it with juce::dsp::Oscillator class ?

            juce::dsp::AudioBlock<float> audioBlock { synthBuffer };
            osc.process (juce::dsp::ProcessContextReplacing<float> (audioBlock));
            gain.process(juce::dsp::ProcessContextReplacing<float> (audioBlock));
            adsr.applyEnvelopeToBuffer (synthBuffer, 0, numSamples);
            //float processedSample;
            float oldSample;
            float lfoSample;
            
            if (lfoParam != 0.0){
                for (int channel = 0; channel < synthBuffer.getNumChannels(); ++channel)
                { 
                    auto* buffer = synthBuffer.getWritePointer (channel, 0);

                    for (int sample = 0;sample < synthBuffer.getNumSamples(); ++sample){
                        oldSample = synthBuffer.getSample(channel, sample);
                        lfoSample = lfo.sinewave(lfoParam);
                        buffer[sample] = oldSample * lfoSample;
                    }
                }
            }

Hopefully the question is obvious, thanks in advance :slight_smile:

ok so first of all you have a bit of a problem in your code rn. you have the channel loop outside and the sample loop inside, which is good because it shows that you wanna write to the buffer’s memory in the order it is laid out in the hardware itself. but you are updating your single lfo in both channels. if you wanna apply a mono lfo to a stereo signal it’s better to pre-buffer it into an AudioBuffer or vector with just 1 channel and maxBlockSize’ size. then you just have to read out the values when being used on the stereo signal. that will give you not only correct behaviour in general, but also less computations and the ability to tweak that LFO signal before using it on the signal. a reasonable thing to think about for example is the range. an oscillator is made to have a range between -1 and 1, but if you use that on the signal it is a different kind of amplitude modulation than if you change the range to [0,1]. the first thing is often called bidirectional / bipolar modulation and the other thing… i guess it’s called normal or smth, idk. synths like vital visualize the difference very well, if you wanna look into that. alternatively you can also consider just using 2 LFOs, so that you can in fact use their output directly. it would change the code structure and options for user-parameters in regard to stereo width and stuff.

that being said: wether you use your own lfo/oscillator class or juce::dsp::Oscillator doesn’t matter as long as it produces a clean signal. alternative approaches to oscillators can make a difference for the user controls tho. for example if you make a wavetable based oscillator you can let users draw a curve. or you are reducing yourself to simple waveforms, then you don’t have to fiddle around with interpolation later, which can be useful. you have to explore all these possibilities to make decisions for your project

1 Like