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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-06-27 13:34:51 -04:00
|
|
|
#include <LibWeb/Bindings/BaseAudioContextPrototype.h>
|
2023-05-21 20:36:22 +01:00
|
|
|
#include <LibWeb/DOM/EventTarget.h>
|
2024-09-18 09:09:18 -04:00
|
|
|
#include <LibWeb/WebAudio/AnalyserNode.h>
|
2024-10-17 21:26:01 +02:00
|
|
|
#include <LibWeb/WebAudio/AudioListener.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>
|
2025-01-01 22:04:49 +00:00
|
|
|
#include <LibWeb/WebAudio/ChannelSplitterNode.h>
|
2025-01-02 10:25:21 +00:00
|
|
|
#include <LibWeb/WebAudio/ConstantSourceNode.h>
|
2025-01-04 00:39:02 +00:00
|
|
|
#include <LibWeb/WebAudio/DelayNode.h>
|
2025-01-02 12:40:56 +00:00
|
|
|
#include <LibWeb/WebAudio/PeriodicWave.h>
|
2024-04-25 14:45:59 +12:00
|
|
|
#include <LibWeb/WebIDL/Types.h>
|
2023-05-21 20:36:22 +01:00
|
|
|
|
|
|
|
namespace Web::WebAudio {
|
|
|
|
|
2024-10-08 12:03:22 +02:00
|
|
|
class AudioDestinationNode;
|
|
|
|
|
2023-05-21 20:36:22 +01:00
|
|
|
// https://webaudio.github.io/web-audio-api/#BaseAudioContext
|
|
|
|
class BaseAudioContext : public DOM::EventTarget {
|
|
|
|
WEB_PLATFORM_OBJECT(BaseAudioContext, DOM::EventTarget);
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~BaseAudioContext() override;
|
|
|
|
|
2024-04-25 14:45:59 +12:00
|
|
|
// https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer-numberofchannels
|
|
|
|
// > An implementation MUST support at least 32 channels.
|
|
|
|
// Other browsers appear to only allow 32 channels - so let's limit ourselves to that too.
|
|
|
|
static constexpr WebIDL::UnsignedLong MAX_NUMBER_OF_CHANNELS { 32 };
|
|
|
|
|
|
|
|
// https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer-samplerate
|
|
|
|
// > An implementation MUST support sample rates in at least the range 8000 to 96000.
|
|
|
|
// This doesn't seem consistent between browsers. We use what firefox accepts from testing BaseAudioContext.createAudioBuffer.
|
|
|
|
static constexpr float MIN_SAMPLE_RATE { 8000 };
|
|
|
|
static constexpr float MAX_SAMPLE_RATE { 192000 };
|
|
|
|
|
2025-01-07 23:24:10 +00:00
|
|
|
GC::Ref<AudioDestinationNode> destination() const { return *m_destination; }
|
2023-07-07 22:48:11 -04:00
|
|
|
float sample_rate() const { return m_sample_rate; }
|
|
|
|
double current_time() const { return m_current_time; }
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<AudioListener> listener() const { return m_listener; }
|
2023-07-07 22:48:11 -04:00
|
|
|
Bindings::AudioContextState state() const { return m_control_thread_state; }
|
2023-06-27 13:34:51 -04:00
|
|
|
|
2024-05-01 21:28:16 +12:00
|
|
|
// https://webaudio.github.io/web-audio-api/#--nyquist-frequency
|
|
|
|
float nyquist_frequency() const { return m_sample_rate / 2; }
|
|
|
|
|
2023-06-27 13:34:51 -04:00
|
|
|
void set_onstatechange(WebIDL::CallbackType*);
|
|
|
|
WebIDL::CallbackType* onstatechange();
|
|
|
|
|
2023-07-07 22:48:11 -04:00
|
|
|
void set_sample_rate(float sample_rate) { m_sample_rate = sample_rate; }
|
|
|
|
void set_control_state(Bindings::AudioContextState state) { m_control_thread_state = state; }
|
|
|
|
void set_rendering_state(Bindings::AudioContextState state) { m_rendering_thread_state = state; }
|
2023-06-27 13:34:51 -04:00
|
|
|
|
2025-01-09 15:17:45 +00:00
|
|
|
static WebIDL::ExceptionOr<void> verify_audio_options_inside_nominal_range(JS::Realm&, float sample_rate);
|
2024-04-25 14:45:59 +12:00
|
|
|
static WebIDL::ExceptionOr<void> verify_audio_options_inside_nominal_range(JS::Realm&, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate);
|
|
|
|
|
2024-09-18 09:09:18 -04:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<AnalyserNode>> create_analyser();
|
2024-11-15 04:01:23 +13:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<BiquadFilterNode>> create_biquad_filter();
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<AudioBuffer>> create_buffer(WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate);
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<AudioBufferSourceNode>> create_buffer_source();
|
2024-11-25 14:30:36 +01:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<ChannelMergerNode>> create_channel_merger(WebIDL::UnsignedLong number_of_inputs);
|
2025-01-02 10:25:21 +00:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<ConstantSourceNode>> create_constant_source();
|
2025-01-01 22:04:49 +00:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<ChannelSplitterNode>> create_channel_splitter(WebIDL::UnsignedLong number_of_outputs);
|
2025-01-04 00:39:02 +00:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<DelayNode>> create_delay(double max_delay_time = 1);
|
2024-11-15 04:01:23 +13:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<OscillatorNode>> create_oscillator();
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<DynamicsCompressorNode>> create_dynamics_compressor();
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<GainNode>> create_gain();
|
2024-12-13 21:41:06 +00:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<PannerNode>> create_panner();
|
2025-01-02 12:40:56 +00:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<PeriodicWave>> create_periodic_wave(Vector<float> const& real, Vector<float> const& imag, Optional<PeriodicWaveConstraints> const& constraints = {});
|
2024-04-28 12:07:14 +12:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<WebIDL::Promise> decode_audio_data(GC::Root<WebIDL::BufferSource>, GC::Ptr<WebIDL::CallbackType>, GC::Ptr<WebIDL::CallbackType>);
|
2024-10-15 00:42:26 +02:00
|
|
|
|
2023-05-21 20:36:22 +01:00
|
|
|
protected:
|
2024-05-02 00:03:32 +12:00
|
|
|
explicit BaseAudioContext(JS::Realm&, float m_sample_rate = 0);
|
2023-05-21 20:36:22 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
void queue_a_media_element_task(GC::Ref<GC::Function<void()>>);
|
2023-06-27 13:34:51 -04:00
|
|
|
|
2024-07-27 16:38:09 +03:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
2024-07-24 17:50:06 +12:00
|
|
|
|
2025-01-07 23:24:10 +00:00
|
|
|
GC::Ptr<AudioDestinationNode> m_destination;
|
2024-11-15 04:01:23 +13:00
|
|
|
Vector<GC::Ref<WebIDL::Promise>> m_pending_promises;
|
2024-10-14 23:43:00 +02:00
|
|
|
|
2023-06-27 13:34:51 -04:00
|
|
|
private:
|
2024-11-15 04:01:23 +13:00
|
|
|
void queue_a_decoding_operation(GC::Ref<JS::PromiseCapability>, GC::Root<WebIDL::BufferSource>, GC::Ptr<WebIDL::CallbackType>, GC::Ptr<WebIDL::CallbackType>);
|
2024-10-15 00:42:26 +02:00
|
|
|
|
2023-06-27 13:34:51 -04:00
|
|
|
float m_sample_rate { 0 };
|
|
|
|
double m_current_time { 0 };
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<AudioListener> m_listener;
|
2024-10-17 21:26:01 +02:00
|
|
|
|
2023-06-27 13:34:51 -04:00
|
|
|
Bindings::AudioContextState m_control_thread_state = Bindings::AudioContextState::Suspended;
|
|
|
|
Bindings::AudioContextState m_rendering_thread_state = Bindings::AudioContextState::Suspended;
|
2024-10-14 23:43:00 +02:00
|
|
|
|
|
|
|
HTML::UniqueTaskSource m_media_element_event_task_source {};
|
2023-05-21 20:36:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|