BaseAudioContext.cpp 3.3 KB

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