The next few features that we will implement involve modifying the values that are sent to the output. Changing the volume of a sound is probably the simplest of them, as it only requires a multiplication.
Let us start by creating a variable and some methods to control the volume. The volume will be stored as a floating point number between 0 (silence) and 1 (full volume). The SetVolume()
method makes sure that the value is always inside this range. We should also reset the volume to 1 whenever a sound begins playing:
public: float GetVolume() const { return volume; } void SetVolume(float value) { if(value < 0.0f) volume = 0.0f; else if(value > 1.0f) volume = 1.0f; else volume = value; } private: float volume;
In order to play the sound at this volume, all we have to do is multiply each of the original values in the audio data by the value of the volume variable, before we write them to the output. Because the volume variable is a floating point number...