1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/blink/ReverbAccumulationBuffer.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,116 @@ 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 +#include "ReverbAccumulationBuffer.h" 1.33 +#include "AudioNodeEngine.h" 1.34 +#include "mozilla/PodOperations.h" 1.35 +#include <algorithm> 1.36 + 1.37 +using namespace mozilla; 1.38 + 1.39 +namespace WebCore { 1.40 + 1.41 +ReverbAccumulationBuffer::ReverbAccumulationBuffer(size_t length) 1.42 + : m_readIndex(0) 1.43 + , m_readTimeFrame(0) 1.44 +{ 1.45 + m_buffer.SetLength(length); 1.46 + PodZero(m_buffer.Elements(), length); 1.47 +} 1.48 + 1.49 +void ReverbAccumulationBuffer::readAndClear(float* destination, size_t numberOfFrames) 1.50 +{ 1.51 + size_t bufferLength = m_buffer.Length(); 1.52 + bool isCopySafe = m_readIndex <= bufferLength && numberOfFrames <= bufferLength; 1.53 + 1.54 + MOZ_ASSERT(isCopySafe); 1.55 + if (!isCopySafe) 1.56 + return; 1.57 + 1.58 + size_t framesAvailable = bufferLength - m_readIndex; 1.59 + size_t numberOfFrames1 = std::min(numberOfFrames, framesAvailable); 1.60 + size_t numberOfFrames2 = numberOfFrames - numberOfFrames1; 1.61 + 1.62 + float* source = m_buffer.Elements(); 1.63 + memcpy(destination, source + m_readIndex, sizeof(float) * numberOfFrames1); 1.64 + memset(source + m_readIndex, 0, sizeof(float) * numberOfFrames1); 1.65 + 1.66 + // Handle wrap-around if necessary 1.67 + if (numberOfFrames2 > 0) { 1.68 + memcpy(destination + numberOfFrames1, source, sizeof(float) * numberOfFrames2); 1.69 + memset(source, 0, sizeof(float) * numberOfFrames2); 1.70 + } 1.71 + 1.72 + m_readIndex = (m_readIndex + numberOfFrames) % bufferLength; 1.73 + m_readTimeFrame += numberOfFrames; 1.74 +} 1.75 + 1.76 +void ReverbAccumulationBuffer::updateReadIndex(int* readIndex, size_t numberOfFrames) const 1.77 +{ 1.78 + // Update caller's readIndex 1.79 + *readIndex = (*readIndex + numberOfFrames) % m_buffer.Length(); 1.80 +} 1.81 + 1.82 +int ReverbAccumulationBuffer::accumulate(float* source, size_t numberOfFrames, int* readIndex, size_t delayFrames) 1.83 +{ 1.84 + size_t bufferLength = m_buffer.Length(); 1.85 + 1.86 + size_t writeIndex = (*readIndex + delayFrames) % bufferLength; 1.87 + 1.88 + // Update caller's readIndex 1.89 + *readIndex = (*readIndex + numberOfFrames) % bufferLength; 1.90 + 1.91 + size_t framesAvailable = bufferLength - writeIndex; 1.92 + size_t numberOfFrames1 = std::min(numberOfFrames, framesAvailable); 1.93 + size_t numberOfFrames2 = numberOfFrames - numberOfFrames1; 1.94 + 1.95 + float* destination = m_buffer.Elements(); 1.96 + 1.97 + bool isSafe = writeIndex <= bufferLength && numberOfFrames1 + writeIndex <= bufferLength && numberOfFrames2 <= bufferLength; 1.98 + MOZ_ASSERT(isSafe); 1.99 + if (!isSafe) 1.100 + return 0; 1.101 + 1.102 + AudioBufferAddWithScale(source, 1.0f, destination + writeIndex, numberOfFrames1); 1.103 + 1.104 + // Handle wrap-around if necessary 1.105 + if (numberOfFrames2 > 0) { 1.106 + AudioBufferAddWithScale(source + numberOfFrames1, 1.0f, destination, numberOfFrames2); 1.107 + } 1.108 + 1.109 + return writeIndex; 1.110 +} 1.111 + 1.112 +void ReverbAccumulationBuffer::reset() 1.113 +{ 1.114 + PodZero(m_buffer.Elements(), m_buffer.Length()); 1.115 + m_readIndex = 0; 1.116 + m_readTimeFrame = 0; 1.117 +} 1.118 + 1.119 +} // namespace WebCore