michael@0: /* michael@0: * Copyright (C) 2010 Google Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of michael@0: * its contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED michael@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE michael@0: * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY michael@0: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; michael@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND michael@0: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #include "HRTFKernel.h" michael@0: namespace WebCore { michael@0: michael@0: // Takes the input audio channel |impulseP| as an input impulse response and calculates the average group delay. michael@0: // This represents the initial delay before the most energetic part of the impulse response. michael@0: // The sample-frame delay is removed from the |impulseP| impulse response, and this value is returned. michael@0: // The |length| of the passed in |impulseP| must be must be a power of 2. michael@0: static float extractAverageGroupDelay(float* impulseP, size_t length) michael@0: { michael@0: // Check for power-of-2. michael@0: MOZ_ASSERT(length && (length & (length - 1)) == 0); michael@0: michael@0: FFTBlock estimationFrame(length); michael@0: estimationFrame.PerformFFT(impulseP); michael@0: michael@0: float frameDelay = static_cast(estimationFrame.ExtractAverageGroupDelay()); michael@0: estimationFrame.GetInverse(impulseP); michael@0: michael@0: return frameDelay; michael@0: } michael@0: michael@0: HRTFKernel::HRTFKernel(float* impulseResponse, size_t length, float sampleRate) michael@0: : m_frameDelay(0) michael@0: , m_sampleRate(sampleRate) michael@0: { michael@0: // Determine the leading delay (average group delay) for the response. michael@0: m_frameDelay = extractAverageGroupDelay(impulseResponse, length); michael@0: michael@0: // The FFT size (with zero padding) needs to be twice the response length michael@0: // in order to do proper convolution. michael@0: unsigned fftSize = 2 * length; michael@0: michael@0: // Quick fade-out (apply window) at truncation point michael@0: // because the impulse response has been truncated. michael@0: unsigned numberOfFadeOutFrames = static_cast(sampleRate / 4410); // 10 sample-frames @44.1KHz sample-rate michael@0: MOZ_ASSERT(numberOfFadeOutFrames < length); michael@0: if (numberOfFadeOutFrames < length) { michael@0: for (unsigned i = length - numberOfFadeOutFrames; i < length; ++i) { michael@0: float x = 1.0f - static_cast(i - (length - numberOfFadeOutFrames)) / numberOfFadeOutFrames; michael@0: impulseResponse[i] *= x; michael@0: } michael@0: } michael@0: michael@0: m_fftFrame = new FFTBlock(fftSize); michael@0: m_fftFrame->PadAndMakeScaledDFT(impulseResponse, length); michael@0: } michael@0: michael@0: // Interpolates two kernels with x: 0 -> 1 and returns the result. michael@0: nsReturnRef HRTFKernel::createInterpolatedKernel(HRTFKernel* kernel1, HRTFKernel* kernel2, float x) michael@0: { michael@0: MOZ_ASSERT(kernel1 && kernel2); michael@0: if (!kernel1 || !kernel2) michael@0: return nsReturnRef(); michael@0: michael@0: MOZ_ASSERT(x >= 0.0 && x < 1.0); michael@0: x = mozilla::clamped(x, 0.0f, 1.0f); michael@0: michael@0: float sampleRate1 = kernel1->sampleRate(); michael@0: float sampleRate2 = kernel2->sampleRate(); michael@0: MOZ_ASSERT(sampleRate1 == sampleRate2); michael@0: if (sampleRate1 != sampleRate2) michael@0: return nsReturnRef(); michael@0: michael@0: float frameDelay = (1 - x) * kernel1->frameDelay() + x * kernel2->frameDelay(); michael@0: michael@0: nsAutoPtr interpolatedFrame( michael@0: FFTBlock::CreateInterpolatedBlock(*kernel1->fftFrame(), *kernel2->fftFrame(), x)); michael@0: return HRTFKernel::create(interpolatedFrame, frameDelay, sampleRate1); michael@0: } michael@0: michael@0: } // namespace WebCore