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