BaseAudioContext.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  4. * Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <LibWeb/Bindings/BaseAudioContextPrototype.h>
  10. #include <LibWeb/DOM/EventTarget.h>
  11. #include <LibWeb/WebAudio/BiquadFilterNode.h>
  12. #include <LibWeb/WebIDL/Types.h>
  13. namespace Web::WebAudio {
  14. class AudioDestinationNode;
  15. // https://webaudio.github.io/web-audio-api/#BaseAudioContext
  16. class BaseAudioContext : public DOM::EventTarget {
  17. WEB_PLATFORM_OBJECT(BaseAudioContext, DOM::EventTarget);
  18. public:
  19. virtual ~BaseAudioContext() override;
  20. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer-numberofchannels
  21. // > An implementation MUST support at least 32 channels.
  22. // Other browsers appear to only allow 32 channels - so let's limit ourselves to that too.
  23. static constexpr WebIDL::UnsignedLong MAX_NUMBER_OF_CHANNELS { 32 };
  24. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer-samplerate
  25. // > An implementation MUST support sample rates in at least the range 8000 to 96000.
  26. // This doesn't seem consistent between browsers. We use what firefox accepts from testing BaseAudioContext.createAudioBuffer.
  27. static constexpr float MIN_SAMPLE_RATE { 8000 };
  28. static constexpr float MAX_SAMPLE_RATE { 192000 };
  29. JS::NonnullGCPtr<AudioDestinationNode> destination() const { return m_destination; }
  30. float sample_rate() const { return m_sample_rate; }
  31. double current_time() const { return m_current_time; }
  32. Bindings::AudioContextState state() const { return m_control_thread_state; }
  33. // https://webaudio.github.io/web-audio-api/#--nyquist-frequency
  34. float nyquist_frequency() const { return m_sample_rate / 2; }
  35. void set_onstatechange(WebIDL::CallbackType*);
  36. WebIDL::CallbackType* onstatechange();
  37. void set_sample_rate(float sample_rate) { m_sample_rate = sample_rate; }
  38. void set_control_state(Bindings::AudioContextState state) { m_control_thread_state = state; }
  39. void set_rendering_state(Bindings::AudioContextState state) { m_rendering_thread_state = state; }
  40. static WebIDL::ExceptionOr<void> verify_audio_options_inside_nominal_range(JS::Realm&, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate);
  41. WebIDL::ExceptionOr<JS::NonnullGCPtr<BiquadFilterNode>> create_biquad_filter();
  42. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> create_buffer(WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate);
  43. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBufferSourceNode>> create_buffer_source();
  44. WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> create_oscillator();
  45. WebIDL::ExceptionOr<JS::NonnullGCPtr<DynamicsCompressorNode>> create_dynamics_compressor();
  46. JS::NonnullGCPtr<GainNode> create_gain();
  47. JS::NonnullGCPtr<JS::Promise> decode_audio_data(JS::Handle<WebIDL::BufferSource>, JS::GCPtr<WebIDL::CallbackType>, JS::GCPtr<WebIDL::CallbackType>);
  48. protected:
  49. explicit BaseAudioContext(JS::Realm&, float m_sample_rate = 0);
  50. void queue_a_media_element_task(JS::NonnullGCPtr<JS::HeapFunction<void()>>);
  51. virtual void initialize(JS::Realm&) override;
  52. virtual void visit_edges(Cell::Visitor&) override;
  53. JS::NonnullGCPtr<AudioDestinationNode> m_destination;
  54. Vector<JS::NonnullGCPtr<WebIDL::Promise>> m_pending_promises;
  55. private:
  56. void queue_a_decoding_operation(JS::NonnullGCPtr<JS::PromiseCapability>, JS::Handle<WebIDL::BufferSource>, JS::GCPtr<WebIDL::CallbackType>, JS::GCPtr<WebIDL::CallbackType>);
  57. float m_sample_rate { 0 };
  58. double m_current_time { 0 };
  59. Bindings::AudioContextState m_control_thread_state = Bindings::AudioContextState::Suspended;
  60. Bindings::AudioContextState m_rendering_thread_state = Bindings::AudioContextState::Suspended;
  61. HTML::UniqueTaskSource m_media_element_event_task_source {};
  62. };
  63. }