2 * Copyright (C) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "modules/webaudio/PannerNode.h"
31 #include "core/dom/ExecutionContext.h"
32 #include "platform/audio/HRTFPanner.h"
33 #include "modules/webaudio/AudioBufferSourceNode.h"
34 #include "modules/webaudio/AudioContext.h"
35 #include "modules/webaudio/AudioNodeInput.h"
36 #include "modules/webaudio/AudioNodeOutput.h"
37 #include "wtf/MathExtras.h"
43 static void fixNANs(double &x)
45 if (std::isnan(x) || std::isinf(x))
49 PannerNode::PannerNode(AudioContext* context, float sampleRate)
50 : AudioNode(context, sampleRate)
51 , m_panningModel(Panner::PanningModelHRTF)
52 , m_distanceModel(DistanceEffect::ModelInverse)
54 , m_orientation(1, 0, 0)
56 , m_isAzimuthElevationDirty(true)
57 , m_isDistanceConeGainDirty(true)
58 , m_isDopplerRateDirty(true)
61 , m_cachedElevation(0)
62 , m_cachedDistanceConeGain(1.0f)
63 , m_cachedDopplerRate(1)
64 , m_connectionCount(0)
66 // Load the HRTF database asynchronously so we don't block the Javascript thread while creating the HRTF database.
67 // The HRTF panner will return zeroes until the database is loaded.
68 m_hrtfDatabaseLoader = HRTFDatabaseLoader::createAndLoadAsynchronouslyIfNecessary(context->sampleRate());
70 ScriptWrappable::init(this);
71 addInput(adoptPtr(new AudioNodeInput(this)));
72 addOutput(adoptPtr(new AudioNodeOutput(this, 2)));
74 // Node-specific default mixing rules.
76 m_channelCountMode = ClampedMax;
77 m_channelInterpretation = AudioBus::Speakers;
79 setNodeType(NodeTypePanner);
84 PannerNode::~PannerNode()
89 void PannerNode::pullInputs(size_t framesToProcess)
91 // We override pullInputs(), so we can detect new AudioSourceNodes which have connected to us when new connections are made.
92 // These AudioSourceNodes need to be made aware of our existence in order to handle doppler shift pitch changes.
93 if (m_connectionCount != context()->connectionCount()) {
94 m_connectionCount = context()->connectionCount();
96 // A map for keeping track if we have visited a node or not. This prevents feedback loops
97 // from recursing infinitely. See crbug.com/331446.
98 HashMap<AudioNode*, bool> visitedNodes;
100 // Recursively go through all nodes connected to us
101 notifyAudioSourcesConnectedToNode(this, visitedNodes);
104 AudioNode::pullInputs(framesToProcess);
107 void PannerNode::process(size_t framesToProcess)
109 AudioBus* destination = output(0)->bus();
111 if (!isInitialized() || !input(0)->isConnected() || !m_panner.get()) {
116 AudioBus* source = input(0)->bus();
122 // The audio thread can't block on this lock, so we call tryLock() instead.
123 MutexTryLocker tryLocker(m_processLock);
124 MutexTryLocker tryListenerLocker(listener()->listenerLock());
126 if (tryLocker.locked() && tryListenerLocker.locked()) {
127 // HRTFDatabase should be loaded before proceeding for offline audio context when the panning model is HRTF.
128 if (m_panningModel == Panner::PanningModelHRTF && !m_hrtfDatabaseLoader->isLoaded()) {
129 if (context()->isOfflineContext()) {
130 m_hrtfDatabaseLoader->waitForLoaderThreadCompletion();
137 // Apply the panning effect.
140 azimuthElevation(&azimuth, &elevation);
142 m_panner->pan(azimuth, elevation, source, destination, framesToProcess);
144 // Get the distance and cone gain.
145 float totalGain = distanceConeGain();
147 // Snap to desired gain at the beginning.
148 if (m_lastGain == -1.0)
149 m_lastGain = totalGain;
151 // Apply gain in-place with de-zippering.
152 destination->copyWithGainFrom(*destination, &m_lastGain, totalGain);
154 // Too bad - The tryLock() failed.
155 // We must be in the middle of changing the properties of the panner or the listener.
160 void PannerNode::initialize()
165 m_panner = Panner::create(m_panningModel, sampleRate(), m_hrtfDatabaseLoader.get());
166 listener()->addPanner(this);
168 AudioNode::initialize();
171 void PannerNode::uninitialize()
173 if (!isInitialized())
177 listener()->removePanner(this);
179 AudioNode::uninitialize();
182 AudioListener* PannerNode::listener()
184 return context()->listener();
187 String PannerNode::panningModel() const
189 switch (m_panningModel) {
190 case Panner::PanningModelEqualPower:
192 case Panner::PanningModelHRTF:
195 ASSERT_NOT_REACHED();
200 void PannerNode::setPanningModel(const String& model)
202 if (model == "equalpower")
203 setPanningModel(Panner::PanningModelEqualPower);
204 else if (model == "HRTF")
205 setPanningModel(Panner::PanningModelHRTF);
208 bool PannerNode::setPanningModel(unsigned model)
211 case Panner::PanningModelEqualPower:
212 case Panner::PanningModelHRTF:
213 if (!m_panner.get() || model != m_panningModel) {
214 // This synchronizes with process().
215 MutexLocker processLocker(m_processLock);
216 OwnPtr<Panner> newPanner = Panner::create(model, sampleRate(), m_hrtfDatabaseLoader.get());
217 m_panner = newPanner.release();
218 m_panningModel = model;
222 ASSERT_NOT_REACHED();
229 String PannerNode::distanceModel() const
231 switch (const_cast<PannerNode*>(this)->m_distanceEffect.model()) {
232 case DistanceEffect::ModelLinear:
234 case DistanceEffect::ModelInverse:
236 case DistanceEffect::ModelExponential:
237 return "exponential";
239 ASSERT_NOT_REACHED();
244 void PannerNode::setDistanceModel(const String& model)
246 if (model == "linear")
247 setDistanceModel(DistanceEffect::ModelLinear);
248 else if (model == "inverse")
249 setDistanceModel(DistanceEffect::ModelInverse);
250 else if (model == "exponential")
251 setDistanceModel(DistanceEffect::ModelExponential);
254 bool PannerNode::setDistanceModel(unsigned model)
257 case DistanceEffect::ModelLinear:
258 case DistanceEffect::ModelInverse:
259 case DistanceEffect::ModelExponential:
260 if (model != m_distanceModel) {
261 // This synchronizes with process().
262 MutexLocker processLocker(m_processLock);
263 m_distanceEffect.setModel(static_cast<DistanceEffect::ModelType>(model), true);
264 m_distanceModel = model;
268 ASSERT_NOT_REACHED();
275 void PannerNode::setRefDistance(double distance)
277 if (refDistance() == distance)
280 // This synchronizes with process().
281 MutexLocker processLocker(m_processLock);
282 m_distanceEffect.setRefDistance(distance);
283 markPannerAsDirty(PannerNode::DistanceConeGainDirty);
286 void PannerNode::setMaxDistance(double distance)
288 if (maxDistance() == distance)
291 // This synchronizes with process().
292 MutexLocker processLocker(m_processLock);
293 m_distanceEffect.setMaxDistance(distance);
294 markPannerAsDirty(PannerNode::DistanceConeGainDirty);
297 void PannerNode::setRolloffFactor(double factor)
299 if (rolloffFactor() == factor)
302 // This synchronizes with process().
303 MutexLocker processLocker(m_processLock);
304 m_distanceEffect.setRolloffFactor(factor);
305 markPannerAsDirty(PannerNode::DistanceConeGainDirty);
308 void PannerNode::setConeInnerAngle(double angle)
310 if (coneInnerAngle() == angle)
313 // This synchronizes with process().
314 MutexLocker processLocker(m_processLock);
315 m_coneEffect.setInnerAngle(angle);
316 markPannerAsDirty(PannerNode::DistanceConeGainDirty);
319 void PannerNode::setConeOuterAngle(double angle)
321 if (coneOuterAngle() == angle)
324 // This synchronizes with process().
325 MutexLocker processLocker(m_processLock);
326 m_coneEffect.setOuterAngle(angle);
327 markPannerAsDirty(PannerNode::DistanceConeGainDirty);
330 void PannerNode::setConeOuterGain(double angle)
332 if (coneOuterGain() == angle)
335 // This synchronizes with process().
336 MutexLocker processLocker(m_processLock);
337 m_coneEffect.setOuterGain(angle);
338 markPannerAsDirty(PannerNode::DistanceConeGainDirty);
341 void PannerNode::setPosition(float x, float y, float z)
343 FloatPoint3D position = FloatPoint3D(x, y, z);
345 if (m_position == position)
348 // This synchronizes with process().
349 MutexLocker processLocker(m_processLock);
350 m_position = position;
351 markPannerAsDirty(PannerNode::AzimuthElevationDirty | PannerNode::DistanceConeGainDirty | PannerNode::DopplerRateDirty);
354 void PannerNode::setOrientation(float x, float y, float z)
356 FloatPoint3D orientation = FloatPoint3D(x, y, z);
358 if (m_orientation == orientation)
361 // This synchronizes with process().
362 MutexLocker processLocker(m_processLock);
363 m_orientation = orientation;
364 markPannerAsDirty(PannerNode::DistanceConeGainDirty);
367 void PannerNode::setVelocity(float x, float y, float z)
369 FloatPoint3D velocity = FloatPoint3D(x, y, z);
371 if (m_velocity == velocity)
374 // This synchronizes with process().
375 MutexLocker processLocker(m_processLock);
376 m_velocity = velocity;
377 markPannerAsDirty(PannerNode::DopplerRateDirty);
380 void PannerNode::calculateAzimuthElevation(double* outAzimuth, double* outElevation)
382 double azimuth = 0.0;
384 // Calculate the source-listener vector
385 FloatPoint3D listenerPosition = listener()->position();
386 FloatPoint3D sourceListener = m_position - listenerPosition;
388 if (sourceListener.isZero()) {
389 // degenerate case if source and listener are at the same point
395 sourceListener.normalize();
398 FloatPoint3D listenerFront = listener()->orientation();
399 FloatPoint3D listenerUp = listener()->upVector();
400 FloatPoint3D listenerRight = listenerFront.cross(listenerUp);
401 listenerRight.normalize();
403 FloatPoint3D listenerFrontNorm = listenerFront;
404 listenerFrontNorm.normalize();
406 FloatPoint3D up = listenerRight.cross(listenerFrontNorm);
408 float upProjection = sourceListener.dot(up);
410 FloatPoint3D projectedSource = sourceListener - upProjection * up;
411 projectedSource.normalize();
413 azimuth = 180.0 * acos(projectedSource.dot(listenerRight)) / piDouble;
414 fixNANs(azimuth); // avoid illegal values
416 // Source in front or behind the listener
417 double frontBack = projectedSource.dot(listenerFrontNorm);
419 azimuth = 360.0 - azimuth;
421 // Make azimuth relative to "front" and not "right" listener vector
422 if ((azimuth >= 0.0) && (azimuth <= 270.0))
423 azimuth = 90.0 - azimuth;
425 azimuth = 450.0 - azimuth;
428 double elevation = 90.0 - 180.0 * acos(sourceListener.dot(up)) / piDouble;
429 fixNANs(elevation); // avoid illegal values
431 if (elevation > 90.0)
432 elevation = 180.0 - elevation;
433 else if (elevation < -90.0)
434 elevation = -180.0 - elevation;
437 *outAzimuth = azimuth;
439 *outElevation = elevation;
442 double PannerNode::calculateDopplerRate()
444 double dopplerShift = 1.0;
445 double dopplerFactor = listener()->dopplerFactor();
447 if (dopplerFactor > 0.0) {
448 double speedOfSound = listener()->speedOfSound();
450 const FloatPoint3D &sourceVelocity = m_velocity;
451 const FloatPoint3D &listenerVelocity = listener()->velocity();
453 // Don't bother if both source and listener have no velocity
454 bool sourceHasVelocity = !sourceVelocity.isZero();
455 bool listenerHasVelocity = !listenerVelocity.isZero();
457 if (sourceHasVelocity || listenerHasVelocity) {
458 // Calculate the source to listener vector
459 FloatPoint3D listenerPosition = listener()->position();
460 FloatPoint3D sourceToListener = m_position - listenerPosition;
462 double sourceListenerMagnitude = sourceToListener.length();
464 double listenerProjection = sourceToListener.dot(listenerVelocity) / sourceListenerMagnitude;
465 double sourceProjection = sourceToListener.dot(sourceVelocity) / sourceListenerMagnitude;
467 listenerProjection = -listenerProjection;
468 sourceProjection = -sourceProjection;
470 double scaledSpeedOfSound = speedOfSound / dopplerFactor;
471 listenerProjection = min(listenerProjection, scaledSpeedOfSound);
472 sourceProjection = min(sourceProjection, scaledSpeedOfSound);
474 dopplerShift = ((speedOfSound - dopplerFactor * listenerProjection) / (speedOfSound - dopplerFactor * sourceProjection));
475 fixNANs(dopplerShift); // avoid illegal values
477 // Limit the pitch shifting to 4 octaves up and 3 octaves down.
478 if (dopplerShift > 16.0)
480 else if (dopplerShift < 0.125)
481 dopplerShift = 0.125;
488 float PannerNode::calculateDistanceConeGain()
490 FloatPoint3D listenerPosition = listener()->position();
492 double listenerDistance = m_position.distanceTo(listenerPosition);
493 double distanceGain = m_distanceEffect.gain(listenerDistance);
494 double coneGain = m_coneEffect.gain(m_position, m_orientation, listenerPosition);
496 return float(distanceGain * coneGain);
499 void PannerNode::azimuthElevation(double* outAzimuth, double* outElevation)
501 ASSERT(context()->isAudioThread());
503 if (isAzimuthElevationDirty()) {
504 calculateAzimuthElevation(&m_cachedAzimuth, &m_cachedElevation);
505 m_isAzimuthElevationDirty = false;
508 *outAzimuth = m_cachedAzimuth;
509 *outElevation = m_cachedElevation;
512 double PannerNode::dopplerRate()
514 ASSERT(context()->isAudioThread());
516 if (isDopplerRateDirty()) {
517 m_cachedDopplerRate = calculateDopplerRate();
518 m_isDopplerRateDirty = false;
521 return m_cachedDopplerRate;
524 float PannerNode::distanceConeGain()
526 ASSERT(context()->isAudioThread());
528 if (isDistanceConeGainDirty()) {
529 m_cachedDistanceConeGain = calculateDistanceConeGain();
530 m_isDistanceConeGainDirty = false;
533 return m_cachedDistanceConeGain;
536 void PannerNode::markPannerAsDirty(unsigned dirty)
538 if (dirty & PannerNode::AzimuthElevationDirty)
539 m_isAzimuthElevationDirty = true;
541 if (dirty & PannerNode::DistanceConeGainDirty)
542 m_isDistanceConeGainDirty = true;
544 if (dirty & PannerNode::DopplerRateDirty)
545 m_isDopplerRateDirty = true;
548 void PannerNode::notifyAudioSourcesConnectedToNode(AudioNode* node, HashMap<AudioNode*, bool>& visitedNodes)
554 // First check if this node is an AudioBufferSourceNode. If so, let it know about us so that doppler shift pitch can be taken into account.
555 if (node->nodeType() == NodeTypeAudioBufferSource) {
556 AudioBufferSourceNode* bufferSourceNode = static_cast<AudioBufferSourceNode*>(node);
557 bufferSourceNode->setPannerNode(this);
559 // Go through all inputs to this node.
560 for (unsigned i = 0; i < node->numberOfInputs(); ++i) {
561 AudioNodeInput* input = node->input(i);
563 // For each input, go through all of its connections, looking for AudioBufferSourceNodes.
564 for (unsigned j = 0; j < input->numberOfRenderingConnections(); ++j) {
565 AudioNodeOutput* connectedOutput = input->renderingOutput(j);
566 AudioNode* connectedNode = connectedOutput->node();
567 HashMap<AudioNode*, bool>::iterator iterator = visitedNodes.find(connectedNode);
569 // If we've seen this node already, we don't need to process it again. Otherwise,
570 // mark it as visited and recurse through the node looking for sources.
571 if (iterator == visitedNodes.end()) {
572 visitedNodes.set(connectedNode, true);
573 notifyAudioSourcesConnectedToNode(connectedNode, visitedNodes); // recurse
580 } // namespace WebCore
582 #endif // ENABLE(WEB_AUDIO)