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/DownSampler.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)DownSampler::DownSampler(size_t inputBlockSize)
425267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    : m_inputBlockSize(inputBlockSize)
435267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    , m_reducedKernel(DefaultKernelSize / 2)
44f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    , m_convolver(inputBlockSize / 2) // runs at 1/2 source sample-rate
455267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    , m_tempBuffer(inputBlockSize / 2)
465267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    , m_inputBuffer(inputBlockSize * 2)
475267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){
485267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    initializeKernel();
495267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)}
505267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
515267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)void DownSampler::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 = DefaultKernelSize;
605267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    int halfSize = n / 2;
615267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
625267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // Half-band filter.
635267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    double sincScaleFactor = 0.5;
645267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
655267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // Compute only the odd terms because the even ones are zero, except
665267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // right in the middle at halfSize, which is 0.5 and we'll handle specially during processing
675267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // after doing the main convolution using m_reducedKernel.
685267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    for (int i = 1; i < n; i += 2) {
695267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        // Compute the sinc() with offset.
705267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        double s = sincScaleFactor * piDouble * (i - halfSize);
715267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        double sinc = !s ? 1.0 : sin(s) / s;
725267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        sinc *= sincScaleFactor;
735267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
745267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        // Compute Blackman window, matching the offset of the sinc().
755267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        double x = static_cast<double>(i) / n;
76d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        double window = a0 - a1 * cos(twoPiDouble * x) + a2 * cos(twoPiDouble * 2.0 * x);
775267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
785267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        // Window the sinc() function.
795267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        // Then store only the odd terms in the kernel.
805267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        // In a sense, this is shifting forward in time by one sample-frame at the destination sample-rate.
815267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        m_reducedKernel[(i - 1) / 2] = sinc * window;
825267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    }
835267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)}
845267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
855267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)void DownSampler::process(const float* sourceP, float* destP, size_t sourceFramesToProcess)
865267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){
875267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    bool isInputBlockSizeGood = sourceFramesToProcess == m_inputBlockSize;
885267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    ASSERT(isInputBlockSizeGood);
895267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    if (!isInputBlockSizeGood)
905267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        return;
915267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
925267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    size_t destFramesToProcess = sourceFramesToProcess / 2;
935267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
945267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    bool isTempBufferGood = destFramesToProcess == m_tempBuffer.size();
955267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    ASSERT(isTempBufferGood);
965267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    if (!isTempBufferGood)
975267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        return;
985267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
995267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    bool isReducedKernelGood = m_reducedKernel.size() == DefaultKernelSize / 2;
1005267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    ASSERT(isReducedKernelGood);
1015267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    if (!isReducedKernelGood)
1025267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        return;
1035267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1045267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    size_t halfSize = DefaultKernelSize / 2;
1055267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1065267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // Copy source samples to 2nd half of input buffer.
1075267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    bool isInputBufferGood = m_inputBuffer.size() == sourceFramesToProcess * 2 && halfSize <= sourceFramesToProcess;
1085267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    ASSERT(isInputBufferGood);
1095267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    if (!isInputBufferGood)
1105267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        return;
1115267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1125267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    float* inputP = m_inputBuffer.data() + sourceFramesToProcess;
1135267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    memcpy(inputP, sourceP, sizeof(float) * sourceFramesToProcess);
1145267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1155267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // Copy the odd sample-frames from sourceP, delayed by one sample-frame (destination sample-rate)
1165267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // to match shifting forward in time in m_reducedKernel.
1175267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    float* oddSamplesP = m_tempBuffer.data();
1185267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    for (unsigned i = 0; i < destFramesToProcess; ++i)
1195267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        oddSamplesP[i] = *((inputP - 1) + i * 2);
1205267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1215267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // Actually process oddSamplesP with m_reducedKernel for efficiency.
1225267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // The theoretical kernel is double this size with 0 values for even terms (except center).
1235267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    m_convolver.process(&m_reducedKernel, oddSamplesP, destP, destFramesToProcess);
1245267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1255267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // Now, account for the 0.5 term right in the middle of the kernel.
1265267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // This amounts to a delay-line of length halfSize (at the source sample-rate),
1275267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // scaled by 0.5.
1285267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1295267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // Sum into the destination.
1305267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    for (unsigned i = 0; i < destFramesToProcess; ++i)
1315267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        destP[i] += 0.5 * *((inputP - halfSize) + i * 2);
1325267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1335267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // Copy 2nd half of input buffer to 1st half.
1345267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    memcpy(m_inputBuffer.data(), inputP, sizeof(float) * sourceFramesToProcess);
1355267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)}
1365267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1375267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)void DownSampler::reset()
1385267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){
1395267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    m_convolver.reset();
1405267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    m_inputBuffer.zero();
1415267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)}
1425267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1435267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)size_t DownSampler::latencyFrames() const
1445267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){
1455267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // Divide by two since this is a linear phase kernel and the delay is at the center of the kernel.
1465267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    return m_reducedKernel.size() / 2;
1475267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)}
1485267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
149c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
1505267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1515267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#endif // ENABLE(WEB_AUDIO)
152