content/media/webaudio/AudioParam.h

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:7f3dbcbf9c21
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 AudioParam_h_
8 #define AudioParam_h_
9
10 #include "AudioParamTimeline.h"
11 #include "nsWrapperCache.h"
12 #include "nsCycleCollectionParticipant.h"
13 #include "nsAutoPtr.h"
14 #include "AudioNode.h"
15 #include "mozilla/dom/TypedArray.h"
16 #include "WebAudioUtils.h"
17 #include "js/TypeDecls.h"
18
19 namespace mozilla {
20
21 namespace dom {
22
23 class AudioParam MOZ_FINAL : public nsWrapperCache,
24 public AudioParamTimeline
25 {
26 public:
27 typedef void (*CallbackType)(AudioNode*);
28
29 AudioParam(AudioNode* aNode,
30 CallbackType aCallback,
31 float aDefaultValue);
32 virtual ~AudioParam();
33
34 NS_IMETHOD_(MozExternalRefCountType) AddRef(void);
35 NS_IMETHOD_(MozExternalRefCountType) Release(void);
36 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioParam)
37
38 AudioContext* GetParentObject() const
39 {
40 return mNode->Context();
41 }
42
43 double DOMTimeToStreamTime(double aTime) const
44 {
45 return mNode->Context()->DOMTimeToStreamTime(aTime);
46 }
47
48 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
49
50 // We override SetValueCurveAtTime to convert the Float32Array to the wrapper
51 // object.
52 void SetValueCurveAtTime(const Float32Array& aValues, double aStartTime, double aDuration, ErrorResult& aRv)
53 {
54 if (!WebAudioUtils::IsTimeValid(aStartTime)) {
55 aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
56 return;
57 }
58 aValues.ComputeLengthAndData();
59 AudioParamTimeline::SetValueCurveAtTime(aValues.Data(), aValues.Length(),
60 DOMTimeToStreamTime(aStartTime), aDuration, aRv);
61 mCallback(mNode);
62 }
63
64 // We override the rest of the mutating AudioParamTimeline methods in order to make
65 // sure that the callback is called every time that this object gets mutated.
66 void SetValue(float aValue)
67 {
68 // Optimize away setting the same value on an AudioParam
69 if (HasSimpleValue() &&
70 WebAudioUtils::FuzzyEqual(GetValue(), aValue)) {
71 return;
72 }
73 AudioParamTimeline::SetValue(aValue);
74 mCallback(mNode);
75 }
76 void SetValueAtTime(float aValue, double aStartTime, ErrorResult& aRv)
77 {
78 if (!WebAudioUtils::IsTimeValid(aStartTime)) {
79 aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
80 return;
81 }
82 AudioParamTimeline::SetValueAtTime(aValue, DOMTimeToStreamTime(aStartTime), aRv);
83 mCallback(mNode);
84 }
85 void LinearRampToValueAtTime(float aValue, double aEndTime, ErrorResult& aRv)
86 {
87 if (!WebAudioUtils::IsTimeValid(aEndTime)) {
88 aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
89 return;
90 }
91 AudioParamTimeline::LinearRampToValueAtTime(aValue, DOMTimeToStreamTime(aEndTime), aRv);
92 mCallback(mNode);
93 }
94 void ExponentialRampToValueAtTime(float aValue, double aEndTime, ErrorResult& aRv)
95 {
96 if (!WebAudioUtils::IsTimeValid(aEndTime)) {
97 aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
98 return;
99 }
100 AudioParamTimeline::ExponentialRampToValueAtTime(aValue, DOMTimeToStreamTime(aEndTime), aRv);
101 mCallback(mNode);
102 }
103 void SetTargetAtTime(float aTarget, double aStartTime, double aTimeConstant, ErrorResult& aRv)
104 {
105 if (!WebAudioUtils::IsTimeValid(aStartTime) ||
106 !WebAudioUtils::IsTimeValid(aTimeConstant)) {
107 aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
108 return;
109 }
110 AudioParamTimeline::SetTargetAtTime(aTarget, DOMTimeToStreamTime(aStartTime), aTimeConstant, aRv);
111 mCallback(mNode);
112 }
113 void CancelScheduledValues(double aStartTime, ErrorResult& aRv)
114 {
115 if (!WebAudioUtils::IsTimeValid(aStartTime)) {
116 aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
117 return;
118 }
119 AudioParamTimeline::CancelScheduledValues(DOMTimeToStreamTime(aStartTime));
120 mCallback(mNode);
121 }
122
123 float DefaultValue() const
124 {
125 return mDefaultValue;
126 }
127
128 AudioNode* Node() const
129 {
130 return mNode;
131 }
132
133 const nsTArray<AudioNode::InputNode>& InputNodes() const
134 {
135 return mInputNodes;
136 }
137
138 void RemoveInputNode(uint32_t aIndex)
139 {
140 mInputNodes.RemoveElementAt(aIndex);
141 }
142
143 AudioNode::InputNode* AppendInputNode()
144 {
145 return mInputNodes.AppendElement();
146 }
147
148 void DisconnectFromGraphAndDestroyStream();
149
150 // May create the stream if it doesn't exist
151 MediaStream* Stream();
152
153 virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
154 {
155 size_t amount = AudioParamTimeline::SizeOfExcludingThis(aMallocSizeOf);
156 // Not owned:
157 // - mNode
158
159 // Just count the array, actual nodes are counted in mNode.
160 amount += mInputNodes.SizeOfExcludingThis(aMallocSizeOf);
161
162 if (mNodeStreamPort) {
163 amount += mNodeStreamPort->SizeOfIncludingThis(aMallocSizeOf);
164 }
165
166 return amount;
167 }
168
169 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
170 {
171 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
172 }
173
174 protected:
175 nsCycleCollectingAutoRefCnt mRefCnt;
176 NS_DECL_OWNINGTHREAD
177
178 private:
179 nsRefPtr<AudioNode> mNode;
180 // For every InputNode, there is a corresponding entry in mOutputParams of the
181 // InputNode's mInputNode.
182 nsTArray<AudioNode::InputNode> mInputNodes;
183 CallbackType mCallback;
184 const float mDefaultValue;
185 // The input port used to connect the AudioParam's stream to its node's stream
186 nsRefPtr<MediaInputPort> mNodeStreamPort;
187 };
188
189 }
190 }
191
192 #endif
193

mercurial