Generating sound with frequency modulation
In this recipe, we will learn how to modulate a sine wave oscillator using another low frequency sine wave.
We will be basing this recipe on the previous recipe, where the y
position of the mouse controlled the frequency of the sine wave; in this recipe, we will use the x
position of the mouse to control the modulation frequency.
Getting ready
We will be using the code from the previous recipe, Generating a sine oscillator.
How to do it…
We will multiply the sine wave created in the previous recipe with another low frequency sine wave.
Add the following member variables:
float mModFrequency; float mModPhase, mModPhaseAdd;
Add the following in the
setup
module to initialize the variables created previously:mModFrequency = 0.0f; mModPhase = 0.0f; mModPhaseAdd = 0.0f;
In the
update
module, add the following code to calculate the modulation frequency based on thex
position of the mouse cursor:float maxModFrequency= 30.0f; float targetModFrequency= ( getMousePos...