2012-11-06 06:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "PannerNode.h"
|
2013-03-12 22:34:55 +04:00
|
|
|
#include "AudioNodeEngine.h"
|
|
|
|
#include "AudioNodeStream.h"
|
|
|
|
#include "AudioListener.h"
|
2014-11-19 20:15:13 +03:00
|
|
|
#include "PanningUtils.h"
|
2013-04-11 16:47:57 +04:00
|
|
|
#include "AudioBufferSourceNode.h"
|
2013-10-25 05:05:43 +04:00
|
|
|
#include "PlayingRefChangeHandler.h"
|
2013-08-09 02:08:06 +04:00
|
|
|
#include "blink/HRTFPanner.h"
|
2013-08-15 23:44:14 +04:00
|
|
|
#include "blink/HRTFDatabaseLoader.h"
|
2013-08-09 02:08:06 +04:00
|
|
|
|
|
|
|
using WebCore::HRTFDatabaseLoader;
|
|
|
|
using WebCore::HRTFPanner;
|
2012-11-06 06:14:13 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-03-21 20:45:53 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PannerNode)
|
|
|
|
|
2013-04-29 22:06:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(PannerNode)
|
|
|
|
if (tmp->Context()) {
|
|
|
|
tmp->Context()->UnregisterPannerNode(tmp);
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(AudioNode)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PannerNode, AudioNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(PannerNode)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(AudioNode)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(PannerNode, AudioNode)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(PannerNode, AudioNode)
|
|
|
|
|
2015-04-28 09:42:00 +03:00
|
|
|
class PannerNodeEngine final : public AudioNodeEngine
|
2013-03-12 22:34:55 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-04-20 20:16:28 +04:00
|
|
|
explicit PannerNodeEngine(AudioNode* aNode)
|
|
|
|
: AudioNodeEngine(aNode)
|
2013-03-12 22:34:55 +04:00
|
|
|
// Please keep these default values consistent with PannerNode::PannerNode below.
|
2015-02-03 13:25:37 +03:00
|
|
|
, mPanningModelFunction(&PannerNodeEngine::EqualPowerPanningFunction)
|
2013-03-21 20:45:53 +04:00
|
|
|
, mDistanceModelFunction(&PannerNodeEngine::InverseGainFunction)
|
2013-03-12 22:34:55 +04:00
|
|
|
, mPosition()
|
|
|
|
, mOrientation(1., 0., 0.)
|
|
|
|
, mVelocity()
|
|
|
|
, mRefDistance(1.)
|
|
|
|
, mMaxDistance(10000.)
|
|
|
|
, mRolloffFactor(1.)
|
|
|
|
, mConeInnerAngle(360.)
|
|
|
|
, mConeOuterAngle(360.)
|
|
|
|
, mConeOuterGain(0.)
|
|
|
|
// These will be initialized when a PannerNode is created, so just initialize them
|
|
|
|
// to some dummy values here.
|
|
|
|
, mListenerDopplerFactor(0.)
|
|
|
|
, mListenerSpeedOfSound(0.)
|
2013-10-25 05:05:43 +04:00
|
|
|
, mLeftOverData(INT_MIN)
|
2013-03-12 22:34:55 +04:00
|
|
|
{
|
2013-08-09 02:08:06 +04:00
|
|
|
// HRTFDatabaseLoader needs to be fetched on the main thread.
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<HRTFDatabaseLoader> loader =
|
2013-08-09 02:08:06 +04:00
|
|
|
HRTFDatabaseLoader::createAndLoadAsynchronouslyIfNecessary(aNode->Context()->SampleRate());
|
2015-06-17 17:00:52 +03:00
|
|
|
mHRTFPanner = new HRTFPanner(aNode->Context()->SampleRate(), Move(loader));
|
2013-03-12 22:34:55 +04:00
|
|
|
}
|
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
void SetInt32Parameter(uint32_t aIndex, int32_t aParam) override
|
2013-03-12 22:34:55 +04:00
|
|
|
{
|
|
|
|
switch (aIndex) {
|
|
|
|
case PannerNode::PANNING_MODEL:
|
2013-10-25 05:05:43 +04:00
|
|
|
switch (PanningModelType(aParam)) {
|
2013-05-06 23:28:13 +04:00
|
|
|
case PanningModelType::Equalpower:
|
2013-03-21 20:45:53 +04:00
|
|
|
mPanningModelFunction = &PannerNodeEngine::EqualPowerPanningFunction;
|
|
|
|
break;
|
2013-05-06 23:28:13 +04:00
|
|
|
case PanningModelType::HRTF:
|
2013-03-21 20:45:53 +04:00
|
|
|
mPanningModelFunction = &PannerNodeEngine::HRTFPanningFunction;
|
|
|
|
break;
|
2013-06-04 03:22:48 +04:00
|
|
|
default:
|
|
|
|
NS_NOTREACHED("We should never see the alternate names here");
|
|
|
|
break;
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
2013-03-12 22:34:55 +04:00
|
|
|
break;
|
|
|
|
case PannerNode::DISTANCE_MODEL:
|
2013-10-25 05:05:43 +04:00
|
|
|
switch (DistanceModelType(aParam)) {
|
2013-05-06 23:28:13 +04:00
|
|
|
case DistanceModelType::Inverse:
|
2013-03-21 20:45:53 +04:00
|
|
|
mDistanceModelFunction = &PannerNodeEngine::InverseGainFunction;
|
|
|
|
break;
|
2013-05-06 23:28:13 +04:00
|
|
|
case DistanceModelType::Linear:
|
2013-03-21 20:45:53 +04:00
|
|
|
mDistanceModelFunction = &PannerNodeEngine::LinearGainFunction;
|
|
|
|
break;
|
2013-05-06 23:28:13 +04:00
|
|
|
case DistanceModelType::Exponential:
|
2013-03-21 20:45:53 +04:00
|
|
|
mDistanceModelFunction = &PannerNodeEngine::ExponentialGainFunction;
|
|
|
|
break;
|
2013-06-04 03:22:48 +04:00
|
|
|
default:
|
|
|
|
NS_NOTREACHED("We should never see the alternate names here");
|
|
|
|
break;
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
2013-03-12 22:34:55 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Bad PannerNodeEngine Int32Parameter");
|
|
|
|
}
|
|
|
|
}
|
2016-01-18 06:22:51 +03:00
|
|
|
void SetThreeDPointParameter(uint32_t aIndex, const ThreeDPoint& aParam) override
|
2013-03-12 22:34:55 +04:00
|
|
|
{
|
|
|
|
switch (aIndex) {
|
|
|
|
case PannerNode::LISTENER_POSITION: mListenerPosition = aParam; break;
|
2013-09-04 13:20:59 +04:00
|
|
|
case PannerNode::LISTENER_FRONT_VECTOR: mListenerFrontVector = aParam; break;
|
|
|
|
case PannerNode::LISTENER_RIGHT_VECTOR: mListenerRightVector = aParam; break;
|
2013-03-12 22:34:55 +04:00
|
|
|
case PannerNode::LISTENER_VELOCITY: mListenerVelocity = aParam; break;
|
|
|
|
case PannerNode::POSITION: mPosition = aParam; break;
|
|
|
|
case PannerNode::ORIENTATION: mOrientation = aParam; break;
|
|
|
|
case PannerNode::VELOCITY: mVelocity = aParam; break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Bad PannerNodeEngine ThreeDPointParameter");
|
|
|
|
}
|
|
|
|
}
|
2016-01-18 06:22:51 +03:00
|
|
|
void SetDoubleParameter(uint32_t aIndex, double aParam) override
|
2013-03-12 22:34:55 +04:00
|
|
|
{
|
|
|
|
switch (aIndex) {
|
|
|
|
case PannerNode::LISTENER_DOPPLER_FACTOR: mListenerDopplerFactor = aParam; break;
|
|
|
|
case PannerNode::LISTENER_SPEED_OF_SOUND: mListenerSpeedOfSound = aParam; break;
|
|
|
|
case PannerNode::REF_DISTANCE: mRefDistance = aParam; break;
|
|
|
|
case PannerNode::MAX_DISTANCE: mMaxDistance = aParam; break;
|
|
|
|
case PannerNode::ROLLOFF_FACTOR: mRolloffFactor = aParam; break;
|
|
|
|
case PannerNode::CONE_INNER_ANGLE: mConeInnerAngle = aParam; break;
|
|
|
|
case PannerNode::CONE_OUTER_ANGLE: mConeOuterAngle = aParam; break;
|
|
|
|
case PannerNode::CONE_OUTER_GAIN: mConeOuterGain = aParam; break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Bad PannerNodeEngine DoubleParameter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
void ProcessBlock(AudioNodeStream* aStream,
|
|
|
|
GraphTime aFrom,
|
|
|
|
const AudioBlock& aInput,
|
|
|
|
AudioBlock* aOutput,
|
|
|
|
bool *aFinished) override
|
2013-03-12 22:34:55 +04:00
|
|
|
{
|
2013-10-25 05:05:43 +04:00
|
|
|
if (aInput.IsNull()) {
|
|
|
|
// mLeftOverData != INT_MIN means that the panning model was HRTF and a
|
|
|
|
// tail-time reference was added. Even if the model is now equalpower,
|
|
|
|
// the reference will need to be removed.
|
2013-11-04 23:22:37 +04:00
|
|
|
if (mLeftOverData > 0 &&
|
|
|
|
mPanningModelFunction == &PannerNodeEngine::HRTFPanningFunction) {
|
2013-10-25 05:05:43 +04:00
|
|
|
mLeftOverData -= WEBAUDIO_BLOCK_SIZE;
|
|
|
|
} else {
|
|
|
|
if (mLeftOverData != INT_MIN) {
|
|
|
|
mLeftOverData = INT_MIN;
|
2015-10-22 23:37:45 +03:00
|
|
|
aStream->ScheduleCheckForInactive();
|
2013-10-25 05:05:43 +04:00
|
|
|
mHRTFPanner->reset();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PlayingRefChangeHandler> refchanged =
|
2013-10-25 05:05:43 +04:00
|
|
|
new PlayingRefChangeHandler(aStream, PlayingRefChangeHandler::RELEASE);
|
|
|
|
aStream->Graph()->
|
|
|
|
DispatchToMainThreadAfterStreamStateUpdate(refchanged.forget());
|
|
|
|
}
|
2015-09-17 15:03:00 +03:00
|
|
|
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
|
2013-10-25 05:05:43 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (mPanningModelFunction == &PannerNodeEngine::HRTFPanningFunction) {
|
|
|
|
if (mLeftOverData == INT_MIN) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PlayingRefChangeHandler> refchanged =
|
2013-10-25 05:05:43 +04:00
|
|
|
new PlayingRefChangeHandler(aStream, PlayingRefChangeHandler::ADDREF);
|
|
|
|
aStream->Graph()->
|
|
|
|
DispatchToMainThreadAfterStreamStateUpdate(refchanged.forget());
|
|
|
|
}
|
|
|
|
mLeftOverData = mHRTFPanner->maxTailFrames();
|
|
|
|
}
|
|
|
|
|
2013-03-21 20:45:53 +04:00
|
|
|
(this->*mPanningModelFunction)(aInput, aOutput);
|
2013-03-12 22:34:55 +04:00
|
|
|
}
|
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
bool IsActive() const override
|
2015-09-08 23:54:03 +03:00
|
|
|
{
|
|
|
|
return mLeftOverData != INT_MIN;
|
|
|
|
}
|
|
|
|
|
2013-03-21 20:45:53 +04:00
|
|
|
void ComputeAzimuthAndElevation(float& aAzimuth, float& aElevation);
|
2013-03-21 22:25:15 +04:00
|
|
|
float ComputeConeGain();
|
2013-08-09 02:08:06 +04:00
|
|
|
// Compute how much the distance contributes to the gain reduction.
|
|
|
|
float ComputeDistanceGain();
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2015-09-03 10:01:50 +03:00
|
|
|
void EqualPowerPanningFunction(const AudioBlock& aInput, AudioBlock* aOutput);
|
|
|
|
void HRTFPanningFunction(const AudioBlock& aInput, AudioBlock* aOutput);
|
2013-03-21 20:45:53 +04:00
|
|
|
|
|
|
|
float LinearGainFunction(float aDistance);
|
|
|
|
float InverseGainFunction(float aDistance);
|
|
|
|
float ExponentialGainFunction(float aDistance);
|
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override
|
2014-04-13 22:08:10 +04:00
|
|
|
{
|
|
|
|
size_t amount = AudioNodeEngine::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
if (mHRTFPanner) {
|
|
|
|
amount += mHRTFPanner->sizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
|
2014-04-13 22:08:10 +04:00
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:08:06 +04:00
|
|
|
nsAutoPtr<HRTFPanner> mHRTFPanner;
|
2015-09-03 10:01:50 +03:00
|
|
|
typedef void (PannerNodeEngine::*PanningModelFunction)(const AudioBlock& aInput, AudioBlock* aOutput);
|
2013-03-21 20:45:53 +04:00
|
|
|
PanningModelFunction mPanningModelFunction;
|
|
|
|
typedef float (PannerNodeEngine::*DistanceModelFunction)(float aDistance);
|
|
|
|
DistanceModelFunction mDistanceModelFunction;
|
2013-03-12 22:34:55 +04:00
|
|
|
ThreeDPoint mPosition;
|
|
|
|
ThreeDPoint mOrientation;
|
|
|
|
ThreeDPoint mVelocity;
|
|
|
|
double mRefDistance;
|
|
|
|
double mMaxDistance;
|
|
|
|
double mRolloffFactor;
|
|
|
|
double mConeInnerAngle;
|
|
|
|
double mConeOuterAngle;
|
|
|
|
double mConeOuterGain;
|
|
|
|
ThreeDPoint mListenerPosition;
|
2013-09-04 13:20:59 +04:00
|
|
|
ThreeDPoint mListenerFrontVector;
|
|
|
|
ThreeDPoint mListenerRightVector;
|
2013-03-12 22:34:55 +04:00
|
|
|
ThreeDPoint mListenerVelocity;
|
|
|
|
double mListenerDopplerFactor;
|
|
|
|
double mListenerSpeedOfSound;
|
2013-10-25 05:05:43 +04:00
|
|
|
int mLeftOverData;
|
2013-03-12 22:34:55 +04:00
|
|
|
};
|
|
|
|
|
2012-11-06 06:14:13 +04:00
|
|
|
PannerNode::PannerNode(AudioContext* aContext)
|
2013-04-28 02:44:50 +04:00
|
|
|
: AudioNode(aContext,
|
|
|
|
2,
|
|
|
|
ChannelCountMode::Clamped_max,
|
|
|
|
ChannelInterpretation::Speakers)
|
2013-03-12 22:34:55 +04:00
|
|
|
// Please keep these default values consistent with PannerNodeEngine::PannerNodeEngine above.
|
2015-02-03 13:25:37 +03:00
|
|
|
, mPanningModel(PanningModelType::Equalpower)
|
2013-05-06 23:28:13 +04:00
|
|
|
, mDistanceModel(DistanceModelType::Inverse)
|
2012-11-06 06:14:13 +04:00
|
|
|
, mPosition()
|
2013-03-12 03:09:27 +04:00
|
|
|
, mOrientation(1., 0., 0.)
|
2012-11-06 06:14:13 +04:00
|
|
|
, mVelocity()
|
2013-03-12 03:09:27 +04:00
|
|
|
, mRefDistance(1.)
|
|
|
|
, mMaxDistance(10000.)
|
|
|
|
, mRolloffFactor(1.)
|
|
|
|
, mConeInnerAngle(360.)
|
|
|
|
, mConeOuterAngle(360.)
|
|
|
|
, mConeOuterGain(0.)
|
2012-11-06 06:14:13 +04:00
|
|
|
{
|
2015-09-08 16:22:16 +03:00
|
|
|
mStream = AudioNodeStream::Create(aContext,
|
2015-08-12 02:26:24 +03:00
|
|
|
new PannerNodeEngine(this),
|
2015-08-13 07:13:34 +03:00
|
|
|
AudioNodeStream::NO_STREAM_FLAGS);
|
2013-03-12 22:34:55 +04:00
|
|
|
// We should register once we have set up our stream and engine.
|
|
|
|
Context()->Listener()->RegisterPannerNode(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
PannerNode::~PannerNode()
|
|
|
|
{
|
2013-04-15 05:52:55 +04:00
|
|
|
if (Context()) {
|
|
|
|
Context()->UnregisterPannerNode(this);
|
|
|
|
}
|
2012-11-06 06:14:13 +04:00
|
|
|
}
|
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
size_t
|
|
|
|
PannerNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
|
2015-07-29 09:24:24 +03:00
|
|
|
amount += mSources.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-04-13 22:08:10 +04:00
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
PannerNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-11-06 06:14:13 +04:00
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
PannerNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2012-11-06 06:14:13 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return PannerNodeBinding::Wrap(aCx, this, aGivenProto);
|
2012-11-06 06:14:13 +04:00
|
|
|
}
|
|
|
|
|
2013-07-16 16:45:41 +04:00
|
|
|
void PannerNode::DestroyMediaStream()
|
|
|
|
{
|
|
|
|
if (Context()) {
|
|
|
|
Context()->UnregisterPannerNode(this);
|
|
|
|
}
|
|
|
|
AudioNode::DestroyMediaStream();
|
|
|
|
}
|
|
|
|
|
2013-03-21 20:45:53 +04:00
|
|
|
// Those three functions are described in the spec.
|
|
|
|
float
|
|
|
|
PannerNodeEngine::LinearGainFunction(float aDistance)
|
|
|
|
{
|
|
|
|
return 1 - mRolloffFactor * (aDistance - mRefDistance) / (mMaxDistance - mRefDistance);
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
PannerNodeEngine::InverseGainFunction(float aDistance)
|
|
|
|
{
|
|
|
|
return mRefDistance / (mRefDistance + mRolloffFactor * (aDistance - mRefDistance));
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
PannerNodeEngine::ExponentialGainFunction(float aDistance)
|
|
|
|
{
|
|
|
|
return pow(aDistance / mRefDistance, -mRolloffFactor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-03 10:01:50 +03:00
|
|
|
PannerNodeEngine::HRTFPanningFunction(const AudioBlock& aInput,
|
|
|
|
AudioBlock* aOutput)
|
2013-03-21 20:45:53 +04:00
|
|
|
{
|
2013-08-09 02:08:06 +04:00
|
|
|
// The output of this node is always stereo, no matter what the inputs are.
|
2015-09-03 10:01:50 +03:00
|
|
|
aOutput->AllocateChannels(2);
|
2013-08-09 02:08:06 +04:00
|
|
|
|
|
|
|
float azimuth, elevation;
|
|
|
|
ComputeAzimuthAndElevation(azimuth, elevation);
|
|
|
|
|
2015-09-03 10:01:50 +03:00
|
|
|
AudioBlock input = aInput;
|
2015-01-14 19:11:06 +03:00
|
|
|
// Gain is applied before the delay and convolution of the HRTF.
|
2014-03-05 01:06:57 +04:00
|
|
|
input.mVolume *= ComputeConeGain() * ComputeDistanceGain();
|
2013-08-09 02:08:06 +04:00
|
|
|
|
2014-03-03 03:49:45 +04:00
|
|
|
mHRTFPanner->pan(azimuth, elevation, &input, aOutput);
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-03 10:01:50 +03:00
|
|
|
PannerNodeEngine::EqualPowerPanningFunction(const AudioBlock& aInput,
|
|
|
|
AudioBlock* aOutput)
|
2013-03-21 20:45:53 +04:00
|
|
|
{
|
2013-08-09 02:08:06 +04:00
|
|
|
float azimuth, elevation, gainL, gainR, normalizedAzimuth, distanceGain, coneGain;
|
2015-09-03 08:30:16 +03:00
|
|
|
int inputChannels = aInput.ChannelCount();
|
2013-03-21 20:45:53 +04:00
|
|
|
|
|
|
|
// If both the listener are in the same spot, and no cone gain is specified,
|
|
|
|
// this node is noop.
|
|
|
|
if (mListenerPosition == mPosition &&
|
|
|
|
mConeInnerAngle == 360 &&
|
|
|
|
mConeOuterAngle == 360) {
|
|
|
|
*aOutput = aInput;
|
2013-03-22 23:32:24 +04:00
|
|
|
return;
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The output of this node is always stereo, no matter what the inputs are.
|
2015-09-03 10:01:50 +03:00
|
|
|
aOutput->AllocateChannels(2);
|
2013-03-21 20:45:53 +04:00
|
|
|
|
|
|
|
ComputeAzimuthAndElevation(azimuth, elevation);
|
2013-03-21 22:25:15 +04:00
|
|
|
coneGain = ComputeConeGain();
|
2013-03-21 20:45:53 +04:00
|
|
|
|
|
|
|
// The following algorithm is described in the spec.
|
|
|
|
// Clamp azimuth in the [-90, 90] range.
|
|
|
|
azimuth = min(180.f, max(-180.f, azimuth));
|
|
|
|
|
|
|
|
// Wrap around
|
|
|
|
if (azimuth < -90.f) {
|
|
|
|
azimuth = -180.f - azimuth;
|
|
|
|
} else if (azimuth > 90) {
|
|
|
|
azimuth = 180.f - azimuth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize the value in the [0, 1] range.
|
|
|
|
if (inputChannels == 1) {
|
|
|
|
normalizedAzimuth = (azimuth + 90.f) / 180.f;
|
|
|
|
} else {
|
|
|
|
if (azimuth <= 0) {
|
|
|
|
normalizedAzimuth = (azimuth + 90.f) / 90.f;
|
|
|
|
} else {
|
|
|
|
normalizedAzimuth = azimuth / 90.f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:08:06 +04:00
|
|
|
distanceGain = ComputeDistanceGain();
|
2013-03-21 20:45:53 +04:00
|
|
|
|
|
|
|
// Actually compute the left and right gain.
|
2014-01-28 09:14:25 +04:00
|
|
|
gainL = cos(0.5 * M_PI * normalizedAzimuth);
|
|
|
|
gainR = sin(0.5 * M_PI * normalizedAzimuth);
|
2013-03-21 20:45:53 +04:00
|
|
|
|
|
|
|
// Compute the output.
|
2014-11-19 20:15:13 +03:00
|
|
|
ApplyStereoPanning(aInput, aOutput, gainL, gainR, azimuth <= 0);
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2014-01-28 09:14:25 +04:00
|
|
|
aOutput->mVolume = aInput.mVolume * distanceGain * coneGain;
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
|
|
|
|
2013-09-04 13:20:59 +04:00
|
|
|
// This algorithm is specified in the webaudio spec.
|
2013-03-21 20:45:53 +04:00
|
|
|
void
|
|
|
|
PannerNodeEngine::ComputeAzimuthAndElevation(float& aAzimuth, float& aElevation)
|
|
|
|
{
|
|
|
|
ThreeDPoint sourceListener = mPosition - mListenerPosition;
|
|
|
|
|
|
|
|
if (sourceListener.IsZero()) {
|
|
|
|
aAzimuth = 0.0;
|
|
|
|
aElevation = 0.0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceListener.Normalize();
|
|
|
|
|
|
|
|
// Project the source-listener vector on the x-z plane.
|
2013-09-04 13:20:59 +04:00
|
|
|
const ThreeDPoint& listenerFront = mListenerFrontVector;
|
|
|
|
const ThreeDPoint& listenerRight = mListenerRightVector;
|
|
|
|
ThreeDPoint up = listenerRight.CrossProduct(listenerFront);
|
2013-03-21 20:45:53 +04:00
|
|
|
|
|
|
|
double upProjection = sourceListener.DotProduct(up);
|
2013-09-04 13:20:59 +04:00
|
|
|
aElevation = 90 - 180 * acos(upProjection) / M_PI;
|
|
|
|
|
|
|
|
if (aElevation > 90) {
|
|
|
|
aElevation = 180 - aElevation;
|
|
|
|
} else if (aElevation < -90) {
|
|
|
|
aElevation = -180 - aElevation;
|
|
|
|
}
|
2013-03-21 20:45:53 +04:00
|
|
|
|
|
|
|
ThreeDPoint projectedSource = sourceListener - up * upProjection;
|
2013-09-04 13:20:59 +04:00
|
|
|
if (projectedSource.IsZero()) {
|
|
|
|
// source - listener direction is up or down.
|
|
|
|
aAzimuth = 0.0;
|
|
|
|
return;
|
|
|
|
}
|
2013-03-21 20:45:53 +04:00
|
|
|
projectedSource.Normalize();
|
|
|
|
|
|
|
|
// Actually compute the angle, and convert to degrees
|
2013-09-04 13:20:59 +04:00
|
|
|
double projection = projectedSource.DotProduct(listenerRight);
|
2013-03-21 20:45:53 +04:00
|
|
|
aAzimuth = 180 * acos(projection) / M_PI;
|
|
|
|
|
|
|
|
// Compute whether the source is in front or behind the listener.
|
2013-09-04 13:20:59 +04:00
|
|
|
double frontBack = projectedSource.DotProduct(listenerFront);
|
2013-03-21 20:45:53 +04:00
|
|
|
if (frontBack < 0) {
|
|
|
|
aAzimuth = 360 - aAzimuth;
|
|
|
|
}
|
|
|
|
// Rotate the azimuth so it is relative to the listener front vector instead
|
|
|
|
// of the right vector.
|
|
|
|
if ((aAzimuth >= 0) && (aAzimuth <= 270)) {
|
|
|
|
aAzimuth = 90 - aAzimuth;
|
|
|
|
} else {
|
|
|
|
aAzimuth = 450 - aAzimuth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 22:25:15 +04:00
|
|
|
// This algorithm is described in the WebAudio spec.
|
|
|
|
float
|
|
|
|
PannerNodeEngine::ComputeConeGain()
|
|
|
|
{
|
|
|
|
// Omnidirectional source
|
|
|
|
if (mOrientation.IsZero() || ((mConeInnerAngle == 360) && (mConeOuterAngle == 360))) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalized source-listener vector
|
|
|
|
ThreeDPoint sourceToListener = mListenerPosition - mPosition;
|
|
|
|
sourceToListener.Normalize();
|
|
|
|
|
|
|
|
// Angle between the source orientation vector and the source-listener vector
|
2013-09-04 13:20:59 +04:00
|
|
|
double dotProduct = sourceToListener.DotProduct(mOrientation);
|
2013-03-21 22:25:15 +04:00
|
|
|
double angle = 180 * acos(dotProduct) / M_PI;
|
|
|
|
double absAngle = fabs(angle);
|
|
|
|
|
|
|
|
// Divide by 2 here since API is entire angle (not half-angle)
|
|
|
|
double absInnerAngle = fabs(mConeInnerAngle) / 2;
|
|
|
|
double absOuterAngle = fabs(mConeOuterAngle) / 2;
|
|
|
|
double gain = 1;
|
|
|
|
|
|
|
|
if (absAngle <= absInnerAngle) {
|
|
|
|
// No attenuation
|
|
|
|
gain = 1;
|
|
|
|
} else if (absAngle >= absOuterAngle) {
|
|
|
|
// Max attenuation
|
|
|
|
gain = mConeOuterGain;
|
|
|
|
} else {
|
|
|
|
// Between inner and outer cones
|
|
|
|
// inner -> outer, x goes from 0 -> 1
|
|
|
|
double x = (absAngle - absInnerAngle) / (absOuterAngle - absInnerAngle);
|
|
|
|
gain = (1 - x) + mConeOuterGain * x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gain;
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:08:06 +04:00
|
|
|
float
|
|
|
|
PannerNodeEngine::ComputeDistanceGain()
|
|
|
|
{
|
|
|
|
ThreeDPoint distanceVec = mPosition - mListenerPosition;
|
|
|
|
float distance = sqrt(distanceVec.DotProduct(distanceVec));
|
2015-01-14 19:11:06 +03:00
|
|
|
return std::max(0.0f, (this->*mDistanceModelFunction)(distance));
|
2013-08-09 02:08:06 +04:00
|
|
|
}
|
|
|
|
|
2013-04-11 16:47:57 +04:00
|
|
|
float
|
|
|
|
PannerNode::ComputeDopplerShift()
|
|
|
|
{
|
|
|
|
double dopplerShift = 1.0; // Initialize to default value
|
|
|
|
|
|
|
|
AudioListener* listener = Context()->Listener();
|
|
|
|
|
|
|
|
if (listener->DopplerFactor() > 0) {
|
|
|
|
// Don't bother if both source and listener have no velocity.
|
|
|
|
if (!mVelocity.IsZero() || !listener->Velocity().IsZero()) {
|
|
|
|
// Calculate the source to listener vector.
|
|
|
|
ThreeDPoint sourceToListener = mPosition - listener->Velocity();
|
|
|
|
|
|
|
|
double sourceListenerMagnitude = sourceToListener.Magnitude();
|
|
|
|
|
|
|
|
double listenerProjection = sourceToListener.DotProduct(listener->Velocity()) / sourceListenerMagnitude;
|
|
|
|
double sourceProjection = sourceToListener.DotProduct(mVelocity) / sourceListenerMagnitude;
|
|
|
|
|
|
|
|
listenerProjection = -listenerProjection;
|
|
|
|
sourceProjection = -sourceProjection;
|
|
|
|
|
2015-04-08 18:18:54 +03:00
|
|
|
double scaledSpeedOfSound = listener->SpeedOfSound() / listener->DopplerFactor();
|
2013-04-11 16:47:57 +04:00
|
|
|
listenerProjection = min(listenerProjection, scaledSpeedOfSound);
|
|
|
|
sourceProjection = min(sourceProjection, scaledSpeedOfSound);
|
|
|
|
|
|
|
|
dopplerShift = ((listener->SpeedOfSound() - listener->DopplerFactor() * listenerProjection) / (listener->SpeedOfSound() - listener->DopplerFactor() * sourceProjection));
|
|
|
|
|
|
|
|
WebAudioUtils::FixNaN(dopplerShift); // Avoid illegal values
|
|
|
|
|
|
|
|
// Limit the pitch shifting to 4 octaves up and 3 octaves down.
|
|
|
|
dopplerShift = min(dopplerShift, 16.);
|
|
|
|
dopplerShift = max(dopplerShift, 0.125);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dopplerShift;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PannerNode::FindConnectedSources()
|
|
|
|
{
|
|
|
|
mSources.Clear();
|
|
|
|
std::set<AudioNode*> cycleSet;
|
|
|
|
FindConnectedSources(this, mSources, cycleSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PannerNode::FindConnectedSources(AudioNode* aNode,
|
|
|
|
nsTArray<AudioBufferSourceNode*>& aSources,
|
|
|
|
std::set<AudioNode*>& aNodesSeen)
|
|
|
|
{
|
|
|
|
if (!aNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsTArray<InputNode>& inputNodes = aNode->InputNodes();
|
|
|
|
|
|
|
|
for(unsigned i = 0; i < inputNodes.Length(); i++) {
|
|
|
|
// Return if we find a node that we have seen already.
|
|
|
|
if (aNodesSeen.find(inputNodes[i].mInputNode) != aNodesSeen.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
aNodesSeen.insert(inputNodes[i].mInputNode);
|
|
|
|
// Recurse
|
|
|
|
FindConnectedSources(inputNodes[i].mInputNode, aSources, aNodesSeen);
|
|
|
|
|
2015-07-02 06:43:52 +03:00
|
|
|
// Check if this node is an AudioBufferSourceNode that still have a stream,
|
|
|
|
// which means it has not finished playing.
|
2013-04-11 16:47:57 +04:00
|
|
|
AudioBufferSourceNode* node = inputNodes[i].mInputNode->AsAudioBufferSourceNode();
|
2015-07-02 08:30:36 +03:00
|
|
|
if (node && node->GetStream()) {
|
2013-04-11 16:47:57 +04:00
|
|
|
aSources.AppendElement(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PannerNode::SendDopplerToSourcesIfNeeded()
|
|
|
|
{
|
|
|
|
// Don't bother sending the doppler shift if both the source and the listener
|
|
|
|
// are not moving, because the doppler shift is going to be 1.0.
|
|
|
|
if (!(Context()->Listener()->Velocity().IsZero() && mVelocity.IsZero())) {
|
|
|
|
for(uint32_t i = 0; i < mSources.Length(); i++) {
|
|
|
|
mSources[i]->SendDopplerShiftToStream(ComputeDopplerShift());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2012-11-06 06:14:13 +04:00
|
|
|
|