130 Audio Effects: Theory, Implementation and Application
channelData[i] = in * (1.0f - depth_ *
lfo(ph, waveform_));
// Update the carrier and LFO phases, keeping them in
// the range 0-1
ph += frequency_*inverseSampleRate;
if(ph >= 1.0)
ph -= 1.0;
}
The code uses the variable ph to keep track of the current phase of the
LFO. The tremolo effect itself is simply a change in volume modulated by
the value of the LFO. This tremolo example includes a choice of LFO styles,
as implemented in the lfo() function.
float lfo(float phase, int waveform)
{
switch(waveform)
{
case kWaveformTriangle:
if(phase < 0.25f)
return 0.5f + 2.0f*phase;
else if(phase < 0.75f)
return 1.0f - 2.0f*(phase - 0.25f);
else
return 2.0f*(phase-0.75f);
case kWaveformSquare: