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 "modules/webaudio/DelayDSPKernel.h"
30
31#include "core/platform/audio/AudioUtilities.h"
32#include <algorithm>
33
34using namespace std;
35
36namespace WebCore {
37
38const float SmoothingTimeConstant = 0.020f; // 20ms
39
40DelayDSPKernel::DelayDSPKernel(DelayProcessor* processor)
41    : AudioDSPKernel(processor)
42    , m_writeIndex(0)
43    , m_firstTime(true)
44    , m_delayTimes(AudioNode::ProcessingSizeInFrames)
45{
46    ASSERT(processor && processor->sampleRate() > 0);
47    if (!(processor && processor->sampleRate() > 0))
48        return;
49
50    m_maxDelayTime = processor->maxDelayTime();
51    ASSERT(m_maxDelayTime >= 0);
52    if (m_maxDelayTime < 0)
53        return;
54
55    m_buffer.allocate(bufferLengthForDelay(m_maxDelayTime, processor->sampleRate()));
56    m_buffer.zero();
57
58    m_smoothingRate = AudioUtilities::discreteTimeConstantForSampleRate(SmoothingTimeConstant, processor->sampleRate());
59}
60
61DelayDSPKernel::DelayDSPKernel(double maxDelayTime, float sampleRate)
62    : AudioDSPKernel(sampleRate)
63    , m_maxDelayTime(maxDelayTime)
64    , m_writeIndex(0)
65    , m_firstTime(true)
66{
67    ASSERT(maxDelayTime > 0.0);
68    if (maxDelayTime <= 0.0)
69        return;
70
71    size_t bufferLength = bufferLengthForDelay(maxDelayTime, sampleRate);
72    ASSERT(bufferLength);
73    if (!bufferLength)
74        return;
75
76    m_buffer.allocate(bufferLength);
77    m_buffer.zero();
78
79    m_smoothingRate = AudioUtilities::discreteTimeConstantForSampleRate(SmoothingTimeConstant, sampleRate);
80}
81
82size_t DelayDSPKernel::bufferLengthForDelay(double maxDelayTime, double sampleRate) const
83{
84    // Compute the length of the buffer needed to handle a max delay of |maxDelayTime|. One is
85    // added to handle the case where the actual delay equals the maximum delay.
86    return 1 + AudioUtilities::timeToSampleFrame(maxDelayTime, sampleRate);
87}
88
89void DelayDSPKernel::process(const float* source, float* destination, size_t framesToProcess)
90{
91    size_t bufferLength = m_buffer.size();
92    float* buffer = m_buffer.data();
93
94    ASSERT(bufferLength);
95    if (!bufferLength)
96        return;
97
98    ASSERT(source && destination);
99    if (!source || !destination)
100        return;
101
102    float sampleRate = this->sampleRate();
103    double delayTime = 0;
104    float* delayTimes = m_delayTimes.data();
105    double maxTime = maxDelayTime();
106
107    bool sampleAccurate = delayProcessor() && delayProcessor()->delayTime()->hasSampleAccurateValues();
108
109    if (sampleAccurate)
110        delayProcessor()->delayTime()->calculateSampleAccurateValues(delayTimes, framesToProcess);
111    else {
112        delayTime = delayProcessor() ? delayProcessor()->delayTime()->finalValue() : m_desiredDelayFrames / sampleRate;
113
114        // Make sure the delay time is in a valid range.
115        delayTime = min(maxTime, delayTime);
116        delayTime = max(0.0, delayTime);
117
118        if (m_firstTime) {
119            m_currentDelayTime = delayTime;
120            m_firstTime = false;
121        }
122    }
123
124    for (unsigned i = 0; i < framesToProcess; ++i) {
125        if (sampleAccurate) {
126            delayTime = delayTimes[i];
127            delayTime = std::min(maxTime, delayTime);
128            delayTime = std::max(0.0, delayTime);
129            m_currentDelayTime = delayTime;
130        } else {
131            // Approach desired delay time.
132            m_currentDelayTime += (delayTime - m_currentDelayTime) * m_smoothingRate;
133        }
134
135        double desiredDelayFrames = m_currentDelayTime * sampleRate;
136
137        double readPosition = m_writeIndex + bufferLength - desiredDelayFrames;
138        if (readPosition >= bufferLength)
139            readPosition -= bufferLength;
140
141        // Linearly interpolate in-between delay times.
142        int readIndex1 = static_cast<int>(readPosition);
143        int readIndex2 = (readIndex1 + 1) % bufferLength;
144        double interpolationFactor = readPosition - readIndex1;
145
146        double input = static_cast<float>(*source++);
147        buffer[m_writeIndex] = static_cast<float>(input);
148        m_writeIndex = (m_writeIndex + 1) % bufferLength;
149
150        double sample1 = buffer[readIndex1];
151        double sample2 = buffer[readIndex2];
152
153        double output = (1.0 - interpolationFactor) * sample1 + interpolationFactor * sample2;
154
155        *destination++ = static_cast<float>(output);
156    }
157}
158
159void DelayDSPKernel::reset()
160{
161    m_firstTime = true;
162    m_buffer.zero();
163}
164
165double DelayDSPKernel::tailTime() const
166{
167    // Account for worst case delay.
168    // Don't try to track actual delay time which can change dynamically.
169    return m_maxDelayTime;
170}
171
172double DelayDSPKernel::latencyTime() const
173{
174    return 0;
175}
176
177} // namespace WebCore
178
179#endif // ENABLE(WEB_AUDIO)
180