RealtimeAnalyser.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef RealtimeAnalyser_h
  25. #define RealtimeAnalyser_h
  26. #include "AudioArray.h"
  27. #include <wtf/Forward.h>
  28. #include <wtf/Noncopyable.h>
  29. #include <wtf/OwnPtr.h>
  30. namespace WebCore {
  31. class AudioBus;
  32. class FFTFrame;
  33. class RealtimeAnalyser {
  34. WTF_MAKE_NONCOPYABLE(RealtimeAnalyser);
  35. public:
  36. RealtimeAnalyser();
  37. virtual ~RealtimeAnalyser();
  38. void reset();
  39. size_t fftSize() const { return m_fftSize; }
  40. bool setFftSize(size_t);
  41. unsigned frequencyBinCount() const { return m_fftSize / 2; }
  42. void setMinDecibels(float k) { m_minDecibels = k; }
  43. float minDecibels() const { return static_cast<float>(m_minDecibels); }
  44. void setMaxDecibels(float k) { m_maxDecibels = k; }
  45. float maxDecibels() const { return static_cast<float>(m_maxDecibels); }
  46. void setSmoothingTimeConstant(float k) { m_smoothingTimeConstant = k; }
  47. float smoothingTimeConstant() const { return static_cast<float>(m_smoothingTimeConstant); }
  48. void getFloatFrequencyData(Float32Array*);
  49. void getByteFrequencyData(Uint8Array*);
  50. void getByteTimeDomainData(Uint8Array*);
  51. // The audio thread writes input data here.
  52. void writeInput(AudioBus*, size_t framesToProcess);
  53. static const double DefaultSmoothingTimeConstant;
  54. static const double DefaultMinDecibels;
  55. static const double DefaultMaxDecibels;
  56. static const unsigned DefaultFFTSize;
  57. static const unsigned MinFFTSize;
  58. static const unsigned MaxFFTSize;
  59. static const unsigned InputBufferSize;
  60. private:
  61. // The audio thread writes the input audio here.
  62. AudioFloatArray m_inputBuffer;
  63. unsigned m_writeIndex;
  64. size_t m_fftSize;
  65. OwnPtr<FFTFrame> m_analysisFrame;
  66. void doFFTAnalysis();
  67. // doFFTAnalysis() stores the floating-point magnitude analysis data here.
  68. AudioFloatArray m_magnitudeBuffer;
  69. AudioFloatArray& magnitudeBuffer() { return m_magnitudeBuffer; }
  70. // A value between 0 and 1 which averages the previous version of m_magnitudeBuffer with the current analysis magnitude data.
  71. double m_smoothingTimeConstant;
  72. // The range used when converting when using getByteFrequencyData().
  73. double m_minDecibels;
  74. double m_maxDecibels;
  75. };
  76. } // namespace WebCore
  77. #endif // RealtimeAnalyser_h