[Webkit-unassigned] [Bug 34912] audio engine: add ReverbConvolver class

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Thu Mar 25 05:16:57 PDT 2010


https://bugs.webkit.org/show_bug.cgi?id=34912





--- Comment #28 from Jeremy Orlow <jorlow at chromium.org>  2010-03-25 05:16:57 PST ---
(From update of attachment 51461)
This + my other comments and this should be about ready for an r+.  Sorry it
took so long.


> diff --git a/WebCore/platform/audio/ReverbConvolver.cpp b/WebCore/platform/audio/ReverbConvolver.cpp
> new file mode 100644
> index 0000000..7ce7df8
> --- /dev/null
> +++ b/WebCore/platform/audio/ReverbConvolver.cpp
> @@ -0,0 +1,223 @@
> +/*
> + * Copyright (C) 2010 Google Inc. All rights reserved.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + *
> + * 1.  Redistributions of source code must retain the above copyright
> + *     notice, this list of conditions and the following disclaimer.
> + * 2.  Redistributions in binary form must reproduce the above copyright
> + *     notice, this list of conditions and the following disclaimer in the
> + *     documentation and/or other materials provided with the distribution.
> + * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
> + *     its contributors may be used to endorse or promote products derived
> + *     from this software without specific prior written permission.
> + *
> + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
> + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
> + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
> + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
> + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
> + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
> + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
> + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
> + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#include "config.h"
> +#include "ReverbConvolver.h"
> +
> +#include "Accelerate.h"
> +#include "AudioBus.h"
> +
> +namespace WebCore {
> +
> +const int InputBufferSize = 8 * 16384;
> +
> +// We only process the leading portion of the impulse response in the real-time thread.  We don't exceed this length.
> +// It turns out then, that the background thread has about 278msec of scheduling slop.
> +// Empirically, this has been found to be a good compromise between giving enough time for scheduling slop,
> +// while still minimizing the amount of processing done in the primary (high-priority) thread.
> +// This was found to be a good value on Mac OS X, and may work well on other platforms as well, assuming
> +// the very rough scheduling latencies are similar on these time-scales.  Of course, this code may need to be
> +// tuned for individual platforms if this assumption is found to be incorrect.
> +const size_t RealtimeFrameLimit = 8192  + 4096; // ~278msec @ 44.1KHz
> +
> +const size_t MinFFTSize = 256;
> +const size_t MaxRealtimeFFTSize = 2048;
> +
> +static void* BackgroundThreadDispatch(void* threadData)
> +{
> +    ReverbConvolver* reverbConvolver = static_cast<ReverbConvolver*>(threadData);
> +    reverbConvolver->backgroundThreadEntry();
> +    return 0;
> +}
> +
> +ReverbConvolver::ReverbConvolver(AudioChannel* impulseResponse, size_t renderSliceSize, size_t maxFFTSize, size_t convolverRenderPhase, bool useBackgroundThreads)
> +    : m_impulseResponseLength(impulseResponse->frameSize())
> +    , m_accumulationBuffer(impulseResponse->frameSize() + renderSliceSize)
> +    , m_inputBuffer(InputBufferSize)
> +    , m_renderSliceSize(renderSliceSize)
> +    , m_minFFTSize(MinFFTSize) // First stage will have this size - successive stages will double in size each time
> +    , m_maxFFTSize(maxFFTSize) // until we hit m_maxFFTSize
> +    , m_useBackgroundThreads(useBackgroundThreads)
> +    , m_wantsToExit(false)
> +    , m_moreInputBuffered(false)
> +{
> +    // If we are using background threads then don't exceed this FFT size for the
> +    // stages which run in the real-time thread.  This avoids having only one or two
> +    // large stages (size 16384 or so) at the end which take a lot of time every several
> +    // processing slices.  This way we amortize the cost over more processing slices.
> +    m_maxRealtimeFFTSize = MaxRealtimeFFTSize;
> +
> +    // For the moment, a good way to know if we have real-time constraint is to check if we're using background threads.
> +    // Otherwise, assume we're being run from a command-line tool.
> +    bool hasRealtimeConstraint = useBackgroundThreads;
> +
> +    float* response = impulseResponse->data();
> +    size_t totalResponseLength = impulseResponse->frameSize();
> +
> +    // Because we're not using direct-convolution in the leading portion, the reverb has an overall latency of half the first-stage FFT size
> +    size_t reverbTotalLatency = m_minFFTSize / 2;
> +
> +    size_t stageOffset = 0;
> +    int i = 0;
> +    size_t fftSize = m_minFFTSize;
> +    while (stageOffset < totalResponseLength) {
> +        size_t stageSize = fftSize / 2;
> +
> +        // For the last stage, it's possible that stageOffset is such that we're straddling the end
> +        // of the impulse response buffer (if we use stageSize), so reduce the last stage's length...
> +        if (stageSize + stageOffset > totalResponseLength)
> +            stageSize = totalResponseLength - stageOffset;
> +
> +        // This "staggers" the time when each FFT happens so they don't all happen at the same time
> +        int renderPhase = convolverRenderPhase + i * renderSliceSize;
> +
> +        ReverbConvolverStage* stage = new ReverbConvolverStage(response, totalResponseLength, reverbTotalLatency, stageOffset, stageSize, fftSize, renderPhase, renderSliceSize, &m_accumulationBuffer);

If you do keep this a raw pointer keep it as close as possible to where it gets
assigned to an OwnPtr.

And, not to beat a dead horse, but did you try making this an OwnPtr and then
call .release() on that when passing it to the array?  I think that'd work and
I think it'd be the best.

> +
> +        bool isBackgroundStage = false;
> +
> +        if (stageOffset <= RealtimeFrameLimit)
> +            m_stages.append(stage);
> +        else {
> +            if (this->useBackgroundThreads()) {
> +                m_backgroundStages.append(stage);
> +                isBackgroundStage = true;
> +            } else
> +                m_stages.append(stage);
> +        }
> +
> +        stageOffset += stageSize;
> +        ++i;
> +
> +        // Figure out next FFT size
> +        fftSize *= 2;
> +        if (hasRealtimeConstraint && !isBackgroundStage && fftSize > m_maxRealtimeFFTSize)
> +            fftSize = m_maxRealtimeFFTSize;
> +        if (fftSize > m_maxFFTSize)
> +            fftSize = m_maxFFTSize;
> +    }
> +
> +    // Start up background thread
> +    // FIXME: would be better to up the thread priority here.  It doesn't need to be real-time, but higher than the default...
> +    if (this->useBackgroundThreads() && m_backgroundStages.size() > 0)
> +        m_backgroundThread = createThread(BackgroundThreadDispatch, this, "convolution background thread");
> +    else
> +        m_backgroundThread = 0;
> +}
> +
> +ReverbConvolver::~ReverbConvolver()
> +{
> +    // Wait for background thread to stop
> +    if (useBackgroundThreads() && m_backgroundThread) {
> +        m_wantsToExit = true;
> +
> +        // Wake up thread so it can return - don't use MutexLocker since lock must be unlocked before we call waitForThreadCompletion().
> +        m_backgroundThreadLock.lock();
> +        m_moreInputBuffered = true;
> +        m_backgroundThreadCondition.signal();
> +        m_backgroundThreadLock.unlock();
> +
> +        waitForThreadCompletion(m_backgroundThread, 0);
> +    }
> +}
> +
> +void ReverbConvolver::backgroundThreadEntry()
> +{
> +    while (!m_wantsToExit) {
> +        // Check to see if there's any more input to consume
> +        int writeIndex = m_inputBuffer.writeIndex();
> +
> +        // Even though it doesn't seem like every stage needs to maintain its own version of readIndex 
> +        // we do this in case we want to run in more than one background thread.
> +        int readIndex;
> +
> +        while ((readIndex = m_backgroundStages[0]->inputReadIndex()) != writeIndex) { // FIXME: do better to detect buffer overrun...
> +            // FIXME: remove hard-coded value

Why is this hard coded?  Is removing it an optimization (fine to leave for now,
but describe more) or code cleanup (probably best to just do now if
possible...otherwise describe why not done now in fixme.)

> +            const int SliceSize = 128;
> +
> +            // Accumulate contributions from each stage
> +            for (size_t i = 0; i < m_backgroundStages.size(); ++i)
> +                m_backgroundStages[i]->processInBackground(this, SliceSize);
> +        }
> +
> +        // Wait for realtime thread to give us more input
> +        m_moreInputBuffered = false;        
> +        MutexLocker locker(m_backgroundThreadLock);
> +        while (!m_moreInputBuffered)
> +            m_backgroundThreadCondition.wait(m_backgroundThreadLock);

Shouldn't this while loop go at the beginning??  You can scope the MutexLocker
with {}'s.

> +    }
> +}
> +
> +size_t ReverbConvolver::impulseResponseLength()
> +{
> +    return m_impulseResponseLength;

Can probably be const and inlined.

> +}
> +
> +void ReverbConvolver::process(float* source, float* destination, size_t framesToProcess)
> +{
> +    bool isSafe = source && destination;
> +    ASSERT(isSafe);
> +    if (!isSafe)
> +        return;
> +
> +    // Feed input buffer (read by all threads)
> +    m_inputBuffer.write(source, framesToProcess);
> +
> +    // Accumulate contributions from each stage
> +    for (size_t i = 0; i < m_stages.size(); ++i)
> +        m_stages[i]->process(source, framesToProcess);
> +
> +    // Finally read from accumulation buffer
> +    m_accumulationBuffer.readAndClear(destination, framesToProcess);
> +        
> +    // Now that we've buffered more input, wake up our background thread.
> +    
> +    // Not using a MutexLocker looks strange, but we use a tryLock() instead because this is run on the real-time
> +    // thread where it is a disaster for the lock to be contended (causes audio glitching).  It's OK if we fail to
> +    // signal from time to time, since we'll get to it the next time we're called.  We're called repeatedly
> +    // and frequently (around every 3ms).  The background thread is processing well into the future and has a considerable amount of 
> +    // leeway here...
> +    if (m_backgroundThreadLock.tryLock()) {
> +        m_moreInputBuffered = true;
> +        m_backgroundThreadCondition.signal();
> +        m_backgroundThreadLock.unlock();
> +    }
> +}
> +
> +void ReverbConvolver::reset()
> +{
> +    for (size_t i = 0; i < m_stages.size(); ++i)
> +        m_stages[i]->reset();
> +
> +    for (size_t i = 0; i < m_backgroundStages.size(); ++i)
> +        m_backgroundStages[i]->reset();
> +
> +    m_accumulationBuffer.reset();
> +    m_inputBuffer.reset();
> +}
> +
> +} // namespace WebCore

> diff --git a/WebCore/platform/audio/ReverbConvolverStage.cpp b/WebCore/platform/audio/ReverbConvolverStage.cpp
> new file mode 100644
> index 0000000..f687753
> --- /dev/null
> +++ b/WebCore/platform/audio/ReverbConvolverStage.cpp
> @@ -0,0 +1,164 @@
> +/*
> + * Copyright (C) 2010 Google Inc. All rights reserved.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + *
> + * 1.  Redistributions of source code must retain the above copyright
> + *     notice, this list of conditions and the following disclaimer.
> + * 2.  Redistributions in binary form must reproduce the above copyright
> + *     notice, this list of conditions and the following disclaimer in the
> + *     documentation and/or other materials provided with the distribution.
> + * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
> + *     its contributors may be used to endorse or promote products derived
> + *     from this software without specific prior written permission.
> + *
> + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
> + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
> + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
> + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
> + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
> + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
> + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
> + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
> + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#include "config.h"
> +#include "ReverbConvolverStage.h"
> +
> +#include "Accelerate.h"
> +#include "ReverbAccumulationBuffer.h"
> +#include "ReverbConvolver.h"
> +#include "ReverbInputBuffer.h"
> +#include <wtf/OwnPtr.h>
> +#include <wtf/PassOwnPtr.h>
> +
> +namespace WebCore {
> +
> +ReverbConvolverStage::ReverbConvolverStage(float* impulseResponse, size_t responseLength, size_t reverbTotalLatency, size_t stageOffset, size_t stageLength,
> +                                           size_t fftSize, size_t renderPhase, size_t renderSliceSize, ReverbAccumulationBuffer* accumulationBuffer)
> +    : m_fftKernel(fftSize)
> +    , m_accumulationBuffer(accumulationBuffer)
> +    , m_accumulationReadIndex(0)
> +    , m_inputReadIndex(0)
> +    , m_impulseResponseLength(responseLength)
> +{
> +    ASSERT(impulseResponse);
> +    ASSERT(accumulationBuffer);
> +    
> +    m_fftKernel.doPaddedFFT(impulseResponse + stageOffset, stageLength);
> +
> +    m_convolver = new FFTConvolver(fftSize);
> +
> +    m_temporaryBuffer.allocate(renderSliceSize);
> +
> +    // The convolution stage at offset stageOffset needs to have a corresponding delay to cancel out the offset.
> +    size_t totalDelay = stageOffset + reverbTotalLatency;
> +
> +

Tidy up whitespace a bit....no 2 lines here...some of the lines above can maybe
be condensed a bit...etc

> +    // But, the FFT convolution itself incurs fftSize / 2 latency, so subtract this out...
> +    size_t halfSize = fftSize / 2;
> +    ASSERT(totalDelay >= halfSize);
> +    if (totalDelay >= halfSize)
> +        totalDelay -= halfSize;
> +
> +    // We divide up the total delay, into pre and post delay sections so that we can schedule at exactly the moment when the FFT will happen.
> +    // This is coordinated with the other stages, so they don't all do their FFTs at the same time...
> +

Maybe delete this newline.

> +    int maxPreDelayLength = std::min(halfSize, totalDelay);
> +    m_preDelayLength = totalDelay > 0 ? renderPhase % maxPreDelayLength : 0;
> +
> +    if (m_preDelayLength > totalDelay)
> +        m_preDelayLength = 0;
> +
> +    m_postDelayLength = totalDelay - m_preDelayLength;
> +    m_preReadWriteIndex = 0;
> +    m_framesProcessed = 0; // total frames processed so far
> +
> +    m_preDelayBuffer.allocate(m_preDelayLength < fftSize ? fftSize : m_preDelayLength);
> +}
> +
> +void ReverbConvolverStage::processInBackground(ReverbConvolver* convolver, size_t framesToProcess)
> +{
> +    ReverbInputBuffer* inputBuffer = convolver->inputBuffer();
> +    float* source = inputBuffer->directReadFrom(&m_inputReadIndex, framesToProcess);
> +    process(source, framesToProcess);
> +}
> +
> +void ReverbConvolverStage::process(float* source, size_t framesToProcess)
> +{
> +    ASSERT(source);
> +    if (!source)
> +        return;
> +    
> +    // Deal with pre-delay stream : note special handling of zero delay.
> +
> +    float* preDelayedSource;
> +    float* temporaryBuffer;
> +    if (m_preDelayLength > 0) {
> +        // Handles both the read case (call to process() ) and the write case (memcpy() )
> +        bool isPreDelaySafe = m_preReadWriteIndex + framesToProcess <= m_preDelayBuffer.size();
> +        ASSERT(isPreDelaySafe);
> +        if (!isPreDelaySafe)
> +            return;
> +
> +        bool isTemporaryBufferSafe = framesToProcess <= m_temporaryBuffer.size();
> +        ASSERT(isTemporaryBufferSafe);
> +        if (!isTemporaryBufferSafe)
> +            return;

This can be pulled out of the if statement and put below.

> +
> +        preDelayedSource = m_preDelayBuffer.data() + m_preReadWriteIndex;
> +        temporaryBuffer = m_temporaryBuffer;        
> +    } else {
> +        // Zero delay
> +        preDelayedSource = source;
> +        temporaryBuffer = m_preDelayBuffer.data();
> +        
> +        bool isTemporaryBufferSafe = framesToProcess <= m_preDelayBuffer.size();
> +        ASSERT(isTemporaryBufferSafe);
> +        if (!isTemporaryBufferSafe)
> +            return;
> +    }
> +
> +    int writeIndex = 0;
> +
> +    if (m_framesProcessed < m_preDelayLength) {
> +        // For the first m_preDelayLength frames don't process the convolver, instead simply buffer in the pre-delay.
> +        // But while buffering the pre-delay, we still need to update our index.
> +        m_accumulationBuffer->updateReadIndex(&m_accumulationReadIndex, framesToProcess);
> +    } else {
> +        // Now, run the convolution (into the delay buffer).
> +        // An expensive FFT will happen every fftSize / 2 frames.
> +        // We process in-place here...
> +        m_convolver->process(&m_fftKernel, preDelayedSource, temporaryBuffer, framesToProcess);
> +
> +        // Now accumulate into reverb's accumulation buffer.
> +        writeIndex = m_accumulationBuffer->accumulate(temporaryBuffer, framesToProcess, &m_accumulationReadIndex, m_postDelayLength);
> +    }
> +
> +    // Finally copy input to pre-delay.
> +    if (m_preDelayLength > 0) {
> +        memcpy(preDelayedSource, source, sizeof(float) * framesToProcess);
> +        m_preReadWriteIndex += framesToProcess;
> +
> +        ASSERT(m_preReadWriteIndex <= m_preDelayLength);
> +        if (m_preReadWriteIndex >= m_preDelayLength)
> +            m_preReadWriteIndex = 0;
> +    }
> +
> +    m_framesProcessed += framesToProcess;
> +}
> +
> +void ReverbConvolverStage::reset()
> +{
> +    m_convolver->reset();
> +    m_preDelayBuffer.zero();
> +    m_accumulationReadIndex = 0;
> +    m_inputReadIndex = 0;
> +    m_framesProcessed = 0;
> +}
> +
> +} // namespace WebCore
> diff --git a/WebCore/platform/audio/ReverbConvolverStage.h b/WebCore/platform/audio/ReverbConvolverStage.h
> new file mode 100644
> index 0000000..88351af
> --- /dev/null
> +++ b/WebCore/platform/audio/ReverbConvolverStage.h
> @@ -0,0 +1,83 @@
> +/*
> + * Copyright (C) 2010 Google Inc. All rights reserved.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + *
> + * 1.  Redistributions of source code must retain the above copyright
> + *     notice, this list of conditions and the following disclaimer.
> + * 2.  Redistributions in binary form must reproduce the above copyright
> + *     notice, this list of conditions and the following disclaimer in the
> + *     documentation and/or other materials provided with the distribution.
> + * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
> + *     its contributors may be used to endorse or promote products derived
> + *     from this software without specific prior written permission.
> + *
> + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
> + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
> + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
> + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
> + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
> + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
> + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
> + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
> + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#ifndef ReverbConvolverStage_h
> +#define ReverbConvolverStage_h
> +
> +#include "AudioFloatArray.h"
> +#include "FFTFrame.h"
> +#include <wtf/OwnPtr.h>
> +
> +namespace WebCore {
> +
> +class ReverbAccumulationBuffer;
> +class ReverbConvolver;
> +class FFTConvolver;
> +    
> +// A ReverbConvolverStage represents the convolution associated with a sub-section of a large impulse response.
> +// It incorporates a delay line to account for the offset of the sub-section within the larger impulse response.
> +class ReverbConvolverStage {
> +public:
> +    // renderPhase is useful to know so that we can manipulate the pre versus post delay so that stages will perform
> +    // their heavy work (FFT processing) on different slices to balance the load in a real-time thread.
> +    ReverbConvolverStage(float* impulseResponse, size_t responseLength, size_t reverbTotalLatency, size_t stageOffset, size_t stageLength,
> +                         size_t fftSize, size_t renderPhase, size_t renderSliceSize, ReverbAccumulationBuffer* accumulationBuffer);
> +
> +    // WARNING: framesToProcess must be such that it evenly divides the delay buffer size (stage_offset).
> +    void process(float* source, size_t framesToProcess);
> +
> +    void processInBackground(ReverbConvolver* convolver, size_t framesToProcess);
> +
> +    void reset();
> +
> +    // Useful for background processing
> +    int inputReadIndex() const { return m_inputReadIndex; }
> +
> +private:
> +    FFTFrame m_fftKernel;
> +    OwnPtr<FFTConvolver> m_convolver;
> +
> +    AudioFloatArray m_preDelayBuffer;
> +
> +    ReverbAccumulationBuffer* m_accumulationBuffer;

Gotta be careful about lifetime issues with stuff like this, but what you've
got looks good.  It's possible the destructor would kill the
RevertAccumulationBuffer first, but there should never be any
ReverbConvolverStage code running at that time, and this class should be
deleted in the same destructor.

> +    int m_accumulationReadIndex;
> +    int m_inputReadIndex;
> +
> +    size_t m_preDelayLength;
> +    size_t m_postDelayLength;
> +    size_t m_preReadWriteIndex;
> +    size_t m_framesProcessed;
> +
> +    AudioFloatArray m_temporaryBuffer;
> +
> +    size_t m_impulseResponseLength;
> +};
> +
> +} // namespace WebCore
> +
> +#endif // ReverbConvolverStage_h
> diff --git a/WebCore/platform/audio/ReverbInputBuffer.cpp b/WebCore/platform/audio/ReverbInputBuffer.cpp
> new file mode 100644
> index 0000000..1c4fb71
> --- /dev/null
> +++ b/WebCore/platform/audio/ReverbInputBuffer.cpp
> @@ -0,0 +1,79 @@
> +/*
> + * Copyright (C) 2010 Google Inc. All rights reserved.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + *
> + * 1.  Redistributions of source code must retain the above copyright
> + *     notice, this list of conditions and the following disclaimer.
> + * 2.  Redistributions in binary form must reproduce the above copyright
> + *     notice, this list of conditions and the following disclaimer in the
> + *     documentation and/or other materials provided with the distribution.
> + * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
> + *     its contributors may be used to endorse or promote products derived
> + *     from this software without specific prior written permission.
> + *
> + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
> + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
> + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
> + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
> + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
> + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
> + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
> + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
> + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#include "config.h"
> +#include "ReverbInputBuffer.h"
> +
> +namespace WebCore {
> +
> +ReverbInputBuffer::ReverbInputBuffer(size_t length)
> +    : m_buffer(length)
> +    , m_writeIndex(0)
> +{
> +}
> +
> +void ReverbInputBuffer::write(float* sourceP, size_t numberOfFrames)
> +{
> +    size_t bufferLength = m_buffer.size();
> +    bool isCopySafe = m_writeIndex + numberOfFrames <= bufferLength;
> +    ASSERT(isCopySafe);
> +    if (!isCopySafe)
> +        return;
> +        
> +    memcpy(m_buffer.data() + m_writeIndex, sourceP, sizeof(float) * numberOfFrames);
> +
> +    m_writeIndex += numberOfFrames;
> +    ASSERT(m_writeIndex <= bufferLength);
> +
> +    if (m_writeIndex >= bufferLength)
> +        m_writeIndex = 0;
> +}
> +
> +float* ReverbInputBuffer::directReadFrom(int* readIndex, size_t numberOfFrames)
> +{
> +    size_t bufferLength = m_buffer.size();
> +    bool isPointerGood = *readIndex >= 0 && *readIndex + numberOfFrames <= bufferLength;
> +    if (!isPointerGood)
> +        CRASH();

What would happen if you just returned here?  Maybe you could just fill in
zeros after asserting?

> +        
> +    float* sourceP = m_buffer;
> +    float* p = sourceP + *readIndex;
> +
> +    // Update readIndex
> +    *readIndex = (*readIndex + numberOfFrames) % bufferLength;
> +
> +    return p;
> +}
> +
> +void ReverbInputBuffer::reset()
> +{
> +    m_buffer.zero();
> +    m_writeIndex = 0;
> +}
> +
> +} // namespace WebCore

-- 
Configure bugmail: https://bugs.webkit.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.



More information about the webkit-unassigned mailing list