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