PannerNode.h (6832B)
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 PannerNode_h_ 8 #define PannerNode_h_ 9 10 #include "AudioNode.h" 11 #include "AudioParam.h" 12 #include "ThreeDPoint.h" 13 #include "mozilla/dom/PannerNodeBinding.h" 14 #include "nsPrintfCString.h" 15 16 namespace mozilla::dom { 17 18 class AudioContext; 19 class AudioBufferSourceNode; 20 struct PannerOptions; 21 22 class PannerNode final : public AudioNode { 23 public: 24 static already_AddRefed<PannerNode> Create(AudioContext& aAudioContext, 25 const PannerOptions& aOptions, 26 ErrorResult& aRv); 27 28 static already_AddRefed<PannerNode> Constructor(const GlobalObject& aGlobal, 29 AudioContext& aAudioContext, 30 const PannerOptions& aOptions, 31 ErrorResult& aRv) { 32 return Create(aAudioContext, aOptions, aRv); 33 } 34 35 JSObject* WrapObject(JSContext* aCx, 36 JS::Handle<JSObject*> aGivenProto) override; 37 38 void SetChannelCount(uint32_t aChannelCount, ErrorResult& aRv) override { 39 if (aChannelCount > 2) { 40 aRv.ThrowNotSupportedError( 41 nsPrintfCString("%u is greater than 2", aChannelCount)); 42 return; 43 } 44 AudioNode::SetChannelCount(aChannelCount, aRv); 45 } 46 void SetChannelCountModeValue(ChannelCountMode aMode, 47 ErrorResult& aRv) override { 48 if (aMode == ChannelCountMode::Max) { 49 aRv.ThrowNotSupportedError("Cannot set channel count mode to \"max\""); 50 return; 51 } 52 AudioNode::SetChannelCountModeValue(aMode, aRv); 53 } 54 55 NS_DECL_ISUPPORTS_INHERITED 56 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PannerNode, AudioNode) 57 58 PanningModelType PanningModel() const { return mPanningModel; } 59 void SetPanningModel(PanningModelType aPanningModel); 60 61 DistanceModelType DistanceModel() const { return mDistanceModel; } 62 void SetDistanceModel(DistanceModelType aDistanceModel) { 63 mDistanceModel = aDistanceModel; 64 SendInt32ParameterToTrack(DISTANCE_MODEL, int32_t(mDistanceModel)); 65 } 66 67 void SetPosition(double aX, double aY, double aZ, ErrorResult& aRv); 68 void SetOrientation(double aX, double aY, double aZ, ErrorResult& aRv); 69 70 double RefDistance() const { return mRefDistance; } 71 void SetRefDistance(double aRefDistance, ErrorResult& aRv) { 72 if (WebAudioUtils::FuzzyEqual(mRefDistance, aRefDistance)) { 73 return; 74 } 75 76 if (aRefDistance < 0) { 77 aRv.ThrowRangeError( 78 "The refDistance value passed to PannerNode must not be negative."); 79 return; 80 } 81 82 mRefDistance = aRefDistance; 83 SendDoubleParameterToTrack(REF_DISTANCE, mRefDistance); 84 } 85 86 double MaxDistance() const { return mMaxDistance; } 87 void SetMaxDistance(double aMaxDistance, ErrorResult& aRv) { 88 if (WebAudioUtils::FuzzyEqual(mMaxDistance, aMaxDistance)) { 89 return; 90 } 91 92 if (aMaxDistance <= 0) { 93 aRv.ThrowRangeError( 94 "The maxDistance value passed to PannerNode must be positive."); 95 return; 96 } 97 98 mMaxDistance = aMaxDistance; 99 SendDoubleParameterToTrack(MAX_DISTANCE, mMaxDistance); 100 } 101 102 double RolloffFactor() const { return mRolloffFactor; } 103 void SetRolloffFactor(double aRolloffFactor, ErrorResult& aRv) { 104 if (WebAudioUtils::FuzzyEqual(mRolloffFactor, aRolloffFactor)) { 105 return; 106 } 107 108 if (aRolloffFactor < 0) { 109 aRv.ThrowRangeError( 110 "The rolloffFactor value passed to PannerNode must not be negative."); 111 return; 112 } 113 114 mRolloffFactor = aRolloffFactor; 115 SendDoubleParameterToTrack(ROLLOFF_FACTOR, mRolloffFactor); 116 } 117 118 double ConeInnerAngle() const { return mConeInnerAngle; } 119 void SetConeInnerAngle(double aConeInnerAngle) { 120 if (WebAudioUtils::FuzzyEqual(mConeInnerAngle, aConeInnerAngle)) { 121 return; 122 } 123 mConeInnerAngle = aConeInnerAngle; 124 SendDoubleParameterToTrack(CONE_INNER_ANGLE, mConeInnerAngle); 125 } 126 127 double ConeOuterAngle() const { return mConeOuterAngle; } 128 void SetConeOuterAngle(double aConeOuterAngle) { 129 if (WebAudioUtils::FuzzyEqual(mConeOuterAngle, aConeOuterAngle)) { 130 return; 131 } 132 mConeOuterAngle = aConeOuterAngle; 133 SendDoubleParameterToTrack(CONE_OUTER_ANGLE, mConeOuterAngle); 134 } 135 136 double ConeOuterGain() const { return mConeOuterGain; } 137 void SetConeOuterGain(double aConeOuterGain, ErrorResult& aRv) { 138 if (WebAudioUtils::FuzzyEqual(mConeOuterGain, aConeOuterGain)) { 139 return; 140 } 141 142 if (aConeOuterGain < 0 || aConeOuterGain > 1) { 143 aRv.ThrowInvalidStateError( 144 nsPrintfCString("%g is not in the range [0, 1]", aConeOuterGain)); 145 return; 146 } 147 148 mConeOuterGain = aConeOuterGain; 149 SendDoubleParameterToTrack(CONE_OUTER_GAIN, mConeOuterGain); 150 } 151 152 AudioParam* PositionX() { return mPositionX; } 153 154 AudioParam* PositionY() { return mPositionY; } 155 156 AudioParam* PositionZ() { return mPositionZ; } 157 158 AudioParam* OrientationX() { return mOrientationX; } 159 160 AudioParam* OrientationY() { return mOrientationY; } 161 162 AudioParam* OrientationZ() { return mOrientationZ; } 163 164 const char* NodeType() const override { return "PannerNode"; } 165 166 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override; 167 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override; 168 169 private: 170 explicit PannerNode(AudioContext* aContext); 171 ~PannerNode() = default; 172 173 friend class AudioListener; 174 friend class PannerNodeEngine; 175 enum EngineParameters { 176 PANNING_MODEL, 177 DISTANCE_MODEL, 178 POSITION, 179 POSITIONX, 180 POSITIONY, 181 POSITIONZ, 182 ORIENTATION, // unit length or zero 183 ORIENTATIONX, 184 ORIENTATIONY, 185 ORIENTATIONZ, 186 REF_DISTANCE, 187 MAX_DISTANCE, 188 ROLLOFF_FACTOR, 189 CONE_INNER_ANGLE, 190 CONE_OUTER_ANGLE, 191 CONE_OUTER_GAIN 192 }; 193 194 ThreeDPoint ConvertAudioParamTo3DP(RefPtr<AudioParam> aX, 195 RefPtr<AudioParam> aY, 196 RefPtr<AudioParam> aZ) { 197 return ThreeDPoint(aX->GetValue(), aY->GetValue(), aZ->GetValue()); 198 } 199 200 PanningModelType mPanningModel; 201 DistanceModelType mDistanceModel; 202 RefPtr<AudioParam> mPositionX; 203 RefPtr<AudioParam> mPositionY; 204 RefPtr<AudioParam> mPositionZ; 205 RefPtr<AudioParam> mOrientationX; 206 RefPtr<AudioParam> mOrientationY; 207 RefPtr<AudioParam> mOrientationZ; 208 209 double mRefDistance; 210 double mMaxDistance; 211 double mRolloffFactor; 212 double mConeInnerAngle; 213 double mConeOuterAngle; 214 double mConeOuterGain; 215 }; 216 217 } // namespace mozilla::dom 218 219 #endif