LCOV - code coverage report
Current view: top level - dom/media/webaudio/blink - HRTFKernel.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 39 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2010 Google Inc. All rights reserved.
       3             :  *
       4             :  * Redistribution and use in source and binary forms, with or without
       5             :  * modification, are permitted provided that the following conditions
       6             :  * are met:
       7             :  *
       8             :  * 1.  Redistributions of source code must retain the above copyright
       9             :  *     notice, this list of conditions and the following disclaimer.
      10             :  * 2.  Redistributions in binary form must reproduce the above copyright
      11             :  *     notice, this list of conditions and the following disclaimer in the
      12             :  *     documentation and/or other materials provided with the distribution.
      13             :  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
      14             :  *     its contributors may be used to endorse or promote products derived
      15             :  *     from this software without specific prior written permission.
      16             :  *
      17             :  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
      18             :  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
      19             :  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      20             :  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
      21             :  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
      22             :  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
      23             :  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
      24             :  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      25             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      26             :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      27             :  */
      28             : 
      29             : #include "HRTFKernel.h"
      30             : namespace WebCore {
      31             : 
      32             : // Takes the input audio channel |impulseP| as an input impulse response and calculates the average group delay.
      33             : // This represents the initial delay before the most energetic part of the impulse response.
      34             : // The sample-frame delay is removed from the |impulseP| impulse response, and this value  is returned.
      35             : // The |length| of the passed in |impulseP| must be must be a power of 2.
      36           0 : static float extractAverageGroupDelay(float* impulseP, size_t length)
      37             : {
      38             :     // Check for power-of-2.
      39           0 :     MOZ_ASSERT(length && (length & (length - 1)) == 0);
      40             : 
      41           0 :     FFTBlock estimationFrame(length);
      42           0 :     estimationFrame.PerformFFT(impulseP);
      43             : 
      44           0 :     float frameDelay = static_cast<float>(estimationFrame.ExtractAverageGroupDelay());
      45           0 :     estimationFrame.GetInverse(impulseP);
      46             : 
      47           0 :     return frameDelay;
      48             : }
      49             : 
      50           0 : HRTFKernel::HRTFKernel(float* impulseResponse, size_t length, float sampleRate)
      51             :     : m_frameDelay(0)
      52           0 :     , m_sampleRate(sampleRate)
      53             : {
      54           0 :     AlignedTArray<float> buffer;
      55             :     // copy to a 32-byte aligned buffer
      56           0 :     if (((uintptr_t)impulseResponse & 31) != 0) {
      57           0 :       buffer.SetLength(length);
      58           0 :       mozilla::PodCopy(buffer.Elements(), impulseResponse, length);
      59           0 :       impulseResponse = buffer.Elements();
      60             :     }
      61             : 
      62             :     // Determine the leading delay (average group delay) for the response.
      63           0 :     m_frameDelay = extractAverageGroupDelay(impulseResponse, length);
      64             : 
      65             :     // The FFT size (with zero padding) needs to be twice the response length
      66             :     // in order to do proper convolution.
      67           0 :     unsigned fftSize = 2 * length;
      68             : 
      69             :     // Quick fade-out (apply window) at truncation point
      70             :     // because the impulse response has been truncated.
      71           0 :     unsigned numberOfFadeOutFrames = static_cast<unsigned>(sampleRate / 4410); // 10 sample-frames @44.1KHz sample-rate
      72           0 :     MOZ_ASSERT(numberOfFadeOutFrames < length);
      73           0 :     if (numberOfFadeOutFrames < length) {
      74           0 :         for (unsigned i = length - numberOfFadeOutFrames; i < length; ++i) {
      75           0 :             float x = 1.0f - static_cast<float>(i - (length - numberOfFadeOutFrames)) / numberOfFadeOutFrames;
      76           0 :             impulseResponse[i] *= x;
      77             :         }
      78             :     }
      79             : 
      80           0 :     m_fftFrame = new FFTBlock(fftSize);
      81           0 :     m_fftFrame->PadAndMakeScaledDFT(impulseResponse, length);
      82           0 : }
      83             : 
      84             : // Interpolates two kernels with x: 0 -> 1 and returns the result.
      85           0 : nsReturnRef<HRTFKernel> HRTFKernel::createInterpolatedKernel(HRTFKernel* kernel1, HRTFKernel* kernel2, float x)
      86             : {
      87           0 :     MOZ_ASSERT(kernel1 && kernel2);
      88           0 :     if (!kernel1 || !kernel2)
      89           0 :         return nsReturnRef<HRTFKernel>();
      90             : 
      91           0 :     MOZ_ASSERT(x >= 0.0 && x < 1.0);
      92           0 :     x = mozilla::clamped(x, 0.0f, 1.0f);
      93             : 
      94           0 :     float sampleRate1 = kernel1->sampleRate();
      95           0 :     float sampleRate2 = kernel2->sampleRate();
      96           0 :     MOZ_ASSERT(sampleRate1 == sampleRate2);
      97           0 :     if (sampleRate1 != sampleRate2)
      98           0 :         return nsReturnRef<HRTFKernel>();
      99             : 
     100           0 :     float frameDelay = (1 - x) * kernel1->frameDelay() + x * kernel2->frameDelay();
     101             : 
     102             :     nsAutoPtr<FFTBlock> interpolatedFrame(
     103           0 :         FFTBlock::CreateInterpolatedBlock(*kernel1->fftFrame(), *kernel2->fftFrame(), x));
     104           0 :     return HRTFKernel::create(interpolatedFrame, frameDelay, sampleRate1);
     105             : }
     106             : 
     107             : } // namespace WebCore

Generated by: LCOV version 1.13