BaseAudioContext.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <LibWeb/Bindings/BaseAudioContextPrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/HTML/EventNames.h>
  11. #include <LibWeb/WebAudio/AudioBuffer.h>
  12. #include <LibWeb/WebAudio/AudioBufferSourceNode.h>
  13. #include <LibWeb/WebAudio/AudioDestinationNode.h>
  14. #include <LibWeb/WebAudio/BaseAudioContext.h>
  15. #include <LibWeb/WebAudio/BiquadFilterNode.h>
  16. #include <LibWeb/WebAudio/DynamicsCompressorNode.h>
  17. #include <LibWeb/WebAudio/GainNode.h>
  18. #include <LibWeb/WebAudio/OscillatorNode.h>
  19. namespace Web::WebAudio {
  20. BaseAudioContext::BaseAudioContext(JS::Realm& realm, float sample_rate)
  21. : DOM::EventTarget(realm)
  22. , m_destination(AudioDestinationNode::construct_impl(realm, *this))
  23. , m_sample_rate(sample_rate)
  24. {
  25. }
  26. BaseAudioContext::~BaseAudioContext() = default;
  27. void BaseAudioContext::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(BaseAudioContext);
  31. }
  32. void BaseAudioContext::visit_edges(Cell::Visitor& visitor)
  33. {
  34. Base::visit_edges(visitor);
  35. visitor.visit(m_destination);
  36. }
  37. void BaseAudioContext::set_onstatechange(WebIDL::CallbackType* event_handler)
  38. {
  39. set_event_handler_attribute(HTML::EventNames::statechange, event_handler);
  40. }
  41. WebIDL::CallbackType* BaseAudioContext::onstatechange()
  42. {
  43. return event_handler_attribute(HTML::EventNames::statechange);
  44. }
  45. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbiquadfilter
  46. WebIDL::ExceptionOr<JS::NonnullGCPtr<BiquadFilterNode>> BaseAudioContext::create_biquad_filter()
  47. {
  48. // Factory method for a BiquadFilterNode representing a second order filter which can be configured as one of several common filter types.
  49. return BiquadFilterNode::create(realm(), *this);
  50. }
  51. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer
  52. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> BaseAudioContext::create_buffer(WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate)
  53. {
  54. // Creates an AudioBuffer of the given size. The audio data in the buffer will be zero-initialized (silent).
  55. // A NotSupportedError exception MUST be thrown if any of the arguments is negative, zero, or outside its nominal range.
  56. return AudioBuffer::create(realm(), number_of_channels, length, sample_rate);
  57. }
  58. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffersource
  59. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBufferSourceNode>> BaseAudioContext::create_buffer_source()
  60. {
  61. // Factory method for a AudioBufferSourceNode.
  62. return AudioBufferSourceNode::create(realm(), *this);
  63. }
  64. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createoscillator
  65. WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> BaseAudioContext::create_oscillator()
  66. {
  67. // Factory method for an OscillatorNode.
  68. return OscillatorNode::create(realm(), *this);
  69. }
  70. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createdynamicscompressor
  71. WebIDL::ExceptionOr<JS::NonnullGCPtr<DynamicsCompressorNode>> BaseAudioContext::create_dynamics_compressor()
  72. {
  73. // Factory method for a DynamicsCompressorNode.
  74. return DynamicsCompressorNode::create(realm(), *this);
  75. }
  76. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-creategain
  77. JS::NonnullGCPtr<GainNode> BaseAudioContext::create_gain()
  78. {
  79. // Factory method for GainNode.
  80. return GainNode::create(realm(), *this);
  81. }
  82. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer
  83. WebIDL::ExceptionOr<void> BaseAudioContext::verify_audio_options_inside_nominal_range(JS::Realm& realm, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate)
  84. {
  85. // A NotSupportedError exception MUST be thrown if any of the arguments is negative, zero, or outside its nominal range.
  86. if (number_of_channels == 0)
  87. return WebIDL::NotSupportedError::create(realm, "Number of channels must not be '0'"_string);
  88. if (number_of_channels > MAX_NUMBER_OF_CHANNELS)
  89. return WebIDL::NotSupportedError::create(realm, "Number of channels is greater than allowed range"_string);
  90. if (length == 0)
  91. return WebIDL::NotSupportedError::create(realm, "Length of buffer must be at least 1"_string);
  92. if (sample_rate < MIN_SAMPLE_RATE || sample_rate > MAX_SAMPLE_RATE)
  93. return WebIDL::NotSupportedError::create(realm, "Sample rate is outside of allowed range"_string);
  94. return {};
  95. }
  96. void BaseAudioContext::queue_a_media_element_task(Function<void()> steps)
  97. {
  98. auto task = HTML::Task::create(vm(), m_media_element_event_task_source.source, HTML::current_settings_object().responsible_document(), JS::create_heap_function(heap(), move(steps)));
  99. HTML::main_thread_event_loop().task_queue().add(move(task));
  100. }
  101. }