| 
									
										
										
										
											2024-11-25 14:30:36 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/WebAudio/BaseAudioContext.h>
 | 
					
						
							|  |  |  | #include <LibWeb/WebAudio/ChannelMergerNode.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::WebAudio { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GC_DEFINE_ALLOCATOR(ChannelMergerNode); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ChannelMergerNode::ChannelMergerNode(JS::Realm& realm, GC::Ref<BaseAudioContext> context, ChannelMergerOptions const& options) | 
					
						
							|  |  |  |     : AudioNode(realm, context) | 
					
						
							|  |  |  |     , m_number_of_inputs(options.number_of_inputs) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ChannelMergerNode::~ChannelMergerNode() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WebIDL::ExceptionOr<GC::Ref<ChannelMergerNode>> ChannelMergerNode::create(JS::Realm& realm, GC::Ref<BaseAudioContext> context, ChannelMergerOptions const& options) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return construct_impl(realm, context, options); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WebIDL::ExceptionOr<GC::Ref<ChannelMergerNode>> ChannelMergerNode::construct_impl(JS::Realm& realm, GC::Ref<BaseAudioContext> context, ChannelMergerOptions const& options) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createchannelmerger
 | 
					
						
							|  |  |  |     // An IndexSizeError exception MUST be thrown if numberOfInputs is less than 1 or is greater
 | 
					
						
							|  |  |  |     // than the number of supported channels.
 | 
					
						
							|  |  |  |     if (options.number_of_inputs < 1 || options.number_of_inputs > BaseAudioContext::MAX_NUMBER_OF_CHANNELS) | 
					
						
							| 
									
										
										
										
											2025-08-07 19:31:52 -04:00
										 |  |  |         return WebIDL::IndexSizeError::create(realm, "Invalid number of inputs"_utf16); | 
					
						
							| 
									
										
										
										
											2024-11-25 14:30:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     auto node = realm.create<ChannelMergerNode>(realm, context, options); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Default options for channel count and interpretation
 | 
					
						
							|  |  |  |     // https://webaudio.github.io/web-audio-api/#BiquadFilterNode
 | 
					
						
							|  |  |  |     AudioNodeDefaultOptions default_options; | 
					
						
							|  |  |  |     default_options.channel_count_mode = Bindings::ChannelCountMode::Explicit; | 
					
						
							|  |  |  |     default_options.channel_interpretation = Bindings::ChannelInterpretation::Speakers; | 
					
						
							|  |  |  |     default_options.channel_count = 1; | 
					
						
							|  |  |  |     // FIXME: Set tail-time to no
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TRY(node->initialize_audio_node_options(options, default_options)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return node; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://webaudio.github.io/web-audio-api/#audionode-channelcount-constraints
 | 
					
						
							|  |  |  | WebIDL::ExceptionOr<void> ChannelMergerNode::set_channel_count(WebIDL::UnsignedLong channel_count) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // The channel count cannot be changed, and an InvalidStateError exception MUST be thrown for
 | 
					
						
							|  |  |  |     // any attempt to change the value.
 | 
					
						
							|  |  |  |     if (channel_count != 1) | 
					
						
							| 
									
										
										
										
											2025-08-07 19:31:52 -04:00
										 |  |  |         return WebIDL::InvalidStateError::create(realm(), "Channel count cannot be changed"_utf16); | 
					
						
							| 
									
										
										
										
											2024-11-25 14:30:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return Base::set_channel_count(channel_count); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WebIDL::ExceptionOr<void> ChannelMergerNode::set_channel_count_mode(Bindings::ChannelCountMode channel_count_mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // The channel count mode cannot be changed from "explicit" and an InvalidStateError exception
 | 
					
						
							|  |  |  |     // MUST be thrown for any attempt to change the value.
 | 
					
						
							|  |  |  |     if (channel_count_mode != Bindings::ChannelCountMode::Explicit) | 
					
						
							| 
									
										
										
										
											2025-08-07 19:31:52 -04:00
										 |  |  |         return WebIDL::InvalidStateError::create(realm(), "Channel count mode cannot be changed"_utf16); | 
					
						
							| 
									
										
										
										
											2024-11-25 14:30:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return Base::set_channel_count_mode(channel_count_mode); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |