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