AnalyserNode.h (2929B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef AnalyserNode_h_ 8 #define AnalyserNode_h_ 9 10 #include "AlignedTArray.h" 11 #include "AudioNode.h" 12 #include "FFTBlock.h" 13 14 namespace mozilla::dom { 15 16 class AudioContext; 17 struct AnalyserOptions; 18 19 class AnalyserNode final : public AudioNode { 20 public: 21 static already_AddRefed<AnalyserNode> Create(AudioContext& aAudioContext, 22 const AnalyserOptions& aOptions, 23 ErrorResult& aRv); 24 25 NS_INLINE_DECL_REFCOUNTING_INHERITED(AnalyserNode, AudioNode) 26 27 virtual JSObject* WrapObject(JSContext* aCx, 28 JS::Handle<JSObject*> aGivenProto) override; 29 30 static already_AddRefed<AnalyserNode> Constructor( 31 const GlobalObject& aGlobal, AudioContext& aAudioContext, 32 const AnalyserOptions& aOptions, ErrorResult& aRv) { 33 return Create(aAudioContext, aOptions, aRv); 34 } 35 36 void GetFloatFrequencyData(const Float32Array& aArray); 37 void GetByteFrequencyData(const Uint8Array& aArray); 38 void GetFloatTimeDomainData(const Float32Array& aArray); 39 void GetByteTimeDomainData(const Uint8Array& aArray); 40 uint32_t FftSize() const { return mAnalysisBlock.FFTSize(); } 41 void SetFftSize(uint32_t aValue, ErrorResult& aRv); 42 uint32_t FrequencyBinCount() const { return FftSize() / 2; } 43 double MinDecibels() const { return mMinDecibels; } 44 void SetMinDecibels(double aValue, ErrorResult& aRv); 45 double MaxDecibels() const { return mMaxDecibels; } 46 void SetMaxDecibels(double aValue, ErrorResult& aRv); 47 double SmoothingTimeConstant() const { return mSmoothingTimeConstant; } 48 void SetSmoothingTimeConstant(double aValue, ErrorResult& aRv); 49 50 virtual const char* NodeType() const override { return "AnalyserNode"; } 51 52 virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override; 53 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override; 54 55 void SetMinAndMaxDecibels(double aMinValue, double aMaxValue, 56 ErrorResult& aRv); 57 58 private: 59 ~AnalyserNode() = default; 60 61 friend class AnalyserNodeEngine; 62 void AppendChunk(const AudioChunk& aChunk); 63 bool AllocateBuffer(); 64 bool FFTAnalysis(); 65 void ApplyBlackmanWindow(float* aBuffer, uint32_t aSize); 66 void GetTimeDomainData(float* aData, size_t aLength); 67 68 private: 69 explicit AnalyserNode(AudioContext* aContext); 70 71 FFTBlock mAnalysisBlock; 72 nsTArray<AudioChunk> mChunks; 73 double mMinDecibels; 74 double mMaxDecibels; 75 double mSmoothingTimeConstant; 76 size_t mCurrentChunk = 0; 77 AlignedTArray<float> mOutputBuffer; 78 }; 79 80 } // namespace mozilla::dom 81 82 #endif