1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/* -*- 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 "DelayNode.h"
#include "mozilla/dom/DelayNodeBinding.h"
#include "AudioNodeEngine.h"
#include "AudioNodeStream.h"
#include "AudioDestinationNode.h"
#include "WebAudioUtils.h"
#include "DelayBuffer.h"
#include "PlayingRefChangeHandler.h"
namespace mozilla {
namespace dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED(DelayNode, AudioNode, mDelay)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DelayNode)
NS_INTERFACE_MAP_END_INHERITING(AudioNode)
NS_IMPL_ADDREF_INHERITED(DelayNode, AudioNode)
NS_IMPL_RELEASE_INHERITED(DelayNode, AudioNode)
class DelayNodeEngine final : public AudioNodeEngine {
typedef PlayingRefChangeHandler PlayingRefChanged;
public:
DelayNodeEngine(AudioNode* aNode, AudioDestinationNode* aDestination,
float aMaxDelayTicks)
: AudioNodeEngine(aNode),
mDestination(aDestination->Stream())
// Keep the default value in sync with the default value in
// DelayNode::DelayNode.
,
mDelay(0.f)
// Use a smoothing range of 20ms
,
mBuffer(
std::max(aMaxDelayTicks, static_cast<float>(WEBAUDIO_BLOCK_SIZE))),
mMaxDelay(aMaxDelayTicks),
mHaveProducedBeforeInput(false),
mLeftOverData(INT32_MIN) {}
DelayNodeEngine* AsDelayNodeEngine() override { return this; }
enum Parameters {
DELAY,
};
void RecvTimelineEvent(uint32_t aIndex, AudioTimelineEvent& aEvent) override {
MOZ_ASSERT(mDestination);
WebAudioUtils::ConvertAudioTimelineEventToTicks(aEvent, mDestination);
switch (aIndex) {
case DELAY:
mDelay.InsertEvent<int64_t>(aEvent);
break;
default:
NS_ERROR("Bad DelayNodeEngine TimelineParameter");
}
}
void ProcessBlock(AudioNodeStream* aStream, GraphTime aFrom,
const AudioBlock& aInput, AudioBlock* aOutput,
bool* aFinished) override {
MOZ_ASSERT(aStream->SampleRate() == mDestination->SampleRate());
if (!aInput.IsSilentOrSubnormal()) {
if (mLeftOverData <= 0) {
RefPtr<PlayingRefChanged> refchanged =
new PlayingRefChanged(aStream, PlayingRefChanged::ADDREF);
aStream->Graph()->DispatchToMainThreadStableState(refchanged.forget());
}
mLeftOverData = mBuffer.MaxDelayTicks();
} else if (mLeftOverData > 0) {
mLeftOverData -= WEBAUDIO_BLOCK_SIZE;
} else {
if (mLeftOverData != INT32_MIN) {
mLeftOverData = INT32_MIN;
aStream->ScheduleCheckForInactive();
// Delete our buffered data now we no longer need it
mBuffer.Reset();
RefPtr<PlayingRefChanged> refchanged =
new PlayingRefChanged(aStream, PlayingRefChanged::RELEASE);
aStream->Graph()->DispatchToMainThreadStableState(refchanged.forget());
}
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
return;
}
mBuffer.Write(aInput);
// Skip output update if mLastChunks has already been set by
// ProduceBlockBeforeInput() when in a cycle.
if (!mHaveProducedBeforeInput) {
UpdateOutputBlock(aStream, aFrom, aOutput, 0.0);
}
mHaveProducedBeforeInput = false;
mBuffer.NextBlock();
}
void UpdateOutputBlock(AudioNodeStream* aStream, GraphTime aFrom,
AudioBlock* aOutput, float minDelay) {
float maxDelay = mMaxDelay;
float sampleRate = aStream->SampleRate();
ChannelInterpretation channelInterpretation =
aStream->GetChannelInterpretation();
if (mDelay.HasSimpleValue()) {
// If this DelayNode is in a cycle, make sure the delay value is at least
// one block, even if that is greater than maxDelay.
float delayFrames = mDelay.GetValue() * sampleRate;
float delayFramesClamped =
std::max(minDelay, std::min(delayFrames, maxDelay));
mBuffer.Read(delayFramesClamped, aOutput, channelInterpretation);
} else {
// Compute the delay values for the duration of the input AudioChunk
// If this DelayNode is in a cycle, make sure the delay value is at least
// one block.
StreamTime tick = mDestination->GraphTimeToStreamTime(aFrom);
float values[WEBAUDIO_BLOCK_SIZE];
mDelay.GetValuesAtTime(tick, values, WEBAUDIO_BLOCK_SIZE);
float computedDelay[WEBAUDIO_BLOCK_SIZE];
for (size_t counter = 0; counter < WEBAUDIO_BLOCK_SIZE; ++counter) {
float delayAtTick = values[counter] * sampleRate;
float delayAtTickClamped =
std::max(minDelay, std::min(delayAtTick, maxDelay));
computedDelay[counter] = delayAtTickClamped;
}
mBuffer.Read(computedDelay, aOutput, channelInterpretation);
}
}
void ProduceBlockBeforeInput(AudioNodeStream* aStream, GraphTime aFrom,
AudioBlock* aOutput) override {
if (mLeftOverData <= 0) {
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
} else {
UpdateOutputBlock(aStream, aFrom, aOutput, WEBAUDIO_BLOCK_SIZE);
}
mHaveProducedBeforeInput = true;
}
bool IsActive() const override { return mLeftOverData != INT32_MIN; }
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override {
size_t amount = AudioNodeEngine::SizeOfExcludingThis(aMallocSizeOf);
// Not owned:
// - mDestination - probably not owned
// - mDelay - shares ref with AudioNode, don't count
amount += mBuffer.SizeOfExcludingThis(aMallocSizeOf);
return amount;
}
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override {
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}
RefPtr<AudioNodeStream> mDestination;
AudioParamTimeline mDelay;
DelayBuffer mBuffer;
float mMaxDelay;
bool mHaveProducedBeforeInput;
// How much data we have in our buffer which needs to be flushed out when our
// inputs finish.
int32_t mLeftOverData;
};
DelayNode::DelayNode(AudioContext* aContext, double aMaxDelay)
: AudioNode(aContext, 2, ChannelCountMode::Max,
ChannelInterpretation::Speakers) {
CreateAudioParam(mDelay, DelayNodeEngine::DELAY, "delayTime", 0.0f, 0.f,
aMaxDelay);
DelayNodeEngine* engine = new DelayNodeEngine(
this, aContext->Destination(), aContext->SampleRate() * aMaxDelay);
mStream = AudioNodeStream::Create(
aContext, engine, AudioNodeStream::NO_STREAM_FLAGS, aContext->Graph());
}
/* static */
already_AddRefed<DelayNode> DelayNode::Create(AudioContext& aAudioContext,
const DelayOptions& aOptions,
ErrorResult& aRv) {
if (aOptions.mMaxDelayTime <= 0. || aOptions.mMaxDelayTime >= 180.) {
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return nullptr;
}
RefPtr<DelayNode> audioNode =
new DelayNode(&aAudioContext, aOptions.mMaxDelayTime);
audioNode->Initialize(aOptions, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
audioNode->DelayTime()->SetValue(aOptions.mDelayTime);
return audioNode.forget();
}
size_t DelayNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
amount += mDelay->SizeOfIncludingThis(aMallocSizeOf);
return amount;
}
size_t DelayNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}
JSObject* DelayNode::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return DelayNode_Binding::Wrap(aCx, this, aGivenProto);
}
} // namespace dom
} // namespace mozilla
|