michael@0: /* michael@0: * Copyright (C) 2010, Google Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED michael@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE michael@0: * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY michael@0: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; michael@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON michael@0: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifndef HRTFPanner_h michael@0: #define HRTFPanner_h michael@0: michael@0: #include "FFTConvolver.h" michael@0: #include "DelayBuffer.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: michael@0: namespace mozilla { michael@0: struct AudioChunk; michael@0: } michael@0: michael@0: namespace WebCore { michael@0: michael@0: class HRTFDatabaseLoader; michael@0: michael@0: using mozilla::AudioChunk; michael@0: michael@0: class HRTFPanner { michael@0: public: michael@0: HRTFPanner(float sampleRate, mozilla::TemporaryRef databaseLoader); michael@0: ~HRTFPanner(); michael@0: michael@0: // chunk durations must be 128 michael@0: void pan(double azimuth, double elevation, const AudioChunk* inputBus, AudioChunk* outputBus); michael@0: void reset(); michael@0: michael@0: size_t fftSize() const { return m_convolverL1.fftSize(); } michael@0: michael@0: float sampleRate() const { return m_sampleRate; } michael@0: michael@0: int maxTailFrames() const; michael@0: michael@0: size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; michael@0: michael@0: private: michael@0: // Given an azimuth angle in the range -180 -> +180, returns the corresponding azimuth index for the database, michael@0: // and azimuthBlend which is an interpolation value from 0 -> 1. michael@0: int calculateDesiredAzimuthIndexAndBlend(double azimuth, double& azimuthBlend); michael@0: michael@0: mozilla::RefPtr m_databaseLoader; michael@0: michael@0: float m_sampleRate; michael@0: michael@0: // We maintain two sets of convolvers for smooth cross-faded interpolations when michael@0: // then azimuth and elevation are dynamically changing. michael@0: // When the azimuth and elevation are not changing, we simply process with one of the two sets. michael@0: // Initially we use CrossfadeSelection1 corresponding to m_convolverL1 and m_convolverR1. michael@0: // Whenever the azimuth or elevation changes, a crossfade is initiated to transition michael@0: // to the new position. So if we're currently processing with CrossfadeSelection1, then michael@0: // we transition to CrossfadeSelection2 (and vice versa). michael@0: // If we're in the middle of a transition, then we wait until it is complete before michael@0: // initiating a new transition. michael@0: michael@0: // Selects either the convolver set (m_convolverL1, m_convolverR1) or (m_convolverL2, m_convolverR2). michael@0: enum CrossfadeSelection { michael@0: CrossfadeSelection1, michael@0: CrossfadeSelection2 michael@0: }; michael@0: michael@0: CrossfadeSelection m_crossfadeSelection; michael@0: michael@0: // azimuth/elevation for CrossfadeSelection1. michael@0: int m_azimuthIndex1; michael@0: double m_elevation1; michael@0: michael@0: // azimuth/elevation for CrossfadeSelection2. michael@0: int m_azimuthIndex2; michael@0: double m_elevation2; michael@0: michael@0: // A crossfade value 0 <= m_crossfadeX <= 1. michael@0: float m_crossfadeX; michael@0: michael@0: // Per-sample-frame crossfade value increment. michael@0: float m_crossfadeIncr; michael@0: michael@0: FFTConvolver m_convolverL1; michael@0: FFTConvolver m_convolverR1; michael@0: FFTConvolver m_convolverL2; michael@0: FFTConvolver m_convolverR2; michael@0: michael@0: mozilla::DelayBuffer m_delayLine; michael@0: michael@0: AudioFloatArray m_tempL1; michael@0: AudioFloatArray m_tempR1; michael@0: AudioFloatArray m_tempL2; michael@0: AudioFloatArray m_tempR2; michael@0: }; michael@0: michael@0: } // namespace WebCore michael@0: michael@0: #endif // HRTFPanner_h