HRTFElevation.h (4897B)
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 */ 28 29 #ifndef HRTFElevation_h 30 #define HRTFElevation_h 31 32 #include "HRTFKernel.h" 33 #include "mozilla/MemoryReporting.h" 34 #include "nsAutoRef.h" 35 36 struct SpeexResamplerState_; 37 typedef struct SpeexResamplerState_ SpeexResamplerState; 38 39 namespace WebCore { 40 41 // HRTFElevation contains all of the HRTFKernels (one left ear and one right ear 42 // per azimuth angle) for a particular elevation. 43 44 class HRTFElevation { 45 public: 46 // Loads and returns an HRTFElevation with the given HRTF database subject 47 // name and elevation from browser (or WebKit.framework) resources. Normally, 48 // there will only be a single HRTF database set, but this API supports the 49 // possibility of multiple ones with different names. Interpolated azimuths 50 // will be generated based on InterpolationFactor. Valid values for elevation 51 // are -45 -> +90 in 15 degree increments. 52 static nsReturnRef<HRTFElevation> createBuiltin(int elevation, 53 float sampleRate); 54 55 // Given two HRTFElevations, and an interpolation factor x: 0 -> 1, returns an 56 // interpolated HRTFElevation. 57 static nsReturnRef<HRTFElevation> createByInterpolatingSlices( 58 HRTFElevation* hrtfElevation1, HRTFElevation* hrtfElevation2, float x, 59 float sampleRate); 60 61 double elevationAngle() const { return m_elevationAngle; } 62 unsigned numberOfAzimuths() const { return NumberOfTotalAzimuths; } 63 float sampleRate() const { return m_sampleRate; } 64 65 // Returns the left and right kernels for the given azimuth index. 66 // The interpolated delays based on azimuthBlend: 0 -> 1 are returned in 67 // frameDelayL and frameDelayR. 68 void getKernelsFromAzimuth(double azimuthBlend, unsigned azimuthIndex, 69 HRTFKernel*& kernelL, HRTFKernel*& kernelR, 70 double& frameDelayL, double& frameDelayR); 71 72 // Total number of azimuths after interpolation. 73 static const unsigned NumberOfTotalAzimuths; 74 75 static size_t fftSizeForSampleRate(float sampleRate); 76 77 size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 78 79 private: 80 HRTFElevation(const HRTFElevation& other) = delete; 81 void operator=(const HRTFElevation& other) = delete; 82 83 HRTFElevation(HRTFKernelList&& kernelListL, int elevation, float sampleRate) 84 : m_kernelListL(std::move(kernelListL)), 85 m_elevationAngle(elevation), 86 m_sampleRate(sampleRate) {} 87 88 // Returns the list of left ear HRTFKernels for all the azimuths going from 0 89 // to 360 degrees. 90 const HRTFKernelList& kernelListL() { return m_kernelListL; } 91 92 // Given a specific azimuth and elevation angle, returns the left HRTFKernel. 93 // Values for azimuth must be multiples of 15 in 0 -> 345, 94 // but not all azimuths are available for elevations > +45. 95 // Valid values for elevation are -45 -> +90 in 15 degree increments. 96 static nsReturnRef<HRTFKernel> calculateKernelForAzimuthElevation( 97 int azimuth, int elevation, SpeexResamplerState* resampler, 98 float sampleRate); 99 100 HRTFKernelList m_kernelListL; 101 double m_elevationAngle; 102 float m_sampleRate; 103 }; 104 105 } // namespace WebCore 106 107 template <> 108 class nsAutoRefTraits<WebCore::HRTFElevation> 109 : public nsPointerRefTraits<WebCore::HRTFElevation> { 110 public: 111 static void Release(WebCore::HRTFElevation* ptr) { delete (ptr); } 112 }; 113 114 #endif // HRTFElevation_h