| 
									
										
										
										
											2024-04-28 13:29:14 +12:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org> | 
					
						
							| 
									
										
										
										
											2025-01-03 18:07:01 +00:00
										 |  |  |  * Copyright (c) 2025, Tim Ledbetter <tim.ledbetter@ladybird.org> | 
					
						
							| 
									
										
										
										
											2024-04-28 13:29:14 +12:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 17:37:23 +00:00
										 |  |  | #include <AK/Math.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-28 13:29:14 +12:00
										 |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/OscillatorNodePrototype.h>
 | 
					
						
							| 
									
										
										
										
											2024-05-01 21:34:44 +12:00
										 |  |  | #include <LibWeb/WebAudio/AudioParam.h>
 | 
					
						
							|  |  |  | #include <LibWeb/WebAudio/BaseAudioContext.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-28 13:29:14 +12:00
										 |  |  | #include <LibWeb/WebAudio/OscillatorNode.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::WebAudio { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(OscillatorNode); | 
					
						
							| 
									
										
										
										
											2024-04-28 13:29:14 +12:00
										 |  |  | 
 | 
					
						
							|  |  |  | OscillatorNode::~OscillatorNode() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | WebIDL::ExceptionOr<GC::Ref<OscillatorNode>> OscillatorNode::create(JS::Realm& realm, GC::Ref<BaseAudioContext> context, OscillatorOptions const& options) | 
					
						
							| 
									
										
										
										
											2024-04-28 13:29:14 +12:00
										 |  |  | { | 
					
						
							|  |  |  |     return construct_impl(realm, context, options); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-oscillatornode
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | WebIDL::ExceptionOr<GC::Ref<OscillatorNode>> OscillatorNode::construct_impl(JS::Realm& realm, GC::Ref<BaseAudioContext> context, OscillatorOptions const& options) | 
					
						
							| 
									
										
										
										
											2024-04-29 20:03:15 +12:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-03 18:07:01 +00:00
										 |  |  |     if (options.type == Bindings::OscillatorType::Custom && !options.periodic_wave) | 
					
						
							| 
									
										
										
										
											2025-08-07 19:31:52 -04:00
										 |  |  |         return WebIDL::InvalidStateError::create(realm, "Oscillator node type 'custom' requires PeriodicWave to be provided"_utf16); | 
					
						
							| 
									
										
										
										
											2025-01-03 18:07:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     auto node = realm.create<OscillatorNode>(realm, context, options); | 
					
						
							| 
									
										
										
										
											2025-01-03 17:28:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 18:07:01 +00:00
										 |  |  |     if (options.type == Bindings::OscillatorType::Custom) | 
					
						
							|  |  |  |         node->set_periodic_wave(options.periodic_wave); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 17:28:51 +00:00
										 |  |  |     // Default options for channel count and interpretation
 | 
					
						
							|  |  |  |     // https://webaudio.github.io/web-audio-api/#OscillatorNode
 | 
					
						
							|  |  |  |     AudioNodeDefaultOptions default_options; | 
					
						
							|  |  |  |     default_options.channel_count = 2; | 
					
						
							|  |  |  |     default_options.channel_count_mode = Bindings::ChannelCountMode::Max; | 
					
						
							|  |  |  |     default_options.channel_interpretation = Bindings::ChannelInterpretation::Speakers; | 
					
						
							|  |  |  |     // FIXME: Set tail-time to no
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TRY(node->initialize_audio_node_options(options, default_options)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 20:53:39 +12:00
										 |  |  |     return node; | 
					
						
							| 
									
										
										
										
											2024-04-29 20:03:15 +12:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | OscillatorNode::OscillatorNode(JS::Realm& realm, GC::Ref<BaseAudioContext> context, OscillatorOptions const& options) | 
					
						
							| 
									
										
										
										
											2024-04-29 20:03:15 +12:00
										 |  |  |     : AudioScheduledSourceNode(realm, context) | 
					
						
							| 
									
										
										
										
											2025-01-03 17:44:09 +00:00
										 |  |  |     , m_type(options.type) | 
					
						
							| 
									
										
										
										
											2025-01-08 20:04:28 +00:00
										 |  |  |     , m_frequency(AudioParam::create(realm, context, options.frequency, -context->nyquist_frequency(), context->nyquist_frequency(), Bindings::AutomationRate::ARate)) | 
					
						
							|  |  |  |     , m_detune(AudioParam::create(realm, context, options.detune, -1200 * AK::log2(NumericLimits<float>::max()), 1200 * AK::log2(NumericLimits<float>::max()), Bindings::AutomationRate::ARate)) | 
					
						
							| 
									
										
										
										
											2024-04-28 13:29:14 +12:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 20:53:39 +12:00
										 |  |  | // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
 | 
					
						
							|  |  |  | Bindings::OscillatorType OscillatorNode::type() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_type; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
 | 
					
						
							| 
									
										
										
										
											2025-01-03 18:07:01 +00:00
										 |  |  | WebIDL::ExceptionOr<void> OscillatorNode::set_type(Bindings::OscillatorType type) | 
					
						
							| 
									
										
										
										
											2024-05-01 20:53:39 +12:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-03 18:07:01 +00:00
										 |  |  |     if (type == Bindings::OscillatorType::Custom && m_type != Bindings::OscillatorType::Custom) | 
					
						
							| 
									
										
										
										
											2025-08-07 19:31:52 -04:00
										 |  |  |         return WebIDL::InvalidStateError::create(realm(), "Oscillator node type cannot be changed to 'custom'"_utf16); | 
					
						
							| 
									
										
										
										
											2024-05-01 20:53:39 +12:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 18:07:01 +00:00
										 |  |  |     // FIXME: An appropriate PeriodicWave should be set here based on the given type.
 | 
					
						
							|  |  |  |     set_periodic_wave(nullptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_type = type; | 
					
						
							| 
									
										
										
										
											2024-05-01 20:53:39 +12:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 18:07:01 +00:00
										 |  |  | // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-setperiodicwave
 | 
					
						
							|  |  |  | void OscillatorNode::set_periodic_wave(GC::Ptr<PeriodicWave> periodic_wave) | 
					
						
							| 
									
										
										
										
											2024-05-01 20:53:39 +12:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-03 18:07:01 +00:00
										 |  |  |     m_periodic_wave = periodic_wave; | 
					
						
							|  |  |  |     m_type = Bindings::OscillatorType::Custom; | 
					
						
							| 
									
										
										
										
											2024-05-01 20:53:39 +12:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-28 13:29:14 +12:00
										 |  |  | void OscillatorNode::initialize(JS::Realm& realm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(OscillatorNode); | 
					
						
							| 
									
										
										
										
											2025-04-20 16:22:57 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2024-04-28 13:29:14 +12:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OscillatorNode::visit_edges(Cell::Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							| 
									
										
										
										
											2024-05-01 21:34:44 +12:00
										 |  |  |     visitor.visit(m_frequency); | 
					
						
							| 
									
										
										
										
											2025-01-03 17:37:23 +00:00
										 |  |  |     visitor.visit(m_detune); | 
					
						
							| 
									
										
										
										
											2025-01-03 18:07:01 +00:00
										 |  |  |     visitor.visit(m_periodic_wave); | 
					
						
							| 
									
										
										
										
											2024-04-28 13:29:14 +12:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |