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"
|
2016-10-13 17:41:24 +03:00
|
|
|
#include "AlignmentUtils.h"
|
|
|
|
#include "AudioDestinationNode.h"
|
2013-03-12 22:34:55 +04:00
|
|
|
#include "AudioNodeEngine.h"
|
2019-10-02 11:46:23 +03:00
|
|
|
#include "AudioNodeStream.h"
|
2013-03-12 22:34:55 +04:00
|
|
|
#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"
|
2016-06-07 23:10:18 +03:00
|
|
|
#include "nsAutoPtr.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-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PannerNode)
|
2016-06-23 20:42:12 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PannerNode, AudioNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPositionX, mPositionY, mPositionZ,
|
|
|
|
mOrientationX, mOrientationY, mOrientationZ)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2013-04-29 22:06:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PannerNode, AudioNode)
|
2016-06-23 20:42:12 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPositionX, mPositionY, mPositionZ,
|
|
|
|
mOrientationX, mOrientationY, mOrientationZ)
|
2013-04-29 22:06:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PannerNode)
|
2013-04-29 22:06:40 +04:00
|
|
|
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:
|
2018-07-17 13:01:24 +03:00
|
|
|
explicit PannerNodeEngine(AudioNode* aNode,
|
|
|
|
AudioDestinationNode* aDestination,
|
2018-07-23 17:47:44 +03:00
|
|
|
AudioListenerEngine* aListenerEngine)
|
2013-04-20 20:16:28 +04:00
|
|
|
: AudioNodeEngine(aNode),
|
2019-10-02 11:46:23 +03:00
|
|
|
mDestination(aDestination->Stream()),
|
2018-07-17 13:01:24 +03:00
|
|
|
mListenerEngine(aListenerEngine)
|
|
|
|
// 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),
|
2016-06-23 20:42:12 +03:00
|
|
|
mPositionX(0.),
|
|
|
|
mPositionY(0.),
|
|
|
|
mPositionZ(0.),
|
|
|
|
mOrientationX(1.),
|
|
|
|
mOrientationY(0.),
|
|
|
|
mOrientationZ(0.),
|
2013-03-12 22:34:55 +04:00
|
|
|
mRefDistance(1.),
|
|
|
|
mMaxDistance(10000.),
|
|
|
|
mRolloffFactor(1.),
|
|
|
|
mConeInnerAngle(360.),
|
|
|
|
mConeOuterAngle(360.),
|
|
|
|
mConeOuterGain(0.),
|
2013-10-25 05:05:43 +04:00
|
|
|
mLeftOverData(INT_MIN) {}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
void RecvTimelineEvent(uint32_t aIndex, AudioTimelineEvent& aEvent) override {
|
|
|
|
MOZ_ASSERT(mDestination);
|
|
|
|
WebAudioUtils::ConvertAudioTimelineEventToTicks(aEvent, mDestination);
|
|
|
|
switch (aIndex) {
|
|
|
|
case PannerNode::POSITIONX:
|
|
|
|
mPositionX.InsertEvent<int64_t>(aEvent);
|
|
|
|
break;
|
|
|
|
case PannerNode::POSITIONY:
|
|
|
|
mPositionY.InsertEvent<int64_t>(aEvent);
|
|
|
|
break;
|
|
|
|
case PannerNode::POSITIONZ:
|
|
|
|
mPositionZ.InsertEvent<int64_t>(aEvent);
|
|
|
|
break;
|
|
|
|
case PannerNode::ORIENTATIONX:
|
|
|
|
mOrientationX.InsertEvent<int64_t>(aEvent);
|
|
|
|
break;
|
|
|
|
case PannerNode::ORIENTATIONY:
|
|
|
|
mOrientationY.InsertEvent<int64_t>(aEvent);
|
|
|
|
break;
|
|
|
|
case PannerNode::ORIENTATIONZ:
|
|
|
|
mOrientationZ.InsertEvent<int64_t>(aEvent);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Bad PannerNode TimelineParameter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 18:15:57 +03:00
|
|
|
void CreateHRTFPanner() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mHRTFPanner) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-09 02:08:06 +04:00
|
|
|
// HRTFDatabaseLoader needs to be fetched on the main thread.
|
2018-09-21 23:45:49 +03:00
|
|
|
RefPtr<HRTFDatabaseLoader> loader =
|
2016-01-21 18:15:57 +03:00
|
|
|
HRTFDatabaseLoader::createAndLoadAsynchronouslyIfNecessary(
|
|
|
|
NodeMainThread()->Context()->SampleRate());
|
2018-09-21 23:45:49 +03:00
|
|
|
mHRTFPanner = new HRTFPanner(NodeMainThread()->Context()->SampleRate(),
|
|
|
|
loader.forget());
|
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:
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("We should never see alternate names here");
|
2013-06-04 03:22:48 +04:00
|
|
|
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:
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("We should never see alternate names here");
|
2013-06-04 03:22:48 +04:00
|
|
|
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 SetDoubleParameter(uint32_t aIndex, double aParam) override {
|
2013-03-12 22:34:55 +04:00
|
|
|
switch (aIndex) {
|
|
|
|
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;
|
2018-11-19 16:25:37 +03:00
|
|
|
break;
|
2013-03-12 22:34:55 +04:00
|
|
|
default:
|
|
|
|
NS_ERROR("Bad PannerNodeEngine DoubleParameter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 11:46:23 +03:00
|
|
|
void ProcessBlock(AudioNodeStream* aStream, GraphTime aFrom,
|
2016-01-18 06:22:51 +03:00
|
|
|
const AudioBlock& aInput, AudioBlock* aOutput,
|
|
|
|
bool* aFinished) override {
|
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;
|
2019-10-02 11:46:23 +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 =
|
2019-10-02 11:46:23 +03:00
|
|
|
new PlayingRefChangeHandler(aStream,
|
2013-10-25 05:05:43 +04:00
|
|
|
PlayingRefChangeHandler::RELEASE);
|
2019-10-02 11:46:23 +03:00
|
|
|
aStream->Graph()->DispatchToMainThreadStableState(
|
|
|
|
refchanged.forget());
|
2013-10-25 05:05:43 +04:00
|
|
|
}
|
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 =
|
2019-10-02 11:46:23 +03:00
|
|
|
new PlayingRefChangeHandler(aStream,
|
2013-10-25 05:05:43 +04:00
|
|
|
PlayingRefChangeHandler::ADDREF);
|
2019-10-02 11:46:23 +03:00
|
|
|
aStream->Graph()->DispatchToMainThreadStableState(refchanged.forget());
|
2013-10-25 05:05:43 +04:00
|
|
|
}
|
|
|
|
mLeftOverData = mHRTFPanner->maxTailFrames();
|
|
|
|
}
|
|
|
|
|
2019-10-02 11:46:23 +03:00
|
|
|
StreamTime tick = mDestination->GraphTimeToStreamTime(aFrom);
|
2016-06-23 20:42:12 +03:00
|
|
|
(this->*mPanningModelFunction)(aInput, aOutput, tick);
|
2013-03-12 22:34:55 +04:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:54:03 +03:00
|
|
|
bool IsActive() const override { return mLeftOverData != INT_MIN; }
|
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
void ComputeAzimuthAndElevation(const ThreeDPoint& position, float& aAzimuth,
|
|
|
|
float& aElevation);
|
|
|
|
float ComputeConeGain(const ThreeDPoint& position,
|
|
|
|
const ThreeDPoint& orientation);
|
2013-08-09 02:08:06 +04:00
|
|
|
// Compute how much the distance contributes to the gain reduction.
|
2016-08-16 18:07:12 +03:00
|
|
|
double ComputeDistanceGain(const ThreeDPoint& position);
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
void EqualPowerPanningFunction(const AudioBlock& aInput, AudioBlock* aOutput,
|
2019-10-02 11:46:23 +03:00
|
|
|
StreamTime tick);
|
2016-06-23 20:42:12 +03:00
|
|
|
void HRTFPanningFunction(const AudioBlock& aInput, AudioBlock* aOutput,
|
2019-10-02 11:46:23 +03:00
|
|
|
StreamTime tick);
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2016-08-16 18:07:12 +03:00
|
|
|
float LinearGainFunction(double aDistance);
|
|
|
|
float InverseGainFunction(double aDistance);
|
|
|
|
float ExponentialGainFunction(double aDistance);
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
ThreeDPoint ConvertAudioParamTimelineTo3DP(AudioParamTimeline& aX,
|
|
|
|
AudioParamTimeline& aY,
|
|
|
|
AudioParamTimeline& aZ,
|
2019-10-02 11:46:23 +03:00
|
|
|
StreamTime& tick);
|
2016-06-23 20:42:12 +03:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-10-02 11:46:23 +03:00
|
|
|
RefPtr<AudioNodeStream> mDestination;
|
2016-01-21 18:15:57 +03:00
|
|
|
// This member is set on the main thread, but is not accessed on the rendering
|
|
|
|
// thread untile mPanningModelFunction has changed, and this happens strictly
|
2019-10-02 11:46:23 +03:00
|
|
|
// later, via a MediaStreamGraph ControlMessage.
|
2013-08-09 02:08:06 +04:00
|
|
|
nsAutoPtr<HRTFPanner> mHRTFPanner;
|
2018-07-23 17:47:44 +03:00
|
|
|
RefPtr<AudioListenerEngine> mListenerEngine;
|
2016-06-23 20:42:12 +03:00
|
|
|
typedef void (PannerNodeEngine::*PanningModelFunction)(
|
2019-10-02 11:46:23 +03:00
|
|
|
const AudioBlock& aInput, AudioBlock* aOutput, StreamTime tick);
|
2013-03-21 20:45:53 +04:00
|
|
|
PanningModelFunction mPanningModelFunction;
|
2016-08-16 18:07:12 +03:00
|
|
|
typedef float (PannerNodeEngine::*DistanceModelFunction)(double aDistance);
|
2013-03-21 20:45:53 +04:00
|
|
|
DistanceModelFunction mDistanceModelFunction;
|
2016-06-23 20:42:12 +03:00
|
|
|
AudioParamTimeline mPositionX;
|
|
|
|
AudioParamTimeline mPositionY;
|
|
|
|
AudioParamTimeline mPositionZ;
|
|
|
|
AudioParamTimeline mOrientationX;
|
|
|
|
AudioParamTimeline mOrientationY;
|
|
|
|
AudioParamTimeline mOrientationZ;
|
2013-03-12 22:34:55 +04:00
|
|
|
double mRefDistance;
|
|
|
|
double mMaxDistance;
|
|
|
|
double mRolloffFactor;
|
|
|
|
double mConeInnerAngle;
|
|
|
|
double mConeOuterAngle;
|
|
|
|
double mConeOuterGain;
|
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),
|
2013-03-12 03:09:27 +04:00
|
|
|
mRefDistance(1.),
|
|
|
|
mMaxDistance(10000.),
|
|
|
|
mRolloffFactor(1.),
|
|
|
|
mConeInnerAngle(360.),
|
|
|
|
mConeOuterAngle(360.),
|
|
|
|
mConeOuterGain(0.) {
|
2019-04-23 20:50:54 +03:00
|
|
|
CreateAudioParam(mPositionX, PannerNode::POSITIONX, this->NodeType(), 0.f);
|
|
|
|
CreateAudioParam(mPositionY, PannerNode::POSITIONY, this->NodeType(), 0.f);
|
|
|
|
CreateAudioParam(mPositionZ, PannerNode::POSITIONZ, this->NodeType(), 0.f);
|
|
|
|
CreateAudioParam(mOrientationX, PannerNode::ORIENTATIONX, this->NodeType(),
|
|
|
|
1.0f);
|
|
|
|
CreateAudioParam(mOrientationY, PannerNode::ORIENTATIONY, this->NodeType(),
|
|
|
|
0.f);
|
|
|
|
CreateAudioParam(mOrientationZ, PannerNode::ORIENTATIONZ, this->NodeType(),
|
|
|
|
0.f);
|
2019-10-02 11:46:23 +03:00
|
|
|
mStream = AudioNodeStream::Create(
|
2018-07-17 13:01:24 +03:00
|
|
|
aContext,
|
|
|
|
new PannerNodeEngine(this, aContext->Destination(),
|
|
|
|
aContext->Listener()->Engine()),
|
2019-10-02 11:46:23 +03:00
|
|
|
AudioNodeStream::NO_STREAM_FLAGS, aContext->Graph());
|
2012-11-06 06:14:13 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<PannerNode> PannerNode::Create(AudioContext& aAudioContext,
|
|
|
|
const PannerOptions& aOptions,
|
|
|
|
ErrorResult& aRv) {
|
2016-12-15 21:24:42 +03:00
|
|
|
RefPtr<PannerNode> audioNode = new PannerNode(&aAudioContext);
|
|
|
|
|
|
|
|
audioNode->Initialize(aOptions, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
audioNode->SetPanningModel(aOptions.mPanningModel);
|
|
|
|
audioNode->SetDistanceModel(aOptions.mDistanceModel);
|
|
|
|
audioNode->SetPosition(aOptions.mPositionX, aOptions.mPositionY,
|
|
|
|
aOptions.mPositionZ);
|
|
|
|
audioNode->SetOrientation(aOptions.mOrientationX, aOptions.mOrientationY,
|
|
|
|
aOptions.mOrientationZ);
|
2018-09-05 15:30:28 +03:00
|
|
|
audioNode->SetRefDistance(aOptions.mRefDistance, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
audioNode->SetMaxDistance(aOptions.mMaxDistance, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
audioNode->SetRolloffFactor(aOptions.mRolloffFactor, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-12-15 21:24:42 +03:00
|
|
|
audioNode->SetConeInnerAngle(aOptions.mConeInnerAngle);
|
|
|
|
audioNode->SetConeOuterAngle(aOptions.mConeOuterAngle);
|
2018-09-05 15:30:28 +03:00
|
|
|
audioNode->SetConeOuterGain(aOptions.mConeOuterGain, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-12-15 21:24:42 +03:00
|
|
|
|
|
|
|
return audioNode.forget();
|
|
|
|
}
|
|
|
|
|
2016-01-21 18:15:57 +03:00
|
|
|
void PannerNode::SetPanningModel(PanningModelType aPanningModel) {
|
|
|
|
mPanningModel = aPanningModel;
|
|
|
|
if (mPanningModel == PanningModelType::HRTF) {
|
|
|
|
// We can set the engine's `mHRTFPanner` member here from the main thread,
|
2019-10-02 11:46:23 +03:00
|
|
|
// because the engine will not touch it from the MediaStreamGraph
|
2016-01-21 18:15:57 +03:00
|
|
|
// thread until the PANNING_MODEL message sent below is received.
|
2019-10-02 11:46:23 +03:00
|
|
|
static_cast<PannerNodeEngine*>(mStream->Engine())->CreateHRTFPanner();
|
2016-01-21 18:15:57 +03:00
|
|
|
}
|
2019-10-02 11:46:23 +03:00
|
|
|
SendInt32ParameterToStream(PANNING_MODEL, int32_t(mPanningModel));
|
2016-01-21 18:15:57 +03:00
|
|
|
}
|
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
size_t PannerNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
|
2018-07-19 12:19:29 +03:00
|
|
|
return AudioNode::SizeOfExcludingThis(aMallocSizeOf);
|
2014-04-13 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t PannerNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
JSObject* PannerNode::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return PannerNode_Binding::Wrap(aCx, this, aGivenProto);
|
2012-11-06 06:14:13 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 20:45:53 +04:00
|
|
|
// Those three functions are described in the spec.
|
2016-08-16 18:07:12 +03:00
|
|
|
float PannerNodeEngine::LinearGainFunction(double aDistance) {
|
|
|
|
return 1 - mRolloffFactor *
|
|
|
|
(std::max(std::min(aDistance, mMaxDistance), mRefDistance) -
|
|
|
|
mRefDistance) /
|
|
|
|
(mMaxDistance - mRefDistance);
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
|
|
|
|
2016-08-16 18:07:12 +03:00
|
|
|
float PannerNodeEngine::InverseGainFunction(double aDistance) {
|
|
|
|
return mRefDistance /
|
|
|
|
(mRefDistance +
|
|
|
|
mRolloffFactor * (std::max(aDistance, mRefDistance) - mRefDistance));
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
|
|
|
|
2016-08-16 18:07:12 +03:00
|
|
|
float PannerNodeEngine::ExponentialGainFunction(double aDistance) {
|
|
|
|
return pow(std::max(aDistance, mRefDistance) / mRefDistance, -mRolloffFactor);
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
|
|
|
|
2015-09-03 10:01:50 +03:00
|
|
|
void PannerNodeEngine::HRTFPanningFunction(const AudioBlock& aInput,
|
2016-06-23 20:42:12 +03:00
|
|
|
AudioBlock* aOutput,
|
2019-10-02 11:46:23 +03:00
|
|
|
StreamTime tick) {
|
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;
|
2016-06-23 20:42:12 +03:00
|
|
|
|
|
|
|
ThreeDPoint position =
|
|
|
|
ConvertAudioParamTimelineTo3DP(mPositionX, mPositionY, mPositionZ, tick);
|
|
|
|
ThreeDPoint orientation = ConvertAudioParamTimelineTo3DP(
|
|
|
|
mOrientationX, mOrientationY, mOrientationZ, tick);
|
|
|
|
if (!orientation.IsZero()) {
|
|
|
|
orientation.Normalize();
|
|
|
|
}
|
|
|
|
ComputeAzimuthAndElevation(position, azimuth, elevation);
|
2013-08-09 02:08:06 +04:00
|
|
|
|
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.
|
2016-06-23 20:42:12 +03:00
|
|
|
input.mVolume *=
|
|
|
|
ComputeConeGain(position, orientation) * ComputeDistanceGain(position);
|
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
|
|
|
}
|
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
ThreeDPoint PannerNodeEngine::ConvertAudioParamTimelineTo3DP(
|
|
|
|
AudioParamTimeline& aX, AudioParamTimeline& aY, AudioParamTimeline& aZ,
|
2019-10-02 11:46:23 +03:00
|
|
|
StreamTime& tick) {
|
2016-06-23 20:42:12 +03:00
|
|
|
return ThreeDPoint(aX.GetValueAtTime(tick), aY.GetValueAtTime(tick),
|
|
|
|
aZ.GetValueAtTime(tick));
|
|
|
|
}
|
|
|
|
|
2015-09-03 10:01:50 +03:00
|
|
|
void PannerNodeEngine::EqualPowerPanningFunction(const AudioBlock& aInput,
|
2016-06-23 20:42:12 +03:00
|
|
|
AudioBlock* aOutput,
|
2019-10-02 11:46:23 +03:00
|
|
|
StreamTime tick) {
|
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
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
// Optimize the case where the position and orientation is constant for this
|
|
|
|
// processing block: we can just apply a constant gain on the left and right
|
|
|
|
// channel
|
|
|
|
if (mPositionX.HasSimpleValue() && mPositionY.HasSimpleValue() &&
|
|
|
|
mPositionZ.HasSimpleValue() && mOrientationX.HasSimpleValue() &&
|
|
|
|
mOrientationY.HasSimpleValue() && mOrientationZ.HasSimpleValue()) {
|
|
|
|
ThreeDPoint position = ConvertAudioParamTimelineTo3DP(
|
|
|
|
mPositionX, mPositionY, mPositionZ, tick);
|
|
|
|
ThreeDPoint orientation = ConvertAudioParamTimelineTo3DP(
|
|
|
|
mOrientationX, mOrientationY, mOrientationZ, tick);
|
|
|
|
if (!orientation.IsZero()) {
|
|
|
|
orientation.Normalize();
|
|
|
|
}
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2018-03-02 21:47:34 +03:00
|
|
|
// For a stereo source, when both the listener and the panner are in
|
|
|
|
// the same spot, and no cone gain is specified, this node is noop.
|
2018-07-17 13:01:24 +03:00
|
|
|
if (inputChannels == 2 && mListenerEngine->Position() == position &&
|
|
|
|
mConeInnerAngle == 360 && mConeOuterAngle == 360) {
|
2016-06-23 20:42:12 +03:00
|
|
|
*aOutput = aInput;
|
|
|
|
return;
|
|
|
|
}
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
ComputeAzimuthAndElevation(position, azimuth, elevation);
|
|
|
|
coneGain = ComputeConeGain(position, orientation);
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
// The following algorithm is described in the spec.
|
|
|
|
// Clamp azimuth in the [-90, 90] range.
|
2019-09-02 15:40:00 +03:00
|
|
|
azimuth = std::min(180.f, std::max(-180.f, azimuth));
|
2016-06-23 20:42:12 +03:00
|
|
|
|
|
|
|
// Wrap around
|
|
|
|
if (azimuth < -90.f) {
|
|
|
|
azimuth = -180.f - azimuth;
|
|
|
|
} else if (azimuth > 90) {
|
|
|
|
azimuth = 180.f - azimuth;
|
|
|
|
}
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
distanceGain = ComputeDistanceGain(position);
|
|
|
|
|
|
|
|
// Actually compute the left and right gain.
|
|
|
|
gainL = cos(0.5 * M_PI * normalizedAzimuth);
|
|
|
|
gainR = sin(0.5 * M_PI * normalizedAzimuth);
|
|
|
|
|
|
|
|
// Compute the output.
|
|
|
|
ApplyStereoPanning(aInput, aOutput, gainL, gainR, azimuth <= 0);
|
|
|
|
|
2018-10-19 12:36:08 +03:00
|
|
|
aOutput->mVolume *= distanceGain * coneGain;
|
2013-03-21 20:45:53 +04:00
|
|
|
} else {
|
2016-06-23 20:42:12 +03:00
|
|
|
float positionX[WEBAUDIO_BLOCK_SIZE];
|
|
|
|
float positionY[WEBAUDIO_BLOCK_SIZE];
|
|
|
|
float positionZ[WEBAUDIO_BLOCK_SIZE];
|
|
|
|
float orientationX[WEBAUDIO_BLOCK_SIZE];
|
|
|
|
float orientationY[WEBAUDIO_BLOCK_SIZE];
|
|
|
|
float orientationZ[WEBAUDIO_BLOCK_SIZE];
|
|
|
|
|
|
|
|
if (!mPositionX.HasSimpleValue()) {
|
|
|
|
mPositionX.GetValuesAtTime(tick, positionX, WEBAUDIO_BLOCK_SIZE);
|
2013-03-21 20:45:53 +04:00
|
|
|
} else {
|
2016-06-23 20:42:12 +03:00
|
|
|
positionX[0] = mPositionX.GetValueAtTime(tick);
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
2016-06-23 20:42:12 +03:00
|
|
|
if (!mPositionY.HasSimpleValue()) {
|
|
|
|
mPositionY.GetValuesAtTime(tick, positionY, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
} else {
|
|
|
|
positionY[0] = mPositionY.GetValueAtTime(tick);
|
|
|
|
}
|
|
|
|
if (!mPositionZ.HasSimpleValue()) {
|
|
|
|
mPositionZ.GetValuesAtTime(tick, positionZ, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
} else {
|
|
|
|
positionZ[0] = mPositionZ.GetValueAtTime(tick);
|
|
|
|
}
|
|
|
|
if (!mOrientationX.HasSimpleValue()) {
|
|
|
|
mOrientationX.GetValuesAtTime(tick, orientationX, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
} else {
|
|
|
|
orientationX[0] = mOrientationX.GetValueAtTime(tick);
|
|
|
|
}
|
|
|
|
if (!mOrientationY.HasSimpleValue()) {
|
|
|
|
mOrientationY.GetValuesAtTime(tick, orientationY, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
} else {
|
|
|
|
orientationY[0] = mOrientationY.GetValueAtTime(tick);
|
|
|
|
}
|
|
|
|
if (!mOrientationZ.HasSimpleValue()) {
|
|
|
|
mOrientationZ.GetValuesAtTime(tick, orientationZ, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
} else {
|
|
|
|
orientationZ[0] = mOrientationZ.GetValueAtTime(tick);
|
|
|
|
}
|
|
|
|
|
2018-07-05 19:17:51 +03:00
|
|
|
float buffer[3 * WEBAUDIO_BLOCK_SIZE + 4];
|
2016-06-23 20:42:12 +03:00
|
|
|
bool onLeft[WEBAUDIO_BLOCK_SIZE];
|
|
|
|
|
2018-07-05 19:17:51 +03:00
|
|
|
float* alignedPanningL = ALIGNED16(buffer);
|
|
|
|
float* alignedPanningR = alignedPanningL + WEBAUDIO_BLOCK_SIZE;
|
|
|
|
float* alignedGain = alignedPanningR + WEBAUDIO_BLOCK_SIZE;
|
|
|
|
ASSERT_ALIGNED16(alignedPanningL);
|
|
|
|
ASSERT_ALIGNED16(alignedPanningR);
|
|
|
|
ASSERT_ALIGNED16(alignedGain);
|
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
for (size_t counter = 0; counter < WEBAUDIO_BLOCK_SIZE; ++counter) {
|
|
|
|
ThreeDPoint position(
|
|
|
|
mPositionX.HasSimpleValue() ? positionX[0] : positionX[counter],
|
|
|
|
mPositionY.HasSimpleValue() ? positionY[0] : positionY[counter],
|
|
|
|
mPositionZ.HasSimpleValue() ? positionZ[0] : positionZ[counter]);
|
|
|
|
ThreeDPoint orientation(
|
|
|
|
mOrientationX.HasSimpleValue() ? orientationX[0]
|
|
|
|
: orientationX[counter],
|
|
|
|
mOrientationY.HasSimpleValue() ? orientationY[0]
|
|
|
|
: orientationY[counter],
|
|
|
|
mOrientationZ.HasSimpleValue() ? orientationZ[0]
|
|
|
|
: orientationZ[counter]);
|
|
|
|
if (!orientation.IsZero()) {
|
|
|
|
orientation.Normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
ComputeAzimuthAndElevation(position, azimuth, elevation);
|
|
|
|
coneGain = ComputeConeGain(position, orientation);
|
|
|
|
|
|
|
|
// The following algorithm is described in the spec.
|
|
|
|
// Clamp azimuth in the [-90, 90] range.
|
2019-09-02 15:40:00 +03:00
|
|
|
azimuth = std::min(180.f, std::max(-180.f, azimuth));
|
2016-06-23 20:42:12 +03:00
|
|
|
|
|
|
|
// Wrap around
|
|
|
|
if (azimuth < -90.f) {
|
|
|
|
azimuth = -180.f - azimuth;
|
|
|
|
} else if (azimuth > 90) {
|
|
|
|
azimuth = 180.f - azimuth;
|
|
|
|
}
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
distanceGain = ComputeDistanceGain(position);
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
// Actually compute the left and right gain.
|
2018-07-05 19:17:51 +03:00
|
|
|
float gainL = cos(0.5 * M_PI * normalizedAzimuth);
|
|
|
|
float gainR = sin(0.5 * M_PI * normalizedAzimuth);
|
|
|
|
|
|
|
|
alignedPanningL[counter] = gainL;
|
|
|
|
alignedPanningR[counter] = gainR;
|
2018-10-19 12:36:08 +03:00
|
|
|
alignedGain[counter] = distanceGain * coneGain;
|
2016-06-23 20:42:12 +03:00
|
|
|
onLeft[counter] = azimuth <= 0;
|
|
|
|
}
|
|
|
|
|
2018-07-05 19:17:51 +03:00
|
|
|
// Apply the panning to the output buffer
|
|
|
|
ApplyStereoPanning(aInput, aOutput, alignedPanningL, alignedPanningR,
|
|
|
|
onLeft);
|
2013-03-21 20:45:53 +04:00
|
|
|
|
2018-07-05 19:17:51 +03:00
|
|
|
// Apply the input volume, cone and distance gain to the output buffer.
|
|
|
|
float* outputL = aOutput->ChannelFloatsForWrite(0);
|
|
|
|
float* outputR = aOutput->ChannelFloatsForWrite(1);
|
|
|
|
AudioBlockInPlaceScale(outputL, alignedGain);
|
|
|
|
AudioBlockInPlaceScale(outputR, alignedGain);
|
2016-06-23 20:42:12 +03:00
|
|
|
}
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
|
|
|
|
2013-09-04 13:20:59 +04:00
|
|
|
// This algorithm is specified in the webaudio spec.
|
2016-06-23 20:42:12 +03:00
|
|
|
void PannerNodeEngine::ComputeAzimuthAndElevation(const ThreeDPoint& position,
|
|
|
|
float& aAzimuth,
|
|
|
|
float& aElevation) {
|
2018-07-17 13:01:24 +03:00
|
|
|
ThreeDPoint sourceListener = position - mListenerEngine->Position();
|
2013-03-21 20:45:53 +04:00
|
|
|
if (sourceListener.IsZero()) {
|
|
|
|
aAzimuth = 0.0;
|
|
|
|
aElevation = 0.0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceListener.Normalize();
|
|
|
|
|
|
|
|
// Project the source-listener vector on the x-z plane.
|
2018-07-17 13:01:24 +03:00
|
|
|
const ThreeDPoint& listenerFront = mListenerEngine->FrontVector();
|
|
|
|
const ThreeDPoint& listenerRight = mListenerEngine->RightVector();
|
2013-09-04 13:20:59 +04:00
|
|
|
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.
|
2016-06-23 20:42:12 +03:00
|
|
|
float PannerNodeEngine::ComputeConeGain(const ThreeDPoint& position,
|
|
|
|
const ThreeDPoint& orientation) {
|
2013-03-21 22:25:15 +04:00
|
|
|
// Omnidirectional source
|
2016-06-23 20:42:12 +03:00
|
|
|
if (orientation.IsZero() ||
|
|
|
|
((mConeInnerAngle == 360) && (mConeOuterAngle == 360))) {
|
2013-03-21 22:25:15 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalized source-listener vector
|
2018-07-17 13:01:24 +03:00
|
|
|
ThreeDPoint sourceToListener = mListenerEngine->Position() - position;
|
2013-03-21 22:25:15 +04:00
|
|
|
sourceToListener.Normalize();
|
|
|
|
|
|
|
|
// Angle between the source orientation vector and the source-listener vector
|
2016-06-23 20:42:12 +03:00
|
|
|
double dotProduct = sourceToListener.DotProduct(orientation);
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-23 20:42:12 +03:00
|
|
|
double PannerNodeEngine::ComputeDistanceGain(const ThreeDPoint& position) {
|
2018-07-17 13:01:24 +03:00
|
|
|
ThreeDPoint distanceVec = position - mListenerEngine->Position();
|
2013-08-09 02:08:06 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|