Bug 1322883 - AudioNode constructors - part 13 - OscillatorNode, r=padenot

This commit is contained in:
Andrea Marchesini 2016-12-15 19:24:42 +01:00
parent 28750547b1
commit 91ff6d8f42
5 changed files with 54 additions and 17 deletions

View File

@ -25,6 +25,7 @@
#include "mozilla/dom/IIRFilterNodeBinding.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/dom/OfflineAudioContextBinding.h"
#include "mozilla/dom/OscillatorNodeBinding.h"
#include "mozilla/dom/PannerNodeBinding.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/StereoPannerNodeBinding.h"
@ -474,13 +475,7 @@ AudioContext::CreateIIRFilter(const mozilla::dom::binding_detail::AutoSequence<d
already_AddRefed<OscillatorNode>
AudioContext::CreateOscillator(ErrorResult& aRv)
{
if (CheckClosed(aRv)) {
return nullptr;
}
RefPtr<OscillatorNode> oscillatorNode =
new OscillatorNode(this);
return oscillatorNode.forget();
return OscillatorNode::Create(*this, OscillatorOptions(), aRv);
}
already_AddRefed<PeriodicWave>

View File

@ -426,8 +426,35 @@ OscillatorNode::OscillatorNode(AudioContext* aContext)
mStream->AddMainThreadListener(this);
}
OscillatorNode::~OscillatorNode()
/* static */ already_AddRefed<OscillatorNode>
OscillatorNode::Create(AudioContext& aAudioContext,
const OscillatorOptions& aOptions,
ErrorResult& aRv)
{
if (aAudioContext.CheckClosed(aRv)) {
return nullptr;
}
RefPtr<OscillatorNode> audioNode = new OscillatorNode(&aAudioContext);
audioNode->Initialize(aOptions, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
audioNode->SetType(aOptions.mType, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
audioNode->Frequency()->SetValue(aOptions.mFrequency);
audioNode->Detune()->SetValue(aOptions.mDetune);
if (aOptions.mPeriodicWave.WasPassed()) {
audioNode->SetPeriodicWave(aOptions.mPeriodicWave.Value());
}
return audioNode.forget();
}
size_t

View File

@ -16,16 +16,26 @@ namespace mozilla {
namespace dom {
class AudioContext;
struct OscillatorOptions;
class OscillatorNode final : public AudioNode,
public MainThreadMediaStreamListener
{
public:
explicit OscillatorNode(AudioContext* aContext);
static already_AddRefed<OscillatorNode>
Create(AudioContext& aAudioContext, const OscillatorOptions& aOptions,
ErrorResult& aRv);
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OscillatorNode, AudioNode)
static already_AddRefed<OscillatorNode>
Constructor(const GlobalObject& aGlobal, AudioContext& aAudioContext,
const OscillatorOptions& aOptions, ErrorResult& aRv)
{
return Create(aAudioContext, aOptions, aRv);
}
JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
void DestroyMediaStream() override;
@ -82,14 +92,13 @@ public:
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;
protected:
virtual ~OscillatorNode();
private:
explicit OscillatorNode(AudioContext* aContext);
~OscillatorNode() = default;
void SendTypeToStream();
void SendPeriodicWaveToStream();
private:
OscillatorType mType;
RefPtr<PeriodicWave> mPeriodicWave;
RefPtr<AudioParam> mFrequency;
@ -101,4 +110,3 @@ private:
} // namespace mozilla
#endif

View File

@ -14,7 +14,7 @@ SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
var context = new AudioContext();
var osc = context.createOscillator();
var osc = new OscillatorNode(context);
is(osc.channelCount, 2, "Oscillator node has 2 input channels by default");
is(osc.channelCountMode, "max", "Correct channelCountMode for the Oscillator node");

View File

@ -18,7 +18,15 @@ enum OscillatorType {
"custom"
};
[Pref="dom.webaudio.enabled"]
dictionary OscillatorOptions : AudioNodeOptions {
OscillatorType type = "sine";
float frequency = 440;
float detune = 0;
PeriodicWave periodicWave;
};
[Pref="dom.webaudio.enabled",
Constructor(AudioContext context, optional OscillatorOptions options)]
interface OscillatorNode : AudioNode {
[SetterThrows]
@ -39,4 +47,3 @@ interface OscillatorNode : AudioNode {
// Mozilla extensions
OscillatorNode implements AudioNodePassThrough;