2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "platform/audio/UpSampler.h"
37 #include "wtf/MathExtras.h"
41 UpSampler::UpSampler(size_t inputBlockSize)
42 : m_inputBlockSize(inputBlockSize)
43 , m_kernel(DefaultKernelSize)
44 , m_convolver(inputBlockSize)
45 , m_tempBuffer(inputBlockSize)
46 , m_inputBuffer(inputBlockSize * 2)
51 void UpSampler::initializeKernel()
53 // Blackman window parameters.
55 double a0 = 0.5 * (1.0 - alpha);
57 double a2 = 0.5 * alpha;
59 int n = m_kernel.size();
61 double subsampleOffset = -0.5;
63 for (int i = 0; i < n; ++i) {
64 // Compute the sinc() with offset.
65 double s = piDouble * (i - halfSize - subsampleOffset);
66 double sinc = !s ? 1.0 : sin(s) / s;
68 // Compute Blackman window, matching the offset of the sinc().
69 double x = (i - subsampleOffset) / n;
70 double window = a0 - a1 * cos(twoPiDouble * x) + a2 * cos(twoPiDouble * 2.0 * x);
72 // Window the sinc() function.
73 m_kernel[i] = sinc * window;
77 void UpSampler::process(const float* sourceP, float* destP, size_t sourceFramesToProcess)
79 bool isInputBlockSizeGood = sourceFramesToProcess == m_inputBlockSize;
80 ASSERT(isInputBlockSizeGood);
81 if (!isInputBlockSizeGood)
84 bool isTempBufferGood = sourceFramesToProcess == m_tempBuffer.size();
85 ASSERT(isTempBufferGood);
86 if (!isTempBufferGood)
89 bool isKernelGood = m_kernel.size() == DefaultKernelSize;
94 size_t halfSize = m_kernel.size() / 2;
96 // Copy source samples to 2nd half of input buffer.
97 bool isInputBufferGood = m_inputBuffer.size() == sourceFramesToProcess * 2 && halfSize <= sourceFramesToProcess;
98 ASSERT(isInputBufferGood);
99 if (!isInputBufferGood)
102 float* inputP = m_inputBuffer.data() + sourceFramesToProcess;
103 memcpy(inputP, sourceP, sizeof(float) * sourceFramesToProcess);
105 // Copy even sample-frames 0,2,4,6... (delayed by the linear phase delay) directly into destP.
106 for (unsigned i = 0; i < sourceFramesToProcess; ++i)
107 destP[i * 2] = *((inputP - halfSize) + i);
109 // Compute odd sample-frames 1,3,5,7...
110 float* oddSamplesP = m_tempBuffer.data();
111 m_convolver.process(&m_kernel, sourceP, oddSamplesP, sourceFramesToProcess);
113 for (unsigned i = 0; i < sourceFramesToProcess; ++i)
114 destP[i * 2 + 1] = oddSamplesP[i];
116 // Copy 2nd half of input buffer to 1st half.
117 memcpy(m_inputBuffer.data(), inputP, sizeof(float) * sourceFramesToProcess);
120 void UpSampler::reset()
123 m_inputBuffer.zero();
126 size_t UpSampler::latencyFrames() const
128 // Divide by two since this is a linear phase kernel and the delay is at the center of the kernel.
129 return m_kernel.size() / 2;
134 #endif // ENABLE(WEB_AUDIO)