tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

HRTFDatabase.cpp (5192B)


      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 #include "HRTFDatabase.h"
     30 
     31 #include "HRTFElevation.h"
     32 
     33 namespace WebCore {
     34 
     35 const int HRTFDatabase::MinElevation = -45;
     36 const int HRTFDatabase::MaxElevation = 90;
     37 const unsigned HRTFDatabase::RawElevationAngleSpacing = 15;
     38 const unsigned HRTFDatabase::NumberOfRawElevations =
     39    10;  // -45 -> +90 (each 15 degrees)
     40 const unsigned HRTFDatabase::InterpolationFactor = 1;
     41 const unsigned HRTFDatabase::NumberOfTotalElevations =
     42    NumberOfRawElevations * InterpolationFactor;
     43 
     44 nsReturnRef<HRTFDatabase> HRTFDatabase::create(float sampleRate) {
     45  return nsReturnRef<HRTFDatabase>(new HRTFDatabase(sampleRate));
     46 }
     47 
     48 HRTFDatabase::HRTFDatabase(float sampleRate) : m_sampleRate(sampleRate) {
     49  m_elevations.SetLength(NumberOfTotalElevations);
     50 
     51  unsigned elevationIndex = 0;
     52  for (int elevation = MinElevation; elevation <= MaxElevation;
     53       elevation += RawElevationAngleSpacing) {
     54    nsAutoRef<HRTFElevation> hrtfElevation(
     55        HRTFElevation::createBuiltin(elevation, sampleRate));
     56    MOZ_ASSERT(hrtfElevation.get());
     57    if (!hrtfElevation.get()) return;
     58 
     59    m_elevations[elevationIndex] = hrtfElevation.out();
     60    elevationIndex += InterpolationFactor;
     61  }
     62 
     63  // Now, go back and interpolate elevations.
     64  if (InterpolationFactor > 1) {
     65    for (unsigned i = 0; i < NumberOfTotalElevations;
     66         i += InterpolationFactor) {
     67      unsigned j = (i + InterpolationFactor);
     68      if (j >= NumberOfTotalElevations)
     69        j = i;  // for last elevation interpolate with itself
     70 
     71      // Create the interpolated convolution kernels and delays.
     72      for (unsigned jj = 1; jj < InterpolationFactor; ++jj) {
     73        float x =
     74            static_cast<float>(jj) / static_cast<float>(InterpolationFactor);
     75        m_elevations[i + jj] = HRTFElevation::createByInterpolatingSlices(
     76            m_elevations[i].get(), m_elevations[j].get(), x, sampleRate);
     77        MOZ_ASSERT(m_elevations[i + jj].get());
     78      }
     79    }
     80  }
     81 }
     82 
     83 size_t HRTFDatabase::sizeOfIncludingThis(
     84    mozilla::MallocSizeOf aMallocSizeOf) const {
     85  size_t amount = aMallocSizeOf(this);
     86  amount += m_elevations.ShallowSizeOfExcludingThis(aMallocSizeOf);
     87  for (size_t i = 0; i < m_elevations.Length(); i++) {
     88    amount += m_elevations[i]->sizeOfIncludingThis(aMallocSizeOf);
     89  }
     90 
     91  return amount;
     92 }
     93 
     94 void HRTFDatabase::getKernelsFromAzimuthElevation(
     95    double azimuthBlend, unsigned azimuthIndex, double elevationAngle,
     96    HRTFKernel*& kernelL, HRTFKernel*& kernelR, double& frameDelayL,
     97    double& frameDelayR) {
     98  unsigned elevationIndex = indexFromElevationAngle(elevationAngle);
     99  MOZ_ASSERT(elevationIndex < m_elevations.Length() &&
    100             m_elevations.Length() > 0);
    101 
    102  if (!m_elevations.Length()) {
    103    kernelL = 0;
    104    kernelR = 0;
    105    return;
    106  }
    107 
    108  if (elevationIndex > m_elevations.Length() - 1)
    109    elevationIndex = m_elevations.Length() - 1;
    110 
    111  HRTFElevation* hrtfElevation = m_elevations[elevationIndex].get();
    112  MOZ_ASSERT(hrtfElevation);
    113  if (!hrtfElevation) {
    114    kernelL = 0;
    115    kernelR = 0;
    116    return;
    117  }
    118 
    119  hrtfElevation->getKernelsFromAzimuth(azimuthBlend, azimuthIndex, kernelL,
    120                                       kernelR, frameDelayL, frameDelayR);
    121 }
    122 
    123 unsigned HRTFDatabase::indexFromElevationAngle(double elevationAngle) {
    124  // Clamp to allowed range.
    125  elevationAngle = std::clamp(elevationAngle, static_cast<double>(MinElevation),
    126                              static_cast<double>(MaxElevation));
    127 
    128  unsigned elevationIndex =
    129      static_cast<int>(InterpolationFactor * (elevationAngle - MinElevation) /
    130                       RawElevationAngleSpacing);
    131  return elevationIndex;
    132 }
    133 
    134 }  // namespace WebCore