Bug 894150 - Unregister the PannerNode in the AudioListener when destroying the strea...
[gecko.git] / content / media / webaudio / PannerNode.h
blob065d1d2d1d9bf3f6037fe80243638b2b06c0f6cd
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/. */
7 #ifndef PannerNode_h_
8 #define PannerNode_h_
10 #include "AudioNode.h"
11 #include "AudioParam.h"
12 #include "mozilla/ErrorResult.h"
13 #include "mozilla/TypedEnum.h"
14 #include "mozilla/dom/PannerNodeBinding.h"
15 #include "ThreeDPoint.h"
16 #include "mozilla/WeakPtr.h"
17 #include "mozilla/Preferences.h"
18 #include "WebAudioUtils.h"
19 #include <set>
21 namespace mozilla {
22 namespace dom {
24 class AudioContext;
25 class AudioBufferSourceNode;
27 class PannerNode : public AudioNode,
28 public SupportsWeakPtr<PannerNode>
30 public:
31 explicit PannerNode(AudioContext* aContext);
32 virtual ~PannerNode();
35 virtual JSObject* WrapObject(JSContext* aCx,
36 JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
38 virtual void DestroyMediaStream() MOZ_OVERRIDE;
40 NS_DECL_ISUPPORTS_INHERITED
41 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PannerNode, AudioNode)
43 PanningModelType PanningModel() const
45 return mPanningModel;
47 void SetPanningModel(PanningModelType aPanningModel)
49 if (!Preferences::GetBool("media.webaudio.legacy.PannerNode")) {
50 // Do not accept the alternate enum values unless the legacy pref
51 // has been turned on.
52 switch (aPanningModel) {
53 case PanningModelType::_0:
54 case PanningModelType::_1:
55 // Do nothing in order to emulate setting an invalid enum value.
56 return;
57 default:
58 // Shut up the compiler warning
59 break;
63 // Handle the alternate enum values
64 switch (aPanningModel) {
65 case PanningModelType::_0: aPanningModel = PanningModelType::Equalpower; break;
66 case PanningModelType::_1: aPanningModel = PanningModelType::HRTF; break;
67 default:
68 // Shut up the compiler warning
69 break;
72 mPanningModel = aPanningModel;
73 SendInt32ParameterToStream(PANNING_MODEL, int32_t(mPanningModel));
76 DistanceModelType DistanceModel() const
78 return mDistanceModel;
80 void SetDistanceModel(DistanceModelType aDistanceModel)
82 if (!Preferences::GetBool("media.webaudio.legacy.PannerNode")) {
83 // Do not accept the alternate enum values unless the legacy pref
84 // has been turned on.
85 switch (aDistanceModel) {
86 case DistanceModelType::_0:
87 case DistanceModelType::_1:
88 case DistanceModelType::_2:
89 // Do nothing in order to emulate setting an invalid enum value.
90 return;
91 default:
92 // Shut up the compiler warning
93 break;
97 // Handle the alternate enum values
98 switch (aDistanceModel) {
99 case DistanceModelType::_0: aDistanceModel = DistanceModelType::Linear; break;
100 case DistanceModelType::_1: aDistanceModel = DistanceModelType::Inverse; break;
101 case DistanceModelType::_2: aDistanceModel = DistanceModelType::Exponential; break;
102 default:
103 // Shut up the compiler warning
104 break;
107 mDistanceModel = aDistanceModel;
108 SendInt32ParameterToStream(DISTANCE_MODEL, int32_t(mDistanceModel));
111 void SetPosition(double aX, double aY, double aZ)
113 if (WebAudioUtils::FuzzyEqual(mPosition.x, aX) &&
114 WebAudioUtils::FuzzyEqual(mPosition.y, aY) &&
115 WebAudioUtils::FuzzyEqual(mPosition.z, aZ)) {
116 return;
118 mPosition.x = aX;
119 mPosition.y = aY;
120 mPosition.z = aZ;
121 SendThreeDPointParameterToStream(POSITION, mPosition);
124 void SetOrientation(double aX, double aY, double aZ)
126 if (WebAudioUtils::FuzzyEqual(mOrientation.x, aX) &&
127 WebAudioUtils::FuzzyEqual(mOrientation.y, aY) &&
128 WebAudioUtils::FuzzyEqual(mOrientation.z, aZ)) {
129 return;
131 mOrientation.x = aX;
132 mOrientation.y = aY;
133 mOrientation.z = aZ;
134 SendThreeDPointParameterToStream(ORIENTATION, mOrientation);
137 void SetVelocity(double aX, double aY, double aZ)
139 if (WebAudioUtils::FuzzyEqual(mVelocity.x, aX) &&
140 WebAudioUtils::FuzzyEqual(mVelocity.y, aY) &&
141 WebAudioUtils::FuzzyEqual(mVelocity.z, aZ)) {
142 return;
144 mVelocity.x = aX;
145 mVelocity.y = aY;
146 mVelocity.z = aZ;
147 SendThreeDPointParameterToStream(VELOCITY, mVelocity);
148 SendDopplerToSourcesIfNeeded();
151 double RefDistance() const
153 return mRefDistance;
155 void SetRefDistance(double aRefDistance)
157 if (WebAudioUtils::FuzzyEqual(mRefDistance, aRefDistance)) {
158 return;
160 mRefDistance = aRefDistance;
161 SendDoubleParameterToStream(REF_DISTANCE, mRefDistance);
164 double MaxDistance() const
166 return mMaxDistance;
168 void SetMaxDistance(double aMaxDistance)
170 if (WebAudioUtils::FuzzyEqual(mMaxDistance, aMaxDistance)) {
171 return;
173 mMaxDistance = aMaxDistance;
174 SendDoubleParameterToStream(MAX_DISTANCE, mMaxDistance);
177 double RolloffFactor() const
179 return mRolloffFactor;
181 void SetRolloffFactor(double aRolloffFactor)
183 if (WebAudioUtils::FuzzyEqual(mRolloffFactor, aRolloffFactor)) {
184 return;
186 mRolloffFactor = aRolloffFactor;
187 SendDoubleParameterToStream(ROLLOFF_FACTOR, mRolloffFactor);
190 double ConeInnerAngle() const
192 return mConeInnerAngle;
194 void SetConeInnerAngle(double aConeInnerAngle)
196 if (WebAudioUtils::FuzzyEqual(mConeInnerAngle, aConeInnerAngle)) {
197 return;
199 mConeInnerAngle = aConeInnerAngle;
200 SendDoubleParameterToStream(CONE_INNER_ANGLE, mConeInnerAngle);
203 double ConeOuterAngle() const
205 return mConeOuterAngle;
207 void SetConeOuterAngle(double aConeOuterAngle)
209 if (WebAudioUtils::FuzzyEqual(mConeOuterAngle, aConeOuterAngle)) {
210 return;
212 mConeOuterAngle = aConeOuterAngle;
213 SendDoubleParameterToStream(CONE_OUTER_ANGLE, mConeOuterAngle);
216 double ConeOuterGain() const
218 return mConeOuterGain;
220 void SetConeOuterGain(double aConeOuterGain)
222 if (WebAudioUtils::FuzzyEqual(mConeOuterGain, aConeOuterGain)) {
223 return;
225 mConeOuterGain = aConeOuterGain;
226 SendDoubleParameterToStream(CONE_OUTER_GAIN, mConeOuterGain);
229 float ComputeDopplerShift();
230 void SendDopplerToSourcesIfNeeded();
231 void FindConnectedSources();
232 void FindConnectedSources(AudioNode* aNode, nsTArray<AudioBufferSourceNode*>& aSources, std::set<AudioNode*>& aSeenNodes);
234 private:
235 friend class AudioListener;
236 friend class PannerNodeEngine;
237 enum EngineParameters {
238 LISTENER_POSITION,
239 LISTENER_ORIENTATION,
240 LISTENER_UPVECTOR,
241 LISTENER_VELOCITY,
242 LISTENER_DOPPLER_FACTOR,
243 LISTENER_SPEED_OF_SOUND,
244 PANNING_MODEL,
245 DISTANCE_MODEL,
246 POSITION,
247 ORIENTATION,
248 VELOCITY,
249 REF_DISTANCE,
250 MAX_DISTANCE,
251 ROLLOFF_FACTOR,
252 CONE_INNER_ANGLE,
253 CONE_OUTER_ANGLE,
254 CONE_OUTER_GAIN
257 private:
258 PanningModelType mPanningModel;
259 DistanceModelType mDistanceModel;
260 ThreeDPoint mPosition;
261 ThreeDPoint mOrientation;
262 ThreeDPoint mVelocity;
263 double mRefDistance;
264 double mMaxDistance;
265 double mRolloffFactor;
266 double mConeInnerAngle;
267 double mConeOuterAngle;
268 double mConeOuterGain;
270 // An array of all the AudioBufferSourceNode connected directly or indirectly
271 // to this AudioPannerNode.
272 nsTArray<AudioBufferSourceNode*> mSources;
278 #endif