DynamicsCompressorNode.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/DynamicsCompressorNodePrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/WebAudio/AudioParam.h>
  9. #include <LibWeb/WebAudio/DynamicsCompressorNode.h>
  10. namespace Web::WebAudio {
  11. GC_DEFINE_ALLOCATOR(DynamicsCompressorNode);
  12. DynamicsCompressorNode::~DynamicsCompressorNode() = default;
  13. WebIDL::ExceptionOr<GC::Ref<DynamicsCompressorNode>> DynamicsCompressorNode::create(JS::Realm& realm, GC::Ref<BaseAudioContext> context, DynamicsCompressorOptions const& options)
  14. {
  15. return construct_impl(realm, context, options);
  16. }
  17. // https://webaudio.github.io/web-audio-api/#dom-dynamicscompressornode-dynamicscompressornode
  18. WebIDL::ExceptionOr<GC::Ref<DynamicsCompressorNode>> DynamicsCompressorNode::construct_impl(JS::Realm& realm, GC::Ref<BaseAudioContext> context, DynamicsCompressorOptions const& options)
  19. {
  20. // Create the node and allocate memory
  21. auto node = realm.create<DynamicsCompressorNode>(realm, context, options);
  22. // Default options for channel count and interpretation
  23. // https://webaudio.github.io/web-audio-api/#DynamicsCompressorNode
  24. AudioNodeDefaultOptions default_options;
  25. default_options.channel_count_mode = Bindings::ChannelCountMode::ClampedMax;
  26. default_options.channel_interpretation = Bindings::ChannelInterpretation::Speakers;
  27. default_options.channel_count = 2;
  28. // FIXME: Set tail-time to yes
  29. TRY(node->initialize_audio_node_options(options, default_options));
  30. return node;
  31. }
  32. DynamicsCompressorNode::DynamicsCompressorNode(JS::Realm& realm, GC::Ref<BaseAudioContext> context, DynamicsCompressorOptions const& options)
  33. : AudioNode(realm, context)
  34. , m_threshold(AudioParam::create(realm, options.threshold, -100, 0, Bindings::AutomationRate::KRate))
  35. , m_knee(AudioParam::create(realm, options.knee, 0, 40, Bindings::AutomationRate::KRate))
  36. , m_ratio(AudioParam::create(realm, options.ratio, 1, 20, Bindings::AutomationRate::KRate))
  37. , m_attack(AudioParam::create(realm, options.attack, 0, 1, Bindings::AutomationRate::KRate))
  38. , m_release(AudioParam::create(realm, options.release, 0, 1, Bindings::AutomationRate::KRate))
  39. {
  40. }
  41. void DynamicsCompressorNode::initialize(JS::Realm& realm)
  42. {
  43. Base::initialize(realm);
  44. WEB_SET_PROTOTYPE_FOR_INTERFACE(DynamicsCompressorNode);
  45. }
  46. void DynamicsCompressorNode::visit_edges(Cell::Visitor& visitor)
  47. {
  48. Base::visit_edges(visitor);
  49. visitor.visit(m_threshold);
  50. visitor.visit(m_knee);
  51. visitor.visit(m_ratio);
  52. visitor.visit(m_attack);
  53. visitor.visit(m_release);
  54. }
  55. // https://webaudio.github.io/web-audio-api/#dom-audionode-channelcountmode
  56. WebIDL::ExceptionOr<void> DynamicsCompressorNode::set_channel_count_mode(Bindings::ChannelCountMode mode)
  57. {
  58. if (mode == Bindings::ChannelCountMode::Max) {
  59. // Return a NotSupportedError if 'max' is used
  60. return WebIDL::NotSupportedError::create(realm(), "DynamicsCompressorNode does not support 'max' as channelCountMode."_string);
  61. }
  62. // If the mode is valid, call the base class implementation
  63. return AudioNode::set_channel_count_mode(mode);
  64. }
  65. // https://webaudio.github.io/web-audio-api/#dom-audionode-channelcount
  66. WebIDL::ExceptionOr<void> DynamicsCompressorNode::set_channel_count(WebIDL::UnsignedLong channel_count)
  67. {
  68. if (channel_count > 2) {
  69. // Return a NotSupportedError if 'max' is used
  70. return WebIDL::NotSupportedError::create(realm(), "DynamicsCompressorNode does not support channel count greater than 2"_string);
  71. }
  72. // If the mode is valid, call the base class implementation
  73. return AudioNode::set_channel_count(channel_count);
  74. }
  75. }