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 "ReverbConvolverStage.h"
30 
31 #include "ReverbAccumulationBuffer.h"
32 #include "ReverbConvolver.h"
33 #include "ReverbInputBuffer.h"
34 #include "mozilla/PodOperations.h"
35 
36 using namespace mozilla;
37 
38 namespace WebCore {
39 
ReverbConvolverStage(const float * impulseResponse,size_t,size_t reverbTotalLatency,size_t stageOffset,size_t stageLength,size_t fftSize,size_t renderPhase,ReverbAccumulationBuffer * accumulationBuffer)40 ReverbConvolverStage::ReverbConvolverStage(
41     const float* impulseResponse, size_t, size_t reverbTotalLatency,
42     size_t stageOffset, size_t stageLength, size_t fftSize, size_t renderPhase,
43     ReverbAccumulationBuffer* accumulationBuffer)
44     : m_accumulationBuffer(accumulationBuffer),
45       m_accumulationReadIndex(0),
46       m_inputReadIndex(0) {
47   MOZ_ASSERT(impulseResponse);
48   MOZ_ASSERT(accumulationBuffer);
49 
50   m_fftKernel = MakeUnique<FFTBlock>(fftSize);
51   m_fftKernel->PadAndMakeScaledDFT(impulseResponse + stageOffset, stageLength);
52   m_fftConvolver = MakeUnique<FFTConvolver>(fftSize, renderPhase);
53 
54   // The convolution stage at offset stageOffset needs to have a corresponding
55   // delay to cancel out the offset.
56   size_t totalDelay = stageOffset + reverbTotalLatency;
57 
58   // But, the FFT convolution itself incurs latency, so subtract this out...
59   size_t fftLatency = m_fftConvolver->latencyFrames();
60   MOZ_ASSERT(totalDelay >= fftLatency);
61   totalDelay -= fftLatency;
62 
63   m_postDelayLength = totalDelay;
64 }
65 
sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const66 size_t ReverbConvolverStage::sizeOfIncludingThis(
67     mozilla::MallocSizeOf aMallocSizeOf) const {
68   size_t amount = aMallocSizeOf(this);
69 
70   if (m_fftKernel) {
71     amount += m_fftKernel->SizeOfIncludingThis(aMallocSizeOf);
72   }
73 
74   if (m_fftConvolver) {
75     amount += m_fftConvolver->sizeOfIncludingThis(aMallocSizeOf);
76   }
77 
78   return amount;
79 }
80 
processInBackground(ReverbConvolver * convolver)81 void ReverbConvolverStage::processInBackground(ReverbConvolver* convolver) {
82   ReverbInputBuffer* inputBuffer = convolver->inputBuffer();
83   float* source =
84       inputBuffer->directReadFrom(&m_inputReadIndex, WEBAUDIO_BLOCK_SIZE);
85   process(source);
86 }
87 
process(const float * source)88 void ReverbConvolverStage::process(const float* source) {
89   MOZ_ASSERT(source);
90   if (!source) return;
91 
92   // Now, run the convolution (into the delay buffer).
93   // An expensive FFT will happen every fftSize / 2 frames.
94   const float* output = m_fftConvolver->process(m_fftKernel.get(), source);
95 
96   // Now accumulate into reverb's accumulation buffer.
97   m_accumulationBuffer->accumulate(output, WEBAUDIO_BLOCK_SIZE,
98                                    &m_accumulationReadIndex, m_postDelayLength);
99 }
100 
101 }  // namespace WebCore
102