add downsampling so RAW audio for sound assumed to be 48Khz

This commit is contained in:
Stephen Birarda 2014-01-05 17:40:37 -08:00
parent 5b272bfd6a
commit a06f18328c

View file

@ -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);
}
}
}