Before we end this chapter, it is also worth realizing that not every sound needs to come from an audio file. It is also possible to generate sounds from scratch, using only mathematical formulas. We call this process, sound synthesis, and there are entire books just about this subject.
Certain sound waves are particularly common in sound synthesis because of how easy they are to calculate. We have already talked about one of these sound waves before, the sine wave. Other common examples are the square wave, the sawtooth wave, and the triangle wave, all represented in the following figure:

We will now see how to synthesize each of these sound waves, by creating a class MyOscillator
. The use case for this class is pretty much the same as the MyDelay
class described earlier; just create an instance of it, and call the WriteSoundData()
method from the audio callback to make it play:
#include <math.h> #define PI 3.14159265359 #define TWO_PI 6.28318530718 class MyOscillator...