From dc716461e3be559f23d2a6b253f407eda6d7bd49 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 27 Jan 2016 14:31:47 -0800 Subject: [PATCH] fix for a couple of resampling edge cases --- libraries/audio/src/Sound.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/libraries/audio/src/Sound.cpp b/libraries/audio/src/Sound.cpp index 12f63e0a12..bbd6af5465 100644 --- a/libraries/audio/src/Sound.cpp +++ b/libraries/audio/src/Sound.cpp @@ -98,11 +98,23 @@ void Sound::downSample(const QByteArray& rawAudioByteArray) { int numSourceSamples = rawAudioByteArray.size() / sizeof(AudioConstants::AudioSample); - int numDestinationBytes = rawAudioByteArray.size() / sizeof(AudioConstants::AudioSample); - if (_isStereo && numSourceSamples % 2 != 0) { - numDestinationBytes += sizeof(AudioConstants::AudioSample); + if (_isStereo && numSourceSamples % 2 != 0){ + // in the unlikely case that we have stereo audio but we seem to be missing a sample + // (the sample for one channel is missing in a set of interleaved samples) + // then drop the odd sample + --numSourceSamples; } + int numDestinationSamples = numSourceSamples / 2.0f; + + if (_isStereo && numDestinationSamples % 2 != 0) { + // if this is stereo we need to make sure we produce stereo output + // which means we should have an even number of output samples + numDestinationSamples += 1; + } + + int numDestinationBytes = numDestinationSamples * sizeof(AudioConstants::AudioSample); + _byteArray.resize(numDestinationBytes); int16_t* sourceSamples = (int16_t*) rawAudioByteArray.data();