AudioParam.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/AudioParamPrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/WebAudio/AudioParam.h>
  9. #include <LibWeb/WebIDL/ExceptionOr.h>
  10. namespace Web::WebAudio {
  11. GC_DEFINE_ALLOCATOR(AudioParam);
  12. AudioParam::AudioParam(JS::Realm& realm, float default_value, float min_value, float max_value, Bindings::AutomationRate automation_rate)
  13. : Bindings::PlatformObject(realm)
  14. , m_current_value(default_value)
  15. , m_default_value(default_value)
  16. , m_min_value(min_value)
  17. , m_max_value(max_value)
  18. , m_automation_rate(automation_rate)
  19. {
  20. }
  21. GC::Ref<AudioParam> AudioParam::create(JS::Realm& realm, float default_value, float min_value, float max_value, Bindings::AutomationRate automation_rate)
  22. {
  23. return realm.create<AudioParam>(realm, default_value, min_value, max_value, automation_rate);
  24. }
  25. AudioParam::~AudioParam() = default;
  26. // https://webaudio.github.io/web-audio-api/#dom-audioparam-value
  27. // https://webaudio.github.io/web-audio-api/#simple-nominal-range
  28. float AudioParam::value() const
  29. {
  30. // Each AudioParam includes minValue and maxValue attributes that together form the simple nominal range
  31. // for the parameter. In effect, value of the parameter is clamped to the range [minValue, maxValue].
  32. return clamp(m_current_value, min_value(), max_value());
  33. }
  34. // https://webaudio.github.io/web-audio-api/#dom-audioparam-value
  35. void AudioParam::set_value(float value)
  36. {
  37. m_current_value = value;
  38. }
  39. // https://webaudio.github.io/web-audio-api/#dom-audioparam-automationrate
  40. Bindings::AutomationRate AudioParam::automation_rate() const
  41. {
  42. return m_automation_rate;
  43. }
  44. // https://webaudio.github.io/web-audio-api/#dom-audioparam-automationrate
  45. WebIDL::ExceptionOr<void> AudioParam::set_automation_rate(Bindings::AutomationRate automation_rate)
  46. {
  47. dbgln("FIXME: Fully implement AudioParam::set_automation_rate");
  48. m_automation_rate = automation_rate;
  49. return {};
  50. }
  51. // https://webaudio.github.io/web-audio-api/#dom-audioparam-defaultvalue
  52. float AudioParam::default_value() const
  53. {
  54. return m_default_value;
  55. }
  56. // https://webaudio.github.io/web-audio-api/#dom-audioparam-minvalue
  57. float AudioParam::min_value() const
  58. {
  59. return m_min_value;
  60. }
  61. // https://webaudio.github.io/web-audio-api/#dom-audioparam-maxvalue
  62. float AudioParam::max_value() const
  63. {
  64. return m_max_value;
  65. }
  66. // https://webaudio.github.io/web-audio-api/#dom-audioparam-setvalueattime
  67. WebIDL::ExceptionOr<GC::Ref<AudioParam>> AudioParam::set_value_at_time(float value, double start_time)
  68. {
  69. (void)value;
  70. (void)start_time;
  71. dbgln("FIXME: Implement AudioParam::set_value_at_time");
  72. return GC::Ref<AudioParam> { *this };
  73. }
  74. // https://webaudio.github.io/web-audio-api/#dom-audioparam-linearramptovalueattime
  75. WebIDL::ExceptionOr<GC::Ref<AudioParam>> AudioParam::linear_ramp_to_value_at_time(float value, double end_time)
  76. {
  77. (void)value;
  78. (void)end_time;
  79. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::linear_ramp_to_value_at_time"_string);
  80. }
  81. // https://webaudio.github.io/web-audio-api/#dom-audioparam-exponentialramptovalueattime
  82. WebIDL::ExceptionOr<GC::Ref<AudioParam>> AudioParam::exponential_ramp_to_value_at_time(float value, double end_time)
  83. {
  84. (void)value;
  85. (void)end_time;
  86. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::exponential_ramp_to_value_at_time"_string);
  87. }
  88. // https://webaudio.github.io/web-audio-api/#dom-audioparam-settargetattime
  89. WebIDL::ExceptionOr<GC::Ref<AudioParam>> AudioParam::set_target_at_time(float target, double start_time, float time_constant)
  90. {
  91. (void)target;
  92. (void)start_time;
  93. (void)time_constant;
  94. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::set_target_at_time"_string);
  95. }
  96. // https://webaudio.github.io/web-audio-api/#dom-audioparam-setvaluecurveattime
  97. WebIDL::ExceptionOr<GC::Ref<AudioParam>> AudioParam::set_value_curve_at_time(Span<float> values, double start_time, double duration)
  98. {
  99. (void)values;
  100. (void)start_time;
  101. (void)duration;
  102. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::set_value_curve_at_time"_string);
  103. }
  104. // https://webaudio.github.io/web-audio-api/#dom-audioparam-cancelscheduledvalues
  105. WebIDL::ExceptionOr<GC::Ref<AudioParam>> AudioParam::cancel_scheduled_values(double cancel_time)
  106. {
  107. (void)cancel_time;
  108. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::cancel_scheduled_values"_string);
  109. }
  110. // https://webaudio.github.io/web-audio-api/#dom-audioparam-cancelandholdattime
  111. WebIDL::ExceptionOr<GC::Ref<AudioParam>> AudioParam::cancel_and_hold_at_time(double cancel_time)
  112. {
  113. (void)cancel_time;
  114. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::cancel_and_hold_at_time"_string);
  115. }
  116. void AudioParam::initialize(JS::Realm& realm)
  117. {
  118. Base::initialize(realm);
  119. WEB_SET_PROTOTYPE_FOR_INTERFACE(AudioParam);
  120. }
  121. void AudioParam::visit_edges(Cell::Visitor& visitor)
  122. {
  123. Base::visit_edges(visitor);
  124. }
  125. }