fix resampled stereo audio array size

This commit is contained in:
Stephen Birarda 2015-04-02 10:41:52 -07:00
parent 01dc1b058c
commit 164c06a190
2 changed files with 6 additions and 6 deletions

View file

@ -59,6 +59,8 @@ qint64 AudioInjectorLocalBuffer::readData(char* data, qint64 maxSize) {
if (!_shouldLoop && bytesRead == bytesToEnd) { if (!_shouldLoop && bytesRead == bytesToEnd) {
// we hit the end of the buffer, emit a signal // we hit the end of the buffer, emit a signal
emit bufferEmpty(); emit bufferEmpty();
} else if (_shouldLoop && _currentOffset == _rawAudioArray.size()) {
_currentOffset = 0;
} }
return bytesRead; return bytesRead;
@ -80,7 +82,7 @@ qint64 AudioInjectorLocalBuffer::recursiveReadFromFront(char* data, qint64 maxSi
// check if we need to call ourselves again and pull from the front again // check if we need to call ourselves again and pull from the front again
if (bytesRead < maxSize) { if (bytesRead < maxSize) {
return bytesRead + recursiveReadFromFront(data, maxSize); return bytesRead + recursiveReadFromFront(data, maxSize - bytesRead);
} else { } else {
_currentOffset = bytesRead; _currentOffset = bytesRead;
return bytesRead; return bytesRead;

View file

@ -77,7 +77,7 @@ void Sound::downloadFinished(QNetworkReply* reply) {
// since it's raw the only way for us to know that is if the file was called .stereo.raw // since it's raw the only way for us to know that is if the file was called .stereo.raw
if (reply->url().fileName().toLower().endsWith("stereo.raw")) { if (reply->url().fileName().toLower().endsWith("stereo.raw")) {
_isStereo = true; _isStereo = true;
qDebug() << "Processing sound from" << reply->url() << "as stereo audio file."; qDebug() << "Processing sound of" << rawAudioByteArray.size() << "bytes from" << reply->url() << "as stereo audio file.";
} }
// Process as RAW file // Process as RAW file
@ -102,13 +102,12 @@ void Sound::downSample(const QByteArray& rawAudioByteArray) {
int numSourceSamples = rawAudioByteArray.size() / sizeof(int16_t); int numSourceSamples = rawAudioByteArray.size() / sizeof(int16_t);
int numDestinationBytes = rawAudioByteArray.size() / 2; int numDestinationBytes = rawAudioByteArray.size() / 2;
if (_isStereo && numSourceSamples % 4 != 0) { if (_isStereo && numSourceSamples % 2 != 0) {
numDestinationBytes += 1; numDestinationBytes += sizeof(int16_t);
} }
_byteArray.resize(numDestinationBytes); _byteArray.resize(numDestinationBytes);
int16_t* sourceSamples = (int16_t*) rawAudioByteArray.data(); int16_t* sourceSamples = (int16_t*) rawAudioByteArray.data();
int16_t* destinationSamples = (int16_t*) _byteArray.data(); int16_t* destinationSamples = (int16_t*) _byteArray.data();
@ -121,7 +120,6 @@ void Sound::downSample(const QByteArray& rawAudioByteArray) {
destinationSamples[i / 2] = (sourceSamples[i] + sourceSamples[i + 2]) / 2; destinationSamples[i / 2] = (sourceSamples[i] + sourceSamples[i + 2]) / 2;
destinationSamples[(i / 2) + 1] = (sourceSamples[i + 1] + sourceSamples[i + 3]) / 2; destinationSamples[(i / 2) + 1] = (sourceSamples[i + 1] + sourceSamples[i + 3]) / 2;
} }
} }
} else { } else {
for (int i = 1; i < numSourceSamples; i += 2) { for (int i = 1; i < numSourceSamples; i += 2) {