|
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 AudioListener_h_ |
|
8 #define AudioListener_h_ |
|
9 |
|
10 #include "nsWrapperCache.h" |
|
11 #include "nsCycleCollectionParticipant.h" |
|
12 #include "mozilla/Attributes.h" |
|
13 #include "nsAutoPtr.h" |
|
14 #include "ThreeDPoint.h" |
|
15 #include "AudioContext.h" |
|
16 #include "PannerNode.h" |
|
17 #include "WebAudioUtils.h" |
|
18 #include "js/TypeDecls.h" |
|
19 #include "mozilla/MemoryReporting.h" |
|
20 |
|
21 namespace mozilla { |
|
22 |
|
23 namespace dom { |
|
24 |
|
25 class AudioListener MOZ_FINAL : public nsWrapperCache |
|
26 { |
|
27 public: |
|
28 explicit AudioListener(AudioContext* aContext); |
|
29 |
|
30 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(AudioListener) |
|
31 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioListener) |
|
32 |
|
33 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; |
|
34 |
|
35 AudioContext* GetParentObject() const |
|
36 { |
|
37 return mContext; |
|
38 } |
|
39 |
|
40 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
41 |
|
42 double DopplerFactor() const |
|
43 { |
|
44 return mDopplerFactor; |
|
45 } |
|
46 void SetDopplerFactor(double aDopplerFactor) |
|
47 { |
|
48 if (WebAudioUtils::FuzzyEqual(mDopplerFactor, aDopplerFactor)) { |
|
49 return; |
|
50 } |
|
51 mDopplerFactor = aDopplerFactor; |
|
52 SendDoubleParameterToStream(PannerNode::LISTENER_DOPPLER_FACTOR, mDopplerFactor); |
|
53 } |
|
54 |
|
55 double SpeedOfSound() const |
|
56 { |
|
57 return mSpeedOfSound; |
|
58 } |
|
59 void SetSpeedOfSound(double aSpeedOfSound) |
|
60 { |
|
61 if (WebAudioUtils::FuzzyEqual(mSpeedOfSound, aSpeedOfSound)) { |
|
62 return; |
|
63 } |
|
64 mSpeedOfSound = aSpeedOfSound; |
|
65 SendDoubleParameterToStream(PannerNode::LISTENER_SPEED_OF_SOUND, mSpeedOfSound); |
|
66 } |
|
67 |
|
68 void SetPosition(double aX, double aY, double aZ) |
|
69 { |
|
70 if (WebAudioUtils::FuzzyEqual(mPosition.x, aX) && |
|
71 WebAudioUtils::FuzzyEqual(mPosition.y, aY) && |
|
72 WebAudioUtils::FuzzyEqual(mPosition.z, aZ)) { |
|
73 return; |
|
74 } |
|
75 mPosition.x = aX; |
|
76 mPosition.y = aY; |
|
77 mPosition.z = aZ; |
|
78 SendThreeDPointParameterToStream(PannerNode::LISTENER_POSITION, mPosition); |
|
79 } |
|
80 |
|
81 const ThreeDPoint& Position() const |
|
82 { |
|
83 return mPosition; |
|
84 } |
|
85 |
|
86 void SetOrientation(double aX, double aY, double aZ, |
|
87 double aXUp, double aYUp, double aZUp); |
|
88 |
|
89 const ThreeDPoint& Velocity() const |
|
90 { |
|
91 return mVelocity; |
|
92 } |
|
93 |
|
94 void SetVelocity(double aX, double aY, double aZ) |
|
95 { |
|
96 if (WebAudioUtils::FuzzyEqual(mVelocity.x, aX) && |
|
97 WebAudioUtils::FuzzyEqual(mVelocity.y, aY) && |
|
98 WebAudioUtils::FuzzyEqual(mVelocity.z, aZ)) { |
|
99 return; |
|
100 } |
|
101 mVelocity.x = aX; |
|
102 mVelocity.y = aY; |
|
103 mVelocity.z = aZ; |
|
104 SendThreeDPointParameterToStream(PannerNode::LISTENER_VELOCITY, mVelocity); |
|
105 UpdatePannersVelocity(); |
|
106 } |
|
107 |
|
108 void RegisterPannerNode(PannerNode* aPannerNode); |
|
109 void UnregisterPannerNode(PannerNode* aPannerNode); |
|
110 |
|
111 private: |
|
112 void SendDoubleParameterToStream(uint32_t aIndex, double aValue); |
|
113 void SendThreeDPointParameterToStream(uint32_t aIndex, const ThreeDPoint& aValue); |
|
114 void UpdatePannersVelocity(); |
|
115 |
|
116 private: |
|
117 friend class PannerNode; |
|
118 nsRefPtr<AudioContext> mContext; |
|
119 ThreeDPoint mPosition; |
|
120 ThreeDPoint mFrontVector; |
|
121 ThreeDPoint mRightVector; |
|
122 ThreeDPoint mVelocity; |
|
123 double mDopplerFactor; |
|
124 double mSpeedOfSound; |
|
125 nsTArray<WeakPtr<PannerNode> > mPanners; |
|
126 }; |
|
127 |
|
128 } |
|
129 } |
|
130 |
|
131 #endif |
|
132 |