BaseAudioContext.h 3.3 KB

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