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 #include "platform/audio/HRTFDatabaseLoader.h"
38 #include "wtf/MainThread.h"
39
40 namespace blink {
41
AudioListener()42 AudioListener::AudioListener()
43 : m_position(0, 0, 0)
44 , m_orientation(0, 0, -1)
45 , m_upVector(0, 1, 0)
46 , m_velocity(0, 0, 0)
47 , m_dopplerFactor(1)
48 , m_speedOfSound(343.3)
49 {
50 }
51
~AudioListener()52 AudioListener::~AudioListener()
53 {
54 }
55
trace(Visitor * visitor)56 void AudioListener::trace(Visitor* visitor)
57 {
58 visitor->trace(m_panners);
59 visitor->trace(m_hrtfDatabaseLoader);
60 }
61
addPanner(PannerNode * panner)62 void AudioListener::addPanner(PannerNode* panner)
63 {
64 ASSERT(isMainThread());
65 if (!panner)
66 return;
67
68 m_panners.append(panner);
69 }
70
removePanner(PannerNode * panner)71 void AudioListener::removePanner(PannerNode* panner)
72 {
73 ASSERT(isMainThread());
74 for (unsigned i = 0; i < m_panners.size(); ++i) {
75 if (panner == m_panners[i]) {
76 m_panners.remove(i);
77 break;
78 }
79 }
80 }
81
createAndLoadHRTFDatabaseLoader(float sampleRate)82 void AudioListener::createAndLoadHRTFDatabaseLoader(float sampleRate)
83 {
84 if (!m_hrtfDatabaseLoader)
85 m_hrtfDatabaseLoader = HRTFDatabaseLoader::createAndLoadAsynchronouslyIfNecessary(sampleRate);
86 }
87
isHRTFDatabaseLoaded()88 bool AudioListener::isHRTFDatabaseLoaded()
89 {
90 return m_hrtfDatabaseLoader->isLoaded();
91 }
92
waitForHRTFDatabaseLoaderThreadCompletion()93 void AudioListener::waitForHRTFDatabaseLoaderThreadCompletion()
94 {
95 if (m_hrtfDatabaseLoader)
96 m_hrtfDatabaseLoader->waitForLoaderThreadCompletion();
97 }
98
markPannersAsDirty(unsigned type)99 void AudioListener::markPannersAsDirty(unsigned type)
100 {
101 for (unsigned i = 0; i < m_panners.size(); ++i)
102 m_panners[i]->markPannerAsDirty(type);
103 }
104
setPosition(const FloatPoint3D & position)105 void AudioListener::setPosition(const FloatPoint3D &position)
106 {
107 if (m_position == position)
108 return;
109
110 // This synchronizes with panner's process().
111 MutexLocker listenerLocker(m_listenerLock);
112 m_position = position;
113 markPannersAsDirty(PannerNode::AzimuthElevationDirty | PannerNode::DistanceConeGainDirty | PannerNode::DopplerRateDirty);
114 }
115
setOrientation(const FloatPoint3D & orientation)116 void AudioListener::setOrientation(const FloatPoint3D &orientation)
117 {
118 if (m_orientation == orientation)
119 return;
120
121 // This synchronizes with panner's process().
122 MutexLocker listenerLocker(m_listenerLock);
123 m_orientation = orientation;
124 markPannersAsDirty(PannerNode::AzimuthElevationDirty);
125 }
126
setUpVector(const FloatPoint3D & upVector)127 void AudioListener::setUpVector(const FloatPoint3D &upVector)
128 {
129 if (m_upVector == upVector)
130 return;
131
132 // This synchronizes with panner's process().
133 MutexLocker listenerLocker(m_listenerLock);
134 m_upVector = upVector;
135 markPannersAsDirty(PannerNode::AzimuthElevationDirty);
136 }
137
setVelocity(const FloatPoint3D & velocity)138 void AudioListener::setVelocity(const FloatPoint3D &velocity)
139 {
140 if (m_velocity == velocity)
141 return;
142
143 // This synchronizes with panner's process().
144 MutexLocker listenerLocker(m_listenerLock);
145 m_velocity = velocity;
146 markPannersAsDirty(PannerNode::DopplerRateDirty);
147 }
148
setDopplerFactor(double dopplerFactor)149 void AudioListener::setDopplerFactor(double dopplerFactor)
150 {
151 if (m_dopplerFactor == dopplerFactor)
152 return;
153
154 // This synchronizes with panner's process().
155 MutexLocker listenerLocker(m_listenerLock);
156 m_dopplerFactor = dopplerFactor;
157 markPannersAsDirty(PannerNode::DopplerRateDirty);
158 }
159
setSpeedOfSound(double speedOfSound)160 void AudioListener::setSpeedOfSound(double speedOfSound)
161 {
162 if (m_speedOfSound == speedOfSound)
163 return;
164
165 // This synchronizes with panner's process().
166 MutexLocker listenerLocker(m_listenerLock);
167 m_speedOfSound = speedOfSound;
168 markPannersAsDirty(PannerNode::DopplerRateDirty);
169 }
170
171 } // namespace blink
172
173 #endif // ENABLE(WEB_AUDIO)
174