blob: 4800335e5cd77c471f1150bc429e64362a056781 [file] [log] [blame]
crogers@google.com18ec8a92013-07-16 04:55:32 +00001/*
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.
mjs@apple.com92047332014-03-15 04:08:27 +000013 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
crogers@google.com18ec8a92013-07-16 04:55:32 +000014 * 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)
crogers@google.com18ec8a92013-07-16 04:55:32 +000041 , m_convolver(inputBlockSize / 2) // runs at 1/2 source sample-rate
42 , m_tempBuffer(inputBlockSize / 2)
43 , m_inputBuffer(inputBlockSize * 2)
44{
45 initializeKernel();
46}
47
48void DownSampler::initializeKernel()
49{
50 // Blackman window parameters.
51 double alpha = 0.16;
52 double a0 = 0.5 * (1.0 - alpha);
53 double a1 = 0.5;
54 double a2 = 0.5 * alpha;
55
56 int n = DefaultKernelSize;
57 int halfSize = n / 2;
58
59 // Half-band filter.
60 double sincScaleFactor = 0.5;
61
62 // Compute only the odd terms because the even ones are zero, except
63 // right in the middle at halfSize, which is 0.5 and we'll handle specially during processing
64 // after doing the main convolution using m_reducedKernel.
65 for (int i = 1; i < n; i += 2) {
66 // Compute the sinc() with offset.
67 double s = sincScaleFactor * piDouble * (i - halfSize);
68 double sinc = !s ? 1.0 : sin(s) / s;
69 sinc *= sincScaleFactor;
70
71 // Compute Blackman window, matching the offset of the sinc().
72 double x = static_cast<double>(i) / n;
73 double window = a0 - a1 * cos(2.0 * piDouble * x) + a2 * cos(4.0 * piDouble * x);
74
75 // Window the sinc() function.
76 // Then store only the odd terms in the kernel.
77 // In a sense, this is shifting forward in time by one sample-frame at the destination sample-rate.
78 m_reducedKernel[(i - 1) / 2] = sinc * window;
79 }
80}
81
82void DownSampler::process(const float* sourceP, float* destP, size_t sourceFramesToProcess)
83{
84 bool isInputBlockSizeGood = sourceFramesToProcess == m_inputBlockSize;
85 ASSERT(isInputBlockSizeGood);
86 if (!isInputBlockSizeGood)
87 return;
88
89 size_t destFramesToProcess = sourceFramesToProcess / 2;
90
91 bool isTempBufferGood = destFramesToProcess == m_tempBuffer.size();
92 ASSERT(isTempBufferGood);
93 if (!isTempBufferGood)
94 return;
95
96 bool isReducedKernelGood = m_reducedKernel.size() == DefaultKernelSize / 2;
97 ASSERT(isReducedKernelGood);
98 if (!isReducedKernelGood)
99 return;
100
101 size_t halfSize = DefaultKernelSize / 2;
102
103 // Copy source samples to 2nd half of input buffer.
104 bool isInputBufferGood = m_inputBuffer.size() == sourceFramesToProcess * 2 && halfSize <= sourceFramesToProcess;
105 ASSERT(isInputBufferGood);
106 if (!isInputBufferGood)
107 return;
108
109 float* inputP = m_inputBuffer.data() + sourceFramesToProcess;
110 memcpy(inputP, sourceP, sizeof(float) * sourceFramesToProcess);
111
112 // Copy the odd sample-frames from sourceP, delayed by one sample-frame (destination sample-rate)
113 // to match shifting forward in time in m_reducedKernel.
114 float* oddSamplesP = m_tempBuffer.data();
115 for (unsigned i = 0; i < destFramesToProcess; ++i)
116 oddSamplesP[i] = *((inputP - 1) + i * 2);
117
118 // Actually process oddSamplesP with m_reducedKernel for efficiency.
119 // The theoretical kernel is double this size with 0 values for even terms (except center).
120 m_convolver.process(&m_reducedKernel, oddSamplesP, destP, destFramesToProcess);
121
122 // Now, account for the 0.5 term right in the middle of the kernel.
123 // This amounts to a delay-line of length halfSize (at the source sample-rate),
124 // scaled by 0.5.
125
126 // Sum into the destination.
127 for (unsigned i = 0; i < destFramesToProcess; ++i)
128 destP[i] += 0.5 * *((inputP - halfSize) + i * 2);
129
130 // Copy 2nd half of input buffer to 1st half.
131 memcpy(m_inputBuffer.data(), inputP, sizeof(float) * sourceFramesToProcess);
132}
133
134void DownSampler::reset()
135{
136 m_convolver.reset();
137 m_inputBuffer.zero();
138}
139
140size_t DownSampler::latencyFrames() const
141{
142 // Divide by two since this is a linear phase kernel and the delay is at the center of the kernel.
143 return m_reducedKernel.size() / 2;
144}
145
146} // namespace WebCore
147
148#endif // ENABLE(WEB_AUDIO)