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 #ifndef HRTFKernel_h
30 #define HRTFKernel_h
31 
32 #include "FFTFrame.h"
33 #include <wtf/OwnPtr.h>
34 #include <wtf/PassOwnPtr.h>
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/RefCounted.h>
37 #include <wtf/RefPtr.h>
38 #include <wtf/Vector.h>
39 
40 namespace WebCore {
41 
42 class AudioChannel;
43 
44 // HRTF stands for Head-Related Transfer Function.
45 // HRTFKernel is a frequency-domain representation of an impulse-response used as part of the spatialized panning system.
46 // For a given azimuth / elevation angle there will be one HRTFKernel for the left ear transfer function, and one for the right ear.
47 // The leading delay (average group delay) for each impulse response is extracted:
48 //      m_fftFrame is the frequency-domain representation of the impulse response with the delay removed
49 //      m_frameDelay is the leading delay of the original impulse response.
50 class HRTFKernel : public RefCounted<HRTFKernel> {
51 public:
52     // Note: this is destructive on the passed in AudioChannel.
53     // The length of channel must be a power of two.
create(AudioChannel * channel,size_t fftSize,double sampleRate,bool bassBoost)54     static PassRefPtr<HRTFKernel> create(AudioChannel* channel, size_t fftSize, double sampleRate, bool bassBoost)
55     {
56         return adoptRef(new HRTFKernel(channel, fftSize, sampleRate, bassBoost));
57     }
58 
create(PassOwnPtr<FFTFrame> fftFrame,double frameDelay,double sampleRate)59     static PassRefPtr<HRTFKernel> create(PassOwnPtr<FFTFrame> fftFrame, double frameDelay, double sampleRate)
60     {
61         return adoptRef(new HRTFKernel(fftFrame, frameDelay, sampleRate));
62     }
63 
64     // Given two HRTFKernels, and an interpolation factor x: 0 -> 1, returns an interpolated HRTFKernel.
65     static PassRefPtr<HRTFKernel> createInterpolatedKernel(HRTFKernel* kernel1, HRTFKernel* kernel2, double x);
66 
fftFrame()67     FFTFrame* fftFrame() { return m_fftFrame.get(); }
68 
fftSize()69     size_t fftSize() const { return m_fftFrame->fftSize(); }
frameDelay()70     double frameDelay() const { return m_frameDelay; }
71 
sampleRate()72     double sampleRate() const { return m_sampleRate; }
nyquist()73     double nyquist() const { return 0.5 * sampleRate(); }
74 
75     // Converts back into impulse-response form.
76     PassOwnPtr<AudioChannel> createImpulseResponse();
77 
78 private:
79     // Note: this is destructive on the passed in AudioChannel.
80     HRTFKernel(AudioChannel* channel, size_t fftSize, double sampleRate, bool bassBoost);
81 
HRTFKernel(PassOwnPtr<FFTFrame> fftFrame,double frameDelay,double sampleRate)82     HRTFKernel(PassOwnPtr<FFTFrame> fftFrame, double frameDelay, double sampleRate)
83         : m_fftFrame(fftFrame)
84         , m_frameDelay(frameDelay)
85         , m_sampleRate(sampleRate)
86     {
87     }
88 
89     OwnPtr<FFTFrame> m_fftFrame;
90     double m_frameDelay;
91     double m_sampleRate;
92 };
93 
94 typedef Vector<RefPtr<HRTFKernel> > HRTFKernelList;
95 
96 } // namespace WebCore
97 
98 #endif // HRTFKernel_h
99