OfflineAudioContext.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/HTML/EventNames.h>
  8. #include <LibWeb/HTML/Window.h>
  9. #include <LibWeb/WebAudio/OfflineAudioContext.h>
  10. namespace Web::WebAudio {
  11. JS_DEFINE_ALLOCATOR(OfflineAudioContext);
  12. WebIDL::ExceptionOr<JS::NonnullGCPtr<OfflineAudioContext>> OfflineAudioContext::construct_impl(JS::Realm& realm, OfflineAudioContextOptions const& context_options)
  13. {
  14. return construct_impl(realm, context_options.number_of_channels, context_options.length, context_options.sample_rate);
  15. }
  16. // https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-offlineaudiocontext-numberofchannels-length-samplerate
  17. WebIDL::ExceptionOr<JS::NonnullGCPtr<OfflineAudioContext>> OfflineAudioContext::construct_impl(JS::Realm& realm,
  18. WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate)
  19. {
  20. // The OfflineAudioContext can be constructed with the same arguments as AudioContext.createBuffer.
  21. // A NotSupportedError exception MUST be thrown if any of the arguments is negative, zero, or outside its nominal range.
  22. TRY(verify_audio_options_inside_nominal_range(realm, number_of_channels, length, sample_rate));
  23. return realm.heap().allocate<OfflineAudioContext>(realm, realm, number_of_channels, length, sample_rate);
  24. }
  25. OfflineAudioContext::~OfflineAudioContext() = default;
  26. // https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-startrendering
  27. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> OfflineAudioContext::start_rendering()
  28. {
  29. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement OfflineAudioContext::start_rendering"_fly_string);
  30. }
  31. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> OfflineAudioContext::resume()
  32. {
  33. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement OfflineAudioContext::resume"_fly_string);
  34. }
  35. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> OfflineAudioContext::suspend(double suspend_time)
  36. {
  37. (void)suspend_time;
  38. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement OfflineAudioContext::suspend"_fly_string);
  39. }
  40. // https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-length
  41. WebIDL::UnsignedLong OfflineAudioContext::length() const
  42. {
  43. // The size of the buffer in sample-frames. This is the same as the value of the length parameter for the constructor.
  44. return m_length;
  45. }
  46. // https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-oncomplete
  47. JS::GCPtr<WebIDL::CallbackType> OfflineAudioContext::oncomplete()
  48. {
  49. return event_handler_attribute(HTML::EventNames::complete);
  50. }
  51. // https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-oncomplete
  52. void OfflineAudioContext::set_oncomplete(JS::GCPtr<WebIDL::CallbackType> value)
  53. {
  54. set_event_handler_attribute(HTML::EventNames::complete, value);
  55. }
  56. OfflineAudioContext::OfflineAudioContext(JS::Realm& realm, OfflineAudioContextOptions const&)
  57. : BaseAudioContext(realm)
  58. {
  59. }
  60. OfflineAudioContext::OfflineAudioContext(JS::Realm& realm, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate)
  61. : BaseAudioContext(realm)
  62. , m_length(length)
  63. {
  64. (void)number_of_channels;
  65. (void)sample_rate;
  66. }
  67. void OfflineAudioContext::initialize(JS::Realm& realm)
  68. {
  69. Base::initialize(realm);
  70. WEB_SET_PROTOTYPE_FOR_INTERFACE(OfflineAudioContext);
  71. }
  72. void OfflineAudioContext::visit_edges(Cell::Visitor& visitor)
  73. {
  74. Base::visit_edges(visitor);
  75. }
  76. }