HRTFPanner.h (4161B)
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 14 * ANY 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 17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #ifndef HRTFPanner_h 26 #define HRTFPanner_h 27 28 #include "DelayBuffer.h" 29 #include "FFTConvolver.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, 47 already_AddRefed<HRTFDatabaseLoader> databaseLoader); 48 ~HRTFPanner(); 49 50 // chunk durations must be 128 51 void pan(double azimuth, double elevation, const AudioBlock* inputBus, 52 AudioBlock* outputBus); 53 void reset(); 54 55 size_t fftSize() const { return m_convolverL1.fftSize(); } 56 57 float sampleRate() const { return m_sampleRate; } 58 59 int maxTailFrames() const; 60 61 size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 62 63 private: 64 // Given an azimuth angle in the range -180 -> +180, returns the corresponding 65 // azimuth index for the database, and azimuthBlend which is an interpolation 66 // value from 0 -> 1. 67 int calculateDesiredAzimuthIndexAndBlend(double azimuth, 68 double& azimuthBlend); 69 70 RefPtr<HRTFDatabaseLoader> m_databaseLoader; 71 72 float m_sampleRate; 73 74 // We maintain two sets of convolvers for smooth cross-faded interpolations 75 // when then azimuth and elevation are dynamically changing. When the azimuth 76 // and elevation are not changing, we simply process with one of the two sets. 77 // Initially we use CrossfadeSelection1 corresponding to m_convolverL1 and 78 // m_convolverR1. Whenever the azimuth or elevation changes, a crossfade is 79 // initiated to transition to the new position. So if we're currently 80 // processing with CrossfadeSelection1, then we transition to 81 // CrossfadeSelection2 (and vice versa). If we're in the middle of a 82 // transition, then we wait until it is complete before initiating a new 83 // transition. 84 85 // Selects either the convolver set (m_convolverL1, m_convolverR1) or 86 // (m_convolverL2, m_convolverR2). 87 enum CrossfadeSelection { CrossfadeSelection1, CrossfadeSelection2 }; 88 89 CrossfadeSelection m_crossfadeSelection; 90 91 // azimuth/elevation for CrossfadeSelection1. 92 int m_azimuthIndex1; 93 double m_elevation1; 94 95 // azimuth/elevation for CrossfadeSelection2. 96 int m_azimuthIndex2; 97 double m_elevation2; 98 99 // A crossfade value 0 <= m_crossfadeX <= 1. 100 float m_crossfadeX; 101 102 // Per-sample-frame crossfade value increment. 103 float m_crossfadeIncr; 104 105 FFTConvolver m_convolverL1; 106 FFTConvolver m_convolverR1; 107 FFTConvolver m_convolverL2; 108 FFTConvolver m_convolverR2; 109 110 mozilla::DelayBuffer m_delayLine; 111 112 AudioFloatArray m_tempL1; 113 AudioFloatArray m_tempR1; 114 AudioFloatArray m_tempL2; 115 AudioFloatArray m_tempR2; 116 }; 117 118 } // namespace WebCore 119 120 #endif // HRTFPanner_h