content/media/webaudio/blink/HRTFPanner.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/webaudio/blink/HRTFPanner.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,115 @@
     1.4 +/*
     1.5 + * Copyright (C) 2010, Google Inc. All rights reserved.
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions
     1.9 + * are met:
    1.10 + * 1.  Redistributions of source code must retain the above copyright
    1.11 + *    notice, this list of conditions and the following disclaimer.
    1.12 + * 2.  Redistributions in binary form must reproduce the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer in the
    1.14 + *    documentation and/or other materials provided with the distribution.
    1.15 + *
    1.16 + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
    1.17 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    1.18 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    1.19 + * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
    1.20 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.21 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    1.22 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
    1.23 + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.24 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.25 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.26 + */
    1.27 +
    1.28 +#ifndef HRTFPanner_h
    1.29 +#define HRTFPanner_h
    1.30 +
    1.31 +#include "FFTConvolver.h"
    1.32 +#include "DelayBuffer.h"
    1.33 +#include "mozilla/MemoryReporting.h"
    1.34 +
    1.35 +namespace mozilla {
    1.36 +struct AudioChunk;
    1.37 +}
    1.38 +
    1.39 +namespace WebCore {
    1.40 +
    1.41 +class HRTFDatabaseLoader;
    1.42 +
    1.43 +using mozilla::AudioChunk;
    1.44 +
    1.45 +class HRTFPanner {
    1.46 +public:
    1.47 +    HRTFPanner(float sampleRate, mozilla::TemporaryRef<HRTFDatabaseLoader> databaseLoader);
    1.48 +    ~HRTFPanner();
    1.49 +
    1.50 +    // chunk durations must be 128
    1.51 +    void pan(double azimuth, double elevation, const AudioChunk* inputBus, AudioChunk* outputBus);
    1.52 +    void reset();
    1.53 +
    1.54 +    size_t fftSize() const { return m_convolverL1.fftSize(); }
    1.55 +
    1.56 +    float sampleRate() const { return m_sampleRate; }
    1.57 +
    1.58 +    int maxTailFrames() const;
    1.59 +
    1.60 +    size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
    1.61 +
    1.62 +private:
    1.63 +    // Given an azimuth angle in the range -180 -> +180, returns the corresponding azimuth index for the database,
    1.64 +    // and azimuthBlend which is an interpolation value from 0 -> 1.
    1.65 +    int calculateDesiredAzimuthIndexAndBlend(double azimuth, double& azimuthBlend);
    1.66 +
    1.67 +    mozilla::RefPtr<HRTFDatabaseLoader> m_databaseLoader;
    1.68 +
    1.69 +    float m_sampleRate;
    1.70 +
    1.71 +    // We maintain two sets of convolvers for smooth cross-faded interpolations when
    1.72 +    // then azimuth and elevation are dynamically changing.
    1.73 +    // When the azimuth and elevation are not changing, we simply process with one of the two sets.
    1.74 +    // Initially we use CrossfadeSelection1 corresponding to m_convolverL1 and m_convolverR1.
    1.75 +    // Whenever the azimuth or elevation changes, a crossfade is initiated to transition
    1.76 +    // to the new position. So if we're currently processing with CrossfadeSelection1, then
    1.77 +    // we transition to CrossfadeSelection2 (and vice versa).
    1.78 +    // If we're in the middle of a transition, then we wait until it is complete before
    1.79 +    // initiating a new transition.
    1.80 +
    1.81 +    // Selects either the convolver set (m_convolverL1, m_convolverR1) or (m_convolverL2, m_convolverR2).
    1.82 +    enum CrossfadeSelection {
    1.83 +        CrossfadeSelection1,
    1.84 +        CrossfadeSelection2
    1.85 +    };
    1.86 +
    1.87 +    CrossfadeSelection m_crossfadeSelection;
    1.88 +
    1.89 +    // azimuth/elevation for CrossfadeSelection1.
    1.90 +    int m_azimuthIndex1;
    1.91 +    double m_elevation1;
    1.92 +
    1.93 +    // azimuth/elevation for CrossfadeSelection2.
    1.94 +    int m_azimuthIndex2;
    1.95 +    double m_elevation2;
    1.96 +
    1.97 +    // A crossfade value 0 <= m_crossfadeX <= 1.
    1.98 +    float m_crossfadeX;
    1.99 +
   1.100 +    // Per-sample-frame crossfade value increment.
   1.101 +    float m_crossfadeIncr;
   1.102 +
   1.103 +    FFTConvolver m_convolverL1;
   1.104 +    FFTConvolver m_convolverR1;
   1.105 +    FFTConvolver m_convolverL2;
   1.106 +    FFTConvolver m_convolverR2;
   1.107 +
   1.108 +    mozilla::DelayBuffer m_delayLine;
   1.109 +
   1.110 +    AudioFloatArray m_tempL1;
   1.111 +    AudioFloatArray m_tempR1;
   1.112 +    AudioFloatArray m_tempL2;
   1.113 +    AudioFloatArray m_tempR2;
   1.114 +};
   1.115 +
   1.116 +} // namespace WebCore
   1.117 +
   1.118 +#endif // HRTFPanner_h

mercurial