/src/serenity/Userland/Libraries/LibWeb/WebAudio/AudioContext.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <LibWeb/Bindings/AudioContextPrototype.h> |
10 | | #include <LibWeb/HighResolutionTime/DOMHighResTimeStamp.h> |
11 | | #include <LibWeb/WebAudio/BaseAudioContext.h> |
12 | | |
13 | | namespace Web::WebAudio { |
14 | | |
15 | | struct AudioContextOptions { |
16 | | Bindings::AudioContextLatencyCategory latency_hint = Bindings::AudioContextLatencyCategory::Interactive; |
17 | | Optional<float> sample_rate; |
18 | | }; |
19 | | |
20 | | struct AudioTimestamp { |
21 | | double context_time { 0 }; |
22 | | double performance_time { 0 }; |
23 | | }; |
24 | | |
25 | | // https://webaudio.github.io/web-audio-api/#AudioContext |
26 | | class AudioContext final : public BaseAudioContext { |
27 | | WEB_PLATFORM_OBJECT(AudioContext, BaseAudioContext); |
28 | | JS_DECLARE_ALLOCATOR(AudioContext); |
29 | | |
30 | | public: |
31 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioContext>> construct_impl(JS::Realm&, AudioContextOptions const& context_options = {}); |
32 | | |
33 | | virtual ~AudioContext() override; |
34 | | |
35 | 0 | double base_latency() const { return m_base_latency; } |
36 | 0 | double output_latency() const { return m_output_latency; } |
37 | | AudioTimestamp get_output_timestamp(); |
38 | | WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> resume(); |
39 | | WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> suspend(); |
40 | | WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> close(); |
41 | | |
42 | | private: |
43 | | explicit AudioContext(JS::Realm&, AudioContextOptions const& context_options); |
44 | | |
45 | | virtual void initialize(JS::Realm&) override; |
46 | | virtual void visit_edges(Cell::Visitor&) override; |
47 | | |
48 | | double m_base_latency { 0 }; |
49 | | double m_output_latency { 0 }; |
50 | | |
51 | | bool m_allowed_to_start = true; |
52 | | Vector<JS::NonnullGCPtr<WebIDL::Promise>> m_pending_resume_promises; |
53 | | bool m_suspended_by_user = false; |
54 | | |
55 | | bool start_rendering_audio_graph(); |
56 | | }; |
57 | | |
58 | | } |