Merge pull request #6961 from birarda/resampling-error

fix a couple of resampling errors in Sound downSample
This commit is contained in:
Ryan Huffman 2016-01-28 13:22:00 -08:00
commit 15db8040d8
3 changed files with 15 additions and 139 deletions

View file

@ -1,112 +0,0 @@
//
// AudioEditBuffer.h
// hifi
//
// Created by Craig Hansen-Sturm on 8/29/14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_AudioEditBuffer_h
#define hifi_AudioEditBuffer_h
template< typename T >
class AudioEditBuffer : public AudioFrameBuffer<T> {
public:
AudioEditBuffer();
AudioEditBuffer(const uint32_t channelCount, const uint32_t frameCount);
~AudioEditBuffer();
bool getZeroCrossing(uint32_t start, bool direction, float32_t epsilon, uint32_t& zero);
void linearFade(uint32_t start, uint32_t stop, bool increasing);
void exponentialFade(uint32_t start, uint32_t stop, bool increasing);
};
template< typename T >
AudioEditBuffer<T>::AudioEditBuffer() :
AudioFrameBuffer<T>() {
}
template< typename T >
AudioEditBuffer<T>::AudioEditBuffer(const uint32_t channelCount, const uint32_t frameCount) :
AudioFrameBuffer<T>(channelCount, frameCount) {
}
template< typename T >
AudioEditBuffer<T>::~AudioEditBuffer() {
}
template< typename T >
inline bool AudioEditBuffer<T>::getZeroCrossing(uint32_t start, bool direction, float32_t epsilon, uint32_t& zero) {
zero = this->_frameCount;
if (direction) { // scan from the left
if (start < this->_frameCount) {
for (uint32_t i = start; i < this->_frameCount; ++i) {
for (uint32_t j = 0; j < this->_channelCount; ++j) {
if (this->_frameBuffer[j][i] >= -epsilon && this->_frameBuffer[j][i] <= epsilon) {
zero = i;
return true;
}
}
}
}
} else { // scan from the right
if (start != 0 && start < this->_frameCount) {
for (uint32_t i = start; i != 0; --i) {
for (uint32_t j = 0; j < this->_channelCount; ++j) {
if (this->_frameBuffer[j][i] >= -epsilon && this->_frameBuffer[j][i] <= epsilon) {
zero = i;
return true;
}
}
}
}
}
return false;
}
template< typename T >
inline void AudioEditBuffer<T>::linearFade(uint32_t start, uint32_t stop, bool increasing) {
if (start >= stop || start > this->_frameCount || stop > this->_frameCount ) {
return;
}
uint32_t count = stop - start;
float32_t delta;
float32_t gain;
if (increasing) { // 0.0 to 1.0f in delta increments
delta = 1.0f / (float32_t)count;
gain = 0.0f;
} else { // 1.0f to 0.0f in delta increments
delta = -1.0f / (float32_t)count;
gain = 1.0f;
}
for (uint32_t i = start; i < stop; ++i) {
for (uint32_t j = 0; j < this->_channelCount; ++j) {
this->_frameBuffer[j][i] *= gain;
}
gain += delta;
}
}
template< typename T >
inline void AudioEditBuffer<T>::exponentialFade(uint32_t start, uint32_t stop, bool increasing) {
// TBD
}
typedef AudioEditBuffer< float32_t > AudioEditBufferFloat32;
typedef AudioEditBuffer< int32_t > AudioEditBufferSInt32;
#endif // hifi_AudioEditBuffer_h

View file

@ -26,7 +26,6 @@
#include "AudioRingBuffer.h"
#include "AudioFormat.h"
#include "AudioBuffer.h"
#include "AudioEditBuffer.h"
#include "AudioLogging.h"
#include "Sound.h"
@ -69,7 +68,6 @@ void Sound::downloadFinished(const QByteArray& data) {
interpretAsWav(rawAudioByteArray, outputAudioByteArray);
downSample(outputAudioByteArray);
trimFrames();
} else if (fileName.endsWith(RAW_EXTENSION)) {
// check if this was a stereo raw file
// since it's raw the only way for us to know that is if the file was called .stereo.raw
@ -80,7 +78,6 @@ void Sound::downloadFinished(const QByteArray& data) {
// Process as RAW file
downSample(rawAudioByteArray);
trimFrames();
} else {
qCDebug(audio) << "Unknown sound file type";
}
@ -98,11 +95,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();
@ -129,26 +138,6 @@ void Sound::downSample(const QByteArray& rawAudioByteArray) {
}
}
void Sound::trimFrames() {
const uint32_t inputFrameCount = _byteArray.size() / sizeof(int16_t);
const uint32_t trimCount = 1024; // number of leading and trailing frames to trim
if (inputFrameCount <= (2 * trimCount)) {
return;
}
int16_t* inputFrameData = (int16_t*)_byteArray.data();
AudioEditBufferFloat32 editBuffer(1, inputFrameCount);
editBuffer.copyFrames(1, inputFrameCount, inputFrameData, false /*copy in*/);
editBuffer.linearFade(0, trimCount, true);
editBuffer.linearFade(inputFrameCount - trimCount, inputFrameCount, false);
editBuffer.copyFrames(1, inputFrameCount, inputFrameData, true /*copy out*/);
}
//
// Format description from https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
//

View file

@ -38,7 +38,6 @@ private:
bool _isStereo;
bool _isReady;
void trimFrames();
void downSample(const QByteArray& rawAudioByteArray);
void interpretAsWav(const QByteArray& inputAudioByteArray, QByteArray& outputAudioByteArray);