michael@0: /* michael@0: * Copyright (C) 2010 Google Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of michael@0: * its contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED michael@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE michael@0: * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY michael@0: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; michael@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND michael@0: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #include "FFTConvolver.h" michael@0: #include "mozilla/PodOperations.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: namespace WebCore { michael@0: michael@0: FFTConvolver::FFTConvolver(size_t fftSize) michael@0: : m_frame(fftSize) michael@0: , m_readWriteIndex(0) michael@0: { michael@0: m_inputBuffer.SetLength(fftSize); michael@0: PodZero(m_inputBuffer.Elements(), fftSize); michael@0: m_outputBuffer.SetLength(fftSize); michael@0: PodZero(m_outputBuffer.Elements(), fftSize); michael@0: m_lastOverlapBuffer.SetLength(fftSize / 2); michael@0: PodZero(m_lastOverlapBuffer.Elements(), fftSize / 2); michael@0: } michael@0: michael@0: size_t FFTConvolver::sizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: size_t amount = 0; michael@0: amount += m_frame.SizeOfExcludingThis(aMallocSizeOf); michael@0: amount += m_inputBuffer.SizeOfExcludingThis(aMallocSizeOf); michael@0: amount += m_outputBuffer.SizeOfExcludingThis(aMallocSizeOf); michael@0: amount += m_lastOverlapBuffer.SizeOfExcludingThis(aMallocSizeOf); michael@0: return amount; michael@0: } michael@0: michael@0: size_t FFTConvolver::sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: return aMallocSizeOf(this) + sizeOfExcludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: void FFTConvolver::process(FFTBlock* fftKernel, const float* sourceP, float* destP, size_t framesToProcess) michael@0: { michael@0: size_t halfSize = fftSize() / 2; michael@0: michael@0: // framesToProcess must be an exact multiple of halfSize, michael@0: // or halfSize is a multiple of framesToProcess when halfSize > framesToProcess. michael@0: bool isGood = !(halfSize % framesToProcess && framesToProcess % halfSize); michael@0: MOZ_ASSERT(isGood); michael@0: if (!isGood) michael@0: return; michael@0: michael@0: size_t numberOfDivisions = halfSize <= framesToProcess ? (framesToProcess / halfSize) : 1; michael@0: size_t divisionSize = numberOfDivisions == 1 ? framesToProcess : halfSize; michael@0: michael@0: for (size_t i = 0; i < numberOfDivisions; ++i, sourceP += divisionSize, destP += divisionSize) { michael@0: // Copy samples to input buffer (note contraint above!) michael@0: float* inputP = m_inputBuffer.Elements(); michael@0: michael@0: // Sanity check michael@0: bool isCopyGood1 = sourceP && inputP && m_readWriteIndex + divisionSize <= m_inputBuffer.Length(); michael@0: MOZ_ASSERT(isCopyGood1); michael@0: if (!isCopyGood1) michael@0: return; michael@0: michael@0: memcpy(inputP + m_readWriteIndex, sourceP, sizeof(float) * divisionSize); michael@0: michael@0: // Copy samples from output buffer michael@0: float* outputP = m_outputBuffer.Elements(); michael@0: michael@0: // Sanity check michael@0: bool isCopyGood2 = destP && outputP && m_readWriteIndex + divisionSize <= m_outputBuffer.Length(); michael@0: MOZ_ASSERT(isCopyGood2); michael@0: if (!isCopyGood2) michael@0: return; michael@0: michael@0: memcpy(destP, outputP + m_readWriteIndex, sizeof(float) * divisionSize); michael@0: m_readWriteIndex += divisionSize; michael@0: michael@0: // Check if it's time to perform the next FFT michael@0: if (m_readWriteIndex == halfSize) { michael@0: // The input buffer is now filled (get frequency-domain version) michael@0: m_frame.PerformFFT(m_inputBuffer.Elements()); michael@0: m_frame.Multiply(*fftKernel); michael@0: m_frame.GetInverseWithoutScaling(m_outputBuffer.Elements()); michael@0: michael@0: // Overlap-add 1st half from previous time michael@0: AudioBufferAddWithScale(m_lastOverlapBuffer.Elements(), 1.0f, michael@0: m_outputBuffer.Elements(), halfSize); michael@0: michael@0: // Finally, save 2nd half of result michael@0: bool isCopyGood3 = m_outputBuffer.Length() == 2 * halfSize && m_lastOverlapBuffer.Length() == halfSize; michael@0: MOZ_ASSERT(isCopyGood3); michael@0: if (!isCopyGood3) michael@0: return; michael@0: michael@0: memcpy(m_lastOverlapBuffer.Elements(), m_outputBuffer.Elements() + halfSize, sizeof(float) * halfSize); michael@0: michael@0: // Reset index back to start for next time michael@0: m_readWriteIndex = 0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void FFTConvolver::reset() michael@0: { michael@0: PodZero(m_lastOverlapBuffer.Elements(), m_lastOverlapBuffer.Length()); michael@0: m_readWriteIndex = 0; michael@0: } michael@0: michael@0: } // namespace WebCore