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 "DelayDSPKernel.h"
31
#include "AudioUtilities.h"
38
const float SmoothingTimeConstant = 0.020f; // 20ms
40
DelayDSPKernel::DelayDSPKernel(DelayProcessor* processor)
41
: AudioDSPKernel(processor)
44
, m_delayTimes(AudioNode::ProcessingSizeInFrames)
46
ASSERT(processor && processor->sampleRate() > 0);
47
if (!(processor && processor->sampleRate() > 0))
50
m_maxDelayTime = processor->maxDelayTime();
51
ASSERT(m_maxDelayTime >= 0);
52
if (m_maxDelayTime < 0)
55
m_buffer.allocate(bufferLengthForDelay(m_maxDelayTime, processor->sampleRate()));
58
m_smoothingRate = AudioUtilities::discreteTimeConstantForSampleRate(SmoothingTimeConstant, processor->sampleRate());
61
DelayDSPKernel::DelayDSPKernel(double maxDelayTime, float sampleRate)
62
: AudioDSPKernel(sampleRate)
63
, m_maxDelayTime(maxDelayTime)
67
ASSERT(maxDelayTime > 0.0);
68
if (maxDelayTime <= 0.0)
71
size_t bufferLength = bufferLengthForDelay(maxDelayTime, sampleRate);
76
m_buffer.allocate(bufferLength);
79
m_smoothingRate = AudioUtilities::discreteTimeConstantForSampleRate(SmoothingTimeConstant, sampleRate);
82
size_t DelayDSPKernel::bufferLengthForDelay(double maxDelayTime, double sampleRate) const
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);
89
void DelayDSPKernel::process(const float* source, float* destination, size_t framesToProcess)
91
size_t bufferLength = m_buffer.size();
92
float* buffer = m_buffer.data();
98
ASSERT(source && destination);
99
if (!source || !destination)
102
float sampleRate = this->sampleRate();
103
double delayTime = 0;
104
float* delayTimes = m_delayTimes.data();
105
double maxTime = maxDelayTime();
107
bool sampleAccurate = delayProcessor() && delayProcessor()->delayTime()->hasSampleAccurateValues();
110
delayProcessor()->delayTime()->calculateSampleAccurateValues(delayTimes, framesToProcess);
112
delayTime = delayProcessor() ? delayProcessor()->delayTime()->finalValue() : m_desiredDelayFrames / sampleRate;
114
// Make sure the delay time is in a valid range.
115
delayTime = min(maxTime, delayTime);
116
delayTime = max(0.0, delayTime);
119
m_currentDelayTime = delayTime;
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;
131
// Approach desired delay time.
132
m_currentDelayTime += (delayTime - m_currentDelayTime) * m_smoothingRate;
135
double desiredDelayFrames = m_currentDelayTime * sampleRate;
137
double readPosition = m_writeIndex + bufferLength - desiredDelayFrames;
138
if (readPosition >= bufferLength)
139
readPosition -= bufferLength;
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;
146
double input = static_cast<float>(*source++);
147
buffer[m_writeIndex] = static_cast<float>(input);
148
m_writeIndex = (m_writeIndex + 1) % bufferLength;
150
double sample1 = buffer[readIndex1];
151
double sample2 = buffer[readIndex2];
153
double output = (1.0 - interpolationFactor) * sample1 + interpolationFactor * sample2;
155
*destination++ = static_cast<float>(output);
159
void DelayDSPKernel::reset()
165
double DelayDSPKernel::tailTime() const
167
return m_maxDelayTime;
170
double DelayDSPKernel::latencyTime() const
175
} // namespace WebCore
177
#endif // ENABLE(WEB_AUDIO)