OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | 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 | 14 * its contributors may be used to endorse or promote products derived |
15 * from this software without specific prior written permission. | 15 * from this software without specific prior written permission. |
16 * | 16 * |
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | 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 | 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 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 | 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | 28 |
| 29 #include "platform/audio/AudioBus.h" |
29 #include "platform/audio/HRTFElevation.h" | 30 #include "platform/audio/HRTFElevation.h" |
30 #include <math.h> | |
31 #include <algorithm> | |
32 #include "platform/audio/AudioBus.h" | |
33 #include "platform/audio/HRTFPanner.h" | 31 #include "platform/audio/HRTFPanner.h" |
| 32 #include "wtf/PtrUtil.h" |
34 #include "wtf/ThreadingPrimitives.h" | 33 #include "wtf/ThreadingPrimitives.h" |
35 #include "wtf/text/StringHash.h" | 34 #include "wtf/text/StringHash.h" |
| 35 #include <algorithm> |
| 36 #include <math.h> |
| 37 #include <memory> |
36 | 38 |
37 namespace blink { | 39 namespace blink { |
38 | 40 |
39 const unsigned HRTFElevation::AzimuthSpacing = 15; | 41 const unsigned HRTFElevation::AzimuthSpacing = 15; |
40 const unsigned HRTFElevation::NumberOfRawAzimuths = 360 / AzimuthSpacing; | 42 const unsigned HRTFElevation::NumberOfRawAzimuths = 360 / AzimuthSpacing; |
41 const unsigned HRTFElevation::InterpolationFactor = 8; | 43 const unsigned HRTFElevation::InterpolationFactor = 8; |
42 const unsigned HRTFElevation::NumberOfTotalAzimuths = NumberOfRawAzimuths * Inte
rpolationFactor; | 44 const unsigned HRTFElevation::NumberOfTotalAzimuths = NumberOfRawAzimuths * Inte
rpolationFactor; |
43 | 45 |
44 // Total number of components of an HRTF database. | 46 // Total number of components of an HRTF database. |
45 const size_t TotalNumberOfResponses = 240; | 47 const size_t TotalNumberOfResponses = 240; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 // Check number of channels and length. For now these are fixed and known. | 91 // Check number of channels and length. For now these are fixed and known. |
90 bool isBusGood = responseLength == expectedLength && bus->numberOfChannels()
== 2; | 92 bool isBusGood = responseLength == expectedLength && bus->numberOfChannels()
== 2; |
91 ASSERT(isBusGood); | 93 ASSERT(isBusGood); |
92 if (!isBusGood) | 94 if (!isBusGood) |
93 return nullptr; | 95 return nullptr; |
94 | 96 |
95 return bus; | 97 return bus; |
96 } | 98 } |
97 #endif | 99 #endif |
98 | 100 |
99 bool HRTFElevation::calculateKernelsForAzimuthElevation(int azimuth, int elevati
on, float sampleRate, const String& subjectName, OwnPtr<HRTFKernel>& kernelL, Ow
nPtr<HRTFKernel>& kernelR) | 101 bool HRTFElevation::calculateKernelsForAzimuthElevation(int azimuth, int elevati
on, float sampleRate, const String& subjectName, std::unique_ptr<HRTFKernel>& ke
rnelL, std::unique_ptr<HRTFKernel>& kernelR) |
100 { | 102 { |
101 // Valid values for azimuth are 0 -> 345 in 15 degree increments. | 103 // Valid values for azimuth are 0 -> 345 in 15 degree increments. |
102 // Valid values for elevation are -45 -> +90 in 15 degree increments. | 104 // Valid values for elevation are -45 -> +90 in 15 degree increments. |
103 | 105 |
104 bool isAzimuthGood = azimuth >= 0 && azimuth <= 345 && (azimuth / 15) * 15 =
= azimuth; | 106 bool isAzimuthGood = azimuth >= 0 && azimuth <= 345 && (azimuth / 15) * 15 =
= azimuth; |
105 ASSERT(isAzimuthGood); | 107 ASSERT(isAzimuthGood); |
106 if (!isAzimuthGood) | 108 if (!isAzimuthGood) |
107 return false; | 109 return false; |
108 | 110 |
109 bool isElevationGood = elevation >= -45 && elevation <= 90 && (elevation / 1
5) * 15 == elevation; | 111 bool isElevationGood = elevation >= -45 && elevation <= 90 && (elevation / 1
5) * 15 == elevation; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 75, // 240 | 214 75, // 240 |
213 45, // 255 | 215 45, // 255 |
214 60, // 270 | 216 60, // 270 |
215 45, // 285 | 217 45, // 285 |
216 75, // 300 | 218 75, // 300 |
217 45, // 315 | 219 45, // 315 |
218 60, // 330 | 220 60, // 330 |
219 45 // 345 | 221 45 // 345 |
220 }; | 222 }; |
221 | 223 |
222 PassOwnPtr<HRTFElevation> HRTFElevation::createForSubject(const String& subjectN
ame, int elevation, float sampleRate) | 224 std::unique_ptr<HRTFElevation> HRTFElevation::createForSubject(const String& sub
jectName, int elevation, float sampleRate) |
223 { | 225 { |
224 bool isElevationGood = elevation >= -45 && elevation <= 90 && (elevation / 1
5) * 15 == elevation; | 226 bool isElevationGood = elevation >= -45 && elevation <= 90 && (elevation / 1
5) * 15 == elevation; |
225 ASSERT(isElevationGood); | 227 ASSERT(isElevationGood); |
226 if (!isElevationGood) | 228 if (!isElevationGood) |
227 return nullptr; | 229 return nullptr; |
228 | 230 |
229 OwnPtr<HRTFKernelList> kernelListL = adoptPtr(new HRTFKernelList(NumberOfTot
alAzimuths)); | 231 std::unique_ptr<HRTFKernelList> kernelListL = wrapUnique(new HRTFKernelList(
NumberOfTotalAzimuths)); |
230 OwnPtr<HRTFKernelList> kernelListR = adoptPtr(new HRTFKernelList(NumberOfTot
alAzimuths)); | 232 std::unique_ptr<HRTFKernelList> kernelListR = wrapUnique(new HRTFKernelList(
NumberOfTotalAzimuths)); |
231 | 233 |
232 // Load convolution kernels from HRTF files. | 234 // Load convolution kernels from HRTF files. |
233 int interpolatedIndex = 0; | 235 int interpolatedIndex = 0; |
234 for (unsigned rawIndex = 0; rawIndex < NumberOfRawAzimuths; ++rawIndex) { | 236 for (unsigned rawIndex = 0; rawIndex < NumberOfRawAzimuths; ++rawIndex) { |
235 // Don't let elevation exceed maximum for this azimuth. | 237 // Don't let elevation exceed maximum for this azimuth. |
236 int maxElevation = maxElevations[rawIndex]; | 238 int maxElevation = maxElevations[rawIndex]; |
237 int actualElevation = std::min(elevation, maxElevation); | 239 int actualElevation = std::min(elevation, maxElevation); |
238 | 240 |
239 bool success = calculateKernelsForAzimuthElevation(rawIndex * AzimuthSpa
cing, actualElevation, sampleRate, subjectName, kernelListL->at(interpolatedInde
x), kernelListR->at(interpolatedIndex)); | 241 bool success = calculateKernelsForAzimuthElevation(rawIndex * AzimuthSpa
cing, actualElevation, sampleRate, subjectName, kernelListL->at(interpolatedInde
x), kernelListR->at(interpolatedIndex)); |
240 if (!success) | 242 if (!success) |
241 return nullptr; | 243 return nullptr; |
242 | 244 |
243 interpolatedIndex += InterpolationFactor; | 245 interpolatedIndex += InterpolationFactor; |
244 } | 246 } |
245 | 247 |
246 // Now go back and interpolate intermediate azimuth values. | 248 // Now go back and interpolate intermediate azimuth values. |
247 for (unsigned i = 0; i < NumberOfTotalAzimuths; i += InterpolationFactor) { | 249 for (unsigned i = 0; i < NumberOfTotalAzimuths; i += InterpolationFactor) { |
248 int j = (i + InterpolationFactor) % NumberOfTotalAzimuths; | 250 int j = (i + InterpolationFactor) % NumberOfTotalAzimuths; |
249 | 251 |
250 // Create the interpolated convolution kernels and delays. | 252 // Create the interpolated convolution kernels and delays. |
251 for (unsigned jj = 1; jj < InterpolationFactor; ++jj) { | 253 for (unsigned jj = 1; jj < InterpolationFactor; ++jj) { |
252 float x = float(jj) / float(InterpolationFactor); // interpolate fro
m 0 -> 1 | 254 float x = float(jj) / float(InterpolationFactor); // interpolate fro
m 0 -> 1 |
253 | 255 |
254 (*kernelListL)[i + jj] = HRTFKernel::createInterpolatedKernel(kernel
ListL->at(i).get(), kernelListL->at(j).get(), x); | 256 (*kernelListL)[i + jj] = HRTFKernel::createInterpolatedKernel(kernel
ListL->at(i).get(), kernelListL->at(j).get(), x); |
255 (*kernelListR)[i + jj] = HRTFKernel::createInterpolatedKernel(kernel
ListR->at(i).get(), kernelListR->at(j).get(), x); | 257 (*kernelListR)[i + jj] = HRTFKernel::createInterpolatedKernel(kernel
ListR->at(i).get(), kernelListR->at(j).get(), x); |
256 } | 258 } |
257 } | 259 } |
258 | 260 |
259 OwnPtr<HRTFElevation> hrtfElevation = adoptPtr(new HRTFElevation(std::move(k
ernelListL), std::move(kernelListR), elevation, sampleRate)); | 261 std::unique_ptr<HRTFElevation> hrtfElevation = wrapUnique(new HRTFElevation(
std::move(kernelListL), std::move(kernelListR), elevation, sampleRate)); |
260 return hrtfElevation; | 262 return hrtfElevation; |
261 } | 263 } |
262 | 264 |
263 PassOwnPtr<HRTFElevation> HRTFElevation::createByInterpolatingSlices(HRTFElevati
on* hrtfElevation1, HRTFElevation* hrtfElevation2, float x, float sampleRate) | 265 std::unique_ptr<HRTFElevation> HRTFElevation::createByInterpolatingSlices(HRTFEl
evation* hrtfElevation1, HRTFElevation* hrtfElevation2, float x, float sampleRat
e) |
264 { | 266 { |
265 ASSERT(hrtfElevation1 && hrtfElevation2); | 267 ASSERT(hrtfElevation1 && hrtfElevation2); |
266 if (!hrtfElevation1 || !hrtfElevation2) | 268 if (!hrtfElevation1 || !hrtfElevation2) |
267 return nullptr; | 269 return nullptr; |
268 | 270 |
269 ASSERT(x >= 0.0 && x < 1.0); | 271 ASSERT(x >= 0.0 && x < 1.0); |
270 | 272 |
271 OwnPtr<HRTFKernelList> kernelListL = adoptPtr(new HRTFKernelList(NumberOfTot
alAzimuths)); | 273 std::unique_ptr<HRTFKernelList> kernelListL = wrapUnique(new HRTFKernelList(
NumberOfTotalAzimuths)); |
272 OwnPtr<HRTFKernelList> kernelListR = adoptPtr(new HRTFKernelList(NumberOfTot
alAzimuths)); | 274 std::unique_ptr<HRTFKernelList> kernelListR = wrapUnique(new HRTFKernelList(
NumberOfTotalAzimuths)); |
273 | 275 |
274 HRTFKernelList* kernelListL1 = hrtfElevation1->kernelListL(); | 276 HRTFKernelList* kernelListL1 = hrtfElevation1->kernelListL(); |
275 HRTFKernelList* kernelListR1 = hrtfElevation1->kernelListR(); | 277 HRTFKernelList* kernelListR1 = hrtfElevation1->kernelListR(); |
276 HRTFKernelList* kernelListL2 = hrtfElevation2->kernelListL(); | 278 HRTFKernelList* kernelListL2 = hrtfElevation2->kernelListL(); |
277 HRTFKernelList* kernelListR2 = hrtfElevation2->kernelListR(); | 279 HRTFKernelList* kernelListR2 = hrtfElevation2->kernelListR(); |
278 | 280 |
279 // Interpolate kernels of corresponding azimuths of the two elevations. | 281 // Interpolate kernels of corresponding azimuths of the two elevations. |
280 for (unsigned i = 0; i < NumberOfTotalAzimuths; ++i) { | 282 for (unsigned i = 0; i < NumberOfTotalAzimuths; ++i) { |
281 (*kernelListL)[i] = HRTFKernel::createInterpolatedKernel(kernelListL1->a
t(i).get(), kernelListL2->at(i).get(), x); | 283 (*kernelListL)[i] = HRTFKernel::createInterpolatedKernel(kernelListL1->a
t(i).get(), kernelListL2->at(i).get(), x); |
282 (*kernelListR)[i] = HRTFKernel::createInterpolatedKernel(kernelListR1->a
t(i).get(), kernelListR2->at(i).get(), x); | 284 (*kernelListR)[i] = HRTFKernel::createInterpolatedKernel(kernelListR1->a
t(i).get(), kernelListR2->at(i).get(), x); |
283 } | 285 } |
284 | 286 |
285 // Interpolate elevation angle. | 287 // Interpolate elevation angle. |
286 double angle = (1.0 - x) * hrtfElevation1->elevationAngle() + x * hrtfElevat
ion2->elevationAngle(); | 288 double angle = (1.0 - x) * hrtfElevation1->elevationAngle() + x * hrtfElevat
ion2->elevationAngle(); |
287 | 289 |
288 OwnPtr<HRTFElevation> hrtfElevation = adoptPtr(new HRTFElevation(std::move(k
ernelListL), std::move(kernelListR), static_cast<int>(angle), sampleRate)); | 290 std::unique_ptr<HRTFElevation> hrtfElevation = wrapUnique(new HRTFElevation(
std::move(kernelListL), std::move(kernelListR), static_cast<int>(angle), sampleR
ate)); |
289 return hrtfElevation; | 291 return hrtfElevation; |
290 } | 292 } |
291 | 293 |
292 void HRTFElevation::getKernelsFromAzimuth(double azimuthBlend, unsigned azimuthI
ndex, HRTFKernel* &kernelL, HRTFKernel* &kernelR, double& frameDelayL, double& f
rameDelayR) | 294 void HRTFElevation::getKernelsFromAzimuth(double azimuthBlend, unsigned azimuthI
ndex, HRTFKernel* &kernelL, HRTFKernel* &kernelR, double& frameDelayL, double& f
rameDelayR) |
293 { | 295 { |
294 bool checkAzimuthBlend = azimuthBlend >= 0.0 && azimuthBlend < 1.0; | 296 bool checkAzimuthBlend = azimuthBlend >= 0.0 && azimuthBlend < 1.0; |
295 ASSERT(checkAzimuthBlend); | 297 ASSERT(checkAzimuthBlend); |
296 if (!checkAzimuthBlend) | 298 if (!checkAzimuthBlend) |
297 azimuthBlend = 0.0; | 299 azimuthBlend = 0.0; |
298 | 300 |
(...skipping 18 matching lines...) Expand all Loading... |
317 double frameDelay2L = m_kernelListL->at(azimuthIndex2)->frameDelay(); | 319 double frameDelay2L = m_kernelListL->at(azimuthIndex2)->frameDelay(); |
318 double frameDelay2R = m_kernelListR->at(azimuthIndex2)->frameDelay(); | 320 double frameDelay2R = m_kernelListR->at(azimuthIndex2)->frameDelay(); |
319 | 321 |
320 // Linearly interpolate delays. | 322 // Linearly interpolate delays. |
321 frameDelayL = (1.0 - azimuthBlend) * frameDelayL + azimuthBlend * frameDelay
2L; | 323 frameDelayL = (1.0 - azimuthBlend) * frameDelayL + azimuthBlend * frameDelay
2L; |
322 frameDelayR = (1.0 - azimuthBlend) * frameDelayR + azimuthBlend * frameDelay
2R; | 324 frameDelayR = (1.0 - azimuthBlend) * frameDelayR + azimuthBlend * frameDelay
2R; |
323 } | 325 } |
324 | 326 |
325 } // namespace blink | 327 } // namespace blink |
326 | 328 |
OLD | NEW |