AudioDestinationNode.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  3. * Copyright (c) 2024, Bar Yemini <bar.ye651@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/AudioDestinationNodePrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/WebAudio/AudioContext.h>
  10. #include <LibWeb/WebAudio/AudioDestinationNode.h>
  11. #include <LibWeb/WebAudio/AudioNode.h>
  12. #include <LibWeb/WebAudio/BaseAudioContext.h>
  13. #include <LibWeb/WebAudio/OfflineAudioContext.h>
  14. namespace Web::WebAudio {
  15. GC_DEFINE_ALLOCATOR(AudioDestinationNode);
  16. AudioDestinationNode::AudioDestinationNode(JS::Realm& realm, GC::Ref<BaseAudioContext> context)
  17. : AudioNode(realm, context)
  18. {
  19. }
  20. AudioDestinationNode::~AudioDestinationNode() = default;
  21. // https://webaudio.github.io/web-audio-api/#dom-audiodestinationnode-maxchannelcount
  22. WebIDL::UnsignedLong AudioDestinationNode::max_channel_count()
  23. {
  24. dbgln("FIXME: Implement Audio::DestinationNode::max_channel_count()");
  25. return 2;
  26. }
  27. GC::Ref<AudioDestinationNode> AudioDestinationNode::construct_impl(JS::Realm& realm, GC::Ref<BaseAudioContext> context)
  28. {
  29. return realm.create<AudioDestinationNode>(realm, context);
  30. }
  31. void AudioDestinationNode::initialize(JS::Realm& realm)
  32. {
  33. Base::initialize(realm);
  34. WEB_SET_PROTOTYPE_FOR_INTERFACE(AudioDestinationNode);
  35. }
  36. void AudioDestinationNode::visit_edges(Cell::Visitor& visitor)
  37. {
  38. Base::visit_edges(visitor);
  39. }
  40. // https://webaudio.github.io/web-audio-api/#dom-audionode-channelcount
  41. WebIDL::ExceptionOr<void> AudioDestinationNode::set_channel_count(WebIDL::UnsignedLong channel_count)
  42. {
  43. // The behavior depends on whether the destination node is the destination of an AudioContext
  44. // or OfflineAudioContext:
  45. // AudioContext: The channel count MUST be between 1 and maxChannelCount. An IndexSizeError
  46. // exception MUST be thrown for any attempt to set the count outside this range.
  47. if (is<AudioContext>(*context())) {
  48. if (channel_count < 1 || channel_count > max_channel_count())
  49. return WebIDL::IndexSizeError::create(realm(), "Channel index is out of range"_string);
  50. }
  51. // OfflineAudioContext: The channel count cannot be changed. An InvalidStateError exception MUST
  52. // be thrown for any attempt to change the value.
  53. if (is<OfflineAudioContext>(*context()))
  54. return WebIDL::InvalidStateError::create(realm(), "Cannot change channel count in an OfflineAudioContext"_string);
  55. return AudioNode::set_channel_count(channel_count);
  56. }
  57. }