1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/blink/ReverbConvolver.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,99 @@ 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 +#ifndef ReverbConvolver_h 1.33 +#define ReverbConvolver_h 1.34 + 1.35 +#include "ReverbAccumulationBuffer.h" 1.36 +#include "ReverbInputBuffer.h" 1.37 +#include "nsAutoPtr.h" 1.38 +#include "mozilla/MemoryReporting.h" 1.39 +#ifdef LOG 1.40 +#undef LOG 1.41 +#endif 1.42 +#include "base/condition_variable.h" 1.43 +#include "base/lock.h" 1.44 +#include "base/thread.h" 1.45 + 1.46 +namespace WebCore { 1.47 + 1.48 +class ReverbConvolverStage; 1.49 + 1.50 +class ReverbConvolver { 1.51 +public: 1.52 + // maxFFTSize can be adjusted (from say 2048 to 32768) depending on how much precision is necessary. 1.53 + // For certain tweaky de-convolving applications the phase errors add up quickly and lead to non-sensical results with 1.54 + // larger FFT sizes and single-precision floats. In these cases 2048 is a good size. 1.55 + // If not doing multi-threaded convolution, then should not go > 8192. 1.56 + ReverbConvolver(const float* impulseResponseData, size_t impulseResponseLength, size_t renderSliceSize, size_t maxFFTSize, size_t convolverRenderPhase, bool useBackgroundThreads); 1.57 + ~ReverbConvolver(); 1.58 + 1.59 + void process(const float* sourceChannelData, size_t sourceChannelLength, 1.60 + float* destinationChannelData, size_t destinationChannelLength, 1.61 + size_t framesToProcess); 1.62 + void reset(); 1.63 + 1.64 + size_t impulseResponseLength() const { return m_impulseResponseLength; } 1.65 + 1.66 + ReverbInputBuffer* inputBuffer() { return &m_inputBuffer; } 1.67 + 1.68 + bool useBackgroundThreads() const { return m_useBackgroundThreads; } 1.69 + void backgroundThreadEntry(); 1.70 + 1.71 + size_t latencyFrames() const; 1.72 + 1.73 + size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 1.74 +private: 1.75 + nsTArray<nsAutoPtr<ReverbConvolverStage> > m_stages; 1.76 + nsTArray<nsAutoPtr<ReverbConvolverStage> > m_backgroundStages; 1.77 + size_t m_impulseResponseLength; 1.78 + 1.79 + ReverbAccumulationBuffer m_accumulationBuffer; 1.80 + 1.81 + // One or more background threads read from this input buffer which is fed from the realtime thread. 1.82 + ReverbInputBuffer m_inputBuffer; 1.83 + 1.84 + // First stage will be of size m_minFFTSize. Each next stage will be twice as big until we hit m_maxFFTSize. 1.85 + size_t m_minFFTSize; 1.86 + size_t m_maxFFTSize; 1.87 + 1.88 + // But don't exceed this size in the real-time thread (if we're doing background processing). 1.89 + size_t m_maxRealtimeFFTSize; 1.90 + 1.91 + // Background thread and synchronization 1.92 + base::Thread m_backgroundThread; 1.93 + Lock m_backgroundThreadLock; 1.94 + ConditionVariable m_backgroundThreadCondition; 1.95 + bool m_useBackgroundThreads; 1.96 + bool m_wantsToExit; 1.97 + bool m_moreInputBuffered; 1.98 +}; 1.99 + 1.100 +} // namespace WebCore 1.101 + 1.102 +#endif // ReverbConvolver_h