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