| 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 |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y | 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y |
| 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N | 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N |
| 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 */ | 23 */ |
| 24 | 24 |
| 25 #include "modules/webaudio/RealtimeAnalyser.h" | 25 #include "modules/webaudio/RealtimeAnalyser.h" |
| 26 #include "platform/audio/AudioBus.h" | 26 #include "platform/audio/AudioBus.h" |
| 27 #include "platform/audio/AudioUtilities.h" | 27 #include "platform/audio/AudioUtilities.h" |
| 28 #include "platform/audio/VectorMath.h" | 28 #include "platform/audio/VectorMath.h" |
| 29 #include "wtf/MathExtras.h" | 29 #include "wtf/MathExtras.h" |
| 30 #include "wtf/PtrUtil.h" |
| 30 #include <algorithm> | 31 #include <algorithm> |
| 31 #include <complex> | 32 #include <complex> |
| 32 #include <limits.h> | 33 #include <limits.h> |
| 33 | 34 |
| 34 namespace blink { | 35 namespace blink { |
| 35 | 36 |
| 36 const double RealtimeAnalyser::DefaultSmoothingTimeConstant = 0.8; | 37 const double RealtimeAnalyser::DefaultSmoothingTimeConstant = 0.8; |
| 37 const double RealtimeAnalyser::DefaultMinDecibels = -100; | 38 const double RealtimeAnalyser::DefaultMinDecibels = -100; |
| 38 const double RealtimeAnalyser::DefaultMaxDecibels = -30; | 39 const double RealtimeAnalyser::DefaultMaxDecibels = -30; |
| 39 | 40 |
| 40 const unsigned RealtimeAnalyser::DefaultFFTSize = 2048; | 41 const unsigned RealtimeAnalyser::DefaultFFTSize = 2048; |
| 41 // All FFT implementations are expected to handle power-of-two sizes MinFFTSize
<= size <= MaxFFTSize. | 42 // All FFT implementations are expected to handle power-of-two sizes MinFFTSize
<= size <= MaxFFTSize. |
| 42 const unsigned RealtimeAnalyser::MinFFTSize = 32; | 43 const unsigned RealtimeAnalyser::MinFFTSize = 32; |
| 43 const unsigned RealtimeAnalyser::MaxFFTSize = 32768; | 44 const unsigned RealtimeAnalyser::MaxFFTSize = 32768; |
| 44 const unsigned RealtimeAnalyser::InputBufferSize = RealtimeAnalyser::MaxFFTSize
* 2; | 45 const unsigned RealtimeAnalyser::InputBufferSize = RealtimeAnalyser::MaxFFTSize
* 2; |
| 45 | 46 |
| 46 RealtimeAnalyser::RealtimeAnalyser() | 47 RealtimeAnalyser::RealtimeAnalyser() |
| 47 : m_inputBuffer(InputBufferSize) | 48 : m_inputBuffer(InputBufferSize) |
| 48 , m_writeIndex(0) | 49 , m_writeIndex(0) |
| 49 , m_downMixBus(AudioBus::create(1, AudioUtilities::kRenderQuantumFrames)) | 50 , m_downMixBus(AudioBus::create(1, AudioUtilities::kRenderQuantumFrames)) |
| 50 , m_fftSize(DefaultFFTSize) | 51 , m_fftSize(DefaultFFTSize) |
| 51 , m_magnitudeBuffer(DefaultFFTSize / 2) | 52 , m_magnitudeBuffer(DefaultFFTSize / 2) |
| 52 , m_smoothingTimeConstant(DefaultSmoothingTimeConstant) | 53 , m_smoothingTimeConstant(DefaultSmoothingTimeConstant) |
| 53 , m_minDecibels(DefaultMinDecibels) | 54 , m_minDecibels(DefaultMinDecibels) |
| 54 , m_maxDecibels(DefaultMaxDecibels) | 55 , m_maxDecibels(DefaultMaxDecibels) |
| 55 , m_lastAnalysisTime(-1) | 56 , m_lastAnalysisTime(-1) |
| 56 { | 57 { |
| 57 m_analysisFrame = adoptPtr(new FFTFrame(DefaultFFTSize)); | 58 m_analysisFrame = wrapUnique(new FFTFrame(DefaultFFTSize)); |
| 58 } | 59 } |
| 59 | 60 |
| 60 bool RealtimeAnalyser::setFftSize(size_t size) | 61 bool RealtimeAnalyser::setFftSize(size_t size) |
| 61 { | 62 { |
| 62 ASSERT(isMainThread()); | 63 ASSERT(isMainThread()); |
| 63 | 64 |
| 64 // Only allow powers of two. | 65 // Only allow powers of two. |
| 65 unsigned log2size = static_cast<unsigned>(log2(size)); | 66 unsigned log2size = static_cast<unsigned>(log2(size)); |
| 66 bool isPOT(1UL << log2size == size); | 67 bool isPOT(1UL << log2size == size); |
| 67 | 68 |
| 68 if (!isPOT || size > MaxFFTSize || size < MinFFTSize) | 69 if (!isPOT || size > MaxFFTSize || size < MinFFTSize) |
| 69 return false; | 70 return false; |
| 70 | 71 |
| 71 if (m_fftSize != size) { | 72 if (m_fftSize != size) { |
| 72 m_analysisFrame = adoptPtr(new FFTFrame(size)); | 73 m_analysisFrame = wrapUnique(new FFTFrame(size)); |
| 73 // m_magnitudeBuffer has size = fftSize / 2 because it contains floats r
educed from complex values in m_analysisFrame. | 74 // m_magnitudeBuffer has size = fftSize / 2 because it contains floats r
educed from complex values in m_analysisFrame. |
| 74 m_magnitudeBuffer.allocate(size / 2); | 75 m_magnitudeBuffer.allocate(size / 2); |
| 75 m_fftSize = size; | 76 m_fftSize = size; |
| 76 } | 77 } |
| 77 | 78 |
| 78 return true; | 79 return true; |
| 79 } | 80 } |
| 80 | 81 |
| 81 void RealtimeAnalyser::writeInput(AudioBus* bus, size_t framesToProcess) | 82 void RealtimeAnalyser::writeInput(AudioBus* bus, size_t framesToProcess) |
| 82 { | 83 { |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 if (scaledValue > UCHAR_MAX) | 318 if (scaledValue > UCHAR_MAX) |
| 318 scaledValue = UCHAR_MAX; | 319 scaledValue = UCHAR_MAX; |
| 319 | 320 |
| 320 destination[i] = static_cast<unsigned char>(scaledValue); | 321 destination[i] = static_cast<unsigned char>(scaledValue); |
| 321 } | 322 } |
| 322 } | 323 } |
| 323 } | 324 } |
| 324 | 325 |
| 325 } // namespace blink | 326 } // namespace blink |
| 326 | 327 |
| OLD | NEW |