BaseAudioContext.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/AudioListener.h>
  12. #include <LibWeb/WebAudio/BiquadFilterNode.h>
  13. #include <LibWeb/WebAudio/ChannelMergerNode.h>
  14. #include <LibWeb/WebIDL/Types.h>
  15. namespace Web::WebAudio {
  16. class AudioDestinationNode;
  17. // https://webaudio.github.io/web-audio-api/#BaseAudioContext
  18. class BaseAudioContext : public DOM::EventTarget {
  19. WEB_PLATFORM_OBJECT(BaseAudioContext, DOM::EventTarget);
  20. public:
  21. virtual ~BaseAudioContext() override;
  22. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer-numberofchannels
  23. // > An implementation MUST support at least 32 channels.
  24. // Other browsers appear to only allow 32 channels - so let's limit ourselves to that too.
  25. static constexpr WebIDL::UnsignedLong MAX_NUMBER_OF_CHANNELS { 32 };
  26. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer-samplerate
  27. // > An implementation MUST support sample rates in at least the range 8000 to 96000.
  28. // This doesn't seem consistent between browsers. We use what firefox accepts from testing BaseAudioContext.createAudioBuffer.
  29. static constexpr float MIN_SAMPLE_RATE { 8000 };
  30. static constexpr float MAX_SAMPLE_RATE { 192000 };
  31. GC::Ref<AudioDestinationNode> destination() const { return m_destination; }
  32. float sample_rate() const { return m_sample_rate; }
  33. double current_time() const { return m_current_time; }
  34. GC::Ref<AudioListener> listener() const { return m_listener; }
  35. Bindings::AudioContextState state() const { return m_control_thread_state; }
  36. // https://webaudio.github.io/web-audio-api/#--nyquist-frequency
  37. float nyquist_frequency() const { return m_sample_rate / 2; }
  38. void set_onstatechange(WebIDL::CallbackType*);
  39. WebIDL::CallbackType* onstatechange();
  40. void set_sample_rate(float sample_rate) { m_sample_rate = sample_rate; }
  41. void set_control_state(Bindings::AudioContextState state) { m_control_thread_state = state; }
  42. void set_rendering_state(Bindings::AudioContextState state) { m_rendering_thread_state = state; }
  43. static WebIDL::ExceptionOr<void> verify_audio_options_inside_nominal_range(JS::Realm&, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate);
  44. WebIDL::ExceptionOr<GC::Ref<BiquadFilterNode>> create_biquad_filter();
  45. WebIDL::ExceptionOr<GC::Ref<AudioBuffer>> create_buffer(WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate);
  46. WebIDL::ExceptionOr<GC::Ref<AudioBufferSourceNode>> create_buffer_source();
  47. WebIDL::ExceptionOr<GC::Ref<ChannelMergerNode>> create_channel_merger(WebIDL::UnsignedLong number_of_inputs);
  48. WebIDL::ExceptionOr<GC::Ref<OscillatorNode>> create_oscillator();
  49. WebIDL::ExceptionOr<GC::Ref<DynamicsCompressorNode>> create_dynamics_compressor();
  50. WebIDL::ExceptionOr<GC::Ref<GainNode>> create_gain();
  51. WebIDL::ExceptionOr<GC::Ref<PannerNode>> create_panner();
  52. GC::Ref<WebIDL::Promise> decode_audio_data(GC::Root<WebIDL::BufferSource>, GC::Ptr<WebIDL::CallbackType>, GC::Ptr<WebIDL::CallbackType>);
  53. protected:
  54. explicit BaseAudioContext(JS::Realm&, float m_sample_rate = 0);
  55. void queue_a_media_element_task(GC::Ref<GC::Function<void()>>);
  56. virtual void initialize(JS::Realm&) override;
  57. virtual void visit_edges(Cell::Visitor&) override;
  58. GC::Ref<AudioDestinationNode> m_destination;
  59. Vector<GC::Ref<WebIDL::Promise>> m_pending_promises;
  60. private:
  61. void queue_a_decoding_operation(GC::Ref<JS::PromiseCapability>, GC::Root<WebIDL::BufferSource>, GC::Ptr<WebIDL::CallbackType>, GC::Ptr<WebIDL::CallbackType>);
  62. float m_sample_rate { 0 };
  63. double m_current_time { 0 };
  64. GC::Ref<AudioListener> m_listener;
  65. Bindings::AudioContextState m_control_thread_state = Bindings::AudioContextState::Suspended;
  66. Bindings::AudioContextState m_rendering_thread_state = Bindings::AudioContextState::Suspended;
  67. HTML::UniqueTaskSource m_media_element_event_task_source {};
  68. };
  69. }