Home | History | Annotate | Download | only in audio
      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 "config.h"
     30 
     31 #if ENABLE(WEB_AUDIO)
     32 
     33 #include "HRTFDatabase.h"
     34 
     35 #include "HRTFElevation.h"
     36 
     37 using namespace std;
     38 
     39 namespace WebCore {
     40 
     41 const int HRTFDatabase::MinElevation = -45;
     42 const int HRTFDatabase::MaxElevation = 90;
     43 const unsigned HRTFDatabase::RawElevationAngleSpacing = 15;
     44 const unsigned HRTFDatabase::NumberOfRawElevations = 10; // -45 -> +90 (each 15 degrees)
     45 const unsigned HRTFDatabase::InterpolationFactor = 1;
     46 const unsigned HRTFDatabase::NumberOfTotalElevations = NumberOfRawElevations * InterpolationFactor;
     47 
     48 PassOwnPtr<HRTFDatabase> HRTFDatabase::create(double sampleRate)
     49 {
     50     OwnPtr<HRTFDatabase> hrtfDatabase = adoptPtr(new HRTFDatabase(sampleRate));
     51     return hrtfDatabase.release();
     52 }
     53 
     54 HRTFDatabase::HRTFDatabase(double sampleRate)
     55     : m_elevations(NumberOfTotalElevations)
     56     , m_sampleRate(sampleRate)
     57 {
     58     unsigned elevationIndex = 0;
     59     for (int elevation = MinElevation; elevation <= MaxElevation; elevation += RawElevationAngleSpacing) {
     60         OwnPtr<HRTFElevation> hrtfElevation = HRTFElevation::createForSubject("Composite", elevation, sampleRate);
     61         ASSERT(hrtfElevation.get());
     62         if (!hrtfElevation.get())
     63             return;
     64 
     65         m_elevations[elevationIndex] = hrtfElevation.release();
     66         elevationIndex += InterpolationFactor;
     67     }
     68 
     69     // Now, go back and interpolate elevations.
     70     if (InterpolationFactor > 1) {
     71         for (unsigned i = 0; i < NumberOfTotalElevations; i += InterpolationFactor) {
     72             unsigned j = (i + InterpolationFactor);
     73             if (j >= NumberOfTotalElevations)
     74                 j = i; // for last elevation interpolate with itself
     75 
     76             // Create the interpolated convolution kernels and delays.
     77             for (unsigned jj = 1; jj < InterpolationFactor; ++jj) {
     78                 double x = static_cast<double>(jj) / static_cast<double>(InterpolationFactor);
     79                 m_elevations[i + jj] = HRTFElevation::createByInterpolatingSlices(m_elevations[i].get(), m_elevations[j].get(), x, sampleRate);
     80                 ASSERT(m_elevations[i + jj].get());
     81             }
     82         }
     83     }
     84 }
     85 
     86 void HRTFDatabase::getKernelsFromAzimuthElevation(double azimuthBlend, unsigned azimuthIndex, double elevationAngle, HRTFKernel* &kernelL, HRTFKernel* &kernelR,
     87                                                   double& frameDelayL, double& frameDelayR)
     88 {
     89     unsigned elevationIndex = indexFromElevationAngle(elevationAngle);
     90     ASSERT(elevationIndex < m_elevations.size() && m_elevations.size() > 0);
     91 
     92     if (!m_elevations.size()) {
     93         kernelL = 0;
     94         kernelR = 0;
     95         return;
     96     }
     97 
     98     if (elevationIndex > m_elevations.size() - 1)
     99         elevationIndex = m_elevations.size() - 1;
    100 
    101     HRTFElevation* hrtfElevation = m_elevations[elevationIndex].get();
    102     ASSERT(hrtfElevation);
    103     if (!hrtfElevation) {
    104         kernelL = 0;
    105         kernelR = 0;
    106         return;
    107     }
    108 
    109     hrtfElevation->getKernelsFromAzimuth(azimuthBlend, azimuthIndex, kernelL, kernelR, frameDelayL, frameDelayR);
    110 }
    111 
    112 unsigned HRTFDatabase::indexFromElevationAngle(double elevationAngle)
    113 {
    114     // Clamp to allowed range.
    115     elevationAngle = max(static_cast<double>(MinElevation), elevationAngle);
    116     elevationAngle = min(static_cast<double>(MaxElevation), elevationAngle);
    117 
    118     unsigned elevationIndex = static_cast<int>(InterpolationFactor * (elevationAngle - MinElevation) / RawElevationAngleSpacing);
    119     return elevationIndex;
    120 }
    121 
    122 } // namespace WebCore
    123 
    124 #endif // ENABLE(WEB_AUDIO)
    125