This source file includes following definitions.
- m_isBufferPrimed
- initializeKernel
- consumeSource
- m_sourceFramesAvailable
- provideInput
- process
- process
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "platform/audio/SincResampler.h"
#include "platform/audio/AudioBus.h"
#include "wtf/MathExtras.h"
#ifdef __SSE2__
#include <emmintrin.h>
#endif
using namespace std;
namespace WebCore {
SincResampler::SincResampler(double scaleFactor, unsigned kernelSize, unsigned numberOfKernelOffsets)
: m_scaleFactor(scaleFactor)
, m_kernelSize(kernelSize)
, m_numberOfKernelOffsets(numberOfKernelOffsets)
, m_kernelStorage(m_kernelSize * (m_numberOfKernelOffsets + 1))
, m_virtualSourceIndex(0)
, m_blockSize(512)
, m_inputBuffer(m_blockSize + m_kernelSize)
, m_source(0)
, m_sourceFramesAvailable(0)
, m_sourceProvider(0)
, m_isBufferPrimed(false)
{
initializeKernel();
}
void SincResampler::initializeKernel()
{
double alpha = 0.16;
double a0 = 0.5 * (1.0 - alpha);
double a1 = 0.5;
double a2 = 0.5 * alpha;
double sincScaleFactor = m_scaleFactor > 1.0 ? 1.0 / m_scaleFactor : 1.0;
sincScaleFactor *= 0.9;
int n = m_kernelSize;
int halfSize = n / 2;
for (unsigned offsetIndex = 0; offsetIndex <= m_numberOfKernelOffsets; ++offsetIndex) {
double subsampleOffset = static_cast<double>(offsetIndex) / m_numberOfKernelOffsets;
for (int i = 0; i < n; ++i) {
double s = sincScaleFactor * piDouble * (i - halfSize - subsampleOffset);
double sinc = !s ? 1.0 : sin(s) / s;
sinc *= sincScaleFactor;
double x = (i - subsampleOffset) / n;
double window = a0 - a1 * cos(twoPiDouble * x) + a2 * cos(twoPiDouble * 2.0 * x);
m_kernelStorage[i + offsetIndex * m_kernelSize] = sinc * window;
}
}
}
void SincResampler::consumeSource(float* buffer, unsigned numberOfSourceFrames)
{
ASSERT(m_sourceProvider);
if (!m_sourceProvider)
return;
RefPtr<AudioBus> bus = AudioBus::create(1, numberOfSourceFrames, false);
bus->setChannelMemory(0, buffer, numberOfSourceFrames);
m_sourceProvider->provideInput(bus.get(), numberOfSourceFrames);
}
namespace {
class BufferSourceProvider FINAL : public AudioSourceProvider {
public:
BufferSourceProvider(const float* source, size_t numberOfSourceFrames)
: m_source(source)
, m_sourceFramesAvailable(numberOfSourceFrames)
{
}
virtual void provideInput(AudioBus* bus, size_t framesToProcess) OVERRIDE
{
ASSERT(m_source && bus);
if (!m_source || !bus)
return;
float* buffer = bus->channel(0)->mutableData();
size_t framesToCopy = min(m_sourceFramesAvailable, framesToProcess);
memcpy(buffer, m_source, sizeof(float) * framesToCopy);
if (framesToCopy < framesToProcess)
memset(buffer + framesToCopy, 0, sizeof(float) * (framesToProcess - framesToCopy));
m_sourceFramesAvailable -= framesToCopy;
m_source += framesToCopy;
}
private:
const float* m_source;
size_t m_sourceFramesAvailable;
};
}
void SincResampler::process(const float* source, float* destination, unsigned numberOfSourceFrames)
{
BufferSourceProvider sourceProvider(source, numberOfSourceFrames);
unsigned numberOfDestinationFrames = static_cast<unsigned>(numberOfSourceFrames / m_scaleFactor);
unsigned remaining = numberOfDestinationFrames;
while (remaining) {
unsigned framesThisTime = min(remaining, m_blockSize);
process(&sourceProvider, destination, framesThisTime);
destination += framesThisTime;
remaining -= framesThisTime;
}
}
void SincResampler::process(AudioSourceProvider* sourceProvider, float* destination, size_t framesToProcess)
{
bool isGood = sourceProvider && m_blockSize > m_kernelSize && m_inputBuffer.size() >= m_blockSize + m_kernelSize && !(m_kernelSize % 2);
ASSERT(isGood);
if (!isGood)
return;
m_sourceProvider = sourceProvider;
unsigned numberOfDestinationFrames = framesToProcess;
float* r0 = m_inputBuffer.data() + m_kernelSize / 2;
float* r1 = m_inputBuffer.data();
float* r2 = r0;
float* r3 = r0 + m_blockSize - m_kernelSize / 2;
float* r4 = r0 + m_blockSize;
float* r5 = r0 + m_kernelSize / 2;
if (!m_isBufferPrimed) {
consumeSource(r0, m_blockSize + m_kernelSize / 2);
m_isBufferPrimed = true;
}
while (numberOfDestinationFrames) {
while (m_virtualSourceIndex < m_blockSize) {
int sourceIndexI = static_cast<int>(m_virtualSourceIndex);
double subsampleRemainder = m_virtualSourceIndex - sourceIndexI;
double virtualOffsetIndex = subsampleRemainder * m_numberOfKernelOffsets;
int offsetIndex = static_cast<int>(virtualOffsetIndex);
float* k1 = m_kernelStorage.data() + offsetIndex * m_kernelSize;
float* k2 = k1 + m_kernelSize;
float* inputP = r1 + sourceIndexI;
float sum1 = 0;
float sum2 = 0;
double kernelInterpolationFactor = virtualOffsetIndex - offsetIndex;
int n = m_kernelSize;
#define CONVOLVE_ONE_SAMPLE \
input = *inputP++; \
sum1 += input * *k1; \
sum2 += input * *k2; \
++k1; \
++k2;
{
float input;
#ifdef __SSE2__
while ((reinterpret_cast<uintptr_t>(inputP) & 0x0F) && n) {
CONVOLVE_ONE_SAMPLE
n--;
}
float* endP = inputP + n - n % 4;
__m128 mInput;
__m128 mK1;
__m128 mK2;
__m128 mul1;
__m128 mul2;
__m128 sums1 = _mm_setzero_ps();
__m128 sums2 = _mm_setzero_ps();
bool k1Aligned = !(reinterpret_cast<uintptr_t>(k1) & 0x0F);
bool k2Aligned = !(reinterpret_cast<uintptr_t>(k2) & 0x0F);
#define LOAD_DATA(l1, l2) \
mInput = _mm_load_ps(inputP); \
mK1 = _mm_##l1##_ps(k1); \
mK2 = _mm_##l2##_ps(k2);
#define CONVOLVE_4_SAMPLES \
mul1 = _mm_mul_ps(mInput, mK1); \
mul2 = _mm_mul_ps(mInput, mK2); \
sums1 = _mm_add_ps(sums1, mul1); \
sums2 = _mm_add_ps(sums2, mul2); \
inputP += 4; \
k1 += 4; \
k2 += 4;
if (k1Aligned && k2Aligned) {
while (inputP < endP) {
LOAD_DATA(load, load)
CONVOLVE_4_SAMPLES
}
} else if (!k1Aligned && k2Aligned) {
while (inputP < endP) {
LOAD_DATA(loadu, load)
CONVOLVE_4_SAMPLES
}
} else if (k1Aligned && !k2Aligned) {
while (inputP < endP) {
LOAD_DATA(load, loadu)
CONVOLVE_4_SAMPLES
}
} else {
while (inputP < endP) {
LOAD_DATA(loadu, loadu)
CONVOLVE_4_SAMPLES
}
}
float* groupSumP = reinterpret_cast<float*>(&sums1);
sum1 += groupSumP[0] + groupSumP[1] + groupSumP[2] + groupSumP[3];
groupSumP = reinterpret_cast<float*>(&sums2);
sum2 += groupSumP[0] + groupSumP[1] + groupSumP[2] + groupSumP[3];
n %= 4;
while (n) {
CONVOLVE_ONE_SAMPLE
n--;
}
#else
if (n == 32) {
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
} else if (n == 64) {
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
CON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CONVOLVE_ONE_SAMPLE
CONVOLVE_ONE_SAMPLE
} else {
while (n--) {
CONVOLVE_ONE_SAMPLE
}
}
#endif
}
double result = (1.0 - kernelInterpolationFactor) * sum1 + kernelInterpolationFactor * sum2;
*destination++ = result;
m_virtualSourceIndex += m_scaleFactor;
--numberOfDestinationFrames;
if (!numberOfDestinationFrames)
return;
}
m_virtualSourceIndex -= m_blockSize;
memcpy(r1, r3, sizeof(float) * (m_kernelSize / 2));
memcpy(r2, r4, sizeof(float) * (m_kernelSize / 2));
consumeSource(r5, m_blockSize);
}
}
}
#endif