2 * Copyright (C) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "platform/audio/AudioResamplerKernel.h"
32 #include "platform/audio/AudioResampler.h"
38 const size_t AudioResamplerKernel::MaxFramesToProcess = 128;
40 AudioResamplerKernel::AudioResamplerKernel(AudioResampler* resampler)
41 : m_resampler(resampler)
42 // The buffer size must be large enough to hold up to two extra sample frames for the linear interpolation.
43 , m_sourceBuffer(2 + static_cast<int>(MaxFramesToProcess * AudioResampler::MaxRate))
44 , m_virtualReadIndex(0.0)
47 m_lastValues[0] = 0.0f;
48 m_lastValues[1] = 0.0f;
51 float* AudioResamplerKernel::getSourcePointer(size_t framesToProcess, size_t* numberOfSourceFramesNeededP)
53 ASSERT(framesToProcess <= MaxFramesToProcess);
55 // Calculate the next "virtual" index. After process() is called, m_virtualReadIndex will equal this value.
56 double nextFractionalIndex = m_virtualReadIndex + framesToProcess * rate();
58 // Because we're linearly interpolating between the previous and next sample we need to round up so we include the next sample.
59 int endIndex = static_cast<int>(nextFractionalIndex + 1.0); // round up to next integer index
61 // Determine how many input frames we'll need.
62 // We need to fill the buffer up to and including endIndex (so add 1) but we've already buffered m_fillIndex frames from last time.
63 size_t framesNeeded = 1 + endIndex - m_fillIndex;
64 if (numberOfSourceFramesNeededP)
65 *numberOfSourceFramesNeededP = framesNeeded;
67 // Do bounds checking for the source buffer.
68 bool isGood = m_fillIndex < m_sourceBuffer.size() && m_fillIndex + framesNeeded <= m_sourceBuffer.size();
73 return m_sourceBuffer.data() + m_fillIndex;
76 void AudioResamplerKernel::process(float* destination, size_t framesToProcess)
78 ASSERT(framesToProcess <= MaxFramesToProcess);
80 float* source = m_sourceBuffer.data();
82 double rate = this->rate();
83 rate = max(0.0, rate);
84 rate = min(AudioResampler::MaxRate, rate);
86 // Start out with the previous saved values (if any).
87 if (m_fillIndex > 0) {
88 source[0] = m_lastValues[0];
89 source[1] = m_lastValues[1];
93 double virtualReadIndex = m_virtualReadIndex;
95 // Sanity check source buffer access.
96 ASSERT(framesToProcess > 0);
97 ASSERT(virtualReadIndex >= 0 && 1 + static_cast<unsigned>(virtualReadIndex + (framesToProcess - 1) * rate) < m_sourceBuffer.size());
99 // Do the linear interpolation.
100 int n = framesToProcess;
102 unsigned readIndex = static_cast<unsigned>(virtualReadIndex);
103 double interpolationFactor = virtualReadIndex - readIndex;
105 double sample1 = source[readIndex];
106 double sample2 = source[readIndex + 1];
108 double sample = (1.0 - interpolationFactor) * sample1 + interpolationFactor * sample2;
110 *destination++ = static_cast<float>(sample);
112 virtualReadIndex += rate;
115 // Save the last two sample-frames which will later be used at the beginning of the source buffer the next time around.
116 int readIndex = static_cast<int>(virtualReadIndex);
117 m_lastValues[0] = source[readIndex];
118 m_lastValues[1] = source[readIndex + 1];
121 // Wrap the virtual read index back to the start of the buffer.
122 virtualReadIndex -= readIndex;
124 // Put local copy back into member variable.
125 m_virtualReadIndex = virtualReadIndex;
128 void AudioResamplerKernel::reset()
130 m_virtualReadIndex = 0.0;
132 m_lastValues[0] = 0.0f;
133 m_lastValues[1] = 0.0f;
136 double AudioResamplerKernel::rate() const
138 return m_resampler->rate();
143 #endif // ENABLE(WEB_AUDIO)