| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-27 13:34:51 -04:00
										 |  |  | #include <LibWeb/Bindings/AudioContextPrototype.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HighResolutionTime/DOMHighResTimeStamp.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  | #include <LibWeb/WebAudio/BaseAudioContext.h>
 | 
					
						
							| 
									
										
										
										
											2025-01-15 23:08:23 +00:00
										 |  |  | #include <LibWeb/WebAudio/MediaElementAudioSourceNode.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::WebAudio { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-27 13:34:51 -04:00
										 |  |  | struct AudioContextOptions { | 
					
						
							|  |  |  |     Bindings::AudioContextLatencyCategory latency_hint = Bindings::AudioContextLatencyCategory::Interactive; | 
					
						
							|  |  |  |     Optional<float> sample_rate; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct AudioTimestamp { | 
					
						
							|  |  |  |     double context_time { 0 }; | 
					
						
							|  |  |  |     double performance_time { 0 }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  | // https://webaudio.github.io/web-audio-api/#AudioContext
 | 
					
						
							|  |  |  | class AudioContext final : public BaseAudioContext { | 
					
						
							|  |  |  |     WEB_PLATFORM_OBJECT(AudioContext, BaseAudioContext); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC_DECLARE_ALLOCATOR(AudioContext); | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2025-01-09 15:17:45 +00:00
										 |  |  |     static WebIDL::ExceptionOr<GC::Ref<AudioContext>> construct_impl(JS::Realm&, Optional<AudioContextOptions> const& context_options = {}); | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual ~AudioContext() override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 22:48:11 -04:00
										 |  |  |     double base_latency() const { return m_base_latency; } | 
					
						
							|  |  |  |     double output_latency() const { return m_output_latency; } | 
					
						
							| 
									
										
										
										
											2023-06-27 13:34:51 -04:00
										 |  |  |     AudioTimestamp get_output_timestamp(); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> resume(); | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> suspend(); | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> close(); | 
					
						
							| 
									
										
										
										
											2023-06-27 13:34:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-15 23:08:23 +00:00
										 |  |  |     WebIDL::ExceptionOr<GC::Ref<MediaElementAudioSourceNode>> create_media_element_source(GC::Ptr<HTML::HTMLMediaElement>); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2025-01-09 15:17:45 +00:00
										 |  |  |     explicit AudioContext(JS::Realm& realm) | 
					
						
							|  |  |  |         : BaseAudioContext(realm) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     virtual void initialize(JS::Realm&) override; | 
					
						
							| 
									
										
										
										
											2023-06-27 13:34:51 -04:00
										 |  |  |     virtual void visit_edges(Cell::Visitor&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     double m_base_latency { 0 }; | 
					
						
							|  |  |  |     double m_output_latency { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool m_allowed_to_start = true; | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     Vector<GC::Ref<WebIDL::Promise>> m_pending_resume_promises; | 
					
						
							| 
									
										
										
										
											2023-06-27 13:34:51 -04:00
										 |  |  |     bool m_suspended_by_user = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool start_rendering_audio_graph(); | 
					
						
							| 
									
										
										
										
											2023-05-21 20:36:22 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |