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 OscillatorNode_h_
8 #define OscillatorNode_h_
9 
10 #include "AudioScheduledSourceNode.h"
11 #include "AudioParam.h"
12 #include "PeriodicWave.h"
13 #include "mozilla/dom/OscillatorNodeBinding.h"
14 
15 namespace mozilla {
16 namespace dom {
17 
18 class AudioContext;
19 struct OscillatorOptions;
20 
21 class OscillatorNode final : public AudioScheduledSourceNode,
22                              public MainThreadMediaStreamListener {
23  public:
24   static already_AddRefed<OscillatorNode> Create(
25       AudioContext& aAudioContext, const OscillatorOptions& aOptions,
26       ErrorResult& aRv);
27 
28   NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OscillatorNode,AudioScheduledSourceNode)29   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OscillatorNode,
30                                            AudioScheduledSourceNode)
31 
32   static already_AddRefed<OscillatorNode> Constructor(
33       const GlobalObject& aGlobal, AudioContext& aAudioContext,
34       const OscillatorOptions& aOptions, ErrorResult& aRv) {
35     return Create(aAudioContext, aOptions, aRv);
36   }
37 
38   JSObject* WrapObject(JSContext* aCx,
39                        JS::Handle<JSObject*> aGivenProto) override;
40 
41   void DestroyMediaStream() override;
42 
NumberOfInputs()43   uint16_t NumberOfInputs() const final { return 0; }
44 
Type()45   OscillatorType Type() const { return mType; }
SetType(OscillatorType aType,ErrorResult & aRv)46   void SetType(OscillatorType aType, ErrorResult& aRv) {
47     if (aType == OscillatorType::Custom) {
48       // ::Custom can only be set by setPeriodicWave().
49       // https://github.com/WebAudio/web-audio-api/issues/105 for exception.
50       aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
51       return;
52     }
53     mType = aType;
54     SendTypeToStream();
55   }
56 
Frequency()57   AudioParam* Frequency() const { return mFrequency; }
Detune()58   AudioParam* Detune() const { return mDetune; }
59 
60   void Start(double aWhen, ErrorResult& aRv) override;
61   void Stop(double aWhen, ErrorResult& aRv) override;
62 
SetPeriodicWave(PeriodicWave & aPeriodicWave)63   void SetPeriodicWave(PeriodicWave& aPeriodicWave) {
64     mPeriodicWave = &aPeriodicWave;
65     // SendTypeToStream will call SendPeriodicWaveToStream for us.
66     mType = OscillatorType::Custom;
67     SendTypeToStream();
68   }
69 
70   void NotifyMainThreadStreamFinished() override;
71 
NodeType()72   const char* NodeType() const override { return "OscillatorNode"; }
73 
74   size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
75   size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;
76 
77  private:
78   explicit OscillatorNode(AudioContext* aContext);
79   ~OscillatorNode() = default;
80 
81   void SendTypeToStream();
82   void SendPeriodicWaveToStream();
83 
84   OscillatorType mType;
85   RefPtr<PeriodicWave> mPeriodicWave;
86   RefPtr<AudioParam> mFrequency;
87   RefPtr<AudioParam> mDetune;
88   bool mStartCalled;
89 };
90 
91 }  // namespace dom
92 }  // namespace mozilla
93 
94 #endif
95