1/*
2 * Copyright (C) 2010, 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 * 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.
12 *
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.
23 */
24
25#include "config.h"
26
27#if ENABLE(WEB_AUDIO)
28
29#include "AudioResamplerKernel.h"
30
31#include "AudioResampler.h"
32#include <algorithm>
33
34using namespace std;
35
36namespace WebCore {
37
38const size_t AudioResamplerKernel::MaxFramesToProcess = 128;
39
40AudioResamplerKernel::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)
45    , m_fillIndex(0)
46{
47    m_lastValues[0] = 0.0f;
48    m_lastValues[1] = 0.0f;
49}
50
51float* AudioResamplerKernel::getSourcePointer(size_t framesToProcess, size_t* numberOfSourceFramesNeededP)
52{
53    ASSERT(framesToProcess <= MaxFramesToProcess);
54
55    // Calculate the next "virtual" index.  After process() is called, m_virtualReadIndex will equal this value.
56    double nextFractionalIndex = m_virtualReadIndex + framesToProcess * rate();
57
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
60
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;
66
67    // Do bounds checking for the source buffer.
68    bool isGood = m_fillIndex < m_sourceBuffer.size() && m_fillIndex + framesNeeded <= m_sourceBuffer.size();
69    ASSERT(isGood);
70    if (!isGood)
71        return 0;
72
73    return m_sourceBuffer.data() + m_fillIndex;
74}
75
76void AudioResamplerKernel::process(float* destination, size_t framesToProcess)
77{
78    ASSERT(framesToProcess <= MaxFramesToProcess);
79
80    float* source = m_sourceBuffer.data();
81
82    double rate = this->rate();
83    rate = max(0.0, rate);
84    rate = min(AudioResampler::MaxRate, rate);
85
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];
90    }
91
92    // Make a local copy.
93    double virtualReadIndex = m_virtualReadIndex;
94
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());
98
99    // Do the linear interpolation.
100    int n = framesToProcess;
101    while (n--) {
102        unsigned readIndex = static_cast<unsigned>(virtualReadIndex);
103        double interpolationFactor = virtualReadIndex - readIndex;
104
105        double sample1 = source[readIndex];
106        double sample2 = source[readIndex + 1];
107
108        double sample = (1.0 - interpolationFactor) * sample1 + interpolationFactor * sample2;
109
110        *destination++ = static_cast<float>(sample);
111
112        virtualReadIndex += rate;
113    }
114
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];
119    m_fillIndex = 2;
120
121    // Wrap the virtual read index back to the start of the buffer.
122    virtualReadIndex -= readIndex;
123
124    // Put local copy back into member variable.
125    m_virtualReadIndex = virtualReadIndex;
126}
127
128void AudioResamplerKernel::reset()
129{
130    m_virtualReadIndex = 0.0;
131    m_fillIndex = 0;
132    m_lastValues[0] = 0.0f;
133    m_lastValues[1] = 0.0f;
134}
135
136double AudioResamplerKernel::rate() const
137{
138    return m_resampler->rate();
139}
140
141} // namespace WebCore
142
143#endif // ENABLE(WEB_AUDIO)
144