2012-10-31 12:09:32 -07: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"
|
2013-01-29 15:37:51 -08:00
|
|
|
#include "AudioNodeEngine.h"
|
2013-06-07 12:25:04 -07:00
|
|
|
#include "AudioNodeStream.h"
|
|
|
|
#include "AudioDestinationNode.h"
|
|
|
|
#include "WebAudioUtils.h"
|
2012-10-31 12:09:32 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(GainNode, AudioNode,
|
|
|
|
mGain)
|
2012-10-31 12:09:32 -07: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)
|
|
|
|
|
2013-06-07 12:25:04 -07:00
|
|
|
class GainNodeEngine : public AudioNodeEngine
|
2013-01-29 15:37:51 -08:00
|
|
|
{
|
|
|
|
public:
|
2013-04-20 09:16:28 -07:00
|
|
|
GainNodeEngine(AudioNode* aNode, AudioDestinationNode* aDestination)
|
|
|
|
: AudioNodeEngine(aNode)
|
2013-06-07 12:25:04 -07:00
|
|
|
, mSource(nullptr)
|
|
|
|
, mDestination(static_cast<AudioNodeStream*> (aDestination->Stream()))
|
|
|
|
// Keep the default value in sync with the default value in GainNode::GainNode.
|
|
|
|
, mGain(1.f)
|
2013-01-29 15:37:51 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-07 12:25:04 -07:00
|
|
|
void SetSourceStream(AudioNodeStream* aSource)
|
|
|
|
{
|
|
|
|
mSource = aSource;
|
|
|
|
}
|
|
|
|
|
2013-01-29 15:37:51 -08:00
|
|
|
enum Parameters {
|
|
|
|
GAIN
|
|
|
|
};
|
2013-05-24 10:10:08 -07:00
|
|
|
void SetTimelineParameter(uint32_t aIndex,
|
|
|
|
const AudioParamTimeline& aValue,
|
|
|
|
TrackRate aSampleRate) MOZ_OVERRIDE
|
2013-01-29 15:37:51 -08:00
|
|
|
{
|
|
|
|
switch (aIndex) {
|
|
|
|
case GAIN:
|
2013-06-07 12:25:04 -07:00
|
|
|
MOZ_ASSERT(mSource && mDestination);
|
|
|
|
mGain = aValue;
|
|
|
|
WebAudioUtils::ConvertAudioParamToTicks(mGain, mSource, mDestination);
|
2013-01-29 15:37:51 -08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Bad GainNodeEngine TimelineParameter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-04 13:09:49 -08:00
|
|
|
virtual void ProcessBlock(AudioNodeStream* aStream,
|
|
|
|
const AudioChunk& aInput,
|
|
|
|
AudioChunk* aOutput,
|
2015-01-01 22:12:54 -08:00
|
|
|
bool* aFinished) MOZ_OVERRIDE
|
2013-01-29 15:37:51 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSource == aStream, "Invalid source stream");
|
|
|
|
|
2013-05-07 20:32:23 -07:00
|
|
|
if (aInput.IsNull()) {
|
|
|
|
// If input is silent, so is the output
|
|
|
|
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
|
2013-06-07 12:25:04 -07:00
|
|
|
} else if (mGain.HasSimpleValue()) {
|
|
|
|
// Optimize the case where we only have a single value set as the volume
|
2014-01-06 15:53:48 -08:00
|
|
|
float gain = mGain.GetValue();
|
|
|
|
if (gain == 0.0f) {
|
|
|
|
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
|
|
|
|
} else {
|
|
|
|
*aOutput = aInput;
|
|
|
|
aOutput->mVolume *= gain;
|
|
|
|
}
|
2013-01-29 15:37:51 -08:00
|
|
|
} else {
|
2013-06-07 12:25:04 -07: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.
|
|
|
|
AllocateAudioBlock(aInput.mChannelData.Length(), aOutput);
|
|
|
|
|
|
|
|
// Compute the gain values for the duration of the input AudioChunk
|
|
|
|
// XXX we need to add a method to AudioEventTimeline to compute this buffer directly.
|
|
|
|
float computedGain[WEBAUDIO_BLOCK_SIZE];
|
|
|
|
for (size_t counter = 0; counter < WEBAUDIO_BLOCK_SIZE; ++counter) {
|
2014-09-17 22:20:43 -07:00
|
|
|
StreamTime tick = aStream->GetCurrentPosition();
|
2013-06-07 12:25:04 -07:00
|
|
|
computedGain[counter] = mGain.GetValueAtTime(tick, counter) * aInput.mVolume;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the gain to the output buffer
|
|
|
|
for (size_t channel = 0; channel < aOutput->mChannelData.Length(); ++channel) {
|
|
|
|
const float* inputBuffer = static_cast<const float*> (aInput.mChannelData[channel]);
|
|
|
|
float* buffer = static_cast<float*> (const_cast<void*>
|
|
|
|
(aOutput->mChannelData[channel]));
|
|
|
|
AudioBlockCopyChannelWithScale(inputBuffer, computedGain, buffer);
|
2013-01-29 15:37:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-07 12:25:04 -07:00
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
// Not owned:
|
|
|
|
// - mSource (probably)
|
|
|
|
// - mDestination (probably)
|
|
|
|
// - mGain - Internal ref owned by AudioNode
|
|
|
|
return AudioNodeEngine::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2013-06-07 12:25:04 -07:00
|
|
|
AudioNodeStream* mSource;
|
|
|
|
AudioNodeStream* mDestination;
|
|
|
|
AudioParamTimeline mGain;
|
2013-01-29 15:37:51 -08:00
|
|
|
};
|
|
|
|
|
2012-10-31 12:09:32 -07:00
|
|
|
GainNode::GainNode(AudioContext* aContext)
|
2013-04-27 15:44:50 -07:00
|
|
|
: AudioNode(aContext,
|
|
|
|
2,
|
|
|
|
ChannelCountMode::Max,
|
|
|
|
ChannelInterpretation::Speakers)
|
2015-01-06 21:39:46 -08:00
|
|
|
, mGain(new AudioParam(this, SendGainToStream, 1.0f))
|
2012-10-31 12:09:32 -07:00
|
|
|
{
|
2013-04-20 09:16:28 -07:00
|
|
|
GainNodeEngine* engine = new GainNodeEngine(this, aContext->Destination());
|
2013-03-17 17:37:47 -07:00
|
|
|
mStream = aContext->Graph()->CreateAudioNodeStream(engine, MediaStreamGraph::INTERNAL_STREAM);
|
2013-01-29 15:37:51 -08:00
|
|
|
engine->SetSourceStream(static_cast<AudioNodeStream*> (mStream.get()));
|
|
|
|
}
|
|
|
|
|
2014-07-08 14:23:17 -07:00
|
|
|
GainNode::~GainNode()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-13 11:08:10 -07: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 12:09:32 -07:00
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
GainNode::WrapObject(JSContext* aCx)
|
2012-10-31 12:09:32 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return GainNodeBinding::Wrap(aCx, this);
|
2012-10-31 12:09:32 -07:00
|
|
|
}
|
|
|
|
|
2013-01-29 15:37:51 -08:00
|
|
|
void
|
|
|
|
GainNode::SendGainToStream(AudioNode* aNode)
|
|
|
|
{
|
|
|
|
GainNode* This = static_cast<GainNode*>(aNode);
|
2013-03-29 18:46:03 -07:00
|
|
|
SendTimelineParameterToStream(This, GainNodeEngine::GAIN, *This->mGain);
|
2013-01-29 15:37:51 -08:00
|
|
|
}
|
|
|
|
|
2012-10-31 12:09:32 -07:00
|
|
|
}
|
|
|
|
}
|