1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/blink/ZeroPole.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,78 @@ 1.4 +/* 1.5 + * Copyright (C) 2011 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 "ZeroPole.h" 1.33 + 1.34 +#include <cmath> 1.35 +#include <float.h> 1.36 + 1.37 +namespace WebCore { 1.38 + 1.39 +void ZeroPole::process(const float *source, float *destination, int framesToProcess) 1.40 +{ 1.41 + float zero = m_zero; 1.42 + float pole = m_pole; 1.43 + 1.44 + // Gain compensation to make 0dB @ 0Hz 1.45 + const float k1 = 1 / (1 - zero); 1.46 + const float k2 = 1 - pole; 1.47 + 1.48 + // Member variables to locals. 1.49 + float lastX = m_lastX; 1.50 + float lastY = m_lastY; 1.51 + 1.52 + for (int i = 0; i < framesToProcess; ++i) { 1.53 + float input = source[i]; 1.54 + 1.55 + // Zero 1.56 + float output1 = k1 * (input - zero * lastX); 1.57 + lastX = input; 1.58 + 1.59 + // Pole 1.60 + float output2 = k2 * output1 + pole * lastY; 1.61 + lastY = output2; 1.62 + 1.63 + destination[i] = output2; 1.64 + } 1.65 + 1.66 + // Locals to member variables. Flush denormals here so we don't 1.67 + // slow down the inner loop above. 1.68 + if (lastX == 0.0f && lastY != 0.0f && fabsf(lastY) < FLT_MIN) { 1.69 + // Flush future values to zero (until there is new input). 1.70 + lastY = 0.0; 1.71 + // Flush calculated values. 1.72 + for (int i = framesToProcess; i-- && fabsf(destination[i]) < FLT_MIN; ) { 1.73 + destination[i] = 0.0f; 1.74 + } 1.75 + } 1.76 + 1.77 + m_lastX = lastX; 1.78 + m_lastY = lastY; 1.79 +} 1.80 + 1.81 +} // namespace WebCore