content/media/webaudio/AudioListener.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/webaudio/AudioListener.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "AudioListener.h"
    1.11 +#include "AudioContext.h"
    1.12 +#include "mozilla/dom/AudioListenerBinding.h"
    1.13 +
    1.14 +namespace mozilla {
    1.15 +namespace dom {
    1.16 +
    1.17 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(AudioListener, mContext)
    1.18 +
    1.19 +NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AudioListener, AddRef)
    1.20 +NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AudioListener, Release)
    1.21 +
    1.22 +AudioListener::AudioListener(AudioContext* aContext)
    1.23 +  : mContext(aContext)
    1.24 +  , mPosition()
    1.25 +  , mFrontVector(0., 0., -1.)
    1.26 +  , mRightVector(1., 0., 0.)
    1.27 +  , mVelocity()
    1.28 +  , mDopplerFactor(1.)
    1.29 +  , mSpeedOfSound(343.3) // meters/second
    1.30 +{
    1.31 +  MOZ_ASSERT(aContext);
    1.32 +  SetIsDOMBinding();
    1.33 +}
    1.34 +
    1.35 +JSObject*
    1.36 +AudioListener::WrapObject(JSContext* aCx)
    1.37 +{
    1.38 +  return AudioListenerBinding::Wrap(aCx, this);
    1.39 +}
    1.40 +
    1.41 +void
    1.42 +AudioListener::SetOrientation(double aX, double aY, double aZ,
    1.43 +                              double aXUp, double aYUp, double aZUp)
    1.44 +{
    1.45 +  ThreeDPoint front(aX, aY, aZ);
    1.46 +  // The panning effect and the azimuth and elevation calculation in the Web
    1.47 +  // Audio spec becomes undefined with linearly dependent vectors, so keep
    1.48 +  // existing state in these situations.
    1.49 +  if (front.IsZero()) {
    1.50 +    return;
    1.51 +  }
    1.52 +  // Normalize before using CrossProduct() to avoid overflow.
    1.53 +  front.Normalize();
    1.54 +  ThreeDPoint up(aXUp, aYUp, aZUp);
    1.55 +  if (up.IsZero()) {
    1.56 +    return;
    1.57 +  }
    1.58 +  up.Normalize();
    1.59 +  ThreeDPoint right = front.CrossProduct(up);
    1.60 +  if (right.IsZero()) {
    1.61 +    return;
    1.62 +  }
    1.63 +  right.Normalize();
    1.64 +
    1.65 +  if (!mFrontVector.FuzzyEqual(front)) {
    1.66 +    mFrontVector = front;
    1.67 +    SendThreeDPointParameterToStream(PannerNode::LISTENER_FRONT_VECTOR, front);
    1.68 +  }
    1.69 +  if (!mRightVector.FuzzyEqual(right)) {
    1.70 +    mRightVector = right;
    1.71 +    SendThreeDPointParameterToStream(PannerNode::LISTENER_RIGHT_VECTOR, right);
    1.72 +  }
    1.73 +}
    1.74 +
    1.75 +void
    1.76 +AudioListener::RegisterPannerNode(PannerNode* aPannerNode)
    1.77 +{
    1.78 +  mPanners.AppendElement(aPannerNode->asWeakPtr());
    1.79 +
    1.80 +  // Let the panner node know about our parameters
    1.81 +  aPannerNode->SendThreeDPointParameterToStream(PannerNode::LISTENER_POSITION, mPosition);
    1.82 +  aPannerNode->SendThreeDPointParameterToStream(PannerNode::LISTENER_FRONT_VECTOR, mFrontVector);
    1.83 +  aPannerNode->SendThreeDPointParameterToStream(PannerNode::LISTENER_RIGHT_VECTOR, mRightVector);
    1.84 +  aPannerNode->SendThreeDPointParameterToStream(PannerNode::LISTENER_VELOCITY, mVelocity);
    1.85 +  aPannerNode->SendDoubleParameterToStream(PannerNode::LISTENER_DOPPLER_FACTOR, mDopplerFactor);
    1.86 +  aPannerNode->SendDoubleParameterToStream(PannerNode::LISTENER_SPEED_OF_SOUND, mSpeedOfSound);
    1.87 +  UpdatePannersVelocity();
    1.88 +}
    1.89 +
    1.90 +void AudioListener::UnregisterPannerNode(PannerNode* aPannerNode)
    1.91 +{
    1.92 +  mPanners.RemoveElement(aPannerNode);
    1.93 +}
    1.94 +
    1.95 +void
    1.96 +AudioListener::SendDoubleParameterToStream(uint32_t aIndex, double aValue)
    1.97 +{
    1.98 +  for (uint32_t i = 0; i < mPanners.Length(); ++i) {
    1.99 +    if (mPanners[i]) {
   1.100 +      mPanners[i]->SendDoubleParameterToStream(aIndex, aValue);
   1.101 +    }
   1.102 +  }
   1.103 +}
   1.104 +
   1.105 +void
   1.106 +AudioListener::SendThreeDPointParameterToStream(uint32_t aIndex, const ThreeDPoint& aValue)
   1.107 +{
   1.108 +  for (uint32_t i = 0; i < mPanners.Length(); ++i) {
   1.109 +    if (mPanners[i]) {
   1.110 +      mPanners[i]->SendThreeDPointParameterToStream(aIndex, aValue);
   1.111 +    }
   1.112 +  }
   1.113 +}
   1.114 +
   1.115 +void AudioListener::UpdatePannersVelocity()
   1.116 +{
   1.117 +  for (uint32_t i = 0; i < mPanners.Length(); ++i) {
   1.118 +    if (mPanners[i]) {
   1.119 +      mPanners[i]->SendDopplerToSourcesIfNeeded();
   1.120 +    }
   1.121 +  }
   1.122 +}
   1.123 +
   1.124 +size_t
   1.125 +AudioListener::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
   1.126 +{
   1.127 +  size_t amount = aMallocSizeOf(this);
   1.128 +  // AudioNodes are tracked separately
   1.129 +  amount += mPanners.SizeOfExcludingThis(aMallocSizeOf);
   1.130 +  return amount;
   1.131 +}
   1.132 +
   1.133 +}
   1.134 +}
   1.135 +

mercurial