content/media/webaudio/blink/HRTFKernel.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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  *
     8  * 1.  Redistributions of source code must retain the above copyright
     9  *     notice, this list of conditions and the following disclaimer.
    10  * 2.  Redistributions in binary form must reproduce the above copyright
    11  *     notice, this list of conditions and the following disclaimer in the
    12  *     documentation and/or other materials provided with the distribution.
    13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
    14  *     its contributors may be used to endorse or promote products derived
    15  *     from this software without specific prior written permission.
    16  *
    17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
    18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
    21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #ifndef HRTFKernel_h
    30 #define HRTFKernel_h
    32 #include "nsAutoPtr.h"
    33 #include "nsAutoRef.h"
    34 #include "nsTArray.h"
    35 #include "mozilla/FFTBlock.h"
    36 #include "mozilla/MemoryReporting.h"
    38 namespace WebCore {
    40 using mozilla::FFTBlock;
    42 // HRTF stands for Head-Related Transfer Function.
    43 // HRTFKernel is a frequency-domain representation of an impulse-response used as part of the spatialized panning system.
    44 // For a given azimuth / elevation angle there will be one HRTFKernel for the left ear transfer function, and one for the right ear.
    45 // The leading delay (average group delay) for each impulse response is extracted:
    46 //      m_fftFrame is the frequency-domain representation of the impulse response with the delay removed
    47 //      m_frameDelay is the leading delay of the original impulse response.
    48 class HRTFKernel {
    49 public:
    50     // Note: this is destructive on the passed in |impulseResponse|.
    51     // The |length| of |impulseResponse| must be a power of two.
    52     // The size of the DFT will be |2 * length|.
    53     static nsReturnRef<HRTFKernel> create(float* impulseResponse, size_t length, float sampleRate);
    55     static nsReturnRef<HRTFKernel> create(nsAutoPtr<FFTBlock> fftFrame, float frameDelay, float sampleRate);
    57     // Given two HRTFKernels, and an interpolation factor x: 0 -> 1, returns an interpolated HRTFKernel.
    58     static nsReturnRef<HRTFKernel> createInterpolatedKernel(HRTFKernel* kernel1, HRTFKernel* kernel2, float x);
    60     FFTBlock* fftFrame() { return m_fftFrame.get(); }
    62     size_t fftSize() const { return m_fftFrame->FFTSize(); }
    63     float frameDelay() const { return m_frameDelay; }
    65     float sampleRate() const { return m_sampleRate; }
    66     double nyquist() const { return 0.5 * sampleRate(); }
    68     size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
    69     {
    70         size_t amount = aMallocSizeOf(this);
    71         amount += m_fftFrame->SizeOfIncludingThis(aMallocSizeOf);
    72         return amount;
    73     }
    75 private:
    76     HRTFKernel(const HRTFKernel& other) MOZ_DELETE;
    77     void operator=(const HRTFKernel& other) MOZ_DELETE;
    79     // Note: this is destructive on the passed in |impulseResponse|.
    80     HRTFKernel(float* impulseResponse, size_t fftSize, float sampleRate);
    82     HRTFKernel(nsAutoPtr<FFTBlock> fftFrame, float frameDelay, float sampleRate)
    83         : m_fftFrame(fftFrame)
    84         , m_frameDelay(frameDelay)
    85         , m_sampleRate(sampleRate)
    86     {
    87     }
    89     nsAutoPtr<FFTBlock> m_fftFrame;
    90     float m_frameDelay;
    91     float m_sampleRate;
    92 };
    94 typedef nsTArray<nsAutoRef<HRTFKernel> > HRTFKernelList;
    96 } // namespace WebCore
    98 template <>
    99 class nsAutoRefTraits<WebCore::HRTFKernel> :
   100     public nsPointerRefTraits<WebCore::HRTFKernel> {
   101 public:
   102     static void Release(WebCore::HRTFKernel* ptr) { delete(ptr); }
   103 };
   105 namespace WebCore {
   107 inline nsReturnRef<HRTFKernel> HRTFKernel::create(float* impulseResponse, size_t length, float sampleRate)
   108 {
   109     return nsReturnRef<HRTFKernel>(new HRTFKernel(impulseResponse, length, sampleRate));
   110 }
   112 inline nsReturnRef<HRTFKernel> HRTFKernel::create(nsAutoPtr<FFTBlock> fftFrame, float frameDelay, float sampleRate)
   113 {
   114     return nsReturnRef<HRTFKernel>(new HRTFKernel(fftFrame, frameDelay, sampleRate));
   115 }
   117 } // namespace WebCore
   118 #endif // HRTFKernel_h

mercurial