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 "platform/audio/ReverbConvolverStage.h"
34
35#include "platform/audio/ReverbAccumulationBuffer.h"
36#include "platform/audio/ReverbConvolver.h"
37#include "platform/audio/ReverbInputBuffer.h"
38#include "platform/audio/VectorMath.h"
39#include "wtf/PassOwnPtr.h"
40
41namespace blink {
42
43using namespace VectorMath;
44
45ReverbConvolverStage::ReverbConvolverStage(const float* impulseResponse, size_t, size_t reverbTotalLatency, size_t stageOffset, size_t stageLength,
46                                           size_t fftSize, size_t renderPhase, size_t renderSliceSize, ReverbAccumulationBuffer* accumulationBuffer, bool directMode)
47    : m_accumulationBuffer(accumulationBuffer)
48    , m_accumulationReadIndex(0)
49    , m_inputReadIndex(0)
50    , m_directMode(directMode)
51{
52    ASSERT(impulseResponse);
53    ASSERT(accumulationBuffer);
54
55    if (!m_directMode) {
56        m_fftKernel = adoptPtr(new FFTFrame(fftSize));
57        m_fftKernel->doPaddedFFT(impulseResponse + stageOffset, stageLength);
58        m_fftConvolver = adoptPtr(new FFTConvolver(fftSize));
59    } else {
60        ASSERT(!stageOffset);
61        ASSERT(stageLength <= fftSize / 2);
62
63        m_directKernel = adoptPtr(new AudioFloatArray(fftSize / 2));
64        m_directKernel->copyToRange(impulseResponse, 0, stageLength);
65        m_directConvolver = adoptPtr(new DirectConvolver(renderSliceSize));
66    }
67    m_temporaryBuffer.allocate(renderSliceSize);
68
69    // The convolution stage at offset stageOffset needs to have a corresponding delay to cancel out the offset.
70    size_t totalDelay = stageOffset + reverbTotalLatency;
71
72    // But, the FFT convolution itself incurs fftSize / 2 latency, so subtract this out...
73    size_t halfSize = fftSize / 2;
74    if (!m_directMode) {
75        ASSERT(totalDelay >= halfSize);
76        if (totalDelay >= halfSize)
77            totalDelay -= halfSize;
78    }
79
80    // 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.
81    // This is coordinated with the other stages, so they don't all do their FFTs at the same time...
82    int maxPreDelayLength = std::min(halfSize, totalDelay);
83    m_preDelayLength = totalDelay > 0 ? renderPhase % maxPreDelayLength : 0;
84    if (m_preDelayLength > totalDelay)
85        m_preDelayLength = 0;
86
87    m_postDelayLength = totalDelay - m_preDelayLength;
88    m_preReadWriteIndex = 0;
89    m_framesProcessed = 0; // total frames processed so far
90
91    size_t delayBufferSize = m_preDelayLength < fftSize ? fftSize : m_preDelayLength;
92    delayBufferSize = delayBufferSize < renderSliceSize ? renderSliceSize : delayBufferSize;
93    m_preDelayBuffer.allocate(delayBufferSize);
94}
95
96void ReverbConvolverStage::processInBackground(ReverbConvolver* convolver, size_t framesToProcess)
97{
98    ReverbInputBuffer* inputBuffer = convolver->inputBuffer();
99    float* source = inputBuffer->directReadFrom(&m_inputReadIndex, framesToProcess);
100    process(source, framesToProcess);
101}
102
103void ReverbConvolverStage::process(const float* source, size_t framesToProcess)
104{
105    ASSERT(source);
106    if (!source)
107        return;
108
109    // Deal with pre-delay stream : note special handling of zero delay.
110
111    const float* preDelayedSource;
112    float* preDelayedDestination;
113    float* temporaryBuffer;
114    bool isTemporaryBufferSafe = false;
115    if (m_preDelayLength > 0) {
116        // Handles both the read case (call to process() ) and the write case (memcpy() )
117        bool isPreDelaySafe = m_preReadWriteIndex + framesToProcess <= m_preDelayBuffer.size();
118        ASSERT(isPreDelaySafe);
119        if (!isPreDelaySafe)
120            return;
121
122        isTemporaryBufferSafe = framesToProcess <= m_temporaryBuffer.size();
123
124        preDelayedDestination = m_preDelayBuffer.data() + m_preReadWriteIndex;
125        preDelayedSource = preDelayedDestination;
126        temporaryBuffer = m_temporaryBuffer.data();
127    } else {
128        // Zero delay
129        preDelayedDestination = 0;
130        preDelayedSource = source;
131        temporaryBuffer = m_preDelayBuffer.data();
132
133        isTemporaryBufferSafe = framesToProcess <= m_preDelayBuffer.size();
134    }
135
136    ASSERT(isTemporaryBufferSafe);
137    if (!isTemporaryBufferSafe)
138        return;
139
140    if (m_framesProcessed < m_preDelayLength) {
141        // For the first m_preDelayLength frames don't process the convolver, instead simply buffer in the pre-delay.
142        // But while buffering the pre-delay, we still need to update our index.
143        m_accumulationBuffer->updateReadIndex(&m_accumulationReadIndex, framesToProcess);
144    } else {
145        // Now, run the convolution (into the delay buffer).
146        // An expensive FFT will happen every fftSize / 2 frames.
147        // We process in-place here...
148        if (!m_directMode)
149            m_fftConvolver->process(m_fftKernel.get(), preDelayedSource, temporaryBuffer, framesToProcess);
150        else
151            m_directConvolver->process(m_directKernel.get(), preDelayedSource, temporaryBuffer, framesToProcess);
152
153        // Now accumulate into reverb's accumulation buffer.
154        m_accumulationBuffer->accumulate(temporaryBuffer, framesToProcess, &m_accumulationReadIndex, m_postDelayLength);
155    }
156
157    // Finally copy input to pre-delay.
158    if (m_preDelayLength > 0) {
159        memcpy(preDelayedDestination, source, sizeof(float) * framesToProcess);
160        m_preReadWriteIndex += framesToProcess;
161
162        ASSERT(m_preReadWriteIndex <= m_preDelayLength);
163        if (m_preReadWriteIndex >= m_preDelayLength)
164            m_preReadWriteIndex = 0;
165    }
166
167    m_framesProcessed += framesToProcess;
168}
169
170void ReverbConvolverStage::reset()
171{
172    if (!m_directMode)
173        m_fftConvolver->reset();
174    else
175        m_directConvolver->reset();
176    m_preDelayBuffer.zero();
177    m_accumulationReadIndex = 0;
178    m_inputReadIndex = 0;
179    m_framesProcessed = 0;
180}
181
182} // namespace blink
183
184#endif // ENABLE(WEB_AUDIO)
185