diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 9a3f1aa3392..eec1844af0d 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -673,6 +673,7 @@ set(SOURCES WebAudio/AudioScheduledSourceNode.cpp WebAudio/BaseAudioContext.cpp WebAudio/DynamicsCompressorNode.cpp + WebAudio/GainNode.cpp WebAudio/OfflineAudioContext.cpp WebAudio/OscillatorNode.cpp WebAudio/PeriodicWave.cpp diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index c0c92abed15..ab0cc5f346d 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -700,6 +700,7 @@ class AudioParam; class AudioScheduledSourceNode; class BaseAudioContext; class DynamicsCompressorNode; +class GainNode; class OfflineAudioContext; class OscillatorNode; class PeriodicWave; diff --git a/Userland/Libraries/LibWeb/WebAudio/GainNode.cpp b/Userland/Libraries/LibWeb/WebAudio/GainNode.cpp new file mode 100644 index 00000000000..ca37d3a7b42 --- /dev/null +++ b/Userland/Libraries/LibWeb/WebAudio/GainNode.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2024, Shannon Booth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace Web::WebAudio { + +JS_DEFINE_ALLOCATOR(GainNode); + +GainNode::~GainNode() = default; + +JS::NonnullGCPtr GainNode::create(JS::Realm& realm, JS::NonnullGCPtr context, GainOptions const& options) +{ + return construct_impl(realm, context, options); +} + +// https://webaudio.github.io/web-audio-api/#dom-gainnode-gainnode +JS::NonnullGCPtr GainNode::construct_impl(JS::Realm& realm, JS::NonnullGCPtr context, GainOptions const& options) +{ + // FIXME: Invoke "Initialize the AudioNode" steps. + return realm.vm().heap().allocate(realm, realm, context, options); +} + +GainNode::GainNode(JS::Realm& realm, JS::NonnullGCPtr context, GainOptions const& options) + : AudioNode(realm, context) + , m_gain(AudioParam::create(realm, options.gain, NumericLimits::lowest(), NumericLimits::max(), Bindings::AutomationRate::ARate)) +{ +} + +void GainNode::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(GainNode); +} + +void GainNode::visit_edges(Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_gain); +} + +} diff --git a/Userland/Libraries/LibWeb/WebAudio/GainNode.h b/Userland/Libraries/LibWeb/WebAudio/GainNode.h new file mode 100644 index 00000000000..59551d77ac3 --- /dev/null +++ b/Userland/Libraries/LibWeb/WebAudio/GainNode.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024, Shannon Booth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::WebAudio { + +// https://webaudio.github.io/web-audio-api/#GainOptions +struct GainOptions : AudioNodeOptions { + float gain { 1.0 }; +}; + +// https://webaudio.github.io/web-audio-api/#GainNode +class GainNode : public AudioNode { + WEB_PLATFORM_OBJECT(GainNode, AudioNode); + JS_DECLARE_ALLOCATOR(GainNode); + +public: + virtual ~GainNode() override; + + static JS::NonnullGCPtr create(JS::Realm&, JS::NonnullGCPtr, GainOptions const& = {}); + static JS::NonnullGCPtr construct_impl(JS::Realm&, JS::NonnullGCPtr, GainOptions const& = {}); + + JS::NonnullGCPtr gain() const { return m_gain; } + +protected: + GainNode(JS::Realm&, JS::NonnullGCPtr, GainOptions const& = {}); + + virtual void initialize(JS::Realm&) override; + virtual void visit_edges(Cell::Visitor&) override; + +private: + // https://webaudio.github.io/web-audio-api/#dom-gainnode-gain + JS::NonnullGCPtr m_gain; +}; + +} diff --git a/Userland/Libraries/LibWeb/WebAudio/GainNode.idl b/Userland/Libraries/LibWeb/WebAudio/GainNode.idl new file mode 100644 index 00000000000..ff1d2604022 --- /dev/null +++ b/Userland/Libraries/LibWeb/WebAudio/GainNode.idl @@ -0,0 +1,15 @@ +#import +#import +#import + +// https://webaudio.github.io/web-audio-api/#GainOptions +dictionary GainOptions : AudioNodeOptions { + float gain = 1.0; +}; + +// https://webaudio.github.io/web-audio-api/#GainNode +[Exposed=Window] +interface GainNode : AudioNode { + constructor(BaseAudioContext context, optional GainOptions options = {}); + readonly attribute AudioParam gain; +}; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index c68f02d7fdf..7a9e9c32bc4 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -306,6 +306,7 @@ libweb_js_bindings(WebAudio/AudioParam) libweb_js_bindings(WebAudio/AudioScheduledSourceNode) libweb_js_bindings(WebAudio/BaseAudioContext) libweb_js_bindings(WebAudio/DynamicsCompressorNode) +libweb_js_bindings(WebAudio/GainNode) libweb_js_bindings(WebAudio/OfflineAudioContext) libweb_js_bindings(WebAudio/OscillatorNode) libweb_js_bindings(WebAudio/PeriodicWave)