BaseAudioContext.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/BaseAudioContext.h>
  11. #include <LibWeb/WebAudio/OscillatorNode.h>
  12. namespace Web::WebAudio {
  13. BaseAudioContext::BaseAudioContext(JS::Realm& realm)
  14. : DOM::EventTarget(realm)
  15. {
  16. }
  17. BaseAudioContext::~BaseAudioContext() = default;
  18. void BaseAudioContext::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. WEB_SET_PROTOTYPE_FOR_INTERFACE(BaseAudioContext);
  22. }
  23. void BaseAudioContext::set_onstatechange(WebIDL::CallbackType* event_handler)
  24. {
  25. set_event_handler_attribute(HTML::EventNames::statechange, event_handler);
  26. }
  27. WebIDL::CallbackType* BaseAudioContext::onstatechange()
  28. {
  29. return event_handler_attribute(HTML::EventNames::statechange);
  30. }
  31. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createoscillator
  32. WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> BaseAudioContext::create_oscillator()
  33. {
  34. // Factory method for an OscillatorNode.
  35. return OscillatorNode::create(realm(), *this);
  36. }
  37. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer
  38. WebIDL::ExceptionOr<void> BaseAudioContext::verify_audio_options_inside_nominal_range(JS::Realm& realm, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate)
  39. {
  40. // A NotSupportedError exception MUST be thrown if any of the arguments is negative, zero, or outside its nominal range.
  41. if (number_of_channels == 0)
  42. return WebIDL::NotSupportedError::create(realm, "Number of channels must not be '0'"_fly_string);
  43. if (number_of_channels > MAX_NUMBER_OF_CHANNELS)
  44. return WebIDL::NotSupportedError::create(realm, "Number of channels is greater than allowed range"_fly_string);
  45. if (length == 0)
  46. return WebIDL::NotSupportedError::create(realm, "Length of buffer must be at least 1"_fly_string);
  47. if (sample_rate < MIN_SAMPLE_RATE || sample_rate > MAX_SAMPLE_RATE)
  48. return WebIDL::NotSupportedError::create(realm, "Sample rate is outside of allowed range"_fly_string);
  49. return {};
  50. }
  51. }