gecko/content/media/webaudio/OscillatorNode.cpp

252 lines
7.0 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 "OscillatorNode.h"
#include "AudioNodeEngine.h"
#include "AudioNodeStream.h"
#include "AudioDestinationNode.h"
#include "WebAudioUtils.h"
namespace mozilla {
namespace dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED_3(OscillatorNode, AudioNode,
mPeriodicWave, mFrequency, mDetune)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(OscillatorNode)
NS_INTERFACE_MAP_END_INHERITING(AudioNode)
NS_IMPL_ADDREF_INHERITED(OscillatorNode, AudioNode)
NS_IMPL_RELEASE_INHERITED(OscillatorNode, AudioNode)
class OscillatorNodeEngine : public AudioNodeEngine
{
public:
OscillatorNodeEngine(AudioNode* aNode, AudioDestinationNode* aDestination)
: AudioNodeEngine(aNode)
, mSource(nullptr)
, mDestination(static_cast<AudioNodeStream*> (aDestination->Stream()))
, mStart(0)
, mStop(TRACK_TICKS_MAX)
// Keep the default value in sync with the default value in OscillatorNode::OscillatorNode.
, mFrequency(440.f)
, mDetune(0.f)
, mType(OscillatorType::Sine)
{
}
void SetSourceStream(AudioNodeStream* aSource)
{
mSource = aSource;
}
enum Parameters {
FREQUENCY,
DETUNE,
TYPE,
PERIODICWAVE,
START,
STOP,
};
void SetTimelineParameter(uint32_t aIndex,
const AudioParamTimeline& aValue,
TrackRate aSampleRate) MOZ_OVERRIDE
{
switch (aIndex) {
case FREQUENCY:
MOZ_ASSERT(mSource && mDestination);
mFrequency = aValue;
WebAudioUtils::ConvertAudioParamToTicks(mFrequency, mSource, mDestination);
break;
case DETUNE:
MOZ_ASSERT(mSource && mDestination);
mDetune = aValue;
WebAudioUtils::ConvertAudioParamToTicks(mDetune, mSource, mDestination);
break;
default:
NS_ERROR("Bad OscillatorNodeEngine TimelineParameter");
}
}
virtual void SetStreamTimeParameter(uint32_t aIndex, TrackTicks aParam)
{
switch (aIndex) {
case START: mStart = aParam; break;
case STOP: mStop = aParam; break;
default:
NS_ERROR("Bad OscillatorNodeEngine StreamTimeParameter");
}
}
virtual void SetInt32Parameter(uint32_t aIndex, int32_t aParam)
{
switch (aIndex) {
case TYPE: mType = static_cast<OscillatorType>(aParam); break;
default:
NS_ERROR("Bad OscillatorNodeEngine Int32Parameter");
}
}
virtual void ProduceAudioBlock(AudioNodeStream* aStream,
const AudioChunk& aInput,
AudioChunk* aOutput,
bool* aFinished) MOZ_OVERRIDE
{
MOZ_ASSERT(mSource == aStream, "Invalid source stream");
// TODO: Synthesize a waveform here.
*aOutput = aInput;
}
AudioNodeStream* mSource;
AudioNodeStream* mDestination;
TrackTicks mStart;
TrackTicks mStop;
AudioParamTimeline mFrequency;
AudioParamTimeline mDetune;
OscillatorType mType;
};
OscillatorNode::OscillatorNode(AudioContext* aContext)
: AudioNode(aContext,
2,
ChannelCountMode::Max,
ChannelInterpretation::Speakers)
, mType(OscillatorType::Sine)
, mFrequency(new AudioParam(MOZ_THIS_IN_INITIALIZER_LIST(),
SendFrequencyToStream, 440.0f))
, mDetune(new AudioParam(MOZ_THIS_IN_INITIALIZER_LIST(),
SendDetuneToStream, 0.0f))
, mStartCalled(false)
, mStopped(false)
{
OscillatorNodeEngine* engine = new OscillatorNodeEngine(this, aContext->Destination());
mStream = aContext->Graph()->CreateAudioNodeStream(engine, MediaStreamGraph::SOURCE_STREAM);
engine->SetSourceStream(static_cast<AudioNodeStream*> (mStream.get()));
}
JSObject*
OscillatorNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
{
return OscillatorNodeBinding::Wrap(aCx, aScope, this);
}
void
OscillatorNode::SendFrequencyToStream(AudioNode* aNode)
{
OscillatorNode* This = static_cast<OscillatorNode*>(aNode);
SendTimelineParameterToStream(This, OscillatorNodeEngine::FREQUENCY, *This->mFrequency);
}
void
OscillatorNode::SendDetuneToStream(AudioNode* aNode)
{
OscillatorNode* This = static_cast<OscillatorNode*>(aNode);
SendTimelineParameterToStream(This, OscillatorNodeEngine::DETUNE, *This->mDetune);
}
void
OscillatorNode::SendTypeToStream()
{
SendInt32ParameterToStream(OscillatorNodeEngine::TYPE, static_cast<int32_t>(mType));
if (mType == OscillatorType::Custom) {
// TODO: Send the custom wave table somehow
}
}
void
OscillatorNode::Start(double aWhen, ErrorResult& aRv)
{
if (!WebAudioUtils::IsTimeValid(aWhen)) {
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return;
}
if (mStartCalled) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return;
}
mStartCalled = true;
AudioNodeStream* ns = static_cast<AudioNodeStream*>(mStream.get());
if (!ns) {
// Nothing to play, or we're already dead for some reason
return;
}
// TODO: Perhaps we need to do more here.
ns->SetStreamTimeParameter(OscillatorNodeEngine::START,
Context()->DestinationStream(),
aWhen);
MOZ_ASSERT(!mPlayingRef, "We can only accept a successful start() call once");
mPlayingRef.Take(this);
}
void
OscillatorNode::Stop(double aWhen, ErrorResult& aRv)
{
if (!WebAudioUtils::IsTimeValid(aWhen)) {
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return;
}
if (!mStartCalled) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return;
}
mPlayingRef.Drop(this);
AudioNodeStream* ns = static_cast<AudioNodeStream*>(mStream.get());
if (!ns || !Context()) {
// We've already stopped and had our stream shut down
return;
}
// TODO: Perhaps we need to do more here.
ns->SetStreamTimeParameter(OscillatorNodeEngine::STOP,
Context()->DestinationStream(),
std::max(0.0, aWhen));
}
void
OscillatorNode::NotifyMainThreadStateChanged()
{
if (mStream->IsFinished()) {
class EndedEventDispatcher : public nsRunnable
{
public:
explicit EndedEventDispatcher(OscillatorNode* aNode)
: mNode(aNode) {}
NS_IMETHODIMP Run()
{
// If it's not safe to run scripts right now, schedule this to run later
if (!nsContentUtils::IsSafeToRunScript()) {
nsContentUtils::AddScriptRunner(this);
return NS_OK;
}
mNode->DispatchTrustedEvent(NS_LITERAL_STRING("ended"));
return NS_OK;
}
private:
nsRefPtr<OscillatorNode> mNode;
};
if (!mStopped) {
// Only dispatch the ended event once
NS_DispatchToMainThread(new EndedEventDispatcher(this));
mStopped = true;
}
// Drop the playing reference
// Warning: The below line might delete this.
mPlayingRef.Drop(this);
}
}
}
}