From a06f18328ca60327d083fd9732a24f14d4439484 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Sun, 5 Jan 2014 17:40:37 -0800 Subject: [PATCH] add downsampling so RAW audio for sound assumed to be 48Khz --- libraries/audio/src/Sound.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/libraries/audio/src/Sound.cpp b/libraries/audio/src/Sound.cpp index 02f8aaef9c..36d2b9563a 100644 --- a/libraries/audio/src/Sound.cpp +++ b/libraries/audio/src/Sound.cpp @@ -27,5 +27,25 @@ Sound::Sound(const QUrl& sampleURL, QObject* parent) : void Sound::replyFinished(QNetworkReply* reply) { // replace our byte array with the downloaded data - _byteArray = reply->readAll(); + QByteArray rawAudioByteArray = reply->readAll(); + + // assume that this was a RAW file and is now an array of samples that are + // signed, 16-bit, 48Khz, mono + + // we want to convert it to the format that the audio-mixer wants + // which is signed, 16-bit, 24Khz, mono + + _byteArray.resize(rawAudioByteArray.size() / 2); + + int numSourceSamples = rawAudioByteArray.size() / sizeof(int16_t); + 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] = (rawAudioByteArray[i - 1] / 2) + (rawAudioByteArray[i] / 2); + } else { + destinationSamples[(i - 1) / 2] = (sourceSamples[i - 1] / 4) + (sourceSamples[i] / 2) + (sourceSamples[i + 1] / 4); + } + } } \ No newline at end of file