2012-11-01 00:26:03 +00: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 "DelayNode.h"
|
|
|
|
#include "mozilla/dom/DelayNodeBinding.h"
|
2013-03-25 12:34:59 +00:00
|
|
|
#include "AudioNodeEngine.h"
|
|
|
|
#include "AudioNodeStream.h"
|
|
|
|
#include "AudioDestinationNode.h"
|
|
|
|
#include "WebAudioUtils.h"
|
2013-08-08 22:07:49 +00:00
|
|
|
#include "DelayProcessor.h"
|
2013-08-15 19:44:14 +00:00
|
|
|
#include "PlayingRefChangeHandler.h"
|
2012-11-01 00:26:03 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2012-11-27 23:08:22 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(DelayNode, AudioNode,
|
|
|
|
mDelay)
|
2012-11-01 00:26:03 +00:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DelayNode)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(AudioNode)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(DelayNode, AudioNode)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(DelayNode, AudioNode)
|
|
|
|
|
2013-03-25 12:34:59 +00:00
|
|
|
class DelayNodeEngine : public AudioNodeEngine
|
|
|
|
{
|
2013-07-04 01:37:39 +00:00
|
|
|
typedef PlayingRefChangeHandler<DelayNode> PlayingRefChanged;
|
2013-03-25 12:34:59 +00:00
|
|
|
public:
|
2013-08-08 22:07:49 +00:00
|
|
|
DelayNodeEngine(AudioNode* aNode, AudioDestinationNode* aDestination,
|
|
|
|
int aMaxDelayFrames)
|
2013-04-20 16:16:28 +00:00
|
|
|
: AudioNodeEngine(aNode)
|
|
|
|
, mSource(nullptr)
|
2013-03-25 12:34:59 +00:00
|
|
|
, mDestination(static_cast<AudioNodeStream*> (aDestination->Stream()))
|
|
|
|
// Keep the default value in sync with the default value in DelayNode::DelayNode.
|
|
|
|
, mDelay(0.f)
|
2013-08-08 22:07:49 +00:00
|
|
|
// Use a smoothing range of 20ms
|
|
|
|
, mProcessor(aMaxDelayFrames,
|
|
|
|
WebAudioUtils::ComputeSmoothingRate(0.02,
|
|
|
|
mDestination->SampleRate()))
|
2013-04-15 01:52:55 +00:00
|
|
|
, mLeftOverData(INT32_MIN)
|
2013-03-25 12:34:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSourceStream(AudioNodeStream* aSource)
|
|
|
|
{
|
|
|
|
mSource = aSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Parameters {
|
|
|
|
DELAY,
|
|
|
|
};
|
2013-05-24 17:10:08 +00:00
|
|
|
void SetTimelineParameter(uint32_t aIndex,
|
|
|
|
const AudioParamTimeline& aValue,
|
|
|
|
TrackRate aSampleRate) MOZ_OVERRIDE
|
2013-03-25 12:34:59 +00:00
|
|
|
{
|
|
|
|
switch (aIndex) {
|
|
|
|
case DELAY:
|
|
|
|
MOZ_ASSERT(mSource && mDestination);
|
|
|
|
mDelay = aValue;
|
|
|
|
WebAudioUtils::ConvertAudioParamToTicks(mDelay, mSource, mDestination);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Bad DelayNodeEngine TimelineParameter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ProduceAudioBlock(AudioNodeStream* aStream,
|
|
|
|
const AudioChunk& aInput,
|
|
|
|
AudioChunk* aOutput,
|
|
|
|
bool* aFinished)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSource == aStream, "Invalid source stream");
|
2013-08-08 22:07:49 +00:00
|
|
|
MOZ_ASSERT(aStream->SampleRate() == mDestination->SampleRate());
|
2013-03-25 12:34:59 +00:00
|
|
|
|
2013-04-15 01:52:55 +00:00
|
|
|
const uint32_t numChannels = aInput.IsNull() ?
|
2013-08-08 22:07:49 +00:00
|
|
|
mProcessor.BufferChannelCount() :
|
2013-04-15 01:52:55 +00:00
|
|
|
aInput.mChannelData.Length();
|
|
|
|
|
|
|
|
bool playedBackAllLeftOvers = false;
|
2013-08-08 22:07:49 +00:00
|
|
|
if (mProcessor.BufferChannelCount() &&
|
2013-04-15 01:52:55 +00:00
|
|
|
mLeftOverData == INT32_MIN &&
|
|
|
|
aStream->AllInputsFinished()) {
|
2013-08-08 22:07:49 +00:00
|
|
|
mLeftOverData = mProcessor.CurrentDelayFrames() - WEBAUDIO_BLOCK_SIZE;
|
2013-04-15 01:52:55 +00:00
|
|
|
|
2013-05-16 00:51:47 +00:00
|
|
|
if (mLeftOverData > 0) {
|
|
|
|
nsRefPtr<PlayingRefChanged> refchanged =
|
|
|
|
new PlayingRefChanged(aStream, PlayingRefChanged::ADDREF);
|
|
|
|
NS_DispatchToMainThread(refchanged);
|
|
|
|
}
|
2013-04-15 01:52:55 +00:00
|
|
|
} else if (mLeftOverData != INT32_MIN) {
|
|
|
|
mLeftOverData -= WEBAUDIO_BLOCK_SIZE;
|
|
|
|
if (mLeftOverData <= 0) {
|
2013-07-05 01:49:53 +00:00
|
|
|
// Continue spamming the main thread with messages until we are destroyed.
|
|
|
|
// This isn't great, but it ensures a message will get through even if
|
|
|
|
// some are ignored by DelayNode::AcceptPlayingRefRelease
|
|
|
|
mLeftOverData = 0;
|
2013-04-15 01:52:55 +00:00
|
|
|
playedBackAllLeftOvers = true;
|
|
|
|
|
|
|
|
nsRefPtr<PlayingRefChanged> refchanged =
|
2013-04-20 16:16:28 +00:00
|
|
|
new PlayingRefChanged(aStream, PlayingRefChanged::RELEASE);
|
2013-04-15 01:52:55 +00:00
|
|
|
NS_DispatchToMainThread(refchanged);
|
|
|
|
}
|
|
|
|
}
|
2013-03-25 12:34:59 +00:00
|
|
|
|
|
|
|
AllocateAudioBlock(numChannels, aOutput);
|
|
|
|
|
2013-08-08 22:07:49 +00:00
|
|
|
AudioChunk input = aInput;
|
|
|
|
if (!aInput.IsNull() && aInput.mVolume != 1.0f) {
|
|
|
|
// Pre-multiply the input's volume
|
|
|
|
AllocateAudioBlock(numChannels, &input);
|
|
|
|
for (uint32_t i = 0; i < numChannels; ++i) {
|
|
|
|
const float* src = static_cast<const float*>(aInput.mChannelData[i]);
|
|
|
|
float* dest = static_cast<float*>(const_cast<void*>(input.mChannelData[i]));
|
|
|
|
AudioBlockCopyChannelWithScale(src, aInput.mVolume, dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const float* const* inputChannels = input.IsNull() ? nullptr :
|
|
|
|
reinterpret_cast<const float* const*>(input.mChannelData.Elements());
|
|
|
|
float* const* outputChannels = reinterpret_cast<float* const*>
|
|
|
|
(const_cast<void* const*>(aOutput->mChannelData.Elements()));
|
2013-03-25 12:34:59 +00:00
|
|
|
|
2013-08-08 22:07:49 +00:00
|
|
|
double sampleRate = aStream->SampleRate();
|
2013-03-25 12:34:59 +00:00
|
|
|
if (mDelay.HasSimpleValue()) {
|
2013-08-08 22:07:49 +00:00
|
|
|
double delayFrames = mDelay.GetValue() * sampleRate;
|
|
|
|
mProcessor.Process(delayFrames, inputChannels, outputChannels,
|
|
|
|
numChannels, WEBAUDIO_BLOCK_SIZE);
|
2013-03-25 12:34:59 +00:00
|
|
|
} else {
|
|
|
|
// Compute the delay values for the duration of the input AudioChunk
|
2013-08-08 22:07:49 +00:00
|
|
|
double computedDelay[WEBAUDIO_BLOCK_SIZE];
|
2013-03-25 12:34:59 +00:00
|
|
|
TrackTicks tick = aStream->GetCurrentPosition();
|
|
|
|
for (size_t counter = 0; counter < WEBAUDIO_BLOCK_SIZE; ++counter) {
|
2013-08-08 22:07:49 +00:00
|
|
|
computedDelay[counter] =
|
|
|
|
mDelay.GetValueAtTime(tick, counter) * sampleRate;
|
2013-03-25 12:34:59 +00:00
|
|
|
}
|
2013-08-08 22:07:49 +00:00
|
|
|
mProcessor.Process(computedDelay, inputChannels, outputChannels,
|
|
|
|
numChannels, WEBAUDIO_BLOCK_SIZE);
|
2013-03-25 12:34:59 +00:00
|
|
|
}
|
|
|
|
|
2013-04-15 01:52:55 +00:00
|
|
|
|
|
|
|
if (playedBackAllLeftOvers) {
|
|
|
|
// Delete our buffered data once we no longer need it
|
2013-08-08 22:07:49 +00:00
|
|
|
mProcessor.Reset();
|
2013-04-15 01:52:55 +00:00
|
|
|
}
|
2013-03-25 12:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioNodeStream* mSource;
|
|
|
|
AudioNodeStream* mDestination;
|
|
|
|
AudioParamTimeline mDelay;
|
2013-08-08 22:07:49 +00:00
|
|
|
DelayProcessor mProcessor;
|
2013-04-15 01:52:55 +00:00
|
|
|
// How much data we have in our buffer which needs to be flushed out when our inputs
|
|
|
|
// finish.
|
|
|
|
int32_t mLeftOverData;
|
2013-03-25 12:34:59 +00:00
|
|
|
};
|
|
|
|
|
2012-11-19 20:52:29 +00:00
|
|
|
DelayNode::DelayNode(AudioContext* aContext, double aMaxDelay)
|
2013-04-27 22:44:50 +00:00
|
|
|
: AudioNode(aContext,
|
|
|
|
2,
|
|
|
|
ChannelCountMode::Max,
|
|
|
|
ChannelInterpretation::Speakers)
|
2013-07-05 01:49:53 +00:00
|
|
|
, mMediaStreamGraphUpdateIndexAtLastInputConnection(0)
|
2013-06-27 11:30:41 +00:00
|
|
|
, mDelay(new AudioParam(MOZ_THIS_IN_INITIALIZER_LIST(),
|
|
|
|
SendDelayToStream, 0.0f))
|
2012-11-01 00:26:03 +00:00
|
|
|
{
|
2013-08-08 22:07:49 +00:00
|
|
|
DelayNodeEngine* engine =
|
|
|
|
new DelayNodeEngine(this, aContext->Destination(),
|
|
|
|
ceil(aContext->SampleRate() * aMaxDelay));
|
2013-03-25 12:34:59 +00:00
|
|
|
mStream = aContext->Graph()->CreateAudioNodeStream(engine, MediaStreamGraph::INTERNAL_STREAM);
|
|
|
|
engine->SetSourceStream(static_cast<AudioNodeStream*> (mStream.get()));
|
2012-11-01 00:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-04-25 16:29:54 +00:00
|
|
|
DelayNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2012-11-01 00:26:03 +00:00
|
|
|
{
|
2013-01-24 00:50:18 +00:00
|
|
|
return DelayNodeBinding::Wrap(aCx, aScope, this);
|
2012-11-01 00:26:03 +00:00
|
|
|
}
|
|
|
|
|
2013-03-25 12:34:59 +00:00
|
|
|
void
|
|
|
|
DelayNode::SendDelayToStream(AudioNode* aNode)
|
|
|
|
{
|
|
|
|
DelayNode* This = static_cast<DelayNode*>(aNode);
|
|
|
|
SendTimelineParameterToStream(This, DelayNodeEngine::DELAY, *This->mDelay);
|
|
|
|
}
|
|
|
|
|
2012-11-01 00:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|