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