AudioNode.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <LibJS/Forward.h>
  9. #include <LibWeb/Bindings/AudioNodePrototype.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/DOM/EventTarget.h>
  12. #include <LibWeb/WebIDL/Types.h>
  13. namespace Web::WebAudio {
  14. // https://webaudio.github.io/web-audio-api/#AudioNodeOptions
  15. struct AudioNodeOptions {
  16. Optional<WebIDL::UnsignedLong> channel_count;
  17. Optional<Bindings::ChannelCountMode> channel_count_mode;
  18. Optional<Bindings::ChannelInterpretation> channel_interpretation;
  19. };
  20. struct AudioNodeDefaultOptions {
  21. WebIDL::UnsignedLong channel_count;
  22. Bindings::ChannelCountMode channel_count_mode;
  23. Bindings::ChannelInterpretation channel_interpretation;
  24. };
  25. // https://webaudio.github.io/web-audio-api/#AudioNode
  26. class AudioNode : public DOM::EventTarget {
  27. WEB_PLATFORM_OBJECT(AudioNode, DOM::EventTarget);
  28. GC_DECLARE_ALLOCATOR(AudioNode);
  29. public:
  30. virtual ~AudioNode() override;
  31. WebIDL::ExceptionOr<GC::Ref<AudioNode>> connect(GC::Ref<AudioNode> destination_node, WebIDL::UnsignedLong output = 0, WebIDL::UnsignedLong input = 0);
  32. void connect(GC::Ref<AudioParam> destination_param, WebIDL::UnsignedLong output = 0);
  33. void disconnect();
  34. void disconnect(WebIDL::UnsignedLong output);
  35. void disconnect(GC::Ref<AudioNode> destination_node);
  36. void disconnect(GC::Ref<AudioNode> destination_node, WebIDL::UnsignedLong output);
  37. void disconnect(GC::Ref<AudioNode> destination_node, WebIDL::UnsignedLong output, WebIDL::UnsignedLong input);
  38. void disconnect(GC::Ref<AudioParam> destination_param);
  39. void disconnect(GC::Ref<AudioParam> destination_param, WebIDL::UnsignedLong output);
  40. // https://webaudio.github.io/web-audio-api/#dom-audionode-context
  41. GC::Ref<BaseAudioContext const> context() const
  42. {
  43. // The BaseAudioContext which owns this AudioNode.
  44. return m_context;
  45. }
  46. // https://webaudio.github.io/web-audio-api/#dom-audionode-numberofinputs
  47. virtual WebIDL::UnsignedLong number_of_inputs() = 0;
  48. // https://webaudio.github.io/web-audio-api/#dom-audionode-numberofoutputs
  49. virtual WebIDL::UnsignedLong number_of_outputs() = 0;
  50. // https://webaudio.github.io/web-audio-api/#dom-audionode-channelcount
  51. virtual WebIDL::ExceptionOr<void> set_channel_count(WebIDL::UnsignedLong);
  52. virtual WebIDL::UnsignedLong channel_count() const { return m_channel_count; }
  53. virtual WebIDL::ExceptionOr<void> set_channel_count_mode(Bindings::ChannelCountMode);
  54. Bindings::ChannelCountMode channel_count_mode();
  55. virtual WebIDL::ExceptionOr<void> set_channel_interpretation(Bindings::ChannelInterpretation);
  56. Bindings::ChannelInterpretation channel_interpretation();
  57. WebIDL::ExceptionOr<void> initialize_audio_node_options(AudioNodeOptions const& given_options, AudioNodeDefaultOptions const& default_options);
  58. protected:
  59. AudioNode(JS::Realm&, GC::Ref<BaseAudioContext>);
  60. virtual void initialize(JS::Realm&) override;
  61. virtual void visit_edges(Cell::Visitor&) override;
  62. private:
  63. GC::Ref<BaseAudioContext> m_context;
  64. WebIDL::UnsignedLong m_channel_count { 2 };
  65. Bindings::ChannelCountMode m_channel_count_mode { Bindings::ChannelCountMode::Max };
  66. Bindings::ChannelInterpretation m_channel_interpretation { Bindings::ChannelInterpretation::Speakers };
  67. };
  68. }