1/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30
31#if ENABLE(WEB_AUDIO)
32
33#include "DownSampler.h"
34
35#include <wtf/MathExtras.h>
36
37namespace WebCore {
38
39DownSampler::DownSampler(size_t inputBlockSize)
40    : m_inputBlockSize(inputBlockSize)
41    , m_reducedKernel(DefaultKernelSize / 2)
42    , m_convolver(inputBlockSize / 2) // runs at 1/2 source sample-rate
43    , m_tempBuffer(inputBlockSize / 2)
44    , m_inputBuffer(inputBlockSize * 2)
45{
46    initializeKernel();
47}
48
49void DownSampler::initializeKernel()
50{
51    // Blackman window parameters.
52    double alpha = 0.16;
53    double a0 = 0.5 * (1.0 - alpha);
54    double a1 = 0.5;
55    double a2 = 0.5 * alpha;
56
57    int n = DefaultKernelSize;
58    int halfSize = n / 2;
59
60    // Half-band filter.
61    double sincScaleFactor = 0.5;
62
63    // Compute only the odd terms because the even ones are zero, except
64    // right in the middle at halfSize, which is 0.5 and we'll handle specially during processing
65    // after doing the main convolution using m_reducedKernel.
66    for (int i = 1; i < n; i += 2) {
67        // Compute the sinc() with offset.
68        double s = sincScaleFactor * piDouble * (i - halfSize);
69        double sinc = !s ? 1.0 : sin(s) / s;
70        sinc *= sincScaleFactor;
71
72        // Compute Blackman window, matching the offset of the sinc().
73        double x = static_cast<double>(i) / n;
74        double window = a0 - a1 * cos(2.0 * piDouble * x) + a2 * cos(4.0 * piDouble * x);
75
76        // Window the sinc() function.
77        // Then store only the odd terms in the kernel.
78        // In a sense, this is shifting forward in time by one sample-frame at the destination sample-rate.
79        m_reducedKernel[(i - 1) / 2] = sinc * window;
80    }
81}
82
83void DownSampler::process(const float* sourceP, float* destP, size_t sourceFramesToProcess)
84{
85    bool isInputBlockSizeGood = sourceFramesToProcess == m_inputBlockSize;
86    ASSERT(isInputBlockSizeGood);
87    if (!isInputBlockSizeGood)
88        return;
89
90    size_t destFramesToProcess = sourceFramesToProcess / 2;
91
92    bool isTempBufferGood = destFramesToProcess == m_tempBuffer.size();
93    ASSERT(isTempBufferGood);
94    if (!isTempBufferGood)
95        return;
96
97    bool isReducedKernelGood = m_reducedKernel.size() == DefaultKernelSize / 2;
98    ASSERT(isReducedKernelGood);
99    if (!isReducedKernelGood)
100        return;
101
102    size_t halfSize = DefaultKernelSize / 2;
103
104    // Copy source samples to 2nd half of input buffer.
105    bool isInputBufferGood = m_inputBuffer.size() == sourceFramesToProcess * 2 && halfSize <= sourceFramesToProcess;
106    ASSERT(isInputBufferGood);
107    if (!isInputBufferGood)
108        return;
109
110    float* inputP = m_inputBuffer.data() + sourceFramesToProcess;
111    memcpy(inputP, sourceP, sizeof(float) * sourceFramesToProcess);
112
113    // Copy the odd sample-frames from sourceP, delayed by one sample-frame (destination sample-rate)
114    // to match shifting forward in time in m_reducedKernel.
115    float* oddSamplesP = m_tempBuffer.data();
116    for (unsigned i = 0; i < destFramesToProcess; ++i)
117        oddSamplesP[i] = *((inputP - 1) + i * 2);
118
119    // Actually process oddSamplesP with m_reducedKernel for efficiency.
120    // The theoretical kernel is double this size with 0 values for even terms (except center).
121    m_convolver.process(&m_reducedKernel, oddSamplesP, destP, destFramesToProcess);
122
123    // Now, account for the 0.5 term right in the middle of the kernel.
124    // This amounts to a delay-line of length halfSize (at the source sample-rate),
125    // scaled by 0.5.
126
127    // Sum into the destination.
128    for (unsigned i = 0; i < destFramesToProcess; ++i)
129        destP[i] += 0.5 * *((inputP - halfSize) + i * 2);
130
131    // Copy 2nd half of input buffer to 1st half.
132    memcpy(m_inputBuffer.data(), inputP, sizeof(float) * sourceFramesToProcess);
133}
134
135void DownSampler::reset()
136{
137    m_convolver.reset();
138    m_inputBuffer.zero();
139}
140
141size_t DownSampler::latencyFrames() const
142{
143    // Divide by two since this is a linear phase kernel and the delay is at the center of the kernel.
144    return m_reducedKernel.size() / 2;
145}
146
147} // namespace WebCore
148
149#endif // ENABLE(WEB_AUDIO)
150