OscillatorNode.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/AudioParam.h>
  9. #include <LibWeb/WebAudio/BaseAudioContext.h>
  10. #include <LibWeb/WebAudio/OscillatorNode.h>
  11. namespace Web::WebAudio {
  12. GC_DEFINE_ALLOCATOR(OscillatorNode);
  13. OscillatorNode::~OscillatorNode() = default;
  14. WebIDL::ExceptionOr<GC::Ref<OscillatorNode>> OscillatorNode::create(JS::Realm& realm, GC::Ref<BaseAudioContext> context, OscillatorOptions const& options)
  15. {
  16. return construct_impl(realm, context, options);
  17. }
  18. // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-oscillatornode
  19. WebIDL::ExceptionOr<GC::Ref<OscillatorNode>> OscillatorNode::construct_impl(JS::Realm& realm, GC::Ref<BaseAudioContext> context, OscillatorOptions const& options)
  20. {
  21. // FIXME: Invoke "Initialize the AudioNode" steps.
  22. TRY(verify_valid_type(realm, options.type));
  23. auto node = realm.create<OscillatorNode>(realm, context, options);
  24. return node;
  25. }
  26. OscillatorNode::OscillatorNode(JS::Realm& realm, GC::Ref<BaseAudioContext> context, OscillatorOptions const& options)
  27. : AudioScheduledSourceNode(realm, context)
  28. , m_frequency(AudioParam::create(realm, options.frequency, -context->nyquist_frequency(), context->nyquist_frequency(), Bindings::AutomationRate::ARate))
  29. {
  30. }
  31. // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
  32. Bindings::OscillatorType OscillatorNode::type() const
  33. {
  34. return m_type;
  35. }
  36. // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
  37. WebIDL::ExceptionOr<void> OscillatorNode::verify_valid_type(JS::Realm& realm, Bindings::OscillatorType type)
  38. {
  39. // The shape of the periodic waveform. It may directly be set to any of the type constant values except
  40. // for "custom". ⌛ Doing so MUST throw an InvalidStateError exception. The setPeriodicWave() method can
  41. // be used to set a custom waveform, which results in this attribute being set to "custom". The default
  42. // value is "sine". When this attribute is set, the phase of the oscillator MUST be conserved.
  43. if (type == Bindings::OscillatorType::Custom)
  44. return WebIDL::InvalidStateError::create(realm, "Oscillator node type cannot be set to 'custom'"_string);
  45. return {};
  46. }
  47. // https://webaudio.github.io/web-audio-api/#dom-oscillatornode-type
  48. WebIDL::ExceptionOr<void> OscillatorNode::set_type(Bindings::OscillatorType type)
  49. {
  50. TRY(verify_valid_type(realm(), type));
  51. m_type = type;
  52. return {};
  53. }
  54. void OscillatorNode::initialize(JS::Realm& realm)
  55. {
  56. Base::initialize(realm);
  57. WEB_SET_PROTOTYPE_FOR_INTERFACE(OscillatorNode);
  58. }
  59. void OscillatorNode::visit_edges(Cell::Visitor& visitor)
  60. {
  61. Base::visit_edges(visitor);
  62. visitor.visit(m_frequency);
  63. }
  64. }