| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org> | 
					
						
							| 
									
										
										
										
											2024-04-25 14:45:59 +12:00
										 |  |  |  |  * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org> | 
					
						
							| 
									
										
										
										
											2024-10-14 23:43:00 +02:00
										 |  |  |  |  * Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-27 12:09:58 +12:00
										 |  |  |  | #include <LibWeb/Bindings/BaseAudioContextPrototype.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							| 
									
										
										
										
											2023-06-27 13:34:51 -04:00
										 |  |  |  | #include <LibWeb/HTML/EventNames.h>
 | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  | #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							| 
									
										
										
										
											2024-09-18 09:09:18 -04:00
										 |  |  |  | #include <LibWeb/WebAudio/AnalyserNode.h>
 | 
					
						
							| 
									
										
										
										
											2024-05-26 11:56:11 +12:00
										 |  |  |  | #include <LibWeb/WebAudio/AudioBuffer.h>
 | 
					
						
							| 
									
										
										
										
											2024-07-21 15:24:25 +03:00
										 |  |  |  | #include <LibWeb/WebAudio/AudioBufferSourceNode.h>
 | 
					
						
							| 
									
										
										
										
											2024-07-24 17:50:06 +12:00
										 |  |  |  | #include <LibWeb/WebAudio/AudioDestinationNode.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  |  | #include <LibWeb/WebAudio/BaseAudioContext.h>
 | 
					
						
							| 
									
										
										
										
											2024-07-21 21:25:14 +03:00
										 |  |  |  | #include <LibWeb/WebAudio/BiquadFilterNode.h>
 | 
					
						
							| 
									
										
										
										
											2024-11-25 14:30:36 +01:00
										 |  |  |  | #include <LibWeb/WebAudio/ChannelMergerNode.h>
 | 
					
						
							| 
									
										
										
										
											2024-05-05 11:18:02 +12:00
										 |  |  |  | #include <LibWeb/WebAudio/DynamicsCompressorNode.h>
 | 
					
						
							| 
									
										
										
										
											2024-05-28 15:48:10 +12:00
										 |  |  |  | #include <LibWeb/WebAudio/GainNode.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-28 12:07:14 +12:00
										 |  |  |  | #include <LibWeb/WebAudio/OscillatorNode.h>
 | 
					
						
							| 
									
										
										
										
											2024-12-13 21:41:06 +00:00
										 |  |  |  | #include <LibWeb/WebAudio/PannerNode.h>
 | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  | #include <LibWeb/WebIDL/AbstractOperations.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/WebIDL/Promise.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Web::WebAudio { | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 00:03:32 +12:00
										 |  |  |  | BaseAudioContext::BaseAudioContext(JS::Realm& realm, float sample_rate) | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  |  |     : DOM::EventTarget(realm) | 
					
						
							| 
									
										
										
										
											2024-05-02 00:03:32 +12:00
										 |  |  |  |     , m_sample_rate(sample_rate) | 
					
						
							| 
									
										
										
										
											2025-01-08 20:04:28 +00:00
										 |  |  |  |     , m_listener(AudioListener::create(realm, *this)) | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | BaseAudioContext::~BaseAudioContext() = default; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |  | void BaseAudioContext::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2024-03-16 13:13:08 +01:00
										 |  |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(BaseAudioContext); | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 17:50:06 +12:00
										 |  |  |  | void BaseAudioContext::visit_edges(Cell::Visitor& visitor) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |  |     visitor.visit(m_destination); | 
					
						
							| 
									
										
										
										
											2024-10-17 10:46:27 +02:00
										 |  |  |  |     visitor.visit(m_pending_promises); | 
					
						
							| 
									
										
										
										
											2024-10-17 21:26:01 +02:00
										 |  |  |  |     visitor.visit(m_listener); | 
					
						
							| 
									
										
										
										
											2024-07-24 17:50:06 +12:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-27 13:34:51 -04:00
										 |  |  |  | void BaseAudioContext::set_onstatechange(WebIDL::CallbackType* event_handler) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     set_event_handler_attribute(HTML::EventNames::statechange, event_handler); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | WebIDL::CallbackType* BaseAudioContext::onstatechange() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     return event_handler_attribute(HTML::EventNames::statechange); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-18 09:09:18 -04:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createanalyser
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<GC::Ref<AnalyserNode>> BaseAudioContext::create_analyser() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     return AnalyserNode::create(realm(), *this); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-21 21:25:14 +03:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbiquadfilter
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | WebIDL::ExceptionOr<GC::Ref<BiquadFilterNode>> BaseAudioContext::create_biquad_filter() | 
					
						
							| 
									
										
										
										
											2024-07-21 21:25:14 +03:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // Factory method for a BiquadFilterNode representing a second order filter which can be configured as one of several common filter types.
 | 
					
						
							|  |  |  |  |     return BiquadFilterNode::create(realm(), *this); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-26 11:56:11 +12:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | WebIDL::ExceptionOr<GC::Ref<AudioBuffer>> BaseAudioContext::create_buffer(WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate) | 
					
						
							| 
									
										
										
										
											2024-05-26 11:56:11 +12:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // Creates an AudioBuffer of the given size. The audio data in the buffer will be zero-initialized (silent).
 | 
					
						
							|  |  |  |  |     // A NotSupportedError exception MUST be thrown if any of the arguments is negative, zero, or outside its nominal range.
 | 
					
						
							|  |  |  |  |     return AudioBuffer::create(realm(), number_of_channels, length, sample_rate); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-21 15:24:25 +03:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffersource
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | WebIDL::ExceptionOr<GC::Ref<AudioBufferSourceNode>> BaseAudioContext::create_buffer_source() | 
					
						
							| 
									
										
										
										
											2024-07-21 15:24:25 +03:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // Factory method for a AudioBufferSourceNode.
 | 
					
						
							|  |  |  |  |     return AudioBufferSourceNode::create(realm(), *this); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-25 14:30:36 +01:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createchannelmerger
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<GC::Ref<ChannelMergerNode>> BaseAudioContext::create_channel_merger(WebIDL::UnsignedLong number_of_inputs) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     ChannelMergerOptions options; | 
					
						
							|  |  |  |  |     options.number_of_inputs = number_of_inputs; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return ChannelMergerNode::create(realm(), *this, options); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-02 10:25:21 +00:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createconstantsource
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<GC::Ref<ConstantSourceNode>> BaseAudioContext::create_constant_source() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     return ConstantSourceNode::create(realm(), *this); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 00:39:02 +00:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createdelay
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<GC::Ref<DelayNode>> BaseAudioContext::create_delay(double max_delay_time) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     DelayOptions options; | 
					
						
							|  |  |  |  |     options.max_delay_time = max_delay_time; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return DelayNode::create(realm(), *this, options); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-01 22:04:49 +00:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createchannelsplitter
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<GC::Ref<ChannelSplitterNode>> BaseAudioContext::create_channel_splitter(WebIDL::UnsignedLong number_of_outputs) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     ChannelSplitterOptions options; | 
					
						
							|  |  |  |  |     options.number_of_outputs = number_of_outputs; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return ChannelSplitterNode::create(realm(), *this, options); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-28 12:07:14 +12:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createoscillator
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | WebIDL::ExceptionOr<GC::Ref<OscillatorNode>> BaseAudioContext::create_oscillator() | 
					
						
							| 
									
										
										
										
											2024-04-28 12:07:14 +12:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // Factory method for an OscillatorNode.
 | 
					
						
							|  |  |  |  |     return OscillatorNode::create(realm(), *this); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-05 11:18:02 +12:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createdynamicscompressor
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | WebIDL::ExceptionOr<GC::Ref<DynamicsCompressorNode>> BaseAudioContext::create_dynamics_compressor() | 
					
						
							| 
									
										
										
										
											2024-05-05 11:18:02 +12:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // Factory method for a DynamicsCompressorNode.
 | 
					
						
							|  |  |  |  |     return DynamicsCompressorNode::create(realm(), *this); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-28 15:48:10 +12:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-creategain
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | WebIDL::ExceptionOr<GC::Ref<GainNode>> BaseAudioContext::create_gain() | 
					
						
							| 
									
										
										
										
											2024-05-28 15:48:10 +12:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // Factory method for GainNode.
 | 
					
						
							|  |  |  |  |     return GainNode::create(realm(), *this); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-13 21:41:06 +00:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createpanner
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<GC::Ref<PannerNode>> BaseAudioContext::create_panner() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // Factory method for a PannerNode.
 | 
					
						
							|  |  |  |  |     return PannerNode::create(realm(), *this); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-02 12:40:56 +00:00
										 |  |  |  | WebIDL::ExceptionOr<GC::Ref<PeriodicWave>> BaseAudioContext::create_periodic_wave(Vector<float> const& real, Vector<float> const& imag, Optional<PeriodicWaveConstraints> const& constraints) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     PeriodicWaveOptions options; | 
					
						
							|  |  |  |  |     options.real = real; | 
					
						
							|  |  |  |  |     options.imag = imag; | 
					
						
							|  |  |  |  |     if (constraints.has_value()) | 
					
						
							|  |  |  |  |         options.disable_normalization = constraints->disable_normalization; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return PeriodicWave::construct_impl(realm(), *this, options); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-17 15:00:24 +00:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createstereopanner
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<GC::Ref<StereoPannerNode>> BaseAudioContext::create_stereo_panner() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // Factory method for a StereoPannerNode.
 | 
					
						
							|  |  |  |  |     return StereoPannerNode::create(realm(), *this); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 15:17:45 +00:00
										 |  |  |  | WebIDL::ExceptionOr<void> BaseAudioContext::verify_audio_options_inside_nominal_range(JS::Realm& realm, float sample_rate) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     if (sample_rate < MIN_SAMPLE_RATE || sample_rate > MAX_SAMPLE_RATE) | 
					
						
							|  |  |  |  |         return WebIDL::NotSupportedError::create(realm, "Sample rate is outside of allowed range"_string); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return {}; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-25 14:45:59 +12:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<void> BaseAudioContext::verify_audio_options_inside_nominal_range(JS::Realm& realm, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // A NotSupportedError exception MUST be thrown if any of the arguments is negative, zero, or outside its nominal range.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (number_of_channels == 0) | 
					
						
							| 
									
										
										
										
											2024-10-12 20:56:21 +02:00
										 |  |  |  |         return WebIDL::NotSupportedError::create(realm, "Number of channels must not be '0'"_string); | 
					
						
							| 
									
										
										
										
											2024-04-25 14:45:59 +12:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (number_of_channels > MAX_NUMBER_OF_CHANNELS) | 
					
						
							| 
									
										
										
										
											2024-10-12 20:56:21 +02:00
										 |  |  |  |         return WebIDL::NotSupportedError::create(realm, "Number of channels is greater than allowed range"_string); | 
					
						
							| 
									
										
										
										
											2024-04-25 14:45:59 +12:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (length == 0) | 
					
						
							| 
									
										
										
										
											2024-10-12 20:56:21 +02:00
										 |  |  |  |         return WebIDL::NotSupportedError::create(realm, "Length of buffer must be at least 1"_string); | 
					
						
							| 
									
										
										
										
											2024-04-25 14:45:59 +12:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 15:17:45 +00:00
										 |  |  |  |     TRY(verify_audio_options_inside_nominal_range(realm, sample_rate)); | 
					
						
							| 
									
										
										
										
											2024-04-25 14:45:59 +12:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     return {}; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | void BaseAudioContext::queue_a_media_element_task(GC::Ref<GC::Function<void()>> steps) | 
					
						
							| 
									
										
										
										
											2024-10-14 23:43:00 +02:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-21 13:35:24 +13:00
										 |  |  |  |     auto task = HTML::Task::create(vm(), m_media_element_event_task_source.source, HTML::current_principal_settings_object().responsible_document(), steps); | 
					
						
							| 
									
										
										
										
											2024-10-15 09:34:35 +02:00
										 |  |  |  |     HTML::main_thread_event_loop().task_queue().add(task); | 
					
						
							| 
									
										
										
										
											2024-10-14 23:43:00 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-decodeaudiodata
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | GC::Ref<WebIDL::Promise> BaseAudioContext::decode_audio_data(GC::Root<WebIDL::BufferSource> audio_data, GC::Ptr<WebIDL::CallbackType> success_callback, GC::Ptr<WebIDL::CallbackType> error_callback) | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: When decodeAudioData is called, the following steps MUST be performed on the control thread:
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 1. If this's relevant global object's associated Document is not fully active then return a
 | 
					
						
							|  |  |  |  |     //    promise rejected with "InvalidStateError" DOMException.
 | 
					
						
							| 
									
										
										
										
											2025-01-21 09:12:05 -05:00
										 |  |  |  |     auto const& associated_document = as<HTML::Window>(HTML::relevant_global_object(*this)).associated_document(); | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  |     if (!associated_document.is_fully_active()) { | 
					
						
							|  |  |  |  |         auto error = WebIDL::InvalidStateError::create(realm, "The document is not fully active."_string); | 
					
						
							|  |  |  |  |         return WebIDL::create_rejected_promise_from_exception(realm, error); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Let promise be a new Promise.
 | 
					
						
							|  |  |  |  |     auto promise = WebIDL::create_promise(realm); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: 3. If audioData is detached, execute the following steps:
 | 
					
						
							|  |  |  |  |     if (true) { | 
					
						
							| 
									
										
										
										
											2024-10-17 10:46:27 +02:00
										 |  |  |  |         // 3.1. Append promise to [[pending promises]].
 | 
					
						
							|  |  |  |  |         m_pending_promises.append(promise); | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // FIXME: 3.2. Detach the audioData ArrayBuffer. If this operations throws, jump to the step 3.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 3.3. Queue a decoding operation to be performed on another thread.
 | 
					
						
							|  |  |  |  |         queue_a_decoding_operation(promise, move(audio_data), success_callback, error_callback); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. Else, execute the following error steps:
 | 
					
						
							|  |  |  |  |     else { | 
					
						
							|  |  |  |  |         // 4.1. Let error be a DataCloneError.
 | 
					
						
							|  |  |  |  |         auto error = WebIDL::DataCloneError::create(realm, "Audio data is not detached."_string); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 4.2. Reject promise with error, and remove it from [[pending promises]].
 | 
					
						
							|  |  |  |  |         WebIDL::reject_promise(realm, promise, error); | 
					
						
							| 
									
										
										
										
											2024-10-17 10:46:27 +02:00
										 |  |  |  |         m_pending_promises.remove_first_matching([&promise](auto& pending_promise) { | 
					
						
							|  |  |  |  |             return pending_promise == promise; | 
					
						
							|  |  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 4.3. Queue a media element task to invoke errorCallback with error.
 | 
					
						
							|  |  |  |  |         if (error_callback) { | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |             queue_a_media_element_task(GC::create_function(heap(), [&realm, error_callback, error] { | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  |                 auto completion = WebIDL::invoke_callback(*error_callback, {}, error); | 
					
						
							|  |  |  |  |                 if (completion.is_abrupt()) | 
					
						
							|  |  |  |  |                     HTML::report_exception(completion, realm); | 
					
						
							|  |  |  |  |             })); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 5. Return promise.
 | 
					
						
							| 
									
										
										
										
											2024-10-25 12:38:19 -06:00
										 |  |  |  |     return promise; | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-decodeaudiodata
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | void BaseAudioContext::queue_a_decoding_operation(GC::Ref<JS::PromiseCapability> promise, [[maybe_unused]] GC::Root<WebIDL::BufferSource> audio_data, GC::Ptr<WebIDL::CallbackType> success_callback, GC::Ptr<WebIDL::CallbackType> error_callback) | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: When queuing a decoding operation to be performed on another thread, the following steps
 | 
					
						
							|  |  |  |  |     //        MUST happen on a thread that is not the control thread nor the rendering thread, called
 | 
					
						
							|  |  |  |  |     //        the decoding thread.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 1. Let can decode be a boolean flag, initially set to true.
 | 
					
						
							|  |  |  |  |     auto can_decode { true }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: 2. Attempt to determine the MIME type of audioData, using MIME Sniffing § 6.2 Matching an
 | 
					
						
							|  |  |  |  |     //           audio or video type pattern. If the audio or video type pattern matching algorithm returns
 | 
					
						
							|  |  |  |  |     //           undefined, set can decode to false.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. If can decode is true,
 | 
					
						
							|  |  |  |  |     if (can_decode) { | 
					
						
							|  |  |  |  |         // FIXME: attempt to decode the encoded audioData into linear PCM. In case of
 | 
					
						
							|  |  |  |  |         //        failure, set can decode to false.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // FIXME: If the media byte-stream contains multiple audio tracks, only decode the first track to linear pcm.
 | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. If can decode is false,
 | 
					
						
							|  |  |  |  |     if (!can_decode) { | 
					
						
							|  |  |  |  |         // queue a media element task to execute the following steps:
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |         queue_a_media_element_task(GC::create_function(heap(), [this, &realm, promise, error_callback] { | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  |             // 4.1. Let error be a DOMException whose name is EncodingError.
 | 
					
						
							|  |  |  |  |             auto error = WebIDL::EncodingError::create(realm, "Unable to decode."_string); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 10:46:27 +02:00
										 |  |  |  |             // 4.1.2. Reject promise with error, and remove it from [[pending promises]].
 | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  |             WebIDL::reject_promise(realm, promise, error); | 
					
						
							| 
									
										
										
										
											2024-10-17 10:46:27 +02:00
										 |  |  |  |             m_pending_promises.remove_first_matching([&promise](auto& pending_promise) { | 
					
						
							|  |  |  |  |                 return pending_promise == promise; | 
					
						
							|  |  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2024-10-15 00:42:26 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |             // 4.2. If errorCallback is not missing, invoke errorCallback with error.
 | 
					
						
							|  |  |  |  |             if (error_callback) { | 
					
						
							|  |  |  |  |                 auto completion = WebIDL::invoke_callback(*error_callback, {}, error); | 
					
						
							|  |  |  |  |                 if (completion.is_abrupt()) | 
					
						
							|  |  |  |  |                     HTML::report_exception(completion, realm); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         })); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 5. Otherwise:
 | 
					
						
							|  |  |  |  |     else { | 
					
						
							|  |  |  |  |         // FIXME: 5.1. Take the result, representing the decoded linear PCM audio data, and resample it to the
 | 
					
						
							|  |  |  |  |         //             sample-rate of the BaseAudioContext if it is different from the sample-rate of
 | 
					
						
							|  |  |  |  |         //             audioData.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // FIXME: 5.2. queue a media element task to execute the following steps:
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // FIXME: 5.2.1. Let buffer be an AudioBuffer containing the final result (after possibly performing
 | 
					
						
							|  |  |  |  |         //               sample-rate conversion).
 | 
					
						
							|  |  |  |  |         auto buffer = MUST(create_buffer(2, 1, 44100)); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 5.2.2. Resolve promise with buffer.
 | 
					
						
							|  |  |  |  |         WebIDL::resolve_promise(realm, promise, buffer); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 5.2.3. If successCallback is not missing, invoke successCallback with buffer.
 | 
					
						
							|  |  |  |  |         if (success_callback) { | 
					
						
							|  |  |  |  |             auto completion = WebIDL::invoke_callback(*success_callback, {}, buffer); | 
					
						
							|  |  |  |  |             if (completion.is_abrupt()) | 
					
						
							|  |  |  |  |                 HTML::report_exception(completion, realm); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  |  | } |