Bug 1322883 - AudioNode constructors - part 2 - GainNode, r=padenot

This commit is contained in:
Andrea Marchesini 2016-12-15 19:24:41 +01:00
parent c14a015895
commit 9c8c0e312f
6 changed files with 65 additions and 17 deletions

View File

@ -12,6 +12,9 @@
#include "mozilla/OwningNonNull.h"
#include "mozilla/dom/AnalyserNode.h"
#include "mozilla/dom/AnalyserNodeBinding.h"
#include "mozilla/dom/AudioBufferSourceNodeBinding.h"
#include "mozilla/dom/GainNodeBinding.h"
#include "mozilla/dom/AudioContextBinding.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/dom/OfflineAudioContextBinding.h"
@ -395,12 +398,7 @@ AudioContext::CreateMediaStreamSource(DOMMediaStream& aMediaStream,
already_AddRefed<GainNode>
AudioContext::CreateGain(ErrorResult& aRv)
{
if (CheckClosed(aRv)) {
return nullptr;
}
RefPtr<GainNode> gainNode = new GainNode(this);
return gainNode.forget();
return GainNode::Create(*this, GainOptions(), aRv);
}
already_AddRefed<WaveShaperNode>

View File

@ -128,8 +128,24 @@ GainNode::GainNode(AudioContext* aContext)
aContext->Graph());
}
GainNode::~GainNode()
/* static */ already_AddRefed<GainNode>
GainNode::Create(AudioContext& aAudioContext,
const GainOptions& aOptions,
ErrorResult& aRv)
{
if (aAudioContext.CheckClosed(aRv)) {
return nullptr;
}
RefPtr<GainNode> audioNode = new GainNode(&aAudioContext);
audioNode->Initialize(aOptions, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
audioNode->Gain()->SetValue(aOptions.mGain);
return audioNode.forget();
}
size_t

View File

@ -14,15 +14,25 @@ namespace mozilla {
namespace dom {
class AudioContext;
struct GainOptions;
class GainNode final : public AudioNode
{
public:
explicit GainNode(AudioContext* aContext);
static already_AddRefed<GainNode>
Create(AudioContext& aAudioContext, const GainOptions& aOptions,
ErrorResult& aRv);
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(GainNode, AudioNode)
static already_AddRefed<GainNode>
Constructor(const GlobalObject& aGlobal, AudioContext& aAudioContext,
const GainOptions& aOptions, ErrorResult& aRv)
{
return Create(aAudioContext, aOptions, aRv);
}
JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
AudioParam* Gain() const
@ -38,10 +48,10 @@ public:
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;
protected:
virtual ~GainNode();
private:
explicit GainNode(AudioContext* aContext);
~GainNode() = default;
RefPtr<AudioParam> mGain;
};
@ -49,4 +59,3 @@ private:
} // namespace mozilla
#endif

View File

@ -14,17 +14,32 @@ var gTest = {
length: 2048,
numberOfChannels: 1,
createGraph: function(context) {
var gain = new GainNode(context);
ok(gain.gain, "The audioparam member must exist");
is(gain.gain.value, 1.0, "Correct initial value");
is(gain.gain.defaultValue, 1.0, "Correct default value");
gain.gain.value = 0.5;
is(gain.gain.value, 0.5, "Correct initial value");
is(gain.gain.defaultValue, 1.0, "Correct default value");
gain = new GainNode(context, { gain: 0.5 });
ok(gain.gain, "The audioparam member must exist");
is(gain.gain.value, 0.5, "Correct initial value");
is(gain.gain.defaultValue, 1.0, "Correct default value");
gain.gain.value = 0.5;
is(gain.gain.value, 0.5, "Correct initial value");
is(gain.gain.defaultValue, 1.0, "Correct default value");
var buffer = context.createBuffer(1, 2048, context.sampleRate);
for (var i = 0; i < 2048; ++i) {
buffer.getChannelData(0)[i] = Math.sin(440 * 2 * Math.PI * i / context.sampleRate);
}
var source = context.createBufferSource();
var gain = context.createGain();
source.buffer = buffer;
gain = context.createGain();
source.connect(gain);
ok(gain.gain, "The audioparam member must exist");

View File

@ -10,7 +10,12 @@
* liability, trademark and document use rules apply.
*/
[Pref="dom.webaudio.enabled"]
dictionary GainOptions : AudioNodeOptions {
float gain = 1.0;
};
[Pref="dom.webaudio.enabled",
Constructor(AudioContext context, optional GainOptions options)]
interface GainNode : AudioNode {
readonly attribute AudioParam gain;

View File

@ -125,7 +125,12 @@ interface AudioNode : EventTarget {
};</pre>
<pre id="gain-node-idl">interface GainNode : AudioNode {
<pre id="gain-node-idl">dictionary GainOptions : AudioNodeOptions {
float gain = 1.0;
};
[Constructor(AudioContext context, optional GainOptions options)]
interface GainNode : AudioNode {
readonly attribute AudioParam gain;