OLD | NEW |
---|---|
(Empty) | |
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 Computer, 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 "core/platform/audio/DownSampler.h" | |
34 | |
35 #include "wtf/MathExtras.h" | |
36 | |
37 namespace WebCore { | |
38 | |
39 DownSampler::DownSampler(size_t inputBlockSize) | |
40 : m_inputBlockSize(inputBlockSize) | |
41 , m_reducedKernel(DefaultKernelSize / 2) | |
42 , m_tempBuffer(inputBlockSize / 2) | |
43 , m_inputBuffer(inputBlockSize * 2) | |
44 , m_convolver(inputBlockSize / 2) // runs at 1/2 source sample-rate | |
45 { | |
46 initializeKernel(); | |
47 } | |
48 | |
49 void 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 * piDou ble * 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 t he destination sample-rate. | |
79 m_reducedKernel[(i - 1) / 2] = sinc * window; | |
80 } | |
81 } | |
82 | |
83 void DownSampler::process(const float* sourceP, float* destP, size_t sourceFrame sToProcess) | |
84 { | |
85 ASSERT(sourceFramesToProcess == m_inputBlockSize); | |
86 if (sourceFramesToProcess != m_inputBlockSize) | |
87 return; | |
88 | |
89 size_t destFramesToProcess = sourceFramesToProcess / 2; | |
90 | |
91 ASSERT(destFramesToProcess == m_tempBuffer.size()); | |
92 if (destFramesToProcess != m_tempBuffer.size()) | |
93 return; | |
94 | |
95 // Copy source samples to 2nd half of input buffer. | |
96 float* inputP = m_inputBuffer.data() + m_inputBlockSize; | |
97 memcpy(inputP, sourceP, sizeof(float) * sourceFramesToProcess); | |
98 | |
99 // Copy the odd sample-frames from sourceP, delayed by one sample-frame (des tination sample-rate) | |
100 // to match shifting forward in time in m_reducedKernel. | |
101 float* oddSamplesP = m_tempBuffer.data(); | |
102 for (unsigned i = 0; i < destFramesToProcess; ++i) | |
103 oddSamplesP[i] = inputP[i * 2 - 1]; | |
104 | |
105 // Actually process oddSamplesP with m_reducedKernel for efficiency. | |
106 // The theoretical kernel is double this size with 0 values for even terms ( except center). | |
107 m_convolver.process(&m_reducedKernel, oddSamplesP, destP, destFramesToProces s); | |
Ken Russell (switch to Gerrit)
2013/05/23 02:29:25
Checks are needed around the size of the reduced k
Chris Rogers
2013/05/24 20:09:04
Added ASSERTS to check all (I think) necessary con
| |
108 | |
109 // Now, account for the 0.5 term right in the middle of the kernel. | |
110 // This amounts to a delay-line of length halfSize (at the source sample-rat e), | |
111 // scaled by 0.5. | |
112 int halfSize = DefaultKernelSize / 2; | |
113 | |
114 // Sum into the destination. | |
115 for (unsigned i = 0; i < destFramesToProcess; ++i) | |
116 destP[i] += 0.5 * inputP[i * 2 - halfSize]; | |
117 | |
118 // Copy 2nd half of input buffer to 1st half. | |
119 memcpy(m_inputBuffer.data(), inputP, sizeof(float) * sourceFramesToProcess); | |
Ken Russell (switch to Gerrit)
2013/05/23 02:29:25
I don't understand why m_inputBuffer is needed. It
Chris Rogers
2013/05/24 20:09:04
Added comment in header file for this.
| |
120 } | |
121 | |
122 void DownSampler::reset() | |
123 { | |
124 m_convolver.reset(); | |
125 m_inputBuffer.zero(); | |
126 } | |
127 | |
128 size_t DownSampler::latencyFrames() const | |
129 { | |
130 // Divide by two since this is a linear phase kernel and the delay is at the center of the kernel. | |
131 return m_reducedKernel.size() / 2; | |
132 } | |
133 | |
134 } // namespace WebCore | |
135 | |
136 #endif // ENABLE(WEB_AUDIO) | |
OLD | NEW |