There are many different approaches to implement stereo panning of a sound. In this section, we will cover a simple approach that works just by modifying the volumes of the left and right channels independently.
Before actually doing any calculations, let us prepare the class for panning by adding two private variables, leftGain
and rightGain
, to store the volumes of each channel:
private: float leftGain; float rightGain;
Then, inside the WriteSoundData()
method, we can apply these gains to the data before writing it to the output, just as we did for the volume before. Naturally, we should only apply the values of leftGain
and rightGain
to their respective channels. In addition, because we need to cast to PCM16 after applying the gains, there is no need to keep the cast from earlier:
void MyChannel::WriteSoundData(PCM16* data, int count) { if(sound == 0 || paused) return; for (int i = 0; i < count; i += 2) { if(position >= sound->count) { if(loop)...