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 "DelayDSPKernel.h" |
29 | #include "FFTConvolver.h" |
30 | #include "HRTFDatabaseLoader.h" |
31 | #include "Panner.h" |
32 | |
33 | namespace WebCore { |
34 | |
35 | class HRTFPanner : public Panner { |
36 | public: |
37 | explicit HRTFPanner(float sampleRate, HRTFDatabaseLoader*); |
38 | virtual ~HRTFPanner(); |
39 | |
40 | // Panner |
41 | void pan(double azimuth, double elevation, const AudioBus* inputBus, AudioBus* outputBus, size_t framesToProcess) override; |
42 | 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 | double tailTime() const override; |
50 | double latencyTime() const override; |
51 | |
52 | private: |
53 | // Given an azimuth angle in the range -180 -> +180, returns the corresponding azimuth index for the database, |
54 | // and azimuthBlend which is an interpolation value from 0 -> 1. |
55 | int calculateDesiredAzimuthIndexAndBlend(double azimuth, double& azimuthBlend); |
56 | |
57 | RefPtr<HRTFDatabaseLoader> m_databaseLoader; |
58 | |
59 | float m_sampleRate; |
60 | |
61 | // We maintain two sets of convolvers for smooth cross-faded interpolations when |
62 | // then azimuth and elevation are dynamically changing. |
63 | // When the azimuth and elevation are not changing, we simply process with one of the two sets. |
64 | // Initially we use CrossfadeSelection1 corresponding to m_convolverL1 and m_convolverR1. |
65 | // Whenever the azimuth or elevation changes, a crossfade is initiated to transition |
66 | // to the new position. So if we're currently processing with CrossfadeSelection1, then |
67 | // we transition to CrossfadeSelection2 (and vice versa). |
68 | // If we're in the middle of a transition, then we wait until it is complete before |
69 | // initiating a new transition. |
70 | |
71 | // Selects either the convolver set (m_convolverL1, m_convolverR1) or (m_convolverL2, m_convolverR2). |
72 | enum CrossfadeSelection { |
73 | CrossfadeSelection1, |
74 | CrossfadeSelection2 |
75 | }; |
76 | |
77 | CrossfadeSelection m_crossfadeSelection; |
78 | |
79 | // azimuth/elevation for CrossfadeSelection1. |
80 | int m_azimuthIndex1; |
81 | double m_elevation1; |
82 | |
83 | // azimuth/elevation for CrossfadeSelection2. |
84 | int m_azimuthIndex2; |
85 | double m_elevation2; |
86 | |
87 | // A crossfade value 0 <= m_crossfadeX <= 1. |
88 | float m_crossfadeX; |
89 | |
90 | // Per-sample-frame crossfade value increment. |
91 | float m_crossfadeIncr; |
92 | |
93 | FFTConvolver m_convolverL1; |
94 | FFTConvolver m_convolverR1; |
95 | FFTConvolver m_convolverL2; |
96 | FFTConvolver m_convolverR2; |
97 | |
98 | DelayDSPKernel m_delayLineL; |
99 | DelayDSPKernel m_delayLineR; |
100 | |
101 | AudioFloatArray m_tempL1; |
102 | AudioFloatArray m_tempR1; |
103 | AudioFloatArray m_tempL2; |
104 | AudioFloatArray m_tempR2; |
105 | }; |
106 | |
107 | } // namespace WebCore |
108 | |
109 | #endif // HRTFPanner_h |
110 | |