15267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)/* 25267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved. 35267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * 45267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without 55267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * modification, are permitted provided that the following conditions are 65267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * met: 75267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * 85267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * * Redistributions of source code must retain the above copyright 95267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * notice, this list of conditions and the following disclaimer. 105267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * * Redistributions in binary form must reproduce the above 115267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer 125267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * in the documentation and/or other materials provided with the 135267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * distribution. 145267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * * Neither the name of Google Inc. nor the names of its 155267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * contributors may be used to endorse or promote products derived from 165267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * this software without specific prior written permission. 175267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * 185267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 195267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 205267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 215267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 225267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 235267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 245267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 255267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 265267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 275267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 285267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 295267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) */ 305267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 315267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "config.h" 325267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 335267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#if ENABLE(WEB_AUDIO) 345267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 351e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "platform/audio/UpSampler.h" 365267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 375267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "wtf/MathExtras.h" 385267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 39c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink { 405267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 415267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)UpSampler::UpSampler(size_t inputBlockSize) 425267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) : m_inputBlockSize(inputBlockSize) 435267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) , m_kernel(DefaultKernelSize) 44f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) , m_convolver(inputBlockSize) 455267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) , m_tempBuffer(inputBlockSize) 465267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) , m_inputBuffer(inputBlockSize * 2) 475267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){ 485267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) initializeKernel(); 495267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)} 505267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 515267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)void UpSampler::initializeKernel() 525267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){ 535267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) // Blackman window parameters. 545267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) double alpha = 0.16; 555267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) double a0 = 0.5 * (1.0 - alpha); 565267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) double a1 = 0.5; 575267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) double a2 = 0.5 * alpha; 585267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 595267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) int n = m_kernel.size(); 605267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) int halfSize = n / 2; 615267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) double subsampleOffset = -0.5; 625267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 635267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) for (int i = 0; i < n; ++i) { 645267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) // Compute the sinc() with offset. 655267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) double s = piDouble * (i - halfSize - subsampleOffset); 665267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) double sinc = !s ? 1.0 : sin(s) / s; 675267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 685267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) // Compute Blackman window, matching the offset of the sinc(). 695267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) double x = (i - subsampleOffset) / n; 70d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) double window = a0 - a1 * cos(twoPiDouble * x) + a2 * cos(twoPiDouble * 2.0 * x); 715267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 725267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) // Window the sinc() function. 735267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) m_kernel[i] = sinc * window; 745267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) } 755267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)} 765267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 775267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)void UpSampler::process(const float* sourceP, float* destP, size_t sourceFramesToProcess) 785267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){ 795267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) bool isInputBlockSizeGood = sourceFramesToProcess == m_inputBlockSize; 805267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) ASSERT(isInputBlockSizeGood); 815267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) if (!isInputBlockSizeGood) 825267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) return; 835267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 845267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) bool isTempBufferGood = sourceFramesToProcess == m_tempBuffer.size(); 855267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) ASSERT(isTempBufferGood); 865267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) if (!isTempBufferGood) 875267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) return; 885267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 895267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) bool isKernelGood = m_kernel.size() == DefaultKernelSize; 905267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) ASSERT(isKernelGood); 915267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) if (!isKernelGood) 925267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) return; 935267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 945267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) size_t halfSize = m_kernel.size() / 2; 955267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 965267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) // Copy source samples to 2nd half of input buffer. 975267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) bool isInputBufferGood = m_inputBuffer.size() == sourceFramesToProcess * 2 && halfSize <= sourceFramesToProcess; 985267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) ASSERT(isInputBufferGood); 995267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) if (!isInputBufferGood) 1005267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) return; 1015267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 1025267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) float* inputP = m_inputBuffer.data() + sourceFramesToProcess; 1035267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) memcpy(inputP, sourceP, sizeof(float) * sourceFramesToProcess); 1045267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 1055267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) // Copy even sample-frames 0,2,4,6... (delayed by the linear phase delay) directly into destP. 1065267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) for (unsigned i = 0; i < sourceFramesToProcess; ++i) 1075267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) destP[i * 2] = *((inputP - halfSize) + i); 1085267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 1095267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) // Compute odd sample-frames 1,3,5,7... 1105267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) float* oddSamplesP = m_tempBuffer.data(); 1115267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) m_convolver.process(&m_kernel, sourceP, oddSamplesP, sourceFramesToProcess); 1125267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 1135267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) for (unsigned i = 0; i < sourceFramesToProcess; ++i) 1145267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) destP[i * 2 + 1] = oddSamplesP[i]; 1155267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 1165267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) // Copy 2nd half of input buffer to 1st half. 1175267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) memcpy(m_inputBuffer.data(), inputP, sizeof(float) * sourceFramesToProcess); 1185267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)} 1195267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 1205267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)void UpSampler::reset() 1215267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){ 1225267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) m_convolver.reset(); 1235267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) m_inputBuffer.zero(); 1245267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)} 1255267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 1265267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)size_t UpSampler::latencyFrames() const 1275267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){ 1285267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) // Divide by two since this is a linear phase kernel and the delay is at the center of the kernel. 1295267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) return m_kernel.size() / 2; 1305267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)} 1315267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 132c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink 1335267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 1345267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#endif // ENABLE(WEB_AUDIO) 135