AudioNode.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/WebAudio/AudioNode.h>
  8. #include <LibWeb/WebAudio/BaseAudioContext.h>
  9. namespace Web::WebAudio {
  10. GC_DEFINE_ALLOCATOR(AudioNode);
  11. AudioNode::AudioNode(JS::Realm& realm, GC::Ref<BaseAudioContext> context)
  12. : DOM::EventTarget(realm)
  13. , m_context(context)
  14. {
  15. }
  16. AudioNode::~AudioNode() = default;
  17. WebIDL::ExceptionOr<void> AudioNode::initialize_audio_node_options(AudioNodeOptions const& given_options, AudioNodeDefaultOptions const& default_options)
  18. {
  19. // Set channel count, fallback to default if not provided
  20. if (given_options.channel_count.has_value()) {
  21. TRY(set_channel_count(given_options.channel_count.value()));
  22. } else {
  23. TRY(set_channel_count(default_options.channel_count));
  24. }
  25. // Set channel count mode, fallback to default if not provided
  26. if (given_options.channel_count_mode.has_value()) {
  27. TRY(set_channel_count_mode(given_options.channel_count_mode.value()));
  28. } else {
  29. TRY(set_channel_count_mode(default_options.channel_count_mode));
  30. }
  31. // Set channel interpretation, fallback to default if not provided
  32. if (given_options.channel_interpretation.has_value()) {
  33. TRY(set_channel_interpretation(given_options.channel_interpretation.value()));
  34. } else {
  35. TRY(set_channel_interpretation(default_options.channel_interpretation));
  36. }
  37. return {};
  38. }
  39. // https://webaudio.github.io/web-audio-api/#dom-audionode-connect
  40. WebIDL::ExceptionOr<GC::Ref<AudioNode>> AudioNode::connect(GC::Ref<AudioNode> destination_node, WebIDL::UnsignedLong output, WebIDL::UnsignedLong input)
  41. {
  42. // There can only be one connection between a given output of one specific node and a given input of another specific node.
  43. // Multiple connections with the same termini are ignored.
  44. // If the destination parameter is an AudioNode that has been created using another AudioContext, an InvalidAccessError MUST be thrown.
  45. if (m_context != destination_node->m_context) {
  46. return WebIDL::InvalidAccessError::create(realm(), "Cannot connect to an AudioNode in a different AudioContext"_string);
  47. }
  48. (void)output;
  49. (void)input;
  50. dbgln("FIXME: Implement Audio::connect(AudioNode)");
  51. return destination_node;
  52. }
  53. // https://webaudio.github.io/web-audio-api/#dom-audionode-connect-destinationparam-output
  54. void AudioNode::connect(GC::Ref<AudioParam> destination_param, WebIDL::UnsignedLong output)
  55. {
  56. (void)destination_param;
  57. (void)output;
  58. dbgln("FIXME: Implement AudioNode::connect(AudioParam)");
  59. }
  60. // https://webaudio.github.io/web-audio-api/#dom-audionode-disconnect
  61. void AudioNode::disconnect()
  62. {
  63. dbgln("FIXME: Implement AudioNode::disconnect()");
  64. }
  65. // https://webaudio.github.io/web-audio-api/#dom-audionode-disconnect-output
  66. void AudioNode::disconnect(WebIDL::UnsignedLong output)
  67. {
  68. (void)output;
  69. dbgln("FIXME: Implement AudioNode::disconnect(output)");
  70. }
  71. // https://webaudio.github.io/web-audio-api/#dom-audionode-disconnect-destinationnode
  72. void AudioNode::disconnect(GC::Ref<AudioNode> destination_node)
  73. {
  74. (void)destination_node;
  75. dbgln("FIXME: Implement AudioNode::disconnect(destination_node)");
  76. }
  77. // https://webaudio.github.io/web-audio-api/#dom-audionode-disconnect-destinationnode-output
  78. void AudioNode::disconnect(GC::Ref<AudioNode> destination_node, WebIDL::UnsignedLong output)
  79. {
  80. (void)destination_node;
  81. (void)output;
  82. dbgln("FIXME: Implement AudioNode::disconnect(destination_node, output)");
  83. }
  84. // https://webaudio.github.io/web-audio-api/#dom-audionode-disconnect-destinationnode-output-input
  85. void AudioNode::disconnect(GC::Ref<AudioNode> destination_node, WebIDL::UnsignedLong output, WebIDL::UnsignedLong input)
  86. {
  87. (void)destination_node;
  88. (void)output;
  89. (void)input;
  90. dbgln("FIXME: Implement AudioNode::disconnect(destination_node, output, input)");
  91. }
  92. // https://webaudio.github.io/web-audio-api/#dom-audionode-disconnect-destinationparam
  93. void AudioNode::disconnect(GC::Ref<AudioParam> destination_param)
  94. {
  95. (void)destination_param;
  96. dbgln("FIXME: Implement AudioNode::disconnect(destination_param)");
  97. }
  98. // https://webaudio.github.io/web-audio-api/#dom-audionode-disconnect-destinationparam-output
  99. void AudioNode::disconnect(GC::Ref<AudioParam> destination_param, WebIDL::UnsignedLong output)
  100. {
  101. (void)destination_param;
  102. (void)output;
  103. dbgln("FIXME: Implement AudioNode::disconnect(destination_param, output)");
  104. }
  105. // https://webaudio.github.io/web-audio-api/#dom-audionode-channelcount
  106. WebIDL::ExceptionOr<void> AudioNode::set_channel_count(WebIDL::UnsignedLong channel_count)
  107. {
  108. // If this value is set to zero or to a value greater than the implementation’s maximum number
  109. // of channels the implementation MUST throw a NotSupportedError exception.
  110. if (channel_count == 0 || channel_count > BaseAudioContext::MAX_NUMBER_OF_CHANNELS)
  111. return WebIDL::NotSupportedError::create(realm(), "Invalid channel count"_string);
  112. m_channel_count = channel_count;
  113. return {};
  114. }
  115. // https://webaudio.github.io/web-audio-api/#dom-audionode-channelcountmode
  116. WebIDL::ExceptionOr<void> AudioNode::set_channel_count_mode(Bindings::ChannelCountMode channel_count_mode)
  117. {
  118. m_channel_count_mode = channel_count_mode;
  119. return {};
  120. }
  121. // https://webaudio.github.io/web-audio-api/#dom-audionode-channelcountmode
  122. Bindings::ChannelCountMode AudioNode::channel_count_mode()
  123. {
  124. return m_channel_count_mode;
  125. }
  126. // https://webaudio.github.io/web-audio-api/#dom-audionode-channelinterpretation
  127. WebIDL::ExceptionOr<void> AudioNode::set_channel_interpretation(Bindings::ChannelInterpretation channel_interpretation)
  128. {
  129. m_channel_interpretation = channel_interpretation;
  130. return {};
  131. }
  132. // https://webaudio.github.io/web-audio-api/#dom-audionode-channelinterpretation
  133. Bindings::ChannelInterpretation AudioNode::channel_interpretation()
  134. {
  135. return m_channel_interpretation;
  136. }
  137. void AudioNode::initialize(JS::Realm& realm)
  138. {
  139. Base::initialize(realm);
  140. WEB_SET_PROTOTYPE_FOR_INTERFACE(AudioNode);
  141. }
  142. void AudioNode::visit_edges(Cell::Visitor& visitor)
  143. {
  144. Base::visit_edges(visitor);
  145. visitor.visit(m_context);
  146. }
  147. }