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 "ReverbConvolver.h" michael@0: #include "ReverbConvolverStage.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: template<> michael@0: struct RunnableMethodTraits michael@0: { michael@0: static void RetainCallee(WebCore::ReverbConvolver* obj) {} michael@0: static void ReleaseCallee(WebCore::ReverbConvolver* obj) {} michael@0: }; michael@0: michael@0: namespace WebCore { michael@0: michael@0: const int InputBufferSize = 8 * 16384; michael@0: michael@0: // We only process the leading portion of the impulse response in the real-time thread. We don't exceed this length. michael@0: // It turns out then, that the background thread has about 278msec of scheduling slop. michael@0: // Empirically, this has been found to be a good compromise between giving enough time for scheduling slop, michael@0: // while still minimizing the amount of processing done in the primary (high-priority) thread. michael@0: // This was found to be a good value on Mac OS X, and may work well on other platforms as well, assuming michael@0: // the very rough scheduling latencies are similar on these time-scales. Of course, this code may need to be michael@0: // tuned for individual platforms if this assumption is found to be incorrect. michael@0: const size_t RealtimeFrameLimit = 8192 + 4096; // ~278msec @ 44.1KHz michael@0: michael@0: const size_t MinFFTSize = 128; michael@0: const size_t MaxRealtimeFFTSize = 2048; michael@0: michael@0: ReverbConvolver::ReverbConvolver(const float* impulseResponseData, size_t impulseResponseLength, size_t renderSliceSize, size_t maxFFTSize, size_t convolverRenderPhase, bool useBackgroundThreads) michael@0: : m_impulseResponseLength(impulseResponseLength) michael@0: , m_accumulationBuffer(impulseResponseLength + renderSliceSize) michael@0: , m_inputBuffer(InputBufferSize) michael@0: , m_minFFTSize(MinFFTSize) // First stage will have this size - successive stages will double in size each time michael@0: , m_maxFFTSize(maxFFTSize) // until we hit m_maxFFTSize michael@0: , m_backgroundThread("ConvolverWorker") michael@0: , m_backgroundThreadCondition(&m_backgroundThreadLock) michael@0: , m_useBackgroundThreads(useBackgroundThreads) michael@0: , m_wantsToExit(false) michael@0: , m_moreInputBuffered(false) michael@0: { michael@0: // If we are using background threads then don't exceed this FFT size for the michael@0: // stages which run in the real-time thread. This avoids having only one or two michael@0: // large stages (size 16384 or so) at the end which take a lot of time every several michael@0: // processing slices. This way we amortize the cost over more processing slices. michael@0: m_maxRealtimeFFTSize = MaxRealtimeFFTSize; michael@0: michael@0: // For the moment, a good way to know if we have real-time constraint is to check if we're using background threads. michael@0: // Otherwise, assume we're being run from a command-line tool. michael@0: bool hasRealtimeConstraint = useBackgroundThreads; michael@0: michael@0: const float* response = impulseResponseData; michael@0: size_t totalResponseLength = impulseResponseLength; michael@0: michael@0: // The total latency is zero because the direct-convolution is used in the leading portion. michael@0: size_t reverbTotalLatency = 0; michael@0: michael@0: size_t stageOffset = 0; michael@0: int i = 0; michael@0: size_t fftSize = m_minFFTSize; michael@0: while (stageOffset < totalResponseLength) { michael@0: size_t stageSize = fftSize / 2; michael@0: michael@0: // For the last stage, it's possible that stageOffset is such that we're straddling the end michael@0: // of the impulse response buffer (if we use stageSize), so reduce the last stage's length... michael@0: if (stageSize + stageOffset > totalResponseLength) michael@0: stageSize = totalResponseLength - stageOffset; michael@0: michael@0: // This "staggers" the time when each FFT happens so they don't all happen at the same time michael@0: int renderPhase = convolverRenderPhase + i * renderSliceSize; michael@0: michael@0: bool useDirectConvolver = !stageOffset; michael@0: michael@0: nsAutoPtr stage(new ReverbConvolverStage(response, totalResponseLength, reverbTotalLatency, stageOffset, stageSize, fftSize, renderPhase, renderSliceSize, &m_accumulationBuffer, useDirectConvolver)); michael@0: michael@0: bool isBackgroundStage = false; michael@0: michael@0: if (this->useBackgroundThreads() && stageOffset > RealtimeFrameLimit) { michael@0: m_backgroundStages.AppendElement(stage.forget()); michael@0: isBackgroundStage = true; michael@0: } else michael@0: m_stages.AppendElement(stage.forget()); michael@0: michael@0: stageOffset += stageSize; michael@0: ++i; michael@0: michael@0: if (!useDirectConvolver) { michael@0: // Figure out next FFT size michael@0: fftSize *= 2; michael@0: } michael@0: michael@0: if (hasRealtimeConstraint && !isBackgroundStage && fftSize > m_maxRealtimeFFTSize) michael@0: fftSize = m_maxRealtimeFFTSize; michael@0: if (fftSize > m_maxFFTSize) michael@0: fftSize = m_maxFFTSize; michael@0: } michael@0: michael@0: // Start up background thread michael@0: // FIXME: would be better to up the thread priority here. It doesn't need to be real-time, but higher than the default... michael@0: if (this->useBackgroundThreads() && m_backgroundStages.Length() > 0) { michael@0: if (!m_backgroundThread.Start()) { michael@0: NS_WARNING("Cannot start convolver thread."); michael@0: return; michael@0: } michael@0: CancelableTask* task = NewRunnableMethod(this, &ReverbConvolver::backgroundThreadEntry); michael@0: m_backgroundThread.message_loop()->PostTask(FROM_HERE, task); michael@0: } michael@0: } michael@0: michael@0: ReverbConvolver::~ReverbConvolver() michael@0: { michael@0: // Wait for background thread to stop michael@0: if (useBackgroundThreads() && m_backgroundThread.IsRunning()) { michael@0: m_wantsToExit = true; michael@0: michael@0: // Wake up thread so it can return michael@0: { michael@0: AutoLock locker(m_backgroundThreadLock); michael@0: m_moreInputBuffered = true; michael@0: m_backgroundThreadCondition.Signal(); michael@0: } michael@0: michael@0: m_backgroundThread.Stop(); michael@0: } michael@0: } michael@0: michael@0: size_t ReverbConvolver::sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: size_t amount = aMallocSizeOf(this); michael@0: amount += m_stages.SizeOfExcludingThis(aMallocSizeOf); michael@0: for (size_t i = 0; i < m_stages.Length(); i++) { michael@0: if (m_stages[i]) { michael@0: amount += m_stages[i]->sizeOfIncludingThis(aMallocSizeOf); michael@0: } michael@0: } michael@0: michael@0: amount += m_backgroundStages.SizeOfExcludingThis(aMallocSizeOf); michael@0: for (size_t i = 0; i < m_backgroundStages.Length(); i++) { michael@0: if (m_backgroundStages[i]) { michael@0: amount += m_backgroundStages[i]->sizeOfIncludingThis(aMallocSizeOf); michael@0: } michael@0: } michael@0: michael@0: // NB: The buffer sizes are static, so even though they might be accessed michael@0: // in another thread it's safe to measure them. michael@0: amount += m_accumulationBuffer.sizeOfExcludingThis(aMallocSizeOf); michael@0: amount += m_inputBuffer.sizeOfExcludingThis(aMallocSizeOf); michael@0: michael@0: // Possible future measurements: michael@0: // - m_backgroundThread michael@0: // - m_backgroundThreadLock michael@0: // - m_backgroundThreadCondition michael@0: return amount; michael@0: } michael@0: michael@0: void ReverbConvolver::backgroundThreadEntry() michael@0: { michael@0: while (!m_wantsToExit) { michael@0: // Wait for realtime thread to give us more input michael@0: m_moreInputBuffered = false; michael@0: { michael@0: AutoLock locker(m_backgroundThreadLock); michael@0: while (!m_moreInputBuffered && !m_wantsToExit) michael@0: m_backgroundThreadCondition.Wait(); michael@0: } michael@0: michael@0: // Process all of the stages until their read indices reach the input buffer's write index michael@0: int writeIndex = m_inputBuffer.writeIndex(); michael@0: michael@0: // Even though it doesn't seem like every stage needs to maintain its own version of readIndex michael@0: // we do this in case we want to run in more than one background thread. michael@0: int readIndex; michael@0: michael@0: while ((readIndex = m_backgroundStages[0]->inputReadIndex()) != writeIndex) { // FIXME: do better to detect buffer overrun... michael@0: // The ReverbConvolverStages need to process in amounts which evenly divide half the FFT size michael@0: const int SliceSize = MinFFTSize / 2; michael@0: michael@0: // Accumulate contributions from each stage michael@0: for (size_t i = 0; i < m_backgroundStages.Length(); ++i) michael@0: m_backgroundStages[i]->processInBackground(this, SliceSize); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void ReverbConvolver::process(const float* sourceChannelData, size_t sourceChannelLength, michael@0: float* destinationChannelData, size_t destinationChannelLength, michael@0: size_t framesToProcess) michael@0: { michael@0: bool isSafe = sourceChannelData && destinationChannelData && sourceChannelLength >= framesToProcess && destinationChannelLength >= framesToProcess; michael@0: MOZ_ASSERT(isSafe); michael@0: if (!isSafe) michael@0: return; michael@0: michael@0: const float* source = sourceChannelData; michael@0: float* destination = destinationChannelData; michael@0: bool isDataSafe = source && destination; michael@0: MOZ_ASSERT(isDataSafe); michael@0: if (!isDataSafe) michael@0: return; michael@0: michael@0: // Feed input buffer (read by all threads) michael@0: m_inputBuffer.write(source, framesToProcess); michael@0: michael@0: // Accumulate contributions from each stage michael@0: for (size_t i = 0; i < m_stages.Length(); ++i) michael@0: m_stages[i]->process(source, framesToProcess); michael@0: michael@0: // Finally read from accumulation buffer michael@0: m_accumulationBuffer.readAndClear(destination, framesToProcess); michael@0: michael@0: // Now that we've buffered more input, wake up our background thread. michael@0: michael@0: // Not using a MutexLocker looks strange, but we use a tryLock() instead because this is run on the real-time michael@0: // thread where it is a disaster for the lock to be contended (causes audio glitching). It's OK if we fail to michael@0: // signal from time to time, since we'll get to it the next time we're called. We're called repeatedly michael@0: // and frequently (around every 3ms). The background thread is processing well into the future and has a considerable amount of michael@0: // leeway here... michael@0: if (m_backgroundThreadLock.Try()) { michael@0: m_moreInputBuffered = true; michael@0: m_backgroundThreadCondition.Signal(); michael@0: m_backgroundThreadLock.Release(); michael@0: } michael@0: } michael@0: michael@0: void ReverbConvolver::reset() michael@0: { michael@0: for (size_t i = 0; i < m_stages.Length(); ++i) michael@0: m_stages[i]->reset(); michael@0: michael@0: for (size_t i = 0; i < m_backgroundStages.Length(); ++i) michael@0: m_backgroundStages[i]->reset(); michael@0: michael@0: m_accumulationBuffer.reset(); michael@0: m_inputBuffer.reset(); michael@0: } michael@0: michael@0: size_t ReverbConvolver::latencyFrames() const michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: } // namespace WebCore