diff --git a/Tests/LibWeb/Text/expected/WebAudio/compressor-node-channel-cound-mode.txt b/Tests/LibWeb/Text/expected/WebAudio/compressor-node-channel-cound-mode.txt new file mode 100644 index 00000000000..e0409bb698b --- /dev/null +++ b/Tests/LibWeb/Text/expected/WebAudio/compressor-node-channel-cound-mode.txt @@ -0,0 +1,6 @@ +clamped-max +explicit +NotSupportedError +clamped-max +explicit +NotSupportedError \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/WebAudio/compressor-node-channel-cound-mode.html b/Tests/LibWeb/Text/input/WebAudio/compressor-node-channel-cound-mode.html new file mode 100644 index 00000000000..096b3a17f10 --- /dev/null +++ b/Tests/LibWeb/Text/input/WebAudio/compressor-node-channel-cound-mode.html @@ -0,0 +1,36 @@ + + diff --git a/Userland/Libraries/LibWeb/WebAudio/AudioNode.cpp b/Userland/Libraries/LibWeb/WebAudio/AudioNode.cpp index b96a671a66e..199b1cd1295 100644 --- a/Userland/Libraries/LibWeb/WebAudio/AudioNode.cpp +++ b/Userland/Libraries/LibWeb/WebAudio/AudioNode.cpp @@ -21,6 +21,32 @@ AudioNode::AudioNode(JS::Realm& realm, JS::NonnullGCPtr contex AudioNode::~AudioNode() = default; +WebIDL::ExceptionOr AudioNode::initialize_audio_node_options(JS::NonnullGCPtr context, AudioNodeOptions const& given_options, AudioNodeOptions const& default_options) +{ + // FIXME: Context will be used in the future implementation. + (void)context; // Cast to void to avoid unused parameter warning. + + // Set channel count, fallback to default if not provided + if (given_options.channel_count.has_value()) { + TRY(set_channel_count(given_options.channel_count.value())); + } else if (default_options.channel_count.has_value()) { + TRY(set_channel_count(default_options.channel_count.value())); + } + + // Set channel count mode, fallback to default if not provided + if (given_options.channel_count_mode.has_value()) { + TRY(set_channel_count_mode(given_options.channel_count_mode.value())); + } else if (default_options.channel_count_mode.has_value()) { + TRY(set_channel_count_mode(default_options.channel_count_mode.value())); + } + + // Set channel interpretation, fallback to default if not provided + Bindings::ChannelInterpretation channel_interpretation_to_set = given_options.channel_interpretation.value_or(default_options.channel_interpretation.value_or(Bindings::ChannelInterpretation::Speakers)); + TRY(set_channel_interpretation(channel_interpretation_to_set)); + + return {}; +} + // https://webaudio.github.io/web-audio-api/#dom-audionode-connect WebIDL::ExceptionOr> AudioNode::connect(JS::NonnullGCPtr destination_node, WebIDL::UnsignedLong output, WebIDL::UnsignedLong input) { diff --git a/Userland/Libraries/LibWeb/WebAudio/AudioNode.h b/Userland/Libraries/LibWeb/WebAudio/AudioNode.h index 5cdd3c7c511..9a863532bc5 100644 --- a/Userland/Libraries/LibWeb/WebAudio/AudioNode.h +++ b/Userland/Libraries/LibWeb/WebAudio/AudioNode.h @@ -18,8 +18,8 @@ namespace Web::WebAudio { // https://webaudio.github.io/web-audio-api/#AudioNodeOptions struct AudioNodeOptions { Optional channel_count; - Bindings::ChannelCountMode channel_count_mode; - Bindings::ChannelInterpretation channel_interpretation; + Optional channel_count_mode; + Optional channel_interpretation; }; // https://webaudio.github.io/web-audio-api/#AudioNode @@ -57,11 +57,13 @@ public: virtual WebIDL::ExceptionOr set_channel_count(WebIDL::UnsignedLong); virtual WebIDL::UnsignedLong channel_count() const { return m_channel_count; } - WebIDL::ExceptionOr set_channel_count_mode(Bindings::ChannelCountMode); + virtual WebIDL::ExceptionOr set_channel_count_mode(Bindings::ChannelCountMode); Bindings::ChannelCountMode channel_count_mode(); WebIDL::ExceptionOr set_channel_interpretation(Bindings::ChannelInterpretation); Bindings::ChannelInterpretation channel_interpretation(); + WebIDL::ExceptionOr initialize_audio_node_options(JS::NonnullGCPtr context, AudioNodeOptions const& given_options, AudioNodeOptions const& default_options); + protected: AudioNode(JS::Realm&, JS::NonnullGCPtr); diff --git a/Userland/Libraries/LibWeb/WebAudio/DynamicsCompressorNode.cpp b/Userland/Libraries/LibWeb/WebAudio/DynamicsCompressorNode.cpp index 5cf5342ee2a..d93b29dac3d 100644 --- a/Userland/Libraries/LibWeb/WebAudio/DynamicsCompressorNode.cpp +++ b/Userland/Libraries/LibWeb/WebAudio/DynamicsCompressorNode.cpp @@ -23,10 +23,21 @@ WebIDL::ExceptionOr> DynamicsCompressor // https://webaudio.github.io/web-audio-api/#dom-dynamicscompressornode-dynamicscompressornode WebIDL::ExceptionOr> DynamicsCompressorNode::construct_impl(JS::Realm& realm, JS::NonnullGCPtr context, DynamicsCompressorOptions const& options) { - // FIXME: Invoke "Initialize the AudioNode" steps. - return realm.vm().heap().allocate(realm, realm, context, options); + // Create the node and allocate memory + auto node = realm.vm().heap().allocate(realm, realm, context, options); + + // Default options for channel count and interpretation + AudioNodeOptions default_options; + default_options.channel_count_mode = Bindings::ChannelCountMode::ClampedMax; + default_options.channel_interpretation = Bindings::ChannelInterpretation::Speakers; + + // Initialize the AudioNode with the given options, default options, and context + TRY(node->initialize_audio_node_options(context, options, default_options)); + + return node; } + DynamicsCompressorNode::DynamicsCompressorNode(JS::Realm& realm, JS::NonnullGCPtr context, DynamicsCompressorOptions const& options) : AudioNode(realm, context) , m_threshold(AudioParam::create(realm, options.threshold, -100, 0, Bindings::AutomationRate::KRate)) @@ -53,4 +64,16 @@ void DynamicsCompressorNode::visit_edges(Cell::Visitor& visitor) visitor.visit(m_release); } +// https://webaudio.github.io/web-audio-api/#dom-audionode-channelcountmode +WebIDL::ExceptionOr DynamicsCompressorNode::set_channel_count_mode(Bindings::ChannelCountMode mode) +{ + if (mode == Bindings::ChannelCountMode::Max) { + // Return a NotSupportedError if 'max' is used + return WebIDL::NotSupportedError::create(realm(), "DynamicsCompressorNode does not support 'max' as channelCountMode."_string); + } + + // If the mode is valid, call the base class implementation + return AudioNode::set_channel_count_mode(mode); +} + } diff --git a/Userland/Libraries/LibWeb/WebAudio/DynamicsCompressorNode.h b/Userland/Libraries/LibWeb/WebAudio/DynamicsCompressorNode.h index 1a4a0661556..25aa3166433 100644 --- a/Userland/Libraries/LibWeb/WebAudio/DynamicsCompressorNode.h +++ b/Userland/Libraries/LibWeb/WebAudio/DynamicsCompressorNode.h @@ -40,6 +40,8 @@ public: JS::NonnullGCPtr release() const { return m_release; } float reduction() const { return m_reduction; } + WebIDL::ExceptionOr set_channel_count_mode(Bindings::ChannelCountMode) override; + protected: DynamicsCompressorNode(JS::Realm&, JS::NonnullGCPtr, DynamicsCompressorOptions const& = {});