content/media/webaudio/PannerNode.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef PannerNode_h_
michael@0 8 #define PannerNode_h_
michael@0 9
michael@0 10 #include "AudioNode.h"
michael@0 11 #include "mozilla/dom/PannerNodeBinding.h"
michael@0 12 #include "ThreeDPoint.h"
michael@0 13 #include "mozilla/WeakPtr.h"
michael@0 14 #include "mozilla/Preferences.h"
michael@0 15 #include "WebAudioUtils.h"
michael@0 16 #include <set>
michael@0 17
michael@0 18 namespace mozilla {
michael@0 19 namespace dom {
michael@0 20
michael@0 21 class AudioContext;
michael@0 22 class AudioBufferSourceNode;
michael@0 23
michael@0 24 class PannerNode : public AudioNode,
michael@0 25 public SupportsWeakPtr<PannerNode>
michael@0 26 {
michael@0 27 public:
michael@0 28 MOZ_DECLARE_REFCOUNTED_TYPENAME(PannerNode)
michael@0 29 explicit PannerNode(AudioContext* aContext);
michael@0 30 virtual ~PannerNode();
michael@0 31
michael@0 32
michael@0 33 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
michael@0 34
michael@0 35 virtual void DestroyMediaStream() MOZ_OVERRIDE;
michael@0 36
michael@0 37 virtual void SetChannelCount(uint32_t aChannelCount, ErrorResult& aRv) MOZ_OVERRIDE
michael@0 38 {
michael@0 39 if (aChannelCount > 2) {
michael@0 40 aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
michael@0 41 return;
michael@0 42 }
michael@0 43 AudioNode::SetChannelCount(aChannelCount, aRv);
michael@0 44 }
michael@0 45 virtual void SetChannelCountModeValue(ChannelCountMode aMode, ErrorResult& aRv) MOZ_OVERRIDE
michael@0 46 {
michael@0 47 if (aMode == ChannelCountMode::Max) {
michael@0 48 aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
michael@0 49 return;
michael@0 50 }
michael@0 51 AudioNode::SetChannelCountModeValue(aMode, aRv);
michael@0 52 }
michael@0 53
michael@0 54 NS_DECL_ISUPPORTS_INHERITED
michael@0 55 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PannerNode, AudioNode)
michael@0 56
michael@0 57 PanningModelType PanningModel() const
michael@0 58 {
michael@0 59 return mPanningModel;
michael@0 60 }
michael@0 61 void SetPanningModel(PanningModelType aPanningModel)
michael@0 62 {
michael@0 63 if (!Preferences::GetBool("media.webaudio.legacy.PannerNode")) {
michael@0 64 // Do not accept the alternate enum values unless the legacy pref
michael@0 65 // has been turned on.
michael@0 66 switch (aPanningModel) {
michael@0 67 case PanningModelType::_0:
michael@0 68 case PanningModelType::_1:
michael@0 69 // Do nothing in order to emulate setting an invalid enum value.
michael@0 70 return;
michael@0 71 default:
michael@0 72 // Shut up the compiler warning
michael@0 73 break;
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77 // Handle the alternate enum values
michael@0 78 switch (aPanningModel) {
michael@0 79 case PanningModelType::_0: aPanningModel = PanningModelType::Equalpower; break;
michael@0 80 case PanningModelType::_1: aPanningModel = PanningModelType::HRTF; break;
michael@0 81 default:
michael@0 82 // Shut up the compiler warning
michael@0 83 break;
michael@0 84 }
michael@0 85
michael@0 86 mPanningModel = aPanningModel;
michael@0 87 SendInt32ParameterToStream(PANNING_MODEL, int32_t(mPanningModel));
michael@0 88 }
michael@0 89
michael@0 90 DistanceModelType DistanceModel() const
michael@0 91 {
michael@0 92 return mDistanceModel;
michael@0 93 }
michael@0 94 void SetDistanceModel(DistanceModelType aDistanceModel)
michael@0 95 {
michael@0 96 if (!Preferences::GetBool("media.webaudio.legacy.PannerNode")) {
michael@0 97 // Do not accept the alternate enum values unless the legacy pref
michael@0 98 // has been turned on.
michael@0 99 switch (aDistanceModel) {
michael@0 100 case DistanceModelType::_0:
michael@0 101 case DistanceModelType::_1:
michael@0 102 case DistanceModelType::_2:
michael@0 103 // Do nothing in order to emulate setting an invalid enum value.
michael@0 104 return;
michael@0 105 default:
michael@0 106 // Shut up the compiler warning
michael@0 107 break;
michael@0 108 }
michael@0 109 }
michael@0 110
michael@0 111 // Handle the alternate enum values
michael@0 112 switch (aDistanceModel) {
michael@0 113 case DistanceModelType::_0: aDistanceModel = DistanceModelType::Linear; break;
michael@0 114 case DistanceModelType::_1: aDistanceModel = DistanceModelType::Inverse; break;
michael@0 115 case DistanceModelType::_2: aDistanceModel = DistanceModelType::Exponential; break;
michael@0 116 default:
michael@0 117 // Shut up the compiler warning
michael@0 118 break;
michael@0 119 }
michael@0 120
michael@0 121 mDistanceModel = aDistanceModel;
michael@0 122 SendInt32ParameterToStream(DISTANCE_MODEL, int32_t(mDistanceModel));
michael@0 123 }
michael@0 124
michael@0 125 void SetPosition(double aX, double aY, double aZ)
michael@0 126 {
michael@0 127 if (WebAudioUtils::FuzzyEqual(mPosition.x, aX) &&
michael@0 128 WebAudioUtils::FuzzyEqual(mPosition.y, aY) &&
michael@0 129 WebAudioUtils::FuzzyEqual(mPosition.z, aZ)) {
michael@0 130 return;
michael@0 131 }
michael@0 132 mPosition.x = aX;
michael@0 133 mPosition.y = aY;
michael@0 134 mPosition.z = aZ;
michael@0 135 SendThreeDPointParameterToStream(POSITION, mPosition);
michael@0 136 }
michael@0 137
michael@0 138 void SetOrientation(double aX, double aY, double aZ)
michael@0 139 {
michael@0 140 ThreeDPoint orientation(aX, aY, aZ);
michael@0 141 if (!orientation.IsZero()) {
michael@0 142 orientation.Normalize();
michael@0 143 }
michael@0 144 if (mOrientation.FuzzyEqual(orientation)) {
michael@0 145 return;
michael@0 146 }
michael@0 147 mOrientation = orientation;
michael@0 148 SendThreeDPointParameterToStream(ORIENTATION, mOrientation);
michael@0 149 }
michael@0 150
michael@0 151 void SetVelocity(double aX, double aY, double aZ)
michael@0 152 {
michael@0 153 if (WebAudioUtils::FuzzyEqual(mVelocity.x, aX) &&
michael@0 154 WebAudioUtils::FuzzyEqual(mVelocity.y, aY) &&
michael@0 155 WebAudioUtils::FuzzyEqual(mVelocity.z, aZ)) {
michael@0 156 return;
michael@0 157 }
michael@0 158 mVelocity.x = aX;
michael@0 159 mVelocity.y = aY;
michael@0 160 mVelocity.z = aZ;
michael@0 161 SendThreeDPointParameterToStream(VELOCITY, mVelocity);
michael@0 162 SendDopplerToSourcesIfNeeded();
michael@0 163 }
michael@0 164
michael@0 165 double RefDistance() const
michael@0 166 {
michael@0 167 return mRefDistance;
michael@0 168 }
michael@0 169 void SetRefDistance(double aRefDistance)
michael@0 170 {
michael@0 171 if (WebAudioUtils::FuzzyEqual(mRefDistance, aRefDistance)) {
michael@0 172 return;
michael@0 173 }
michael@0 174 mRefDistance = aRefDistance;
michael@0 175 SendDoubleParameterToStream(REF_DISTANCE, mRefDistance);
michael@0 176 }
michael@0 177
michael@0 178 double MaxDistance() const
michael@0 179 {
michael@0 180 return mMaxDistance;
michael@0 181 }
michael@0 182 void SetMaxDistance(double aMaxDistance)
michael@0 183 {
michael@0 184 if (WebAudioUtils::FuzzyEqual(mMaxDistance, aMaxDistance)) {
michael@0 185 return;
michael@0 186 }
michael@0 187 mMaxDistance = aMaxDistance;
michael@0 188 SendDoubleParameterToStream(MAX_DISTANCE, mMaxDistance);
michael@0 189 }
michael@0 190
michael@0 191 double RolloffFactor() const
michael@0 192 {
michael@0 193 return mRolloffFactor;
michael@0 194 }
michael@0 195 void SetRolloffFactor(double aRolloffFactor)
michael@0 196 {
michael@0 197 if (WebAudioUtils::FuzzyEqual(mRolloffFactor, aRolloffFactor)) {
michael@0 198 return;
michael@0 199 }
michael@0 200 mRolloffFactor = aRolloffFactor;
michael@0 201 SendDoubleParameterToStream(ROLLOFF_FACTOR, mRolloffFactor);
michael@0 202 }
michael@0 203
michael@0 204 double ConeInnerAngle() const
michael@0 205 {
michael@0 206 return mConeInnerAngle;
michael@0 207 }
michael@0 208 void SetConeInnerAngle(double aConeInnerAngle)
michael@0 209 {
michael@0 210 if (WebAudioUtils::FuzzyEqual(mConeInnerAngle, aConeInnerAngle)) {
michael@0 211 return;
michael@0 212 }
michael@0 213 mConeInnerAngle = aConeInnerAngle;
michael@0 214 SendDoubleParameterToStream(CONE_INNER_ANGLE, mConeInnerAngle);
michael@0 215 }
michael@0 216
michael@0 217 double ConeOuterAngle() const
michael@0 218 {
michael@0 219 return mConeOuterAngle;
michael@0 220 }
michael@0 221 void SetConeOuterAngle(double aConeOuterAngle)
michael@0 222 {
michael@0 223 if (WebAudioUtils::FuzzyEqual(mConeOuterAngle, aConeOuterAngle)) {
michael@0 224 return;
michael@0 225 }
michael@0 226 mConeOuterAngle = aConeOuterAngle;
michael@0 227 SendDoubleParameterToStream(CONE_OUTER_ANGLE, mConeOuterAngle);
michael@0 228 }
michael@0 229
michael@0 230 double ConeOuterGain() const
michael@0 231 {
michael@0 232 return mConeOuterGain;
michael@0 233 }
michael@0 234 void SetConeOuterGain(double aConeOuterGain)
michael@0 235 {
michael@0 236 if (WebAudioUtils::FuzzyEqual(mConeOuterGain, aConeOuterGain)) {
michael@0 237 return;
michael@0 238 }
michael@0 239 mConeOuterGain = aConeOuterGain;
michael@0 240 SendDoubleParameterToStream(CONE_OUTER_GAIN, mConeOuterGain);
michael@0 241 }
michael@0 242
michael@0 243 float ComputeDopplerShift();
michael@0 244 void SendDopplerToSourcesIfNeeded();
michael@0 245 void FindConnectedSources();
michael@0 246 void FindConnectedSources(AudioNode* aNode, nsTArray<AudioBufferSourceNode*>& aSources, std::set<AudioNode*>& aSeenNodes);
michael@0 247
michael@0 248 virtual const char* NodeType() const
michael@0 249 {
michael@0 250 return "PannerNode";
michael@0 251 }
michael@0 252
michael@0 253 virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE;
michael@0 254 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE;
michael@0 255
michael@0 256 private:
michael@0 257 friend class AudioListener;
michael@0 258 friend class PannerNodeEngine;
michael@0 259 enum EngineParameters {
michael@0 260 LISTENER_POSITION,
michael@0 261 LISTENER_FRONT_VECTOR, // unit length
michael@0 262 LISTENER_RIGHT_VECTOR, // unit length, orthogonal to LISTENER_FRONT_VECTOR
michael@0 263 LISTENER_VELOCITY,
michael@0 264 LISTENER_DOPPLER_FACTOR,
michael@0 265 LISTENER_SPEED_OF_SOUND,
michael@0 266 PANNING_MODEL,
michael@0 267 DISTANCE_MODEL,
michael@0 268 POSITION,
michael@0 269 ORIENTATION, // unit length or zero
michael@0 270 VELOCITY,
michael@0 271 REF_DISTANCE,
michael@0 272 MAX_DISTANCE,
michael@0 273 ROLLOFF_FACTOR,
michael@0 274 CONE_INNER_ANGLE,
michael@0 275 CONE_OUTER_ANGLE,
michael@0 276 CONE_OUTER_GAIN
michael@0 277 };
michael@0 278
michael@0 279 private:
michael@0 280 PanningModelType mPanningModel;
michael@0 281 DistanceModelType mDistanceModel;
michael@0 282 ThreeDPoint mPosition;
michael@0 283 ThreeDPoint mOrientation;
michael@0 284 ThreeDPoint mVelocity;
michael@0 285 double mRefDistance;
michael@0 286 double mMaxDistance;
michael@0 287 double mRolloffFactor;
michael@0 288 double mConeInnerAngle;
michael@0 289 double mConeOuterAngle;
michael@0 290 double mConeOuterGain;
michael@0 291
michael@0 292 // An array of all the AudioBufferSourceNode connected directly or indirectly
michael@0 293 // to this AudioPannerNode.
michael@0 294 nsTArray<AudioBufferSourceNode*> mSources;
michael@0 295 };
michael@0 296
michael@0 297 }
michael@0 298 }
michael@0 299
michael@0 300 #endif
michael@0 301

mercurial