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/. */
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"
25 class AudioBufferSourceNode
;
27 class PannerNode
: public AudioNode
,
28 public SupportsWeakPtr
<PannerNode
>
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
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.
58 // Shut up the compiler warning
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;
68 // Shut up the compiler warning
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.
92 // Shut up the compiler warning
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;
103 // Shut up the compiler warning
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
)) {
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
)) {
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
)) {
147 SendThreeDPointParameterToStream(VELOCITY
, mVelocity
);
148 SendDopplerToSourcesIfNeeded();
151 double RefDistance() const
155 void SetRefDistance(double aRefDistance
)
157 if (WebAudioUtils::FuzzyEqual(mRefDistance
, aRefDistance
)) {
160 mRefDistance
= aRefDistance
;
161 SendDoubleParameterToStream(REF_DISTANCE
, mRefDistance
);
164 double MaxDistance() const
168 void SetMaxDistance(double aMaxDistance
)
170 if (WebAudioUtils::FuzzyEqual(mMaxDistance
, aMaxDistance
)) {
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
)) {
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
)) {
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
)) {
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
)) {
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
);
235 friend class AudioListener
;
236 friend class PannerNodeEngine
;
237 enum EngineParameters
{
239 LISTENER_ORIENTATION
,
242 LISTENER_DOPPLER_FACTOR
,
243 LISTENER_SPEED_OF_SOUND
,
258 PanningModelType mPanningModel
;
259 DistanceModelType mDistanceModel
;
260 ThreeDPoint mPosition
;
261 ThreeDPoint mOrientation
;
262 ThreeDPoint mVelocity
;
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
;