Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webaudio / PannerNode.h
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 #ifndef PannerNode_h
26 #define PannerNode_h
27
28 #include "platform/audio/AudioBus.h"
29 #include "platform/audio/Cone.h"
30 #include "platform/audio/Distance.h"
31 #include "platform/audio/Panner.h"
32 #include "modules/webaudio/AudioListener.h"
33 #include "modules/webaudio/AudioNode.h"
34 #include "modules/webaudio/AudioParam.h"
35 #include "platform/geometry/FloatPoint3D.h"
36 #include "wtf/HashMap.h"
37 #include "wtf/OwnPtr.h"
38
39 namespace WebCore {
40
41 // PannerNode is an AudioNode with one input and one output.
42 // It positions a sound in 3D space, with the exact effect dependent on the panning model.
43 // It has a position and an orientation in 3D space which is relative to the position and orientation of the context's AudioListener.
44 // A distance effect will attenuate the gain as the position moves away from the listener.
45 // A cone effect will attenuate the gain as the orientation moves away from the listener.
46 // All of these effects follow the OpenAL specification very closely.
47
48 class PannerNode FINAL : public AudioNode {
49 public:
50     // These must be defined as in the .idl file and must match those in the Panner class.
51     enum {
52         EQUALPOWER = 0,
53         HRTF = 1,
54         SOUNDFIELD = 2,
55     };
56
57     // These must be defined as in the .idl file and must match those
58     // in the DistanceEffect class.
59     enum {
60         LINEAR_DISTANCE = 0,
61         INVERSE_DISTANCE = 1,
62         EXPONENTIAL_DISTANCE = 2,
63     };
64
65     static PassRefPtr<PannerNode> create(AudioContext* context, float sampleRate)
66     {
67         return adoptRef(new PannerNode(context, sampleRate));
68     }
69
70     virtual ~PannerNode();
71
72     // AudioNode
73     virtual void process(size_t framesToProcess) OVERRIDE;
74     virtual void pullInputs(size_t framesToProcess) OVERRIDE;
75     virtual void initialize() OVERRIDE;
76     virtual void uninitialize() OVERRIDE;
77
78     // Listener
79     AudioListener* listener();
80
81     // Panning model
82     String panningModel() const;
83     bool setPanningModel(unsigned); // Returns true on success.
84     void setPanningModel(const String&);
85
86     // Position
87     void setPosition(float x, float y, float z) { m_position = FloatPoint3D(x, y, z); }
88
89     // Orientation
90     void setOrientation(float x, float y, float z) { m_orientation = FloatPoint3D(x, y, z); }
91
92     // Velocity
93     void setVelocity(float x, float y, float z) { m_velocity = FloatPoint3D(x, y, z); }
94
95     // Distance parameters
96     String distanceModel() const;
97     bool setDistanceModel(unsigned); // Returns true on success.
98     void setDistanceModel(const String&);
99
100     double refDistance() { return m_distanceEffect.refDistance(); }
101     void setRefDistance(double refDistance) { m_distanceEffect.setRefDistance(refDistance); }
102
103     double maxDistance() { return m_distanceEffect.maxDistance(); }
104     void setMaxDistance(double maxDistance) { m_distanceEffect.setMaxDistance(maxDistance); }
105
106     double rolloffFactor() { return m_distanceEffect.rolloffFactor(); }
107     void setRolloffFactor(double rolloffFactor) { m_distanceEffect.setRolloffFactor(rolloffFactor); }
108
109     // Sound cones - angles in degrees
110     double coneInnerAngle() const { return m_coneEffect.innerAngle(); }
111     void setConeInnerAngle(double angle) { m_coneEffect.setInnerAngle(angle); }
112
113     double coneOuterAngle() const { return m_coneEffect.outerAngle(); }
114     void setConeOuterAngle(double angle) { m_coneEffect.setOuterAngle(angle); }
115
116     double coneOuterGain() const { return m_coneEffect.outerGain(); }
117     void setConeOuterGain(double angle) { m_coneEffect.setOuterGain(angle); }
118
119     void getAzimuthElevation(double* outAzimuth, double* outElevation);
120     float dopplerRate();
121
122     virtual double tailTime() const OVERRIDE { return m_panner ? m_panner->tailTime() : 0; }
123     virtual double latencyTime() const OVERRIDE { return m_panner ? m_panner->latencyTime() : 0; }
124
125 private:
126     PannerNode(AudioContext*, float sampleRate);
127
128     // Returns the combined distance and cone gain attenuation.
129     float distanceConeGain();
130
131     // Notifies any AudioBufferSourceNodes connected to us either directly or indirectly about our existence.
132     // This is in order to handle the pitch change necessary for the doppler shift.
133     void notifyAudioSourcesConnectedToNode(AudioNode*, HashMap<AudioNode*, bool> &visitedNodes);
134
135     OwnPtr<Panner> m_panner;
136     unsigned m_panningModel;
137
138     FloatPoint3D m_position;
139     FloatPoint3D m_orientation;
140     FloatPoint3D m_velocity;
141
142     // Gain
143     RefPtr<AudioParam> m_distanceGain;
144     RefPtr<AudioParam> m_coneGain;
145     DistanceEffect m_distanceEffect;
146     ConeEffect m_coneEffect;
147     float m_lastGain;
148
149     unsigned m_connectionCount;
150
151     // Synchronize process() and setPanningModel() which can change the panner.
152     mutable Mutex m_pannerLock;
153 };
154
155 } // namespace WebCore
156
157 #endif // PannerNode_h