change ring buffer to interleaved for stereo

This commit is contained in:
Stephen Birarda 2013-12-11 12:02:22 -08:00
parent af2fa15f55
commit 6fab49b1b7

View file

@ -162,33 +162,34 @@ void AudioMixer::addBufferToMixForListeningNodeWithBuffer(PositionalAudioRingBuf
int16_t* sourceBuffer = bufferToAdd->getNextOutput(); int16_t* sourceBuffer = bufferToAdd->getNextOutput();
int16_t* goodChannel = (bearingRelativeAngleToSource > 0.0f) // if the bearing relative angle to source is > 0 then the delayed channel is the right one
? _clientSamples int delayedChannelOffset = (bearingRelativeAngleToSource > 0.0f) ? 1 : 0;
: _clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL; int goodChannelOffset = delayedChannelOffset == 0 ? 1 : 0;
int16_t* delayedChannel = (bearingRelativeAngleToSource > 0.0f)
? _clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL
: _clientSamples;
int16_t* delaySamplePointer = bufferToAdd->getNextOutput() == bufferToAdd->getBuffer() int16_t* delaySamplePointer = bufferToAdd->getNextOutput() == bufferToAdd->getBuffer()
? bufferToAdd->getBuffer() + RING_BUFFER_LENGTH_SAMPLES - numSamplesDelay ? bufferToAdd->getBuffer() + RING_BUFFER_LENGTH_SAMPLES - numSamplesDelay
: bufferToAdd->getNextOutput() - numSamplesDelay; : bufferToAdd->getNextOutput() - numSamplesDelay;
for (int s = 0; s < BUFFER_LENGTH_SAMPLES_PER_CHANNEL; s++) { for (int s = 0; s < BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2; s += 2) {
if (s < numSamplesDelay) { if (s < numSamplesDelay) {
// pull the earlier sample for the delayed channel // pull the earlier sample for the delayed channel
int earlierSample = delaySamplePointer[s] * attenuationCoefficient * weakChannelAmplitudeRatio; int earlierSample = delaySamplePointer[s / 2] * attenuationCoefficient * weakChannelAmplitudeRatio;
delayedChannel[s] = glm::clamp(delayedChannel[s] + earlierSample, MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE); _clientSamples[s + delayedChannelOffset] = glm::clamp(_clientSamples[s + delayedChannelOffset] + earlierSample,
MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);
} }
// pull the current sample for the good channel // pull the current sample for the good channel
int16_t currentSample = sourceBuffer[s] * attenuationCoefficient; int16_t currentSample = sourceBuffer[s / 2] * attenuationCoefficient;
goodChannel[s] = glm::clamp(goodChannel[s] + currentSample, MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE); _clientSamples[s + goodChannelOffset] = glm::clamp(_clientSamples[s + goodChannelOffset] + currentSample,
MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);
if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) {
// place the curernt sample at the right spot in the delayed channel // place the curernt sample at the right spot in the delayed channel
int sumSample = delayedChannel[s + numSamplesDelay] + (currentSample * weakChannelAmplitudeRatio); int16_t clampedSample = glm::clamp((int) (_clientSamples[s + numSamplesDelay + delayedChannelOffset]
delayedChannel[s + numSamplesDelay] = glm::clamp(sumSample, MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE); + (currentSample * weakChannelAmplitudeRatio)),
MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);
_clientSamples[s + numSamplesDelay + delayedChannelOffset] = clampedSample;
} }
} }
} }