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 * 1.  Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2.  Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#ifndef HRTFPanner_h
26#define HRTFPanner_h
27
28#include "platform/audio/AudioDelayDSPKernel.h"
29#include "platform/audio/FFTConvolver.h"
30#include "platform/audio/HRTFDatabaseLoader.h"
31#include "platform/audio/Panner.h"
32
33namespace blink {
34
35class PLATFORM_EXPORT HRTFPanner FINAL : public Panner {
36public:
37    HRTFPanner(float sampleRate, HRTFDatabaseLoader*);
38    virtual ~HRTFPanner();
39
40    // Panner
41    virtual void pan(double azimuth, double elevation, const AudioBus* inputBus, AudioBus* outputBus, size_t framesToProcess) OVERRIDE;
42    virtual void reset() OVERRIDE;
43
44    size_t fftSize() const { return fftSizeForSampleRate(m_sampleRate); }
45    static size_t fftSizeForSampleRate(float sampleRate);
46
47    float sampleRate() const { return m_sampleRate; }
48
49    virtual double tailTime() const OVERRIDE;
50    virtual double latencyTime() const OVERRIDE;
51
52    virtual void trace(Visitor*) OVERRIDE;
53
54private:
55    // Given an azimuth angle in the range -180 -> +180, returns the corresponding azimuth index for the database,
56    // and azimuthBlend which is an interpolation value from 0 -> 1.
57    int calculateDesiredAzimuthIndexAndBlend(double azimuth, double& azimuthBlend);
58
59    Member<HRTFDatabaseLoader> m_databaseLoader;
60
61    float m_sampleRate;
62
63    // We maintain two sets of convolvers for smooth cross-faded interpolations when
64    // then azimuth and elevation are dynamically changing.
65    // When the azimuth and elevation are not changing, we simply process with one of the two sets.
66    // Initially we use CrossfadeSelection1 corresponding to m_convolverL1 and m_convolverR1.
67    // Whenever the azimuth or elevation changes, a crossfade is initiated to transition
68    // to the new position. So if we're currently processing with CrossfadeSelection1, then
69    // we transition to CrossfadeSelection2 (and vice versa).
70    // If we're in the middle of a transition, then we wait until it is complete before
71    // initiating a new transition.
72
73    // Selects either the convolver set (m_convolverL1, m_convolverR1) or (m_convolverL2, m_convolverR2).
74    enum CrossfadeSelection {
75        CrossfadeSelection1,
76        CrossfadeSelection2
77    };
78
79    CrossfadeSelection m_crossfadeSelection;
80
81    // azimuth/elevation for CrossfadeSelection1.
82    int m_azimuthIndex1;
83    double m_elevation1;
84
85    // azimuth/elevation for CrossfadeSelection2.
86    int m_azimuthIndex2;
87    double m_elevation2;
88
89    // A crossfade value 0 <= m_crossfadeX <= 1.
90    float m_crossfadeX;
91
92    // Per-sample-frame crossfade value increment.
93    float m_crossfadeIncr;
94
95    FFTConvolver m_convolverL1;
96    FFTConvolver m_convolverR1;
97    FFTConvolver m_convolverL2;
98    FFTConvolver m_convolverR2;
99
100    AudioDelayDSPKernel m_delayLineL;
101    AudioDelayDSPKernel m_delayLineR;
102
103    AudioFloatArray m_tempL1;
104    AudioFloatArray m_tempR1;
105    AudioFloatArray m_tempL2;
106    AudioFloatArray m_tempR2;
107};
108
109} // namespace blink
110
111#endif // HRTFPanner_h
112