iPlug2 Forum

How to set a tempo for a LFO?

Hello, I’m trying to implement a tremolo plugin.

I use the LFO class from IPlug2 but I don’t understand how to assign a tempo.
The effect works when the rate is in Hz.
If the mode is switch to BPM, it seems to be fixed at 120 BPM.
The speed changes with the « kParamLFORateTempo ».

At the beginning of « ProcessBlock » of the LFO Class, I set the tempo variable at 190. or 40, but it doesn’t seem to change anything.

Lfo function :

 /* Block process function */
  void ProcessBlock(T* pOutput, int nFrames, double qnPos = 0., bool transportIsRunning = false, double tempo = 120.)
  {
    tempo = 200;
    T oneOverQNScalar = 1./mQNScalar;
    T phase = IOscillator<T>::mPhase;
    
    if (mRateMode == ERateMode::kBPM && !transportIsRunning) {
      IOscillator<T>::SetFreqCPS(tempo / 60.);
    }
    
    double samplesPerBeat = IOscillator<T>::mSampleRate * (60.0 / (tempo == 0.0 ? 1.0 : tempo)); // samples per beat
    
    T phaseIncr = IOscillator<T>::mPhaseIncr;

    for (int s=0; s<nFrames; s++)
    {
      double sampleAccurateQnPos = qnPos + ((double) s / samplesPerBeat);

      if(mRateMode == ERateMode::kBPM)
      {
        if(transportIsRunning)
          phase = std::fmod(sampleAccurateQnPos, oneOverQNScalar) / oneOverQNScalar;
        else
          phase = WrapPhase(phase + (phaseIncr * mQNScalar));
      }
      else
        phase = WrapPhase(phase + phaseIncr);
      
      pOutput[s] = DoProcess(phase);
    }
    
    IOscillator<T>::mPhase = phase;
  }

Maybe you could try to implement the tremolo’s LFO manually… it’s such a simple effect that I don’t see the point in using built in functions for this. A simple inline sine function should do the trick.

Yes I think it’s what I will do.
Thank you very much for the answer :grin:

1 Like

There is an example of how to use the LFO class in IPlugInstrument