content/media/webaudio/blink/HRTFKernel.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/webaudio/blink/HRTFKernel.cpp	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 +#include "HRTFKernel.h"
    1.33 +namespace WebCore {
    1.34 +
    1.35 +// Takes the input audio channel |impulseP| as an input impulse response and calculates the average group delay.
    1.36 +// This represents the initial delay before the most energetic part of the impulse response.
    1.37 +// The sample-frame delay is removed from the |impulseP| impulse response, and this value  is returned.
    1.38 +// The |length| of the passed in |impulseP| must be must be a power of 2.
    1.39 +static float extractAverageGroupDelay(float* impulseP, size_t length)
    1.40 +{
    1.41 +    // Check for power-of-2.
    1.42 +    MOZ_ASSERT(length && (length & (length - 1)) == 0);
    1.43 +
    1.44 +    FFTBlock estimationFrame(length);
    1.45 +    estimationFrame.PerformFFT(impulseP);
    1.46 +
    1.47 +    float frameDelay = static_cast<float>(estimationFrame.ExtractAverageGroupDelay());
    1.48 +    estimationFrame.GetInverse(impulseP);
    1.49 +
    1.50 +    return frameDelay;
    1.51 +}
    1.52 +
    1.53 +HRTFKernel::HRTFKernel(float* impulseResponse, size_t length, float sampleRate)
    1.54 +    : m_frameDelay(0)
    1.55 +    , m_sampleRate(sampleRate)
    1.56 +{
    1.57 +    // Determine the leading delay (average group delay) for the response.
    1.58 +    m_frameDelay = extractAverageGroupDelay(impulseResponse, length);
    1.59 +
    1.60 +    // The FFT size (with zero padding) needs to be twice the response length
    1.61 +    // in order to do proper convolution.
    1.62 +    unsigned fftSize = 2 * length;
    1.63 +
    1.64 +    // Quick fade-out (apply window) at truncation point
    1.65 +    // because the impulse response has been truncated.
    1.66 +    unsigned numberOfFadeOutFrames = static_cast<unsigned>(sampleRate / 4410); // 10 sample-frames @44.1KHz sample-rate
    1.67 +    MOZ_ASSERT(numberOfFadeOutFrames < length);
    1.68 +    if (numberOfFadeOutFrames < length) {
    1.69 +        for (unsigned i = length - numberOfFadeOutFrames; i < length; ++i) {
    1.70 +            float x = 1.0f - static_cast<float>(i - (length - numberOfFadeOutFrames)) / numberOfFadeOutFrames;
    1.71 +            impulseResponse[i] *= x;
    1.72 +        }
    1.73 +    }
    1.74 +
    1.75 +    m_fftFrame = new FFTBlock(fftSize);
    1.76 +    m_fftFrame->PadAndMakeScaledDFT(impulseResponse, length);
    1.77 +}
    1.78 +
    1.79 +// Interpolates two kernels with x: 0 -> 1 and returns the result.
    1.80 +nsReturnRef<HRTFKernel> HRTFKernel::createInterpolatedKernel(HRTFKernel* kernel1, HRTFKernel* kernel2, float x)
    1.81 +{
    1.82 +    MOZ_ASSERT(kernel1 && kernel2);
    1.83 +    if (!kernel1 || !kernel2)
    1.84 +        return nsReturnRef<HRTFKernel>();
    1.85 + 
    1.86 +    MOZ_ASSERT(x >= 0.0 && x < 1.0);
    1.87 +    x = mozilla::clamped(x, 0.0f, 1.0f);
    1.88 +    
    1.89 +    float sampleRate1 = kernel1->sampleRate();
    1.90 +    float sampleRate2 = kernel2->sampleRate();
    1.91 +    MOZ_ASSERT(sampleRate1 == sampleRate2);
    1.92 +    if (sampleRate1 != sampleRate2)
    1.93 +        return nsReturnRef<HRTFKernel>();
    1.94 +    
    1.95 +    float frameDelay = (1 - x) * kernel1->frameDelay() + x * kernel2->frameDelay();
    1.96 +    
    1.97 +    nsAutoPtr<FFTBlock> interpolatedFrame(
    1.98 +        FFTBlock::CreateInterpolatedBlock(*kernel1->fftFrame(), *kernel2->fftFrame(), x));
    1.99 +    return HRTFKernel::create(interpolatedFrame, frameDelay, sampleRate1);
   1.100 +}
   1.101 +
   1.102 +} // namespace WebCore

mercurial