So far, we have only been playing one sound at a time, but it is quite easy to extend what we are doing to play multiple sounds at once. The act of combining multiple sounds into a single output is known as
audio mixing, and it can be implemented by adding all the audio signals together, and clamping the result to the available range. Looking at our WriteSoundData()
method, all we need to do is change the lines of code that write to the data array, so that the samples are added to the existing values, instead of completely replacing them:
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) { position = 0; } else { Stop(); return; } } float value = sound->data[(int)position] * volume; data[i] = (PCM16)(value * leftGain + data[i]); data[i+1] = (PCM16)(value * rightGain + data[i...