content/media/webaudio/blink/HRTFKernel.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial