1 | /* |
2 | * Copyright (C) 2010, Google Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
16 | * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
17 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
18 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
19 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
22 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
23 | */ |
24 | |
25 | #pragma once |
26 | |
27 | #if ENABLE(WEB_AUDIO) |
28 | |
29 | #include "AudioListener.h" |
30 | #include "AudioNode.h" |
31 | #include "AudioParam.h" |
32 | #include "Cone.h" |
33 | #include "Distance.h" |
34 | #include "FloatPoint3D.h" |
35 | #include "HRTFDatabaseLoader.h" |
36 | #include "Panner.h" |
37 | #include <memory> |
38 | #include <wtf/HashSet.h> |
39 | #include <wtf/Lock.h> |
40 | |
41 | namespace WebCore { |
42 | |
43 | // PannerNode is an AudioNode with one input and one output. |
44 | // It positions a sound in 3D space, with the exact effect dependent on the panning model. |
45 | // It has a position and an orientation in 3D space which is relative to the position and orientation of the context's AudioListener. |
46 | // A distance effect will attenuate the gain as the position moves away from the listener. |
47 | // A cone effect will attenuate the gain as the orientation moves away from the listener. |
48 | // All of these effects follow the OpenAL specification very closely. |
49 | |
50 | class PannerNode final : public AudioNode { |
51 | WTF_MAKE_ISO_ALLOCATED(PannerNode); |
52 | public: |
53 | static Ref<PannerNode> create(AudioContext& context, float sampleRate) |
54 | { |
55 | return adoptRef(*new PannerNode(context, sampleRate)); |
56 | } |
57 | |
58 | virtual ~PannerNode(); |
59 | |
60 | // AudioNode |
61 | void process(size_t framesToProcess) override; |
62 | void pullInputs(size_t framesToProcess) override; |
63 | void reset() override; |
64 | void initialize() override; |
65 | void uninitialize() override; |
66 | |
67 | // Listener |
68 | AudioListener* listener(); |
69 | |
70 | // Panning model |
71 | PanningModelType panningModel() const { return m_panningModel; } |
72 | void setPanningModel(PanningModelType); |
73 | |
74 | // Position |
75 | FloatPoint3D position() const { return m_position; } |
76 | void setPosition(float x, float y, float z) { m_position = FloatPoint3D(x, y, z); } |
77 | |
78 | // Orientation |
79 | FloatPoint3D orientation() const { return m_position; } |
80 | void setOrientation(float x, float y, float z) { m_orientation = FloatPoint3D(x, y, z); } |
81 | |
82 | // Velocity |
83 | FloatPoint3D velocity() const { return m_velocity; } |
84 | void setVelocity(float x, float y, float z) { m_velocity = FloatPoint3D(x, y, z); } |
85 | |
86 | // Distance parameters |
87 | DistanceModelType distanceModel() const; |
88 | void setDistanceModel(DistanceModelType); |
89 | |
90 | double refDistance() { return m_distanceEffect.refDistance(); } |
91 | void setRefDistance(double refDistance) { m_distanceEffect.setRefDistance(refDistance); } |
92 | |
93 | double maxDistance() { return m_distanceEffect.maxDistance(); } |
94 | void setMaxDistance(double maxDistance) { m_distanceEffect.setMaxDistance(maxDistance); } |
95 | |
96 | double rolloffFactor() { return m_distanceEffect.rolloffFactor(); } |
97 | void setRolloffFactor(double rolloffFactor) { m_distanceEffect.setRolloffFactor(rolloffFactor); } |
98 | |
99 | // Sound cones - angles in degrees |
100 | double coneInnerAngle() const { return m_coneEffect.innerAngle(); } |
101 | void setConeInnerAngle(double angle) { m_coneEffect.setInnerAngle(angle); } |
102 | |
103 | double coneOuterAngle() const { return m_coneEffect.outerAngle(); } |
104 | void setConeOuterAngle(double angle) { m_coneEffect.setOuterAngle(angle); } |
105 | |
106 | double coneOuterGain() const { return m_coneEffect.outerGain(); } |
107 | void setConeOuterGain(double angle) { m_coneEffect.setOuterGain(angle); } |
108 | |
109 | void getAzimuthElevation(double* outAzimuth, double* outElevation); |
110 | float dopplerRate(); |
111 | |
112 | // Accessors for dynamically calculated gain values. |
113 | AudioParam* distanceGain() { return m_distanceGain.get(); } |
114 | AudioParam* coneGain() { return m_coneGain.get(); } |
115 | |
116 | double tailTime() const override { return m_panner ? m_panner->tailTime() : 0; } |
117 | double latencyTime() const override { return m_panner ? m_panner->latencyTime() : 0; } |
118 | |
119 | private: |
120 | PannerNode(AudioContext&, float sampleRate); |
121 | |
122 | // Returns the combined distance and cone gain attenuation. |
123 | float distanceConeGain(); |
124 | |
125 | // Notifies any AudioBufferSourceNodes connected to us either directly or indirectly about our existence. |
126 | // This is in order to handle the pitch change necessary for the doppler shift. |
127 | void notifyAudioSourcesConnectedToNode(AudioNode*, HashSet<AudioNode*>& visitedNodes); |
128 | |
129 | std::unique_ptr<Panner> m_panner; |
130 | PanningModelType m_panningModel; |
131 | |
132 | FloatPoint3D m_position; |
133 | FloatPoint3D m_orientation; |
134 | FloatPoint3D m_velocity; |
135 | |
136 | // Gain |
137 | RefPtr<AudioParam> m_distanceGain; |
138 | RefPtr<AudioParam> m_coneGain; |
139 | DistanceEffect m_distanceEffect; |
140 | ConeEffect m_coneEffect; |
141 | float m_lastGain; |
142 | |
143 | // HRTF Database loader |
144 | RefPtr<HRTFDatabaseLoader> m_hrtfDatabaseLoader; |
145 | |
146 | unsigned m_connectionCount; |
147 | |
148 | // Synchronize process() and setPanningModel() which can change the panner. |
149 | mutable Lock m_pannerMutex; |
150 | }; |
151 | |
152 | } // namespace WebCore |
153 | |
154 | #endif |
155 | |