ChannelSplitterNode.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2025, Tim Ledbetter <tim.ledbetter@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/ChannelSplitterNodePrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/WebAudio/BaseAudioContext.h>
  9. #include <LibWeb/WebAudio/ChannelSplitterNode.h>
  10. namespace Web::WebAudio {
  11. GC_DEFINE_ALLOCATOR(ChannelSplitterNode);
  12. ChannelSplitterNode::ChannelSplitterNode(JS::Realm& realm, GC::Ref<BaseAudioContext> context, ChannelSplitterOptions const& options)
  13. : AudioNode(realm, context)
  14. , m_number_of_outputs(options.number_of_outputs)
  15. {
  16. }
  17. ChannelSplitterNode::~ChannelSplitterNode() = default;
  18. WebIDL::ExceptionOr<GC::Ref<ChannelSplitterNode>> ChannelSplitterNode::create(JS::Realm& realm, GC::Ref<BaseAudioContext> context, ChannelSplitterOptions const& options)
  19. {
  20. return construct_impl(realm, context, options);
  21. }
  22. WebIDL::ExceptionOr<GC::Ref<ChannelSplitterNode>> ChannelSplitterNode::construct_impl(JS::Realm& realm, GC::Ref<BaseAudioContext> context, ChannelSplitterOptions const& options)
  23. {
  24. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createchannelsplitter
  25. // An IndexSizeError exception MUST be thrown if numberOfOutputs is less than 1 or is greater than the number of supported channels.
  26. if (options.number_of_outputs < 1 || options.number_of_outputs > BaseAudioContext::MAX_NUMBER_OF_CHANNELS)
  27. return WebIDL::IndexSizeError::create(realm, "Invalid number of outputs"_string);
  28. auto node = realm.create<ChannelSplitterNode>(realm, context, options);
  29. // Default options for channel count and interpretation
  30. // https://webaudio.github.io/web-audio-api/#ChannelSplitterNode
  31. AudioNodeDefaultOptions default_options;
  32. default_options.channel_count_mode = Bindings::ChannelCountMode::Explicit;
  33. default_options.channel_interpretation = Bindings::ChannelInterpretation::Discrete;
  34. default_options.channel_count = node->number_of_outputs();
  35. // FIXME: Set tail-time to no
  36. TRY(node->initialize_audio_node_options(options, default_options));
  37. return node;
  38. }
  39. void ChannelSplitterNode::initialize(JS::Realm& realm)
  40. {
  41. AudioNode::initialize(realm);
  42. WEB_SET_PROTOTYPE_FOR_INTERFACE(ChannelSplitterNode);
  43. }
  44. WebIDL::ExceptionOr<void> ChannelSplitterNode::set_channel_count(WebIDL::UnsignedLong channel_count)
  45. {
  46. // https://webaudio.github.io/web-audio-api/#audionode-channelcount-constraints
  47. // The channel count cannot be changed, and an InvalidStateError exception MUST be thrown for any attempt to change the value.
  48. if (channel_count != m_number_of_outputs)
  49. return WebIDL::InvalidStateError::create(realm(), "Channel count must be equal to number of outputs"_string);
  50. return AudioNode::set_channel_count(channel_count);
  51. }
  52. WebIDL::ExceptionOr<void> ChannelSplitterNode::set_channel_count_mode(Bindings::ChannelCountMode channel_count_mode)
  53. {
  54. // https://webaudio.github.io/web-audio-api/#audionode-channelcountmode-constraints
  55. // The channel count mode cannot be changed from "explicit" and an InvalidStateError exception MUST be thrown for any attempt to change the value.
  56. if (channel_count_mode != Bindings::ChannelCountMode::Explicit)
  57. return WebIDL::InvalidStateError::create(realm(), "Channel count mode must be 'explicit'"_string);
  58. return AudioNode::set_channel_count_mode(channel_count_mode);
  59. }
  60. WebIDL::ExceptionOr<void> ChannelSplitterNode::set_channel_interpretation(Bindings::ChannelInterpretation channel_interpretation)
  61. {
  62. // https://webaudio.github.io/web-audio-api/#audionode-channelinterpretation-constraints
  63. // The channel intepretation can not be changed from "discrete" and a InvalidStateError exception MUST be thrown for any attempt to change the value.
  64. if (channel_interpretation != Bindings::ChannelInterpretation::Discrete)
  65. return WebIDL::InvalidStateError::create(realm(), "Channel interpretation must be 'discrete'"_string);
  66. return AudioNode::set_channel_interpretation(channel_interpretation);
  67. }
  68. }