| 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 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 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 ANY |
| 17 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 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 ON |
| 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 |
| 22 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
| 25 | #pragma once |
| 26 | |
| 27 | #include "AudioArray.h" |
| 28 | #include "FFTFrame.h" |
| 29 | #include <JavaScriptCore/Float32Array.h> |
| 30 | #include <JavaScriptCore/Uint8Array.h> |
| 31 | #include <memory> |
| 32 | #include <wtf/Forward.h> |
| 33 | #include <wtf/Noncopyable.h> |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | class AudioBus; |
| 38 | |
| 39 | class RealtimeAnalyser { |
| 40 | WTF_MAKE_NONCOPYABLE(RealtimeAnalyser); |
| 41 | WTF_MAKE_FAST_ALLOCATED; |
| 42 | public: |
| 43 | RealtimeAnalyser(); |
| 44 | virtual ~RealtimeAnalyser(); |
| 45 | |
| 46 | void reset(); |
| 47 | |
| 48 | size_t fftSize() const { return m_fftSize; } |
| 49 | bool setFftSize(size_t); |
| 50 | |
| 51 | unsigned frequencyBinCount() const { return m_fftSize / 2; } |
| 52 | |
| 53 | void setMinDecibels(double k) { m_minDecibels = k; } |
| 54 | double minDecibels() const { return m_minDecibels; } |
| 55 | |
| 56 | void setMaxDecibels(double k) { m_maxDecibels = k; } |
| 57 | double maxDecibels() const { return m_maxDecibels; } |
| 58 | |
| 59 | void setSmoothingTimeConstant(double k) { m_smoothingTimeConstant = k; } |
| 60 | double smoothingTimeConstant() const { return m_smoothingTimeConstant; } |
| 61 | |
| 62 | void getFloatFrequencyData(JSC::Float32Array*); |
| 63 | void getByteFrequencyData(JSC::Uint8Array*); |
| 64 | void getByteTimeDomainData(JSC::Uint8Array*); |
| 65 | |
| 66 | // The audio thread writes input data here. |
| 67 | void writeInput(AudioBus*, size_t framesToProcess); |
| 68 | |
| 69 | static const double DefaultSmoothingTimeConstant; |
| 70 | static const double DefaultMinDecibels; |
| 71 | static const double DefaultMaxDecibels; |
| 72 | |
| 73 | static const unsigned DefaultFFTSize; |
| 74 | static const unsigned MinFFTSize; |
| 75 | static const unsigned MaxFFTSize; |
| 76 | static const unsigned InputBufferSize; |
| 77 | |
| 78 | private: |
| 79 | // The audio thread writes the input audio here. |
| 80 | AudioFloatArray m_inputBuffer; |
| 81 | unsigned m_writeIndex; |
| 82 | |
| 83 | size_t m_fftSize; |
| 84 | std::unique_ptr<FFTFrame> m_analysisFrame; |
| 85 | void doFFTAnalysis(); |
| 86 | |
| 87 | // doFFTAnalysis() stores the floating-point magnitude analysis data here. |
| 88 | AudioFloatArray m_magnitudeBuffer; |
| 89 | AudioFloatArray& magnitudeBuffer() { return m_magnitudeBuffer; } |
| 90 | |
| 91 | // A value between 0 and 1 which averages the previous version of m_magnitudeBuffer with the current analysis magnitude data. |
| 92 | double m_smoothingTimeConstant; |
| 93 | |
| 94 | // The range used when converting when using getByteFrequencyData(). |
| 95 | double m_minDecibels; |
| 96 | double m_maxDecibels; |
| 97 | }; |
| 98 | |
| 99 | } // namespace WebCore |
| 100 | |