ChannelMergerNode.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/WebAudio/BaseAudioContext.h>
  7. #include <LibWeb/WebAudio/ChannelMergerNode.h>
  8. namespace Web::WebAudio {
  9. GC_DEFINE_ALLOCATOR(ChannelMergerNode);
  10. ChannelMergerNode::ChannelMergerNode(JS::Realm& realm, GC::Ref<BaseAudioContext> context, ChannelMergerOptions const& options)
  11. : AudioNode(realm, context)
  12. , m_number_of_inputs(options.number_of_inputs)
  13. {
  14. }
  15. ChannelMergerNode::~ChannelMergerNode() = default;
  16. WebIDL::ExceptionOr<GC::Ref<ChannelMergerNode>> ChannelMergerNode::create(JS::Realm& realm, GC::Ref<BaseAudioContext> context, ChannelMergerOptions const& options)
  17. {
  18. return construct_impl(realm, context, options);
  19. }
  20. WebIDL::ExceptionOr<GC::Ref<ChannelMergerNode>> ChannelMergerNode::construct_impl(JS::Realm& realm, GC::Ref<BaseAudioContext> context, ChannelMergerOptions const& options)
  21. {
  22. // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createchannelmerger
  23. // An IndexSizeError exception MUST be thrown if numberOfInputs is less than 1 or is greater
  24. // than the number of supported channels.
  25. if (options.number_of_inputs < 1 || options.number_of_inputs > BaseAudioContext::MAX_NUMBER_OF_CHANNELS)
  26. return WebIDL::IndexSizeError::create(realm, "Invalid number of inputs"_string);
  27. auto node = realm.create<ChannelMergerNode>(realm, context, options);
  28. // Default options for channel count and interpretation
  29. // https://webaudio.github.io/web-audio-api/#BiquadFilterNode
  30. AudioNodeDefaultOptions default_options;
  31. default_options.channel_count_mode = Bindings::ChannelCountMode::Explicit;
  32. default_options.channel_interpretation = Bindings::ChannelInterpretation::Speakers;
  33. default_options.channel_count = 1;
  34. // FIXME: Set tail-time to no
  35. TRY(node->initialize_audio_node_options(options, default_options));
  36. return node;
  37. }
  38. // https://webaudio.github.io/web-audio-api/#audionode-channelcount-constraints
  39. WebIDL::ExceptionOr<void> ChannelMergerNode::set_channel_count(WebIDL::UnsignedLong channel_count)
  40. {
  41. // The channel count cannot be changed, and an InvalidStateError exception MUST be thrown for
  42. // any attempt to change the value.
  43. if (channel_count != 1)
  44. return WebIDL::InvalidStateError::create(realm(), "Channel count cannot be changed"_string);
  45. return Base::set_channel_count(channel_count);
  46. }
  47. WebIDL::ExceptionOr<void> ChannelMergerNode::set_channel_count_mode(Bindings::ChannelCountMode channel_count_mode)
  48. {
  49. // The channel count mode cannot be changed from "explicit" and an InvalidStateError exception
  50. // MUST be thrown for any attempt to change the value.
  51. if (channel_count_mode != Bindings::ChannelCountMode::Explicit)
  52. return WebIDL::InvalidStateError::create(realm(), "Channel count mode cannot be changed"_string);
  53. return Base::set_channel_count_mode(channel_count_mode);
  54. }
  55. }