This source file includes following definitions.
- m_fillIndex
- getSourcePointer
- process
- reset
- rate
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "platform/audio/AudioResamplerKernel.h"
#include <algorithm>
#include "platform/audio/AudioResampler.h"
using namespace std;
namespace WebCore {
const size_t AudioResamplerKernel::MaxFramesToProcess = 128;
AudioResamplerKernel::AudioResamplerKernel(AudioResampler* resampler)
: m_resampler(resampler)
, m_sourceBuffer(2 + static_cast<int>(MaxFramesToProcess * AudioResampler::MaxRate))
, m_virtualReadIndex(0.0)
, m_fillIndex(0)
{
m_lastValues[0] = 0.0f;
m_lastValues[1] = 0.0f;
}
float* AudioResamplerKernel::getSourcePointer(size_t framesToProcess, size_t* numberOfSourceFramesNeededP)
{
ASSERT(framesToProcess <= MaxFramesToProcess);
double nextFractionalIndex = m_virtualReadIndex + framesToProcess * rate();
int endIndex = static_cast<int>(nextFractionalIndex + 1.0);
size_t framesNeeded = 1 + endIndex - m_fillIndex;
if (numberOfSourceFramesNeededP)
*numberOfSourceFramesNeededP = framesNeeded;
bool isGood = m_fillIndex < m_sourceBuffer.size() && m_fillIndex + framesNeeded <= m_sourceBuffer.size();
ASSERT(isGood);
if (!isGood)
return 0;
return m_sourceBuffer.data() + m_fillIndex;
}
void AudioResamplerKernel::process(float* destination, size_t framesToProcess)
{
ASSERT(framesToProcess <= MaxFramesToProcess);
float* source = m_sourceBuffer.data();
double rate = this->rate();
rate = max(0.0, rate);
rate = min(AudioResampler::MaxRate, rate);
if (m_fillIndex > 0) {
source[0] = m_lastValues[0];
source[1] = m_lastValues[1];
}
double virtualReadIndex = m_virtualReadIndex;
ASSERT(framesToProcess > 0);
ASSERT(virtualReadIndex >= 0 && 1 + static_cast<unsigned>(virtualReadIndex + (framesToProcess - 1) * rate) < m_sourceBuffer.size());
int n = framesToProcess;
while (n--) {
unsigned readIndex = static_cast<unsigned>(virtualReadIndex);
double interpolationFactor = virtualReadIndex - readIndex;
double sample1 = source[readIndex];
double sample2 = source[readIndex + 1];
double sample = (1.0 - interpolationFactor) * sample1 + interpolationFactor * sample2;
*destination++ = static_cast<float>(sample);
virtualReadIndex += rate;
}
int readIndex = static_cast<int>(virtualReadIndex);
m_lastValues[0] = source[readIndex];
m_lastValues[1] = source[readIndex + 1];
m_fillIndex = 2;
virtualReadIndex -= readIndex;
m_virtualReadIndex = virtualReadIndex;
}
void AudioResamplerKernel::reset()
{
m_virtualReadIndex = 0.0;
m_fillIndex = 0;
m_lastValues[0] = 0.0f;
m_lastValues[1] = 0.0f;
}
double AudioResamplerKernel::rate() const
{
return m_resampler->rate();
}
}
#endif