Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webaudio / AudioListener.cpp
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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30
31 #if ENABLE(WEB_AUDIO)
32
33 #include "modules/webaudio/AudioListener.h"
34
35 #include "modules/webaudio/PannerNode.h"
36 #include "platform/audio/AudioBus.h"
37
38 namespace WebCore {
39
40 AudioListener::AudioListener()
41     : m_position(0, 0, 0)
42     , m_orientation(0, 0, -1)
43     , m_upVector(0, 1, 0)
44     , m_velocity(0, 0, 0)
45     , m_dopplerFactor(1)
46     , m_speedOfSound(343.3)
47 {
48     ScriptWrappable::init(this);
49 }
50
51 AudioListener::~AudioListener()
52 {
53     m_panners.clear();
54 }
55
56 void AudioListener::addPanner(PannerNode* panner)
57 {
58     if (!panner)
59         return;
60
61     m_panners.append(panner);
62 }
63
64 void AudioListener::removePanner(PannerNode* panner)
65 {
66     for (unsigned i = 0; i < m_panners.size(); ++i) {
67         if (panner == m_panners[i]) {
68             m_panners.remove(i);
69             break;
70         }
71     }
72 }
73
74 void AudioListener::markPannersAsDirty(unsigned type)
75 {
76     for (unsigned i = 0; i < m_panners.size(); ++i)
77         m_panners[i]->markPannerAsDirty(type);
78 }
79
80 void AudioListener::setPosition(const FloatPoint3D &position)
81 {
82     if (m_position == position)
83         return;
84
85     // This synchronizes with panner's process().
86     MutexLocker listenerLocker(m_listenerLock);
87     m_position = position;
88     markPannersAsDirty(PannerNode::AzimuthElevationDirty | PannerNode::DistanceConeGainDirty | PannerNode::DopplerRateDirty);
89 }
90
91 void AudioListener::setOrientation(const FloatPoint3D &orientation)
92 {
93     if (m_orientation == orientation)
94         return;
95
96     // This synchronizes with panner's process().
97     MutexLocker listenerLocker(m_listenerLock);
98     m_orientation = orientation;
99     markPannersAsDirty(PannerNode::AzimuthElevationDirty);
100 }
101
102 void AudioListener::setUpVector(const FloatPoint3D &upVector)
103 {
104     if (m_upVector == upVector)
105         return;
106
107     // This synchronizes with panner's process().
108     MutexLocker listenerLocker(m_listenerLock);
109     m_upVector = upVector;
110     markPannersAsDirty(PannerNode::AzimuthElevationDirty);
111 }
112
113 void AudioListener::setVelocity(const FloatPoint3D &velocity)
114 {
115     if (m_velocity == velocity)
116         return;
117
118     // This synchronizes with panner's process().
119     MutexLocker listenerLocker(m_listenerLock);
120     m_velocity = velocity;
121     markPannersAsDirty(PannerNode::DopplerRateDirty);
122 }
123
124 void AudioListener::setDopplerFactor(double dopplerFactor)
125 {
126     if (m_dopplerFactor == dopplerFactor)
127         return;
128
129     // This synchronizes with panner's process().
130     MutexLocker listenerLocker(m_listenerLock);
131     m_dopplerFactor = dopplerFactor;
132     markPannersAsDirty(PannerNode::DopplerRateDirty);
133 }
134
135 void AudioListener::setSpeedOfSound(double speedOfSound)
136 {
137     if (m_speedOfSound == speedOfSound)
138         return;
139
140     // This synchronizes with panner's process().
141     MutexLocker listenerLocker(m_listenerLock);
142     m_speedOfSound = speedOfSound;
143     markPannersAsDirty(PannerNode::DopplerRateDirty);
144 }
145
146 } // namespace WebCore
147
148 #endif // ENABLE(WEB_AUDIO)