OscillatorNode.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/OscillatorNodePrototype.h>
  8. #include <LibWeb/WebAudio/OscillatorNode.h>
  9. namespace Web::WebAudio {
  10. JS_DEFINE_ALLOCATOR(OscillatorNode);
  11. OscillatorNode::~OscillatorNode() = default;
  12. WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> OscillatorNode::create(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext> context, OscillatorOptions const& options)
  13. {
  14. return construct_impl(realm, context, options);
  15. }
  16. // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-oscillatornode
  17. WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> OscillatorNode::construct_impl(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext> context, OscillatorOptions const& options)
  18. {
  19. // FIXME: Invoke "Initialize the AudioNode" steps.
  20. TRY(verify_valid_type(realm, options.type));
  21. auto node = realm.vm().heap().allocate<OscillatorNode>(realm, realm, context, options);
  22. return node;
  23. }
  24. OscillatorNode::OscillatorNode(JS::Realm& realm, JS::NonnullGCPtr<BaseAudioContext> context, OscillatorOptions const&)
  25. : AudioScheduledSourceNode(realm, context)
  26. {
  27. }
  28. // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
  29. Bindings::OscillatorType OscillatorNode::type() const
  30. {
  31. return m_type;
  32. }
  33. // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
  34. WebIDL::ExceptionOr<void> OscillatorNode::verify_valid_type(JS::Realm& realm, Bindings::OscillatorType type)
  35. {
  36. // The shape of the periodic waveform. It may directly be set to any of the type constant values except
  37. // for "custom". ⌛ Doing so MUST throw an InvalidStateError exception. The setPeriodicWave() method can
  38. // be used to set a custom waveform, which results in this attribute being set to "custom". The default
  39. // value is "sine". When this attribute is set, the phase of the oscillator MUST be conserved.
  40. if (type == Bindings::OscillatorType::Custom)
  41. return WebIDL::InvalidStateError::create(realm, "Oscillator node type cannot be set to 'custom'"_fly_string);
  42. return {};
  43. }
  44. // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
  45. WebIDL::ExceptionOr<void> OscillatorNode::set_type(Bindings::OscillatorType type)
  46. {
  47. TRY(verify_valid_type(realm(), type));
  48. m_type = type;
  49. return {};
  50. }
  51. void OscillatorNode::initialize(JS::Realm& realm)
  52. {
  53. Base::initialize(realm);
  54. WEB_SET_PROTOTYPE_FOR_INTERFACE(OscillatorNode);
  55. }
  56. void OscillatorNode::visit_edges(Cell::Visitor& visitor)
  57. {
  58. Base::visit_edges(visitor);
  59. }
  60. }