| 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 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "config.h" | 25 #include "config.h" |
| 26 | 26 |
| 27 #if ENABLE(WEB_AUDIO) | 27 #if ENABLE(WEB_AUDIO) |
| 28 | 28 |
| 29 #include "core/platform/audio/HRTFPanner.h" | 29 #include "core/platform/audio/HRTFPanner.h" |
| 30 | 30 |
| 31 #include <algorithm> | 31 #include <algorithm> |
| 32 #include "core/platform/audio/AudioBus.h" | 32 #include "core/platform/audio/AudioBus.h" |
| 33 #include "core/platform/audio/FFTConvolver.h" | 33 #include "core/platform/audio/FFTConvolver.h" |
| 34 #include "core/platform/audio/HRTFDatabase.h" | 34 #include "core/platform/audio/HRTFDatabase.h" |
| 35 #include "core/platform/audio/HRTFDatabaseLoader.h" | |
| 36 #include <wtf/MathExtras.h> | 35 #include <wtf/MathExtras.h> |
| 37 #include <wtf/RefPtr.h> | 36 #include <wtf/RefPtr.h> |
| 38 | 37 |
| 39 using namespace std; | 38 using namespace std; |
| 40 | 39 |
| 41 namespace WebCore { | 40 namespace WebCore { |
| 42 | 41 |
| 43 // The value of 2 milliseconds is larger than the largest delay which exists in
any HRTFKernel from the default HRTFDatabase (0.0136 seconds). | 42 // The value of 2 milliseconds is larger than the largest delay which exists in
any HRTFKernel from the default HRTFDatabase (0.0136 seconds). |
| 44 // We ASSERT the delay values used in process() with this value. | 43 // We ASSERT the delay values used in process() with this value. |
| 45 const double MaxDelayTimeSeconds = 0.002; | 44 const double MaxDelayTimeSeconds = 0.002; |
| 46 | 45 |
| 47 const int UninitializedAzimuth = -1; | 46 const int UninitializedAzimuth = -1; |
| 48 const unsigned RenderingQuantum = 128; | 47 const unsigned RenderingQuantum = 128; |
| 49 | 48 |
| 50 HRTFPanner::HRTFPanner(float sampleRate) | 49 HRTFPanner::HRTFPanner(float sampleRate, HRTFDatabaseLoader* databaseLoader) |
| 51 : Panner(PanningModelHRTF) | 50 : Panner(PanningModelHRTF) |
| 51 , m_databaseLoader(databaseLoader) |
| 52 , m_sampleRate(sampleRate) | 52 , m_sampleRate(sampleRate) |
| 53 , m_crossfadeSelection(CrossfadeSelection1) | 53 , m_crossfadeSelection(CrossfadeSelection1) |
| 54 , m_azimuthIndex1(UninitializedAzimuth) | 54 , m_azimuthIndex1(UninitializedAzimuth) |
| 55 , m_elevation1(0) | 55 , m_elevation1(0) |
| 56 , m_azimuthIndex2(UninitializedAzimuth) | 56 , m_azimuthIndex2(UninitializedAzimuth) |
| 57 , m_elevation2(0) | 57 , m_elevation2(0) |
| 58 , m_crossfadeX(0) | 58 , m_crossfadeX(0) |
| 59 , m_crossfadeIncr(0) | 59 , m_crossfadeIncr(0) |
| 60 , m_convolverL1(fftSizeForSampleRate(sampleRate)) | 60 , m_convolverL1(fftSizeForSampleRate(sampleRate)) |
| 61 , m_convolverR1(fftSizeForSampleRate(sampleRate)) | 61 , m_convolverR1(fftSizeForSampleRate(sampleRate)) |
| 62 , m_convolverL2(fftSizeForSampleRate(sampleRate)) | 62 , m_convolverL2(fftSizeForSampleRate(sampleRate)) |
| 63 , m_convolverR2(fftSizeForSampleRate(sampleRate)) | 63 , m_convolverR2(fftSizeForSampleRate(sampleRate)) |
| 64 , m_delayLineL(MaxDelayTimeSeconds, sampleRate) | 64 , m_delayLineL(MaxDelayTimeSeconds, sampleRate) |
| 65 , m_delayLineR(MaxDelayTimeSeconds, sampleRate) | 65 , m_delayLineR(MaxDelayTimeSeconds, sampleRate) |
| 66 , m_tempL1(RenderingQuantum) | 66 , m_tempL1(RenderingQuantum) |
| 67 , m_tempR1(RenderingQuantum) | 67 , m_tempR1(RenderingQuantum) |
| 68 , m_tempL2(RenderingQuantum) | 68 , m_tempL2(RenderingQuantum) |
| 69 , m_tempR2(RenderingQuantum) | 69 , m_tempR2(RenderingQuantum) |
| 70 { | 70 { |
| 71 ASSERT(databaseLoader); |
| 71 } | 72 } |
| 72 | 73 |
| 73 HRTFPanner::~HRTFPanner() | 74 HRTFPanner::~HRTFPanner() |
| 74 { | 75 { |
| 75 } | 76 } |
| 76 | 77 |
| 77 size_t HRTFPanner::fftSizeForSampleRate(float sampleRate) | 78 size_t HRTFPanner::fftSizeForSampleRate(float sampleRate) |
| 78 { | 79 { |
| 79 // The HRTF impulse responses (loaded as audio resources) are 512 sample-fra
mes @44.1KHz. | 80 // The HRTF impulse responses (loaded as audio resources) are 512 sample-fra
mes @44.1KHz. |
| 80 // Currently, we truncate the impulse responses to half this size, but an FF
T-size of twice impulse response size is needed (for convolution). | 81 // Currently, we truncate the impulse responses to half this size, but an FF
T-size of twice impulse response size is needed (for convolution). |
| (...skipping 12 matching lines...) Expand all Loading... |
| 93 m_delayLineR.reset(); | 94 m_delayLineR.reset(); |
| 94 } | 95 } |
| 95 | 96 |
| 96 int HRTFPanner::calculateDesiredAzimuthIndexAndBlend(double azimuth, double& azi
muthBlend) | 97 int HRTFPanner::calculateDesiredAzimuthIndexAndBlend(double azimuth, double& azi
muthBlend) |
| 97 { | 98 { |
| 98 // Convert the azimuth angle from the range -180 -> +180 into the range 0 ->
360. | 99 // Convert the azimuth angle from the range -180 -> +180 into the range 0 ->
360. |
| 99 // The azimuth index may then be calculated from this positive value. | 100 // The azimuth index may then be calculated from this positive value. |
| 100 if (azimuth < 0) | 101 if (azimuth < 0) |
| 101 azimuth += 360.0; | 102 azimuth += 360.0; |
| 102 | 103 |
| 103 HRTFDatabase* database = HRTFDatabaseLoader::defaultHRTFDatabase(); | 104 HRTFDatabase* database = m_databaseLoader->database(); |
| 104 ASSERT(database); | 105 ASSERT(database); |
| 105 | 106 |
| 106 int numberOfAzimuths = database->numberOfAzimuths(); | 107 int numberOfAzimuths = database->numberOfAzimuths(); |
| 107 const double angleBetweenAzimuths = 360.0 / numberOfAzimuths; | 108 const double angleBetweenAzimuths = 360.0 / numberOfAzimuths; |
| 108 | 109 |
| 109 // Calculate the azimuth index and the blend (0 -> 1) for interpolation. | 110 // Calculate the azimuth index and the blend (0 -> 1) for interpolation. |
| 110 double desiredAzimuthIndexFloat = azimuth / angleBetweenAzimuths; | 111 double desiredAzimuthIndexFloat = azimuth / angleBetweenAzimuths; |
| 111 int desiredAzimuthIndex = static_cast<int>(desiredAzimuthIndexFloat); | 112 int desiredAzimuthIndex = static_cast<int>(desiredAzimuthIndexFloat); |
| 112 azimuthBlend = desiredAzimuthIndexFloat - static_cast<double>(desiredAzimuth
Index); | 113 azimuthBlend = desiredAzimuthIndexFloat - static_cast<double>(desiredAzimuth
Index); |
| 113 | 114 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 127 | 128 |
| 128 bool isOutputGood = outputBus && outputBus->numberOfChannels() == 2 && frame
sToProcess <= outputBus->length(); | 129 bool isOutputGood = outputBus && outputBus->numberOfChannels() == 2 && frame
sToProcess <= outputBus->length(); |
| 129 ASSERT(isOutputGood); | 130 ASSERT(isOutputGood); |
| 130 | 131 |
| 131 if (!isInputGood || !isOutputGood) { | 132 if (!isInputGood || !isOutputGood) { |
| 132 if (outputBus) | 133 if (outputBus) |
| 133 outputBus->zero(); | 134 outputBus->zero(); |
| 134 return; | 135 return; |
| 135 } | 136 } |
| 136 | 137 |
| 137 // This code only runs as long as the context is alive and after database ha
s been loaded. | 138 HRTFDatabase* database = m_databaseLoader->database(); |
| 138 HRTFDatabase* database = HRTFDatabaseLoader::defaultHRTFDatabase(); | |
| 139 ASSERT(database); | 139 ASSERT(database); |
| 140 if (!database) { | 140 if (!database) { |
| 141 outputBus->zero(); | 141 outputBus->zero(); |
| 142 return; | 142 return; |
| 143 } | 143 } |
| 144 | 144 |
| 145 // IRCAM HRTF azimuths values from the loaded database is reversed from the
panner's notion of azimuth. | 145 // IRCAM HRTF azimuths values from the loaded database is reversed from the
panner's notion of azimuth. |
| 146 double azimuth = -desiredAzimuth; | 146 double azimuth = -desiredAzimuth; |
| 147 | 147 |
| 148 bool isAzimuthGood = azimuth >= -180.0 && azimuth <= 180.0; | 148 bool isAzimuthGood = azimuth >= -180.0 && azimuth <= 180.0; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 double HRTFPanner::latencyTime() const | 305 double HRTFPanner::latencyTime() const |
| 306 { | 306 { |
| 307 // The latency of a FFTConvolver is also fftSize() / 2, and is in addition t
o its tailTime of the | 307 // The latency of a FFTConvolver is also fftSize() / 2, and is in addition t
o its tailTime of the |
| 308 // same value. | 308 // same value. |
| 309 return (fftSize() / 2) / static_cast<double>(sampleRate()); | 309 return (fftSize() / 2) / static_cast<double>(sampleRate()); |
| 310 } | 310 } |
| 311 | 311 |
| 312 } // namespace WebCore | 312 } // namespace WebCore |
| 313 | 313 |
| 314 #endif // ENABLE(WEB_AUDIO) | 314 #endif // ENABLE(WEB_AUDIO) |
| OLD | NEW |