1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/blink/HRTFKernel.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,118 @@ 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.11 + * 1. Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 2. Redistributions in binary form must reproduce the above copyright 1.14 + * notice, this list of conditions and the following disclaimer in the 1.15 + * documentation and/or other materials provided with the distribution. 1.16 + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 1.17 + * its contributors may be used to endorse or promote products derived 1.18 + * from this software without specific prior written permission. 1.19 + * 1.20 + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 1.21 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1.22 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1.23 + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 1.24 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.25 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 1.26 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 1.27 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.28 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.29 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 + 1.32 +#ifndef HRTFKernel_h 1.33 +#define HRTFKernel_h 1.34 + 1.35 +#include "nsAutoPtr.h" 1.36 +#include "nsAutoRef.h" 1.37 +#include "nsTArray.h" 1.38 +#include "mozilla/FFTBlock.h" 1.39 +#include "mozilla/MemoryReporting.h" 1.40 + 1.41 +namespace WebCore { 1.42 + 1.43 +using mozilla::FFTBlock; 1.44 + 1.45 +// HRTF stands for Head-Related Transfer Function. 1.46 +// HRTFKernel is a frequency-domain representation of an impulse-response used as part of the spatialized panning system. 1.47 +// For a given azimuth / elevation angle there will be one HRTFKernel for the left ear transfer function, and one for the right ear. 1.48 +// The leading delay (average group delay) for each impulse response is extracted: 1.49 +// m_fftFrame is the frequency-domain representation of the impulse response with the delay removed 1.50 +// m_frameDelay is the leading delay of the original impulse response. 1.51 +class HRTFKernel { 1.52 +public: 1.53 + // Note: this is destructive on the passed in |impulseResponse|. 1.54 + // The |length| of |impulseResponse| must be a power of two. 1.55 + // The size of the DFT will be |2 * length|. 1.56 + static nsReturnRef<HRTFKernel> create(float* impulseResponse, size_t length, float sampleRate); 1.57 + 1.58 + static nsReturnRef<HRTFKernel> create(nsAutoPtr<FFTBlock> fftFrame, float frameDelay, float sampleRate); 1.59 + 1.60 + // Given two HRTFKernels, and an interpolation factor x: 0 -> 1, returns an interpolated HRTFKernel. 1.61 + static nsReturnRef<HRTFKernel> createInterpolatedKernel(HRTFKernel* kernel1, HRTFKernel* kernel2, float x); 1.62 + 1.63 + FFTBlock* fftFrame() { return m_fftFrame.get(); } 1.64 + 1.65 + size_t fftSize() const { return m_fftFrame->FFTSize(); } 1.66 + float frameDelay() const { return m_frameDelay; } 1.67 + 1.68 + float sampleRate() const { return m_sampleRate; } 1.69 + double nyquist() const { return 0.5 * sampleRate(); } 1.70 + 1.71 + size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const 1.72 + { 1.73 + size_t amount = aMallocSizeOf(this); 1.74 + amount += m_fftFrame->SizeOfIncludingThis(aMallocSizeOf); 1.75 + return amount; 1.76 + } 1.77 + 1.78 +private: 1.79 + HRTFKernel(const HRTFKernel& other) MOZ_DELETE; 1.80 + void operator=(const HRTFKernel& other) MOZ_DELETE; 1.81 + 1.82 + // Note: this is destructive on the passed in |impulseResponse|. 1.83 + HRTFKernel(float* impulseResponse, size_t fftSize, float sampleRate); 1.84 + 1.85 + HRTFKernel(nsAutoPtr<FFTBlock> fftFrame, float frameDelay, float sampleRate) 1.86 + : m_fftFrame(fftFrame) 1.87 + , m_frameDelay(frameDelay) 1.88 + , m_sampleRate(sampleRate) 1.89 + { 1.90 + } 1.91 + 1.92 + nsAutoPtr<FFTBlock> m_fftFrame; 1.93 + float m_frameDelay; 1.94 + float m_sampleRate; 1.95 +}; 1.96 + 1.97 +typedef nsTArray<nsAutoRef<HRTFKernel> > HRTFKernelList; 1.98 + 1.99 +} // namespace WebCore 1.100 + 1.101 +template <> 1.102 +class nsAutoRefTraits<WebCore::HRTFKernel> : 1.103 + public nsPointerRefTraits<WebCore::HRTFKernel> { 1.104 +public: 1.105 + static void Release(WebCore::HRTFKernel* ptr) { delete(ptr); } 1.106 +}; 1.107 + 1.108 +namespace WebCore { 1.109 + 1.110 +inline nsReturnRef<HRTFKernel> HRTFKernel::create(float* impulseResponse, size_t length, float sampleRate) 1.111 +{ 1.112 + return nsReturnRef<HRTFKernel>(new HRTFKernel(impulseResponse, length, sampleRate)); 1.113 +} 1.114 + 1.115 +inline nsReturnRef<HRTFKernel> HRTFKernel::create(nsAutoPtr<FFTBlock> fftFrame, float frameDelay, float sampleRate) 1.116 +{ 1.117 + return nsReturnRef<HRTFKernel>(new HRTFKernel(fftFrame, frameDelay, sampleRate)); 1.118 +} 1.119 + 1.120 +} // namespace WebCore 1.121 +#endif // HRTFKernel_h