Home | History | Annotate | Download | only in webaudio
      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 #ifndef RealtimeAnalyser_h
     26 #define RealtimeAnalyser_h
     27 
     28 #include "AudioArray.h"
     29 #include <wtf/Noncopyable.h>
     30 #include <wtf/OwnPtr.h>
     31 
     32 namespace WebCore {
     33 
     34 class AudioBus;
     35 class FFTFrame;
     36 
     37 #if ENABLE(WEBGL)
     38 class Float32Array;
     39 class Uint8Array;
     40 #endif
     41 
     42 class RealtimeAnalyser {
     43     WTF_MAKE_NONCOPYABLE(RealtimeAnalyser);
     44 public:
     45     RealtimeAnalyser();
     46     virtual ~RealtimeAnalyser();
     47 
     48     void reset();
     49 
     50     size_t fftSize() const { return m_fftSize; }
     51     void setFftSize(size_t size);
     52 
     53     unsigned frequencyBinCount() const { return m_fftSize / 2; }
     54 
     55     void setMinDecibels(float k) { m_minDecibels = k; }
     56     float minDecibels() const { return static_cast<float>(m_minDecibels); }
     57 
     58     void setMaxDecibels(float k) { m_maxDecibels = k; }
     59     float maxDecibels() const { return static_cast<float>(m_maxDecibels); }
     60 
     61     void setSmoothingTimeConstant(float k) { m_smoothingTimeConstant = k; }
     62     float smoothingTimeConstant() const { return static_cast<float>(m_smoothingTimeConstant); }
     63 
     64 #if ENABLE(WEBGL)
     65     void getFloatFrequencyData(Float32Array*);
     66     void getByteFrequencyData(Uint8Array*);
     67     void getByteTimeDomainData(Uint8Array*);
     68 #endif
     69 
     70     // The audio thread writes input data here.
     71     void writeInput(AudioBus*, size_t framesToProcess);
     72 
     73     static const double DefaultSmoothingTimeConstant;
     74     static const double DefaultMinDecibels;
     75     static const double DefaultMaxDecibels;
     76 
     77     static const unsigned DefaultFFTSize;
     78     static const unsigned MaxFFTSize;
     79     static const unsigned InputBufferSize;
     80 
     81 private:
     82     // The audio thread writes the input audio here.
     83     AudioFloatArray m_inputBuffer;
     84     unsigned m_writeIndex;
     85 
     86     size_t m_fftSize;
     87     OwnPtr<FFTFrame> m_analysisFrame;
     88     void doFFTAnalysis();
     89 
     90     // doFFTAnalysis() stores the floating-point magnitude analysis data here.
     91     AudioFloatArray m_magnitudeBuffer;
     92     AudioFloatArray& magnitudeBuffer() { return m_magnitudeBuffer; }
     93 
     94     // A value between 0 and 1 which averages the previous version of m_magnitudeBuffer with the current analysis magnitude data.
     95     double m_smoothingTimeConstant;
     96 
     97     // The range used when converting when using getByteFrequencyData().
     98     double m_minDecibels;
     99     double m_maxDecibels;
    100 };
    101 
    102 } // namespace WebCore
    103 
    104 #endif // RealtimeAnalyser_h
    105