Files
palemoon27/dom/media/webaudio/GainNode.cpp
T
roytam1 7bd3227e5d import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1141282 - DynamicCompressorNode's readonly 'reduction' should be a float. r=ehsan (4685d2cf8)
- Bug 1153783 - Implement the `detune` AudioParam for the AudioBufferSourceNode. r=ehsan (8e6b3aca4)
- Bug 1134034 - Add a chrome-only parentId and name on AudioParam for devtools. r=ehsan (13815cb94)
- Bug 1159290 - "Add final/override to WebAudio classes". r=padenot (83cb303ea)
- Bug 1161946 - MainThreadMediaStreamListener should be notified just when the stream is finished - patch 1, r=padenot (c2a126103)
- Bug 1161946 - MainThreadMediaStreamListener should be notified just when the stream is finished - patch 2, r=padenot (a1f408cce)
- Bug 1161946 - patch 3 - explicit CTOR for NotifyRunnable CLOSED TREE (f304c1ef0)
- Bug 974089 - Destroy WebAudio MediaStream when a source finishes. r=padenot (66d7e20df)
- Bug 1175510 - Update the AudioBufferSourceNode <=> PannerNode mapping when destroying AudioNodeStream. r=karlt (156741d15)
- bug 1179662 uninline DestroyMediaStream overrides so that mStream type need not be complete r=padenot (08d0ebd62)
- bug 1179662 specify AudioNode::mStream as AudioNodeStream r=padenot (3aaa3a544)
- bug 1179662 handle null AudioNodeStream stream r=padenot (980a8296b)
- bug 914392 don't check engine's Node existence in ProcessBlock r=padenot (583176721)
2021-03-16 09:22:31 +08:00

168 lines
5.2 KiB
C++

/* -*- 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"
#include "AudioNodeEngine.h"
#include "AudioNodeStream.h"
#include "AudioDestinationNode.h"
#include "WebAudioUtils.h"
namespace mozilla {
namespace dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED(GainNode, AudioNode,
mGain)
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)
class GainNodeEngine final : public AudioNodeEngine
{
public:
GainNodeEngine(AudioNode* aNode, AudioDestinationNode* aDestination)
: AudioNodeEngine(aNode)
, mSource(nullptr)
, mDestination(aDestination->Stream())
// Keep the default value in sync with the default value in GainNode::GainNode.
, mGain(1.f)
{
}
void SetSourceStream(AudioNodeStream* aSource)
{
mSource = aSource;
}
enum Parameters {
GAIN
};
void SetTimelineParameter(uint32_t aIndex,
const AudioParamTimeline& aValue,
TrackRate aSampleRate) override
{
switch (aIndex) {
case GAIN:
MOZ_ASSERT(mSource && mDestination);
mGain = aValue;
WebAudioUtils::ConvertAudioParamToTicks(mGain, mSource, mDestination);
break;
default:
NS_ERROR("Bad GainNodeEngine TimelineParameter");
}
}
virtual void ProcessBlock(AudioNodeStream* aStream,
const AudioChunk& aInput,
AudioChunk* aOutput,
bool* aFinished) override
{
MOZ_ASSERT(mSource == aStream, "Invalid source stream");
if (aInput.IsNull()) {
// If input is silent, so is the output
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
} else if (mGain.HasSimpleValue()) {
// Optimize the case where we only have a single value set as the volume
float gain = mGain.GetValue();
if (gain == 0.0f) {
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
} else {
*aOutput = aInput;
aOutput->mVolume *= gain;
}
} else {
// 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) {
StreamTime tick = aStream->GetCurrentPosition();
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);
}
}
}
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override
{
// Not owned:
// - mSource (probably)
// - mDestination (probably)
// - mGain - Internal ref owned by AudioNode
return AudioNodeEngine::SizeOfExcludingThis(aMallocSizeOf);
}
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
{
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}
AudioNodeStream* mSource;
AudioNodeStream* mDestination;
AudioParamTimeline mGain;
};
GainNode::GainNode(AudioContext* aContext)
: AudioNode(aContext,
2,
ChannelCountMode::Max,
ChannelInterpretation::Speakers)
, mGain(new AudioParam(this, SendGainToStream, 1.0f, "gain"))
{
GainNodeEngine* engine = new GainNodeEngine(this, aContext->Destination());
mStream = aContext->Graph()->CreateAudioNodeStream(engine, MediaStreamGraph::INTERNAL_STREAM);
engine->SetSourceStream(mStream);
}
GainNode::~GainNode()
{
}
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);
}
JSObject*
GainNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return GainNodeBinding::Wrap(aCx, this, aGivenProto);
}
void
GainNode::SendGainToStream(AudioNode* aNode)
{
GainNode* This = static_cast<GainNode*>(aNode);
SendTimelineParameterToStream(This, GainNodeEngine::GAIN, *This->mGain);
}
} // namespace dom
} // namespace mozilla