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 "FFTConvolver.h"
30 #include "mozilla/PodOperations.h"
31
32 using namespace mozilla;
33
34 namespace WebCore {
35
FFTConvolver(size_t fftSize,size_t renderPhase)36 FFTConvolver::FFTConvolver(size_t fftSize, size_t renderPhase)
37 : m_frame(fftSize), m_readWriteIndex(renderPhase % (fftSize / 2)) {
38 MOZ_ASSERT(fftSize >= 2 * WEBAUDIO_BLOCK_SIZE);
39 m_inputBuffer.SetLength(fftSize);
40 PodZero(m_inputBuffer.Elements(), fftSize);
41 m_outputBuffer.SetLength(fftSize);
42 PodZero(m_outputBuffer.Elements(), fftSize);
43 m_lastOverlapBuffer.SetLength(fftSize / 2);
44 PodZero(m_lastOverlapBuffer.Elements(), fftSize / 2);
45 }
46
sizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const47 size_t FFTConvolver::sizeOfExcludingThis(
48 mozilla::MallocSizeOf aMallocSizeOf) const {
49 size_t amount = 0;
50 amount += m_frame.SizeOfExcludingThis(aMallocSizeOf);
51 amount += m_inputBuffer.ShallowSizeOfExcludingThis(aMallocSizeOf);
52 amount += m_outputBuffer.ShallowSizeOfExcludingThis(aMallocSizeOf);
53 amount += m_lastOverlapBuffer.ShallowSizeOfExcludingThis(aMallocSizeOf);
54 return amount;
55 }
56
sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const57 size_t FFTConvolver::sizeOfIncludingThis(
58 mozilla::MallocSizeOf aMallocSizeOf) const {
59 return aMallocSizeOf(this) + sizeOfExcludingThis(aMallocSizeOf);
60 }
61
process(FFTBlock * fftKernel,const float * sourceP)62 const float* FFTConvolver::process(FFTBlock* fftKernel, const float* sourceP) {
63 size_t halfSize = fftSize() / 2;
64
65 // WEBAUDIO_BLOCK_SIZE must be an exact multiple of halfSize,
66 // halfSize must be a multiple of WEBAUDIO_BLOCK_SIZE
67 // and > WEBAUDIO_BLOCK_SIZE.
68 MOZ_ASSERT(halfSize % WEBAUDIO_BLOCK_SIZE == 0 &&
69 WEBAUDIO_BLOCK_SIZE <= halfSize);
70
71 // Copy samples to input buffer (note contraint above!)
72 float* inputP = m_inputBuffer.Elements();
73
74 MOZ_ASSERT(sourceP && inputP &&
75 m_readWriteIndex + WEBAUDIO_BLOCK_SIZE <= m_inputBuffer.Length());
76
77 memcpy(inputP + m_readWriteIndex, sourceP,
78 sizeof(float) * WEBAUDIO_BLOCK_SIZE);
79
80 float* outputP = m_outputBuffer.Elements();
81 m_readWriteIndex += WEBAUDIO_BLOCK_SIZE;
82
83 // Check if it's time to perform the next FFT
84 if (m_readWriteIndex == halfSize) {
85 // The input buffer is now filled (get frequency-domain version)
86 m_frame.PerformFFT(m_inputBuffer.Elements());
87 m_frame.Multiply(*fftKernel);
88 m_frame.GetInverseWithoutScaling(m_outputBuffer.Elements());
89
90 // Overlap-add 1st half from previous time
91 AudioBufferAddWithScale(m_lastOverlapBuffer.Elements(), 1.0f,
92 m_outputBuffer.Elements(), halfSize);
93
94 // Finally, save 2nd half of result
95 MOZ_ASSERT(m_outputBuffer.Length() == 2 * halfSize &&
96 m_lastOverlapBuffer.Length() == halfSize);
97
98 memcpy(m_lastOverlapBuffer.Elements(), m_outputBuffer.Elements() + halfSize,
99 sizeof(float) * halfSize);
100
101 // Reset index back to start for next time
102 m_readWriteIndex = 0;
103 }
104
105 return outputP + m_readWriteIndex;
106 }
107
reset()108 void FFTConvolver::reset() {
109 PodZero(m_lastOverlapBuffer.Elements(), m_lastOverlapBuffer.Length());
110 m_readWriteIndex = 0;
111 }
112
latencyFrames() const113 size_t FFTConvolver::latencyFrames() const {
114 return std::max<size_t>(fftSize() / 2, WEBAUDIO_BLOCK_SIZE) -
115 WEBAUDIO_BLOCK_SIZE;
116 }
117
118 } // namespace WebCore
119