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 "config.h"
30
31#if ENABLE(WEB_AUDIO)
32
33#include "HRTFKernel.h"
34
35#include "AudioChannel.h"
36#include "Biquad.h"
37#include "FFTFrame.h"
38#include <wtf/MathExtras.h>
39
40using namespace std;
41
42namespace WebCore {
43
44// Takes the input AudioChannel as an input impulse response and calculates the average group delay.
45// This represents the initial delay before the most energetic part of the impulse response.
46// The sample-frame delay is removed from the impulseP impulse response, and this value  is returned.
47// the length of the passed in AudioChannel must be a power of 2.
48static double extractAverageGroupDelay(AudioChannel* channel, size_t analysisFFTSize)
49{
50    ASSERT(channel);
51
52    float* impulseP = channel->data();
53
54    ASSERT(channel->length() >= analysisFFTSize);
55
56    // Check for power-of-2.
57    ASSERT(1UL << static_cast<unsigned>(log2(analysisFFTSize)) == analysisFFTSize);
58
59    FFTFrame estimationFrame(analysisFFTSize);
60    estimationFrame.doFFT(impulseP);
61
62    double frameDelay = estimationFrame.extractAverageGroupDelay();
63    estimationFrame.doInverseFFT(impulseP);
64
65    return frameDelay;
66}
67
68HRTFKernel::HRTFKernel(AudioChannel* channel, size_t fftSize, double sampleRate, bool bassBoost)
69    : m_frameDelay(0.0)
70    , m_sampleRate(sampleRate)
71{
72    ASSERT(channel);
73
74    // Determine the leading delay (average group delay) for the response.
75    m_frameDelay = extractAverageGroupDelay(channel, fftSize / 2);
76
77    float* impulseResponse = channel->data();
78    size_t responseLength = channel->length();
79
80    if (bassBoost) {
81        // Run through some post-processing to boost the bass a little -- the HRTF's seem to be a little bass-deficient.
82        // FIXME: this post-processing should have already been applied to the HRTF file resources.  Once the files are put into this form,
83        // then this code path can be removed along with the bassBoost parameter.
84        Biquad filter;
85        filter.setLowShelfParams(700.0 / nyquist(), 6.0); // boost 6dB at 700Hz
86        filter.process(impulseResponse, impulseResponse, responseLength);
87    }
88
89    // We need to truncate to fit into 1/2 the FFT size (with zero padding) in order to do proper convolution.
90    size_t truncatedResponseLength = min(responseLength, fftSize / 2); // truncate if necessary to max impulse response length allowed by FFT
91
92    // Quick fade-out (apply window) at truncation point
93    unsigned numberOfFadeOutFrames = static_cast<unsigned>(sampleRate / 4410); // 10 sample-frames @44.1KHz sample-rate
94    ASSERT(numberOfFadeOutFrames < truncatedResponseLength);
95    if (numberOfFadeOutFrames < truncatedResponseLength) {
96        for (unsigned i = truncatedResponseLength - numberOfFadeOutFrames; i < truncatedResponseLength; ++i) {
97            float x = 1.0f - static_cast<float>(i - (truncatedResponseLength - numberOfFadeOutFrames)) / numberOfFadeOutFrames;
98            impulseResponse[i] *= x;
99        }
100    }
101
102    m_fftFrame = adoptPtr(new FFTFrame(fftSize));
103    m_fftFrame->doPaddedFFT(impulseResponse, truncatedResponseLength);
104}
105
106PassOwnPtr<AudioChannel> HRTFKernel::createImpulseResponse()
107{
108    OwnPtr<AudioChannel> channel = adoptPtr(new AudioChannel(fftSize()));
109    FFTFrame fftFrame(*m_fftFrame);
110
111    // Add leading delay back in.
112    fftFrame.addConstantGroupDelay(m_frameDelay);
113    fftFrame.doInverseFFT(channel->data());
114
115    return channel.release();
116}
117
118// Interpolates two kernels with x: 0 -> 1 and returns the result.
119PassRefPtr<HRTFKernel> HRTFKernel::createInterpolatedKernel(HRTFKernel* kernel1, HRTFKernel* kernel2, double x)
120{
121    ASSERT(kernel1 && kernel2);
122    if (!kernel1 || !kernel2)
123        return 0;
124
125    ASSERT(x >= 0.0 && x < 1.0);
126    x = min(1.0, max(0.0, x));
127
128    double sampleRate1 = kernel1->sampleRate();
129    double sampleRate2 = kernel2->sampleRate();
130    ASSERT(sampleRate1 == sampleRate2);
131    if (sampleRate1 != sampleRate2)
132        return 0;
133
134    double frameDelay = (1.0 - x) * kernel1->frameDelay() + x * kernel2->frameDelay();
135
136    OwnPtr<FFTFrame> interpolatedFrame = FFTFrame::createInterpolatedFrame(*kernel1->fftFrame(), *kernel2->fftFrame(), x);
137    return HRTFKernel::create(interpolatedFrame.release(), frameDelay, sampleRate1);
138}
139
140} // namespace WebCore
141
142#endif // ENABLE(WEB_AUDIO)
143