From 453869c8e99686f8723c38e80f1ff70a2f5467c6 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 25 Sep 2014 18:11:48 -0700 Subject: [PATCH] Stereo flag + specific resample in Sound class --- .../audio/src/AudioScriptingInterface.cpp | 3 +++ libraries/audio/src/Sound.cpp | 25 +++++++++++++------ libraries/audio/src/Sound.h | 4 ++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/libraries/audio/src/AudioScriptingInterface.cpp b/libraries/audio/src/AudioScriptingInterface.cpp index fa0d3a9565..43c7d35c1d 100644 --- a/libraries/audio/src/AudioScriptingInterface.cpp +++ b/libraries/audio/src/AudioScriptingInterface.cpp @@ -13,6 +13,9 @@ AudioInjector* AudioScriptingInterface::playSound(Sound* sound, const AudioInjectorOptions* injectorOptions) { + if (sound->isStereo()) { + const_cast(injectorOptions)->setIsStereo(true); + } AudioInjector* injector = new AudioInjector(sound, *injectorOptions); QThread* injectorThread = new QThread(); diff --git a/libraries/audio/src/Sound.cpp b/libraries/audio/src/Sound.cpp index 9edb04aa2c..3ac433ca31 100644 --- a/libraries/audio/src/Sound.cpp +++ b/libraries/audio/src/Sound.cpp @@ -31,7 +31,8 @@ // procedural audio version of Sound Sound::Sound(float volume, float frequency, float duration, float decay, QObject* parent) : - QObject(parent) + QObject(parent), + _isStereo(false) { static char monoAudioData[MAX_PACKET_SIZE]; static int16_t* monoAudioSamples = (int16_t*)(monoAudioData); @@ -69,8 +70,9 @@ Sound::Sound(float volume, float frequency, float duration, float decay, QObject } } -Sound::Sound(const QUrl& sampleURL, QObject* parent) : +Sound::Sound(const QUrl& sampleURL, bool isStereo, QObject* parent) : QObject(parent), + _isStereo(isStereo), _hasDownloaded(false) { // assume we have a QApplication or QCoreApplication instance and use the @@ -88,6 +90,7 @@ Sound::Sound(const QUrl& sampleURL, QObject* parent) : Sound::Sound(const QByteArray byteArray, QObject* parent) : QObject(parent), _byteArray(byteArray), + _isStereo(false), _hasDownloaded(true) { } @@ -149,11 +152,19 @@ void Sound::downSample(const QByteArray& rawAudioByteArray) { int16_t* sourceSamples = (int16_t*) rawAudioByteArray.data(); int16_t* destinationSamples = (int16_t*) _byteArray.data(); - for (int i = 1; i < numSourceSamples; i += 2) { - if (i + 1 >= numSourceSamples) { - destinationSamples[(i - 1) / 2] = (sourceSamples[i - 1] / 2) + (sourceSamples[i] / 2); - } else { - destinationSamples[(i - 1) / 2] = (sourceSamples[i - 1] / 4) + (sourceSamples[i] / 2) + (sourceSamples[i + 1] / 4); + + if (_isStereo) { + for (int i = 0; i < numSourceSamples; i += 4) { + destinationSamples[i / 2] = (sourceSamples[i] / 2) + (sourceSamples[i + 2] / 2); + destinationSamples[(i / 2) + 1] = (sourceSamples[i + 1] / 2) + (sourceSamples[i + 3] / 2); + } + } else { + for (int i = 1; i < numSourceSamples; i += 2) { + if (i + 1 >= numSourceSamples) { + destinationSamples[(i - 1) / 2] = (sourceSamples[i - 1] / 2) + (sourceSamples[i] / 2); + } else { + destinationSamples[(i - 1) / 2] = (sourceSamples[i - 1] / 4) + (sourceSamples[i] / 2) + (sourceSamples[i + 1] / 4); + } } } } diff --git a/libraries/audio/src/Sound.h b/libraries/audio/src/Sound.h index fa2dd97903..b8fdc6b458 100644 --- a/libraries/audio/src/Sound.h +++ b/libraries/audio/src/Sound.h @@ -20,17 +20,19 @@ class Sound : public QObject { Q_PROPERTY(bool downloaded READ hasDownloaded) public: - Sound(const QUrl& sampleURL, QObject* parent = NULL); + Sound(const QUrl& sampleURL, bool isStereo = false, QObject* parent = NULL); Sound(float volume, float frequency, float duration, float decay, QObject* parent = NULL); Sound(const QByteArray byteArray, QObject* parent = NULL); void append(const QByteArray byteArray); + bool isStereo() const { return _isStereo; } bool hasDownloaded() const { return _hasDownloaded; } const QByteArray& getByteArray() { return _byteArray; } private: QByteArray _byteArray; + bool _isStereo; bool _hasDownloaded; void trimFrames();