Move WebAudio source code to std::unique_ptr
[WebKit-https.git] / Source / WebCore / platform / audio / HRTFDatabase.cpp
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 namespace WebCore {
38
39 const int HRTFDatabase::MinElevation = -45;
40 const int HRTFDatabase::MaxElevation = 90;
41 const unsigned HRTFDatabase::RawElevationAngleSpacing = 15;
42 const unsigned HRTFDatabase::NumberOfRawElevations = 10; // -45 -> +90 (each 15 degrees)
43 const unsigned HRTFDatabase::InterpolationFactor = 1;
44 const unsigned HRTFDatabase::NumberOfTotalElevations = NumberOfRawElevations * InterpolationFactor;
45
46 HRTFDatabase::HRTFDatabase(float sampleRate)
47     : m_elevations(NumberOfTotalElevations)
48     , m_sampleRate(sampleRate)
49 {
50     unsigned elevationIndex = 0;
51     for (int elevation = MinElevation; elevation <= MaxElevation; elevation += RawElevationAngleSpacing) {
52         std::unique_ptr<HRTFElevation> hrtfElevation = HRTFElevation::createForSubject("Composite", elevation, sampleRate);
53         ASSERT(hrtfElevation.get());
54         if (!hrtfElevation.get())
55             return;
56         
57         m_elevations[elevationIndex] = std::move(hrtfElevation);
58         elevationIndex += InterpolationFactor;
59     }
60
61     // Now, go back and interpolate elevations.
62     if (InterpolationFactor > 1) {
63         for (unsigned i = 0; i < NumberOfTotalElevations; i += InterpolationFactor) {
64             unsigned j = (i + InterpolationFactor);
65             if (j >= NumberOfTotalElevations)
66                 j = i; // for last elevation interpolate with itself
67
68             // Create the interpolated convolution kernels and delays.
69             for (unsigned jj = 1; jj < InterpolationFactor; ++jj) {
70                 float x = static_cast<float>(jj) / static_cast<float>(InterpolationFactor);
71                 m_elevations[i + jj] = HRTFElevation::createByInterpolatingSlices(m_elevations[i].get(), m_elevations[j].get(), x, sampleRate);
72                 ASSERT(m_elevations[i + jj].get());
73             }
74         }
75     }
76 }
77
78 void HRTFDatabase::getKernelsFromAzimuthElevation(double azimuthBlend, unsigned azimuthIndex, double elevationAngle, HRTFKernel* &kernelL, HRTFKernel* &kernelR,
79                                                   double& frameDelayL, double& frameDelayR)
80 {
81     unsigned elevationIndex = indexFromElevationAngle(elevationAngle);
82     ASSERT_WITH_SECURITY_IMPLICATION(elevationIndex < m_elevations.size() && m_elevations.size() > 0);
83     
84     if (!m_elevations.size()) {
85         kernelL = 0;
86         kernelR = 0;
87         return;
88     }
89     
90     if (elevationIndex > m_elevations.size() - 1)
91         elevationIndex = m_elevations.size() - 1;    
92     
93     HRTFElevation* hrtfElevation = m_elevations[elevationIndex].get();
94     ASSERT(hrtfElevation);
95     if (!hrtfElevation) {
96         kernelL = 0;
97         kernelR = 0;
98         return;
99     }
100     
101     hrtfElevation->getKernelsFromAzimuth(azimuthBlend, azimuthIndex, kernelL, kernelR, frameDelayL, frameDelayR);
102 }                                                     
103
104 unsigned HRTFDatabase::indexFromElevationAngle(double elevationAngle)
105 {
106     // Clamp to allowed range.
107     elevationAngle = std::max(static_cast<double>(MinElevation), elevationAngle);
108     elevationAngle = std::min(static_cast<double>(MaxElevation), elevationAngle);
109
110     unsigned elevationIndex = static_cast<int>(InterpolationFactor * (elevationAngle - MinElevation) / RawElevationAngleSpacing);    
111     return elevationIndex;
112 }
113
114 } // namespace WebCore
115
116 #endif // ENABLE(WEB_AUDIO)