Line data Source code
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 "AudioNode.h"
11 : #include "FFTBlock.h"
12 : #include "AlignedTArray.h"
13 :
14 : namespace mozilla {
15 : namespace dom {
16 :
17 : class AudioContext;
18 : struct AnalyserOptions;
19 :
20 : class AnalyserNode final : public AudioNode
21 : {
22 : public:
23 : static already_AddRefed<AnalyserNode>
24 : Create(AudioContext& aAudioContext, const AnalyserOptions& aOptions,
25 : ErrorResult& aRv);
26 :
27 : NS_DECL_ISUPPORTS_INHERITED
28 :
29 : virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
30 :
31 : static already_AddRefed<AnalyserNode>
32 0 : Constructor(const GlobalObject& aGlobal, AudioContext& aAudioContext,
33 : const AnalyserOptions& aOptions, ErrorResult& aRv)
34 : {
35 0 : return Create(aAudioContext, aOptions, aRv);
36 : }
37 :
38 : void GetFloatFrequencyData(const Float32Array& aArray);
39 : void GetByteFrequencyData(const Uint8Array& aArray);
40 : void GetFloatTimeDomainData(const Float32Array& aArray);
41 : void GetByteTimeDomainData(const Uint8Array& aArray);
42 0 : uint32_t FftSize() const
43 : {
44 0 : return mAnalysisBlock.FFTSize();
45 : }
46 : void SetFftSize(uint32_t aValue, ErrorResult& aRv);
47 0 : uint32_t FrequencyBinCount() const
48 : {
49 0 : return FftSize() / 2;
50 : }
51 0 : double MinDecibels() const
52 : {
53 0 : return mMinDecibels;
54 : }
55 : void SetMinDecibels(double aValue, ErrorResult& aRv);
56 0 : double MaxDecibels() const
57 : {
58 0 : return mMaxDecibels;
59 : }
60 : void SetMaxDecibels(double aValue, ErrorResult& aRv);
61 0 : double SmoothingTimeConstant() const
62 : {
63 0 : return mSmoothingTimeConstant;
64 : }
65 : void SetSmoothingTimeConstant(double aValue, ErrorResult& aRv);
66 :
67 0 : virtual const char* NodeType() const override
68 : {
69 0 : return "AnalyserNode";
70 : }
71 :
72 : virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
73 : virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;
74 :
75 : private:
76 0 : ~AnalyserNode() = default;
77 :
78 : friend class AnalyserNodeEngine;
79 : void AppendChunk(const AudioChunk& aChunk);
80 : bool AllocateBuffer();
81 : bool FFTAnalysis();
82 : void ApplyBlackmanWindow(float* aBuffer, uint32_t aSize);
83 : void GetTimeDomainData(float* aData, size_t aLength);
84 :
85 : private:
86 : explicit AnalyserNode(AudioContext* aContext);
87 :
88 : FFTBlock mAnalysisBlock;
89 : nsTArray<AudioChunk> mChunks;
90 : double mMinDecibels;
91 : double mMaxDecibels;
92 : double mSmoothingTimeConstant;
93 : size_t mCurrentChunk = 0;
94 : AlignedTArray<float> mOutputBuffer;
95 : };
96 :
97 : } // namespace dom
98 : } // namespace mozilla
99 :
100 : #endif
101 :
|