From 6fab49b1b71494f220c7a7b72752c8d9bd80580c Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 11 Dec 2013 12:02:22 -0800 Subject: [PATCH] change ring buffer to interleaved for stereo --- assignment-client/src/audio/AudioMixer.cpp | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index 81b7d5e614..cfa5855be4 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -162,33 +162,34 @@ void AudioMixer::addBufferToMixForListeningNodeWithBuffer(PositionalAudioRingBuf int16_t* sourceBuffer = bufferToAdd->getNextOutput(); - int16_t* goodChannel = (bearingRelativeAngleToSource > 0.0f) - ? _clientSamples - : _clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL; - int16_t* delayedChannel = (bearingRelativeAngleToSource > 0.0f) - ? _clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL - : _clientSamples; + // if the bearing relative angle to source is > 0 then the delayed channel is the right one + int delayedChannelOffset = (bearingRelativeAngleToSource > 0.0f) ? 1 : 0; + int goodChannelOffset = delayedChannelOffset == 0 ? 1 : 0; int16_t* delaySamplePointer = bufferToAdd->getNextOutput() == bufferToAdd->getBuffer() ? bufferToAdd->getBuffer() + RING_BUFFER_LENGTH_SAMPLES - 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) { // 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 - int16_t currentSample = sourceBuffer[s] * attenuationCoefficient; - goodChannel[s] = glm::clamp(goodChannel[s] + currentSample, MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE); + int16_t currentSample = sourceBuffer[s / 2] * attenuationCoefficient; + _clientSamples[s + goodChannelOffset] = glm::clamp(_clientSamples[s + goodChannelOffset] + currentSample, + MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE); if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { // place the curernt sample at the right spot in the delayed channel - int sumSample = delayedChannel[s + numSamplesDelay] + (currentSample * weakChannelAmplitudeRatio); - delayedChannel[s + numSamplesDelay] = glm::clamp(sumSample, MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE); + int16_t clampedSample = glm::clamp((int) (_clientSamples[s + numSamplesDelay + delayedChannelOffset] + + (currentSample * weakChannelAmplitudeRatio)), + MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE); + _clientSamples[s + numSamplesDelay + delayedChannelOffset] = clampedSample; } } }