| 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/HRTFElevation.h" |
| 30 #include <math.h> |
| 31 #include <algorithm> |
| 29 #include "platform/audio/AudioBus.h" | 32 #include "platform/audio/AudioBus.h" |
| 30 #include "platform/audio/HRTFElevation.h" | |
| 31 #include "platform/audio/HRTFPanner.h" | 33 #include "platform/audio/HRTFPanner.h" |
| 32 #include "wtf/PtrUtil.h" | |
| 33 #include "wtf/ThreadingPrimitives.h" | 34 #include "wtf/ThreadingPrimitives.h" |
| 34 #include "wtf/text/StringHash.h" | 35 #include "wtf/text/StringHash.h" |
| 35 #include <algorithm> | |
| 36 #include <math.h> | |
| 37 #include <memory> | |
| 38 | 36 |
| 39 namespace blink { | 37 namespace blink { |
| 40 | 38 |
| 41 const unsigned HRTFElevation::AzimuthSpacing = 15; | 39 const unsigned HRTFElevation::AzimuthSpacing = 15; |
| 42 const unsigned HRTFElevation::NumberOfRawAzimuths = 360 / AzimuthSpacing; | 40 const unsigned HRTFElevation::NumberOfRawAzimuths = 360 / AzimuthSpacing; |
| 43 const unsigned HRTFElevation::InterpolationFactor = 8; | 41 const unsigned HRTFElevation::InterpolationFactor = 8; |
| 44 const unsigned HRTFElevation::NumberOfTotalAzimuths = NumberOfRawAzimuths * Inte
rpolationFactor; | 42 const unsigned HRTFElevation::NumberOfTotalAzimuths = NumberOfRawAzimuths * Inte
rpolationFactor; |
| 45 | 43 |
| 46 // Total number of components of an HRTF database. | 44 // Total number of components of an HRTF database. |
| 47 const size_t TotalNumberOfResponses = 240; | 45 const size_t TotalNumberOfResponses = 240; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // Check number of channels and length. For now these are fixed and known. | 89 // Check number of channels and length. For now these are fixed and known. |
| 92 bool isBusGood = responseLength == expectedLength && bus->numberOfChannels()
== 2; | 90 bool isBusGood = responseLength == expectedLength && bus->numberOfChannels()
== 2; |
| 93 ASSERT(isBusGood); | 91 ASSERT(isBusGood); |
| 94 if (!isBusGood) | 92 if (!isBusGood) |
| 95 return nullptr; | 93 return nullptr; |
| 96 | 94 |
| 97 return bus; | 95 return bus; |
| 98 } | 96 } |
| 99 #endif | 97 #endif |
| 100 | 98 |
| 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) | 99 bool HRTFElevation::calculateKernelsForAzimuthElevation(int azimuth, int elevati
on, float sampleRate, const String& subjectName, OwnPtr<HRTFKernel>& kernelL, Ow
nPtr<HRTFKernel>& kernelR) |
| 102 { | 100 { |
| 103 // Valid values for azimuth are 0 -> 345 in 15 degree increments. | 101 // Valid values for azimuth are 0 -> 345 in 15 degree increments. |
| 104 // Valid values for elevation are -45 -> +90 in 15 degree increments. | 102 // Valid values for elevation are -45 -> +90 in 15 degree increments. |
| 105 | 103 |
| 106 bool isAzimuthGood = azimuth >= 0 && azimuth <= 345 && (azimuth / 15) * 15 =
= azimuth; | 104 bool isAzimuthGood = azimuth >= 0 && azimuth <= 345 && (azimuth / 15) * 15 =
= azimuth; |
| 107 ASSERT(isAzimuthGood); | 105 ASSERT(isAzimuthGood); |
| 108 if (!isAzimuthGood) | 106 if (!isAzimuthGood) |
| 109 return false; | 107 return false; |
| 110 | 108 |
| 111 bool isElevationGood = elevation >= -45 && elevation <= 90 && (elevation / 1
5) * 15 == elevation; | 109 bool isElevationGood = elevation >= -45 && elevation <= 90 && (elevation / 1
5) * 15 == elevation; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 75, // 240 | 212 75, // 240 |
| 215 45, // 255 | 213 45, // 255 |
| 216 60, // 270 | 214 60, // 270 |
| 217 45, // 285 | 215 45, // 285 |
| 218 75, // 300 | 216 75, // 300 |
| 219 45, // 315 | 217 45, // 315 |
| 220 60, // 330 | 218 60, // 330 |
| 221 45 // 345 | 219 45 // 345 |
| 222 }; | 220 }; |
| 223 | 221 |
| 224 std::unique_ptr<HRTFElevation> HRTFElevation::createForSubject(const String& sub
jectName, int elevation, float sampleRate) | 222 PassOwnPtr<HRTFElevation> HRTFElevation::createForSubject(const String& subjectN
ame, int elevation, float sampleRate) |
| 225 { | 223 { |
| 226 bool isElevationGood = elevation >= -45 && elevation <= 90 && (elevation / 1
5) * 15 == elevation; | 224 bool isElevationGood = elevation >= -45 && elevation <= 90 && (elevation / 1
5) * 15 == elevation; |
| 227 ASSERT(isElevationGood); | 225 ASSERT(isElevationGood); |
| 228 if (!isElevationGood) | 226 if (!isElevationGood) |
| 229 return nullptr; | 227 return nullptr; |
| 230 | 228 |
| 231 std::unique_ptr<HRTFKernelList> kernelListL = wrapUnique(new HRTFKernelList(
NumberOfTotalAzimuths)); | 229 OwnPtr<HRTFKernelList> kernelListL = adoptPtr(new HRTFKernelList(NumberOfTot
alAzimuths)); |
| 232 std::unique_ptr<HRTFKernelList> kernelListR = wrapUnique(new HRTFKernelList(
NumberOfTotalAzimuths)); | 230 OwnPtr<HRTFKernelList> kernelListR = adoptPtr(new HRTFKernelList(NumberOfTot
alAzimuths)); |
| 233 | 231 |
| 234 // Load convolution kernels from HRTF files. | 232 // Load convolution kernels from HRTF files. |
| 235 int interpolatedIndex = 0; | 233 int interpolatedIndex = 0; |
| 236 for (unsigned rawIndex = 0; rawIndex < NumberOfRawAzimuths; ++rawIndex) { | 234 for (unsigned rawIndex = 0; rawIndex < NumberOfRawAzimuths; ++rawIndex) { |
| 237 // Don't let elevation exceed maximum for this azimuth. | 235 // Don't let elevation exceed maximum for this azimuth. |
| 238 int maxElevation = maxElevations[rawIndex]; | 236 int maxElevation = maxElevations[rawIndex]; |
| 239 int actualElevation = std::min(elevation, maxElevation); | 237 int actualElevation = std::min(elevation, maxElevation); |
| 240 | 238 |
| 241 bool success = calculateKernelsForAzimuthElevation(rawIndex * AzimuthSpa
cing, actualElevation, sampleRate, subjectName, kernelListL->at(interpolatedInde
x), kernelListR->at(interpolatedIndex)); | 239 bool success = calculateKernelsForAzimuthElevation(rawIndex * AzimuthSpa
cing, actualElevation, sampleRate, subjectName, kernelListL->at(interpolatedInde
x), kernelListR->at(interpolatedIndex)); |
| 242 if (!success) | 240 if (!success) |
| 243 return nullptr; | 241 return nullptr; |
| 244 | 242 |
| 245 interpolatedIndex += InterpolationFactor; | 243 interpolatedIndex += InterpolationFactor; |
| 246 } | 244 } |
| 247 | 245 |
| 248 // Now go back and interpolate intermediate azimuth values. | 246 // Now go back and interpolate intermediate azimuth values. |
| 249 for (unsigned i = 0; i < NumberOfTotalAzimuths; i += InterpolationFactor) { | 247 for (unsigned i = 0; i < NumberOfTotalAzimuths; i += InterpolationFactor) { |
| 250 int j = (i + InterpolationFactor) % NumberOfTotalAzimuths; | 248 int j = (i + InterpolationFactor) % NumberOfTotalAzimuths; |
| 251 | 249 |
| 252 // Create the interpolated convolution kernels and delays. | 250 // Create the interpolated convolution kernels and delays. |
| 253 for (unsigned jj = 1; jj < InterpolationFactor; ++jj) { | 251 for (unsigned jj = 1; jj < InterpolationFactor; ++jj) { |
| 254 float x = float(jj) / float(InterpolationFactor); // interpolate fro
m 0 -> 1 | 252 float x = float(jj) / float(InterpolationFactor); // interpolate fro
m 0 -> 1 |
| 255 | 253 |
| 256 (*kernelListL)[i + jj] = HRTFKernel::createInterpolatedKernel(kernel
ListL->at(i).get(), kernelListL->at(j).get(), x); | 254 (*kernelListL)[i + jj] = HRTFKernel::createInterpolatedKernel(kernel
ListL->at(i).get(), kernelListL->at(j).get(), x); |
| 257 (*kernelListR)[i + jj] = HRTFKernel::createInterpolatedKernel(kernel
ListR->at(i).get(), kernelListR->at(j).get(), x); | 255 (*kernelListR)[i + jj] = HRTFKernel::createInterpolatedKernel(kernel
ListR->at(i).get(), kernelListR->at(j).get(), x); |
| 258 } | 256 } |
| 259 } | 257 } |
| 260 | 258 |
| 261 std::unique_ptr<HRTFElevation> hrtfElevation = wrapUnique(new HRTFElevation(
std::move(kernelListL), std::move(kernelListR), elevation, sampleRate)); | 259 OwnPtr<HRTFElevation> hrtfElevation = adoptPtr(new HRTFElevation(std::move(k
ernelListL), std::move(kernelListR), elevation, sampleRate)); |
| 262 return hrtfElevation; | 260 return hrtfElevation; |
| 263 } | 261 } |
| 264 | 262 |
| 265 std::unique_ptr<HRTFElevation> HRTFElevation::createByInterpolatingSlices(HRTFEl
evation* hrtfElevation1, HRTFElevation* hrtfElevation2, float x, float sampleRat
e) | 263 PassOwnPtr<HRTFElevation> HRTFElevation::createByInterpolatingSlices(HRTFElevati
on* hrtfElevation1, HRTFElevation* hrtfElevation2, float x, float sampleRate) |
| 266 { | 264 { |
| 267 ASSERT(hrtfElevation1 && hrtfElevation2); | 265 ASSERT(hrtfElevation1 && hrtfElevation2); |
| 268 if (!hrtfElevation1 || !hrtfElevation2) | 266 if (!hrtfElevation1 || !hrtfElevation2) |
| 269 return nullptr; | 267 return nullptr; |
| 270 | 268 |
| 271 ASSERT(x >= 0.0 && x < 1.0); | 269 ASSERT(x >= 0.0 && x < 1.0); |
| 272 | 270 |
| 273 std::unique_ptr<HRTFKernelList> kernelListL = wrapUnique(new HRTFKernelList(
NumberOfTotalAzimuths)); | 271 OwnPtr<HRTFKernelList> kernelListL = adoptPtr(new HRTFKernelList(NumberOfTot
alAzimuths)); |
| 274 std::unique_ptr<HRTFKernelList> kernelListR = wrapUnique(new HRTFKernelList(
NumberOfTotalAzimuths)); | 272 OwnPtr<HRTFKernelList> kernelListR = adoptPtr(new HRTFKernelList(NumberOfTot
alAzimuths)); |
| 275 | 273 |
| 276 HRTFKernelList* kernelListL1 = hrtfElevation1->kernelListL(); | 274 HRTFKernelList* kernelListL1 = hrtfElevation1->kernelListL(); |
| 277 HRTFKernelList* kernelListR1 = hrtfElevation1->kernelListR(); | 275 HRTFKernelList* kernelListR1 = hrtfElevation1->kernelListR(); |
| 278 HRTFKernelList* kernelListL2 = hrtfElevation2->kernelListL(); | 276 HRTFKernelList* kernelListL2 = hrtfElevation2->kernelListL(); |
| 279 HRTFKernelList* kernelListR2 = hrtfElevation2->kernelListR(); | 277 HRTFKernelList* kernelListR2 = hrtfElevation2->kernelListR(); |
| 280 | 278 |
| 281 // Interpolate kernels of corresponding azimuths of the two elevations. | 279 // Interpolate kernels of corresponding azimuths of the two elevations. |
| 282 for (unsigned i = 0; i < NumberOfTotalAzimuths; ++i) { | 280 for (unsigned i = 0; i < NumberOfTotalAzimuths; ++i) { |
| 283 (*kernelListL)[i] = HRTFKernel::createInterpolatedKernel(kernelListL1->a
t(i).get(), kernelListL2->at(i).get(), x); | 281 (*kernelListL)[i] = HRTFKernel::createInterpolatedKernel(kernelListL1->a
t(i).get(), kernelListL2->at(i).get(), x); |
| 284 (*kernelListR)[i] = HRTFKernel::createInterpolatedKernel(kernelListR1->a
t(i).get(), kernelListR2->at(i).get(), x); | 282 (*kernelListR)[i] = HRTFKernel::createInterpolatedKernel(kernelListR1->a
t(i).get(), kernelListR2->at(i).get(), x); |
| 285 } | 283 } |
| 286 | 284 |
| 287 // Interpolate elevation angle. | 285 // Interpolate elevation angle. |
| 288 double angle = (1.0 - x) * hrtfElevation1->elevationAngle() + x * hrtfElevat
ion2->elevationAngle(); | 286 double angle = (1.0 - x) * hrtfElevation1->elevationAngle() + x * hrtfElevat
ion2->elevationAngle(); |
| 289 | 287 |
| 290 std::unique_ptr<HRTFElevation> hrtfElevation = wrapUnique(new HRTFElevation(
std::move(kernelListL), std::move(kernelListR), static_cast<int>(angle), sampleR
ate)); | 288 OwnPtr<HRTFElevation> hrtfElevation = adoptPtr(new HRTFElevation(std::move(k
ernelListL), std::move(kernelListR), static_cast<int>(angle), sampleRate)); |
| 291 return hrtfElevation; | 289 return hrtfElevation; |
| 292 } | 290 } |
| 293 | 291 |
| 294 void HRTFElevation::getKernelsFromAzimuth(double azimuthBlend, unsigned azimuthI
ndex, HRTFKernel* &kernelL, HRTFKernel* &kernelR, double& frameDelayL, double& f
rameDelayR) | 292 void HRTFElevation::getKernelsFromAzimuth(double azimuthBlend, unsigned azimuthI
ndex, HRTFKernel* &kernelL, HRTFKernel* &kernelR, double& frameDelayL, double& f
rameDelayR) |
| 295 { | 293 { |
| 296 bool checkAzimuthBlend = azimuthBlend >= 0.0 && azimuthBlend < 1.0; | 294 bool checkAzimuthBlend = azimuthBlend >= 0.0 && azimuthBlend < 1.0; |
| 297 ASSERT(checkAzimuthBlend); | 295 ASSERT(checkAzimuthBlend); |
| 298 if (!checkAzimuthBlend) | 296 if (!checkAzimuthBlend) |
| 299 azimuthBlend = 0.0; | 297 azimuthBlend = 0.0; |
| 300 | 298 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 319 double frameDelay2L = m_kernelListL->at(azimuthIndex2)->frameDelay(); | 317 double frameDelay2L = m_kernelListL->at(azimuthIndex2)->frameDelay(); |
| 320 double frameDelay2R = m_kernelListR->at(azimuthIndex2)->frameDelay(); | 318 double frameDelay2R = m_kernelListR->at(azimuthIndex2)->frameDelay(); |
| 321 | 319 |
| 322 // Linearly interpolate delays. | 320 // Linearly interpolate delays. |
| 323 frameDelayL = (1.0 - azimuthBlend) * frameDelayL + azimuthBlend * frameDelay
2L; | 321 frameDelayL = (1.0 - azimuthBlend) * frameDelayL + azimuthBlend * frameDelay
2L; |
| 324 frameDelayR = (1.0 - azimuthBlend) * frameDelayR + azimuthBlend * frameDelay
2R; | 322 frameDelayR = (1.0 - azimuthBlend) * frameDelayR + azimuthBlend * frameDelay
2R; |
| 325 } | 323 } |
| 326 | 324 |
| 327 } // namespace blink | 325 } // namespace blink |
| 328 | 326 |
| OLD | NEW |