BaseAudioContext.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <LibWeb/Bindings/BaseAudioContextPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/HTML/EventNames.h>
  10. #include <LibWeb/WebAudio/AudioBuffer.h>
  11. #include <LibWeb/WebAudio/BaseAudioContext.h>
  12. #include <LibWeb/WebAudio/DynamicsCompressorNode.h>
  13. #include <LibWeb/WebAudio/GainNode.h>
  14. #include <LibWeb/WebAudio/OscillatorNode.h>
  15. namespace Web::WebAudio {
  16. BaseAudioContext::BaseAudioContext(JS::Realm& realm, float sample_rate)
  17. : DOM::EventTarget(realm)
  18. , m_sample_rate(sample_rate)
  19. {
  20. }
  21. BaseAudioContext::~BaseAudioContext() = default;
  22. void BaseAudioContext::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(BaseAudioContext);
  26. }
  27. void BaseAudioContext::set_onstatechange(WebIDL::CallbackType* event_handler)
  28. {
  29. set_event_handler_attribute(HTML::EventNames::statechange, event_handler);
  30. }
  31. WebIDL::CallbackType* BaseAudioContext::onstatechange()
  32. {
  33. return event_handler_attribute(HTML::EventNames::statechange);
  34. }
  35. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer
  36. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> BaseAudioContext::create_buffer(WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate)
  37. {
  38. // Creates an AudioBuffer of the given size. The audio data in the buffer will be zero-initialized (silent).
  39. // A NotSupportedError exception MUST be thrown if any of the arguments is negative, zero, or outside its nominal range.
  40. return AudioBuffer::create(realm(), number_of_channels, length, sample_rate);
  41. }
  42. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createoscillator
  43. WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> BaseAudioContext::create_oscillator()
  44. {
  45. // Factory method for an OscillatorNode.
  46. return OscillatorNode::create(realm(), *this);
  47. }
  48. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createdynamicscompressor
  49. WebIDL::ExceptionOr<JS::NonnullGCPtr<DynamicsCompressorNode>> BaseAudioContext::create_dynamics_compressor()
  50. {
  51. // Factory method for a DynamicsCompressorNode.
  52. return DynamicsCompressorNode::create(realm(), *this);
  53. }
  54. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-creategain
  55. JS::NonnullGCPtr<GainNode> BaseAudioContext::create_gain()
  56. {
  57. // Factory method for GainNode.
  58. return GainNode::create(realm(), *this);
  59. }
  60. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer
  61. WebIDL::ExceptionOr<void> BaseAudioContext::verify_audio_options_inside_nominal_range(JS::Realm& realm, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate)
  62. {
  63. // A NotSupportedError exception MUST be thrown if any of the arguments is negative, zero, or outside its nominal range.
  64. if (number_of_channels == 0)
  65. return WebIDL::NotSupportedError::create(realm, "Number of channels must not be '0'"_fly_string);
  66. if (number_of_channels > MAX_NUMBER_OF_CHANNELS)
  67. return WebIDL::NotSupportedError::create(realm, "Number of channels is greater than allowed range"_fly_string);
  68. if (length == 0)
  69. return WebIDL::NotSupportedError::create(realm, "Length of buffer must be at least 1"_fly_string);
  70. if (sample_rate < MIN_SAMPLE_RATE || sample_rate > MAX_SAMPLE_RATE)
  71. return WebIDL::NotSupportedError::create(realm, "Sample rate is outside of allowed range"_fly_string);
  72. return {};
  73. }
  74. }