AudioParam.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. JS_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. JS::NonnullGCPtr<AudioParam> AudioParam::create(JS::Realm& realm, float default_value, float min_value, float max_value, Bindings::AutomationRate automation_rate)
  22. {
  23. return realm.vm().heap().allocate<AudioParam>(realm, 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<JS::NonnullGCPtr<AudioParam>> AudioParam::set_value_at_time(float value, double start_time)
  68. {
  69. (void)value;
  70. (void)start_time;
  71. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::set_value_at_time"_fly_string);
  72. }
  73. // https://webaudio.github.io/web-audio-api/#dom-audioparam-linearramptovalueattime
  74. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioParam>> AudioParam::linear_ramp_to_value_at_time(float value, double end_time)
  75. {
  76. (void)value;
  77. (void)end_time;
  78. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::linear_ramp_to_value_at_time"_fly_string);
  79. }
  80. // https://webaudio.github.io/web-audio-api/#dom-audioparam-exponentialramptovalueattime
  81. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioParam>> AudioParam::exponential_ramp_to_value_at_time(float value, double end_time)
  82. {
  83. (void)value;
  84. (void)end_time;
  85. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::exponential_ramp_to_value_at_time"_fly_string);
  86. }
  87. // https://webaudio.github.io/web-audio-api/#dom-audioparam-settargetattime
  88. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioParam>> AudioParam::set_target_at_time(float target, double start_time, float time_constant)
  89. {
  90. (void)target;
  91. (void)start_time;
  92. (void)time_constant;
  93. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::set_target_at_time"_fly_string);
  94. }
  95. // https://webaudio.github.io/web-audio-api/#dom-audioparam-setvaluecurveattime
  96. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioParam>> AudioParam::set_value_curve_at_time(Span<float> values, double start_time, double duration)
  97. {
  98. (void)values;
  99. (void)start_time;
  100. (void)duration;
  101. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::set_value_curve_at_time"_fly_string);
  102. }
  103. // https://webaudio.github.io/web-audio-api/#dom-audioparam-cancelscheduledvalues
  104. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioParam>> AudioParam::cancel_scheduled_values(double cancel_time)
  105. {
  106. (void)cancel_time;
  107. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::cancel_scheduled_values"_fly_string);
  108. }
  109. // https://webaudio.github.io/web-audio-api/#dom-audioparam-cancelandholdattime
  110. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioParam>> AudioParam::cancel_and_hold_at_time(double cancel_time)
  111. {
  112. (void)cancel_time;
  113. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioParam::cancel_and_hold_at_time"_fly_string);
  114. }
  115. void AudioParam::initialize(JS::Realm& realm)
  116. {
  117. Base::initialize(realm);
  118. WEB_SET_PROTOTYPE_FOR_INTERFACE(AudioParam);
  119. }
  120. void AudioParam::visit_edges(Cell::Visitor& visitor)
  121. {
  122. Base::visit_edges(visitor);
  123. }
  124. }