2 * Copyright (C) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "platform/audio/AudioDelayDSPKernel.h"
29 #include "platform/audio/FFTConvolver.h"
30 #include "platform/audio/HRTFDatabaseLoader.h"
31 #include "platform/audio/Panner.h"
35 class PLATFORM_EXPORT HRTFPanner final
: public Panner
{
37 HRTFPanner(float sampleRate
, HRTFDatabaseLoader
*);
38 ~HRTFPanner() override
;
41 void pan(double azimuth
, double elevation
, const AudioBus
* inputBus
, AudioBus
* outputBus
, size_t framesToProcess
) override
;
42 void reset() override
;
44 size_t fftSize() const { return fftSizeForSampleRate(m_sampleRate
); }
45 static size_t fftSizeForSampleRate(float sampleRate
);
47 float sampleRate() const { return m_sampleRate
; }
49 double tailTime() const override
;
50 double latencyTime() const override
;
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
);
57 RefPtr
<HRTFDatabaseLoader
> m_databaseLoader
;
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.
71 // Selects either the convolver set (m_convolverL1, m_convolverR1) or (m_convolverL2, m_convolverR2).
72 enum CrossfadeSelection
{
77 CrossfadeSelection m_crossfadeSelection
;
79 // azimuth/elevation for CrossfadeSelection1.
83 // azimuth/elevation for CrossfadeSelection2.
87 // A crossfade value 0 <= m_crossfadeX <= 1.
90 // Per-sample-frame crossfade value increment.
91 float m_crossfadeIncr
;
93 FFTConvolver m_convolverL1
;
94 FFTConvolver m_convolverR1
;
95 FFTConvolver m_convolverL2
;
96 FFTConvolver m_convolverR2
;
98 AudioDelayDSPKernel m_delayLineL
;
99 AudioDelayDSPKernel m_delayLineR
;
101 AudioFloatArray m_tempL1
;
102 AudioFloatArray m_tempR1
;
103 AudioFloatArray m_tempL2
;
104 AudioFloatArray m_tempR2
;
109 #endif // HRTFPanner_h