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