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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30
31 #if ENABLE(WEB_AUDIO)
32
33 #include "ReverbConvolverStage.h"
34
35 #include "VectorMath.h"
36 #include "ReverbAccumulationBuffer.h"
37 #include "ReverbConvolver.h"
38 #include "ReverbInputBuffer.h"
39 #include <wtf/OwnPtr.h>
40 #include <wtf/PassOwnPtr.h>
41
42 namespace WebCore {
43
44 using namespace VectorMath;
45
ReverbConvolverStage(float * impulseResponse,size_t responseLength,size_t reverbTotalLatency,size_t stageOffset,size_t stageLength,size_t fftSize,size_t renderPhase,size_t renderSliceSize,ReverbAccumulationBuffer * accumulationBuffer)46 ReverbConvolverStage::ReverbConvolverStage(float* impulseResponse, size_t responseLength, size_t reverbTotalLatency, size_t stageOffset, size_t stageLength,
47 size_t fftSize, size_t renderPhase, size_t renderSliceSize, ReverbAccumulationBuffer* accumulationBuffer)
48 : m_fftKernel(fftSize)
49 , m_accumulationBuffer(accumulationBuffer)
50 , m_accumulationReadIndex(0)
51 , m_inputReadIndex(0)
52 , m_impulseResponseLength(responseLength)
53 {
54 ASSERT(impulseResponse);
55 ASSERT(accumulationBuffer);
56
57 m_fftKernel.doPaddedFFT(impulseResponse + stageOffset, stageLength);
58 m_convolver = adoptPtr(new FFTConvolver(fftSize));
59 m_temporaryBuffer.resize(renderSliceSize);
60
61 // The convolution stage at offset stageOffset needs to have a corresponding delay to cancel out the offset.
62 size_t totalDelay = stageOffset + reverbTotalLatency;
63
64 // But, the FFT convolution itself incurs fftSize / 2 latency, so subtract this out...
65 size_t halfSize = fftSize / 2;
66 ASSERT(totalDelay >= halfSize);
67 if (totalDelay >= halfSize)
68 totalDelay -= halfSize;
69
70 // We divide up the total delay, into pre and post delay sections so that we can schedule at exactly the moment when the FFT will happen.
71 // This is coordinated with the other stages, so they don't all do their FFTs at the same time...
72 int maxPreDelayLength = std::min(halfSize, totalDelay);
73 m_preDelayLength = totalDelay > 0 ? renderPhase % maxPreDelayLength : 0;
74 if (m_preDelayLength > totalDelay)
75 m_preDelayLength = 0;
76
77 m_postDelayLength = totalDelay - m_preDelayLength;
78 m_preReadWriteIndex = 0;
79 m_framesProcessed = 0; // total frames processed so far
80
81 m_preDelayBuffer.resize(m_preDelayLength < fftSize ? fftSize : m_preDelayLength);
82 }
83
processInBackground(ReverbConvolver * convolver,size_t framesToProcess)84 void ReverbConvolverStage::processInBackground(ReverbConvolver* convolver, size_t framesToProcess)
85 {
86 ReverbInputBuffer* inputBuffer = convolver->inputBuffer();
87 float* source = inputBuffer->directReadFrom(&m_inputReadIndex, framesToProcess);
88 process(source, framesToProcess);
89 }
90
process(float * source,size_t framesToProcess)91 void ReverbConvolverStage::process(float* source, size_t framesToProcess)
92 {
93 ASSERT(source);
94 if (!source)
95 return;
96
97 // Deal with pre-delay stream : note special handling of zero delay.
98
99 float* preDelayedSource;
100 float* temporaryBuffer;
101 bool isTemporaryBufferSafe = false;
102 if (m_preDelayLength > 0) {
103 // Handles both the read case (call to process() ) and the write case (memcpy() )
104 bool isPreDelaySafe = m_preReadWriteIndex + framesToProcess <= m_preDelayBuffer.size();
105 ASSERT(isPreDelaySafe);
106 if (!isPreDelaySafe)
107 return;
108
109 isTemporaryBufferSafe = framesToProcess <= m_temporaryBuffer.size();
110
111 preDelayedSource = m_preDelayBuffer.data() + m_preReadWriteIndex;
112 temporaryBuffer = m_temporaryBuffer.data();
113 } else {
114 // Zero delay
115 preDelayedSource = source;
116 temporaryBuffer = m_preDelayBuffer.data();
117
118 isTemporaryBufferSafe = framesToProcess <= m_preDelayBuffer.size();
119 }
120
121 ASSERT(isTemporaryBufferSafe);
122 if (!isTemporaryBufferSafe)
123 return;
124
125 int writeIndex = 0;
126
127 if (m_framesProcessed < m_preDelayLength) {
128 // For the first m_preDelayLength frames don't process the convolver, instead simply buffer in the pre-delay.
129 // But while buffering the pre-delay, we still need to update our index.
130 m_accumulationBuffer->updateReadIndex(&m_accumulationReadIndex, framesToProcess);
131 } else {
132 // Now, run the convolution (into the delay buffer).
133 // An expensive FFT will happen every fftSize / 2 frames.
134 // We process in-place here...
135 m_convolver->process(&m_fftKernel, preDelayedSource, temporaryBuffer, framesToProcess);
136
137 // Now accumulate into reverb's accumulation buffer.
138 writeIndex = m_accumulationBuffer->accumulate(temporaryBuffer, framesToProcess, &m_accumulationReadIndex, m_postDelayLength);
139 }
140
141 // Finally copy input to pre-delay.
142 if (m_preDelayLength > 0) {
143 memcpy(preDelayedSource, source, sizeof(float) * framesToProcess);
144 m_preReadWriteIndex += framesToProcess;
145
146 ASSERT(m_preReadWriteIndex <= m_preDelayLength);
147 if (m_preReadWriteIndex >= m_preDelayLength)
148 m_preReadWriteIndex = 0;
149 }
150
151 m_framesProcessed += framesToProcess;
152 }
153
reset()154 void ReverbConvolverStage::reset()
155 {
156 m_convolver->reset();
157 m_preDelayBuffer.zero();
158 m_accumulationReadIndex = 0;
159 m_inputReadIndex = 0;
160 m_framesProcessed = 0;
161 }
162
163 } // namespace WebCore
164
165 #endif // ENABLE(WEB_AUDIO)
166