2012-10-31 23:09:32 +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 "GainNode.h"
|
|
|
|
#include "mozilla/dom/GainNodeBinding.h"
|
2016-04-13 22:31:50 +03:00
|
|
|
#include "AlignmentUtils.h"
|
2013-01-30 03:37:51 +04:00
|
|
|
#include "AudioNodeEngine.h"
|
2013-06-07 23:25:04 +04:00
|
|
|
#include "AudioNodeStream.h"
|
|
|
|
#include "AudioDestinationNode.h"
|
|
|
|
#include "WebAudioUtils.h"
|
2012-10-31 23:09:32 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(GainNode, AudioNode,
|
|
|
|
mGain)
|
2012-10-31 23:09:32 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(GainNode)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(AudioNode)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(GainNode, AudioNode)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(GainNode, AudioNode)
|
|
|
|
|
2015-04-28 09:42:00 +03:00
|
|
|
class GainNodeEngine final : public AudioNodeEngine
|
2013-01-30 03:37:51 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-04-20 20:16:28 +04:00
|
|
|
GainNodeEngine(AudioNode* aNode, AudioDestinationNode* aDestination)
|
|
|
|
: AudioNodeEngine(aNode)
|
2015-07-02 08:36:07 +03:00
|
|
|
, mDestination(aDestination->Stream())
|
2013-06-07 23:25:04 +04:00
|
|
|
// Keep the default value in sync with the default value in GainNode::GainNode.
|
|
|
|
, mGain(1.f)
|
2013-01-30 03:37:51 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Parameters {
|
|
|
|
GAIN
|
|
|
|
};
|
2015-09-25 16:57:55 +03:00
|
|
|
void RecvTimelineEvent(uint32_t aIndex,
|
|
|
|
AudioTimelineEvent& aEvent) override
|
2013-01-30 03:37:51 +04:00
|
|
|
{
|
2015-09-22 08:11:52 +03:00
|
|
|
MOZ_ASSERT(mDestination);
|
2015-09-25 16:57:55 +03:00
|
|
|
WebAudioUtils::ConvertAudioTimelineEventToTicks(aEvent,
|
|
|
|
mDestination);
|
|
|
|
|
2013-01-30 03:37:51 +04:00
|
|
|
switch (aIndex) {
|
|
|
|
case GAIN:
|
2015-09-25 16:57:55 +03:00
|
|
|
mGain.InsertEvent<int64_t>(aEvent);
|
2013-01-30 03:37:51 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Bad GainNodeEngine TimelineParameter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
void ProcessBlock(AudioNodeStream* aStream,
|
|
|
|
GraphTime aFrom,
|
|
|
|
const AudioBlock& aInput,
|
|
|
|
AudioBlock* aOutput,
|
|
|
|
bool* aFinished) override
|
2013-01-30 03:37:51 +04:00
|
|
|
{
|
2013-05-08 07:32:23 +04:00
|
|
|
if (aInput.IsNull()) {
|
|
|
|
// If input is silent, so is the output
|
|
|
|
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
|
2013-06-07 23:25:04 +04:00
|
|
|
} else if (mGain.HasSimpleValue()) {
|
|
|
|
// Optimize the case where we only have a single value set as the volume
|
2014-01-07 03:53:48 +04:00
|
|
|
float gain = mGain.GetValue();
|
|
|
|
if (gain == 0.0f) {
|
|
|
|
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
|
|
|
|
} else {
|
|
|
|
*aOutput = aInput;
|
|
|
|
aOutput->mVolume *= gain;
|
|
|
|
}
|
2013-01-30 03:37:51 +04:00
|
|
|
} else {
|
2013-06-07 23:25:04 +04:00
|
|
|
// First, compute a vector of gains for each track tick based on the
|
|
|
|
// timeline at hand, and then for each channel, multiply the values
|
|
|
|
// in the buffer with the gain vector.
|
2015-09-03 10:01:50 +03:00
|
|
|
aOutput->AllocateChannels(aInput.ChannelCount());
|
2013-06-07 23:25:04 +04:00
|
|
|
|
|
|
|
// Compute the gain values for the duration of the input AudioChunk
|
2015-09-22 07:34:45 +03:00
|
|
|
StreamTime tick = mDestination->GraphTimeToStreamTime(aFrom);
|
2016-04-13 22:31:50 +03:00
|
|
|
float computedGain[WEBAUDIO_BLOCK_SIZE + 4];
|
|
|
|
float* alignedComputedGain = ALIGNED16(computedGain);
|
|
|
|
ASSERT_ALIGNED16(alignedComputedGain);
|
|
|
|
mGain.GetValuesAtTime(tick, alignedComputedGain, WEBAUDIO_BLOCK_SIZE);
|
2015-04-22 09:53:33 +03:00
|
|
|
|
2013-06-07 23:25:04 +04:00
|
|
|
for (size_t counter = 0; counter < WEBAUDIO_BLOCK_SIZE; ++counter) {
|
2016-04-13 22:31:50 +03:00
|
|
|
alignedComputedGain[counter] *= aInput.mVolume;
|
2013-06-07 23:25:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the gain to the output buffer
|
2015-09-03 08:30:16 +03:00
|
|
|
for (size_t channel = 0; channel < aOutput->ChannelCount(); ++channel) {
|
2013-06-07 23:25:04 +04:00
|
|
|
const float* inputBuffer = static_cast<const float*> (aInput.mChannelData[channel]);
|
2015-07-22 08:59:21 +03:00
|
|
|
float* buffer = aOutput->ChannelFloatsForWrite(channel);
|
2016-04-13 22:31:50 +03:00
|
|
|
AudioBlockCopyChannelWithScale(inputBuffer, alignedComputedGain, buffer);
|
2013-01-30 03:37:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-07 23:25:04 +04:00
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override
|
2014-04-13 22:08:10 +04:00
|
|
|
{
|
|
|
|
// Not owned:
|
|
|
|
// - mDestination (probably)
|
|
|
|
// - mGain - Internal ref owned by AudioNode
|
|
|
|
return AudioNodeEngine::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
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-06-07 23:25:04 +04:00
|
|
|
AudioNodeStream* mDestination;
|
|
|
|
AudioParamTimeline mGain;
|
2013-01-30 03:37:51 +04:00
|
|
|
};
|
|
|
|
|
2012-10-31 23:09:32 +04:00
|
|
|
GainNode::GainNode(AudioContext* aContext)
|
2013-04-28 02:44:50 +04:00
|
|
|
: AudioNode(aContext,
|
|
|
|
2,
|
|
|
|
ChannelCountMode::Max,
|
|
|
|
ChannelInterpretation::Speakers)
|
2016-12-21 12:53:38 +03:00
|
|
|
, mGain(new AudioParam(this, GainNodeEngine::GAIN, "gain", 1.0f))
|
2012-10-31 23:09:32 +04:00
|
|
|
{
|
2013-04-20 20:16:28 +04:00
|
|
|
GainNodeEngine* engine = new GainNodeEngine(this, aContext->Destination());
|
2015-09-08 16:22:16 +03:00
|
|
|
mStream = AudioNodeStream::Create(aContext, engine,
|
2016-09-05 18:25:41 +03:00
|
|
|
AudioNodeStream::NO_STREAM_FLAGS,
|
|
|
|
aContext->Graph());
|
2013-01-30 03:37:51 +04:00
|
|
|
}
|
|
|
|
|
2016-12-15 21:24:41 +03:00
|
|
|
/* static */ already_AddRefed<GainNode>
|
|
|
|
GainNode::Create(AudioContext& aAudioContext,
|
|
|
|
const GainOptions& aOptions,
|
|
|
|
ErrorResult& aRv)
|
2014-07-09 01:23:17 +04:00
|
|
|
{
|
2016-12-15 21:24:41 +03:00
|
|
|
if (aAudioContext.CheckClosed(aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<GainNode> audioNode = new GainNode(&aAudioContext);
|
|
|
|
|
|
|
|
audioNode->Initialize(aOptions, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
audioNode->Gain()->SetValue(aOptions.mGain);
|
|
|
|
return audioNode.forget();
|
2014-07-09 01:23:17 +04:00
|
|
|
}
|
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
size_t
|
|
|
|
GainNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += mGain->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
GainNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-10-31 23:09:32 +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
|
|
|
GainNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2012-10-31 23:09:32 +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 GainNodeBinding::Wrap(aCx, this, aGivenProto);
|
2012-10-31 23:09:32 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|