1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/blink/FFTConvolver.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,132 @@ 1.4 +/* 1.5 + * Copyright (C) 2010 Google Inc. All rights reserved. 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1.11 + * 1. Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 2. Redistributions in binary form must reproduce the above copyright 1.14 + * notice, this list of conditions and the following disclaimer in the 1.15 + * documentation and/or other materials provided with the distribution. 1.16 + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 1.17 + * its contributors may be used to endorse or promote products derived 1.18 + * from this software without specific prior written permission. 1.19 + * 1.20 + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 1.21 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1.22 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1.23 + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 1.24 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.25 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 1.26 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 1.27 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.28 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.29 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 + 1.32 +#include "FFTConvolver.h" 1.33 +#include "mozilla/PodOperations.h" 1.34 + 1.35 +using namespace mozilla; 1.36 + 1.37 +namespace WebCore { 1.38 + 1.39 +FFTConvolver::FFTConvolver(size_t fftSize) 1.40 + : m_frame(fftSize) 1.41 + , m_readWriteIndex(0) 1.42 +{ 1.43 + m_inputBuffer.SetLength(fftSize); 1.44 + PodZero(m_inputBuffer.Elements(), fftSize); 1.45 + m_outputBuffer.SetLength(fftSize); 1.46 + PodZero(m_outputBuffer.Elements(), fftSize); 1.47 + m_lastOverlapBuffer.SetLength(fftSize / 2); 1.48 + PodZero(m_lastOverlapBuffer.Elements(), fftSize / 2); 1.49 +} 1.50 + 1.51 +size_t FFTConvolver::sizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const 1.52 +{ 1.53 + size_t amount = 0; 1.54 + amount += m_frame.SizeOfExcludingThis(aMallocSizeOf); 1.55 + amount += m_inputBuffer.SizeOfExcludingThis(aMallocSizeOf); 1.56 + amount += m_outputBuffer.SizeOfExcludingThis(aMallocSizeOf); 1.57 + amount += m_lastOverlapBuffer.SizeOfExcludingThis(aMallocSizeOf); 1.58 + return amount; 1.59 +} 1.60 + 1.61 +size_t FFTConvolver::sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const 1.62 +{ 1.63 + return aMallocSizeOf(this) + sizeOfExcludingThis(aMallocSizeOf); 1.64 +} 1.65 + 1.66 +void FFTConvolver::process(FFTBlock* fftKernel, const float* sourceP, float* destP, size_t framesToProcess) 1.67 +{ 1.68 + size_t halfSize = fftSize() / 2; 1.69 + 1.70 + // framesToProcess must be an exact multiple of halfSize, 1.71 + // or halfSize is a multiple of framesToProcess when halfSize > framesToProcess. 1.72 + bool isGood = !(halfSize % framesToProcess && framesToProcess % halfSize); 1.73 + MOZ_ASSERT(isGood); 1.74 + if (!isGood) 1.75 + return; 1.76 + 1.77 + size_t numberOfDivisions = halfSize <= framesToProcess ? (framesToProcess / halfSize) : 1; 1.78 + size_t divisionSize = numberOfDivisions == 1 ? framesToProcess : halfSize; 1.79 + 1.80 + for (size_t i = 0; i < numberOfDivisions; ++i, sourceP += divisionSize, destP += divisionSize) { 1.81 + // Copy samples to input buffer (note contraint above!) 1.82 + float* inputP = m_inputBuffer.Elements(); 1.83 + 1.84 + // Sanity check 1.85 + bool isCopyGood1 = sourceP && inputP && m_readWriteIndex + divisionSize <= m_inputBuffer.Length(); 1.86 + MOZ_ASSERT(isCopyGood1); 1.87 + if (!isCopyGood1) 1.88 + return; 1.89 + 1.90 + memcpy(inputP + m_readWriteIndex, sourceP, sizeof(float) * divisionSize); 1.91 + 1.92 + // Copy samples from output buffer 1.93 + float* outputP = m_outputBuffer.Elements(); 1.94 + 1.95 + // Sanity check 1.96 + bool isCopyGood2 = destP && outputP && m_readWriteIndex + divisionSize <= m_outputBuffer.Length(); 1.97 + MOZ_ASSERT(isCopyGood2); 1.98 + if (!isCopyGood2) 1.99 + return; 1.100 + 1.101 + memcpy(destP, outputP + m_readWriteIndex, sizeof(float) * divisionSize); 1.102 + m_readWriteIndex += divisionSize; 1.103 + 1.104 + // Check if it's time to perform the next FFT 1.105 + if (m_readWriteIndex == halfSize) { 1.106 + // The input buffer is now filled (get frequency-domain version) 1.107 + m_frame.PerformFFT(m_inputBuffer.Elements()); 1.108 + m_frame.Multiply(*fftKernel); 1.109 + m_frame.GetInverseWithoutScaling(m_outputBuffer.Elements()); 1.110 + 1.111 + // Overlap-add 1st half from previous time 1.112 + AudioBufferAddWithScale(m_lastOverlapBuffer.Elements(), 1.0f, 1.113 + m_outputBuffer.Elements(), halfSize); 1.114 + 1.115 + // Finally, save 2nd half of result 1.116 + bool isCopyGood3 = m_outputBuffer.Length() == 2 * halfSize && m_lastOverlapBuffer.Length() == halfSize; 1.117 + MOZ_ASSERT(isCopyGood3); 1.118 + if (!isCopyGood3) 1.119 + return; 1.120 + 1.121 + memcpy(m_lastOverlapBuffer.Elements(), m_outputBuffer.Elements() + halfSize, sizeof(float) * halfSize); 1.122 + 1.123 + // Reset index back to start for next time 1.124 + m_readWriteIndex = 0; 1.125 + } 1.126 + } 1.127 +} 1.128 + 1.129 +void FFTConvolver::reset() 1.130 +{ 1.131 + PodZero(m_lastOverlapBuffer.Elements(), m_lastOverlapBuffer.Length()); 1.132 + m_readWriteIndex = 0; 1.133 +} 1.134 + 1.135 +} // namespace WebCore