Global LFO for multiple SynthesiserVoices

I added a simple LFO to JuceDemoPlugin to control the pitch of the sine. It works as it should but when I press two or more notes the LFO value is not "synced" and each note as a different value. 

What should I do in order to have a "global" LFO across all voices?

(If it helps, I'm using a ScopedPointer for the LFO inside the SineWaveVoice class.)


1 Like

Well, if you want something global, you obviously have to add the LFO as a member of the Synthesiser instance (you will have to subclass it and override at least its renderNextBlock()  method)

You can calculate as many pitch modulation values as you need for each block and make a function like

float *SubclassedSynthesiser::getGlobalPitchValues()

which returns a read pointer (use AudioSampleBuffer::getReadPointer()) to the data.

You should then calculate the pitch values and store them  in an internal buffer BEFORE you call renderNextBlock on the SynthesiserVoices, so they can access the data with the given method (the rest should be the same as the one LFO per voice thing).

The pitch buffer size should be at least as big as the samplesPerBlock parameter in your prepareToPlay callback - some people might argue that it must even be bigger, but I didn't encounter any problems with the exact size yet.

And make sure you adapt the startSample offset generated by midi events which divides the input buffer into the pitch buffer.

Makes sense. I didn't know where to start. Thanks a lot!