This source file includes following definitions.
- m_inputBuffer
- initializeKernel
- process
- reset
- latencyFrames
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "platform/audio/UpSampler.h"
#include "wtf/MathExtras.h"
namespace WebCore {
UpSampler::UpSampler(size_t inputBlockSize)
: m_inputBlockSize(inputBlockSize)
, m_kernel(DefaultKernelSize)
, m_convolver(inputBlockSize)
, m_tempBuffer(inputBlockSize)
, m_inputBuffer(inputBlockSize * 2)
{
initializeKernel();
}
void UpSampler::initializeKernel()
{
double alpha = 0.16;
double a0 = 0.5 * (1.0 - alpha);
double a1 = 0.5;
double a2 = 0.5 * alpha;
int n = m_kernel.size();
int halfSize = n / 2;
double subsampleOffset = -0.5;
for (int i = 0; i < n; ++i) {
double s = piDouble * (i - halfSize - subsampleOffset);
double sinc = !s ? 1.0 : sin(s) / s;
double x = (i - subsampleOffset) / n;
double window = a0 - a1 * cos(twoPiDouble * x) + a2 * cos(twoPiDouble * 2.0 * x);
m_kernel[i] = sinc * window;
}
}
void UpSampler::process(const float* sourceP, float* destP, size_t sourceFramesToProcess)
{
bool isInputBlockSizeGood = sourceFramesToProcess == m_inputBlockSize;
ASSERT(isInputBlockSizeGood);
if (!isInputBlockSizeGood)
return;
bool isTempBufferGood = sourceFramesToProcess == m_tempBuffer.size();
ASSERT(isTempBufferGood);
if (!isTempBufferGood)
return;
bool isKernelGood = m_kernel.size() == DefaultKernelSize;
ASSERT(isKernelGood);
if (!isKernelGood)
return;
size_t halfSize = m_kernel.size() / 2;
bool isInputBufferGood = m_inputBuffer.size() == sourceFramesToProcess * 2 && halfSize <= sourceFramesToProcess;
ASSERT(isInputBufferGood);
if (!isInputBufferGood)
return;
float* inputP = m_inputBuffer.data() + sourceFramesToProcess;
memcpy(inputP, sourceP, sizeof(float) * sourceFramesToProcess);
for (unsigned i = 0; i < sourceFramesToProcess; ++i)
destP[i * 2] = *((inputP - halfSize) + i);
float* oddSamplesP = m_tempBuffer.data();
m_convolver.process(&m_kernel, sourceP, oddSamplesP, sourceFramesToProcess);
for (unsigned i = 0; i < sourceFramesToProcess; ++i)
destP[i * 2 + 1] = oddSamplesP[i];
memcpy(m_inputBuffer.data(), inputP, sizeof(float) * sourceFramesToProcess);
}
void UpSampler::reset()
{
m_convolver.reset();
m_inputBuffer.zero();
}
size_t UpSampler::latencyFrames() const
{
return m_kernel.size() / 2;
}
}
#endif