OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010, Google Inc. All rights reserved. | 2 * Copyright (C) 2010, Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y | 16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE |
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N | 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 23 * DAMAGE. |
23 */ | 24 */ |
24 | 25 |
25 #include "platform/audio/AudioResamplerKernel.h" | 26 #include "platform/audio/AudioResamplerKernel.h" |
26 #include "platform/audio/AudioResampler.h" | 27 #include "platform/audio/AudioResampler.h" |
27 #include "wtf/MathExtras.h" | 28 #include "wtf/MathExtras.h" |
28 | 29 |
29 namespace blink { | 30 namespace blink { |
30 | 31 |
31 const size_t AudioResamplerKernel::MaxFramesToProcess = 128; | 32 const size_t AudioResamplerKernel::MaxFramesToProcess = 128; |
32 | 33 |
33 AudioResamplerKernel::AudioResamplerKernel(AudioResampler* resampler) | 34 AudioResamplerKernel::AudioResamplerKernel(AudioResampler* resampler) |
34 : m_resampler(resampler) | 35 : m_resampler(resampler), |
35 // The buffer size must be large enough to hold up to two extra sample fra
mes for the linear interpolation. | 36 // The buffer size must be large enough to hold up to two extra sample |
36 , | 37 // frames for the linear interpolation. |
37 m_sourceBuffer( | 38 m_sourceBuffer( |
38 2 + static_cast<int>(MaxFramesToProcess * AudioResampler::MaxRate)), | 39 2 + static_cast<int>(MaxFramesToProcess * AudioResampler::MaxRate)), |
39 m_virtualReadIndex(0.0), | 40 m_virtualReadIndex(0.0), |
40 m_fillIndex(0) { | 41 m_fillIndex(0) { |
41 m_lastValues[0] = 0.0f; | 42 m_lastValues[0] = 0.0f; |
42 m_lastValues[1] = 0.0f; | 43 m_lastValues[1] = 0.0f; |
43 } | 44 } |
44 | 45 |
45 float* AudioResamplerKernel::getSourcePointer( | 46 float* AudioResamplerKernel::getSourcePointer( |
46 size_t framesToProcess, | 47 size_t framesToProcess, |
47 size_t* numberOfSourceFramesNeededP) { | 48 size_t* numberOfSourceFramesNeededP) { |
48 ASSERT(framesToProcess <= MaxFramesToProcess); | 49 ASSERT(framesToProcess <= MaxFramesToProcess); |
49 | 50 |
50 // Calculate the next "virtual" index. After process() is called, m_virtualRe
adIndex will equal this value. | 51 // Calculate the next "virtual" index. After process() is called, |
| 52 // m_virtualReadIndex will equal this value. |
51 double nextFractionalIndex = m_virtualReadIndex + framesToProcess * rate(); | 53 double nextFractionalIndex = m_virtualReadIndex + framesToProcess * rate(); |
52 | 54 |
53 // Because we're linearly interpolating between the previous and next sample w
e need to round up so we include the next sample. | 55 // Because we're linearly interpolating between the previous and next sample |
| 56 // we need to round up so we include the next sample. |
54 int endIndex = static_cast<int>(nextFractionalIndex + | 57 int endIndex = static_cast<int>(nextFractionalIndex + |
55 1.0); // round up to next integer index | 58 1.0); // round up to next integer index |
56 | 59 |
57 // Determine how many input frames we'll need. | 60 // Determine how many input frames we'll need. |
58 // We need to fill the buffer up to and including endIndex (so add 1) but we'v
e already buffered m_fillIndex frames from last time. | 61 // We need to fill the buffer up to and including endIndex (so add 1) but |
| 62 // we've already buffered m_fillIndex frames from last time. |
59 size_t framesNeeded = 1 + endIndex - m_fillIndex; | 63 size_t framesNeeded = 1 + endIndex - m_fillIndex; |
60 if (numberOfSourceFramesNeededP) | 64 if (numberOfSourceFramesNeededP) |
61 *numberOfSourceFramesNeededP = framesNeeded; | 65 *numberOfSourceFramesNeededP = framesNeeded; |
62 | 66 |
63 // Do bounds checking for the source buffer. | 67 // Do bounds checking for the source buffer. |
64 bool isGood = m_fillIndex < m_sourceBuffer.size() && | 68 bool isGood = m_fillIndex < m_sourceBuffer.size() && |
65 m_fillIndex + framesNeeded <= m_sourceBuffer.size(); | 69 m_fillIndex + framesNeeded <= m_sourceBuffer.size(); |
66 ASSERT(isGood); | 70 ASSERT(isGood); |
67 if (!isGood) | 71 if (!isGood) |
68 return 0; | 72 return 0; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 double sample2 = source[readIndex + 1]; | 108 double sample2 = source[readIndex + 1]; |
105 | 109 |
106 double sample = | 110 double sample = |
107 (1.0 - interpolationFactor) * sample1 + interpolationFactor * sample2; | 111 (1.0 - interpolationFactor) * sample1 + interpolationFactor * sample2; |
108 | 112 |
109 *destination++ = static_cast<float>(sample); | 113 *destination++ = static_cast<float>(sample); |
110 | 114 |
111 virtualReadIndex += rate; | 115 virtualReadIndex += rate; |
112 } | 116 } |
113 | 117 |
114 // Save the last two sample-frames which will later be used at the beginning o
f the source buffer the next time around. | 118 // Save the last two sample-frames which will later be used at the beginning |
| 119 // of the source buffer the next time around. |
115 int readIndex = static_cast<int>(virtualReadIndex); | 120 int readIndex = static_cast<int>(virtualReadIndex); |
116 m_lastValues[0] = source[readIndex]; | 121 m_lastValues[0] = source[readIndex]; |
117 m_lastValues[1] = source[readIndex + 1]; | 122 m_lastValues[1] = source[readIndex + 1]; |
118 m_fillIndex = 2; | 123 m_fillIndex = 2; |
119 | 124 |
120 // Wrap the virtual read index back to the start of the buffer. | 125 // Wrap the virtual read index back to the start of the buffer. |
121 virtualReadIndex -= readIndex; | 126 virtualReadIndex -= readIndex; |
122 | 127 |
123 // Put local copy back into member variable. | 128 // Put local copy back into member variable. |
124 m_virtualReadIndex = virtualReadIndex; | 129 m_virtualReadIndex = virtualReadIndex; |
125 } | 130 } |
126 | 131 |
127 void AudioResamplerKernel::reset() { | 132 void AudioResamplerKernel::reset() { |
128 m_virtualReadIndex = 0.0; | 133 m_virtualReadIndex = 0.0; |
129 m_fillIndex = 0; | 134 m_fillIndex = 0; |
130 m_lastValues[0] = 0.0f; | 135 m_lastValues[0] = 0.0f; |
131 m_lastValues[1] = 0.0f; | 136 m_lastValues[1] = 0.0f; |
132 } | 137 } |
133 | 138 |
134 double AudioResamplerKernel::rate() const { | 139 double AudioResamplerKernel::rate() const { |
135 return m_resampler->rate(); | 140 return m_resampler->rate(); |
136 } | 141 } |
137 | 142 |
138 } // namespace blink | 143 } // namespace blink |
OLD | NEW |