AudioBuffer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Completion.h>
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibJS/Runtime/TypedArray.h>
  9. #include <LibJS/Runtime/VM.h>
  10. #include <LibWeb/Bindings/Intrinsics.h>
  11. #include <LibWeb/WebAudio/AudioBuffer.h>
  12. #include <LibWeb/WebAudio/BaseAudioContext.h>
  13. #include <LibWeb/WebIDL/DOMException.h>
  14. namespace Web::WebAudio {
  15. JS_DEFINE_ALLOCATOR(AudioBuffer);
  16. WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> AudioBuffer::construct_impl(JS::Realm& realm, AudioBufferOptions const& options)
  17. {
  18. auto& vm = realm.vm();
  19. // 1. If any of the values in options lie outside its nominal range, throw a NotSupportedError exception and abort the following steps.
  20. TRY(BaseAudioContext::verify_audio_options_inside_nominal_range(realm, options.number_of_channels, options.length, options.sample_rate));
  21. // 2. Let b be a new AudioBuffer object.
  22. // 3. Respectively assign the values of the attributes numberOfChannels, length, sampleRate of the AudioBufferOptions passed in the
  23. // constructor to the internal slots [[number of channels]], [[length]], [[sample rate]].
  24. auto buffer = vm.heap().allocate<AudioBuffer>(realm, realm, options);
  25. // 4. Set the internal slot [[internal data]] of this AudioBuffer to the result of calling CreateByteDataBlock([[length]] * [[number of channels]]).
  26. buffer->m_channels.ensure_capacity(options.number_of_channels);
  27. for (WebIDL::UnsignedLong i = 0; i < options.number_of_channels; ++i)
  28. buffer->m_channels.unchecked_append(TRY(JS::Float32Array::create(realm, options.length)));
  29. return buffer;
  30. }
  31. AudioBuffer::~AudioBuffer() = default;
  32. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-samplerate
  33. float AudioBuffer::sample_rate() const
  34. {
  35. // The sample-rate for the PCM audio data in samples per second. This MUST return the value of [[sample rate]].
  36. return m_sample_rate;
  37. }
  38. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-length
  39. WebIDL::UnsignedLong AudioBuffer::length() const
  40. {
  41. // Length of the PCM audio data in sample-frames. This MUST return the value of [[length]].
  42. return m_length;
  43. }
  44. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-duration
  45. double AudioBuffer::duration() const
  46. {
  47. // Duration of the PCM audio data in seconds.
  48. // This is computed from the [[sample rate]] and the [[length]] of the AudioBuffer by performing a division between the [[length]] and the [[sample rate]].
  49. return m_length / static_cast<double>(m_sample_rate);
  50. }
  51. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-numberofchannels
  52. WebIDL::UnsignedLong AudioBuffer::number_of_channels() const
  53. {
  54. // The number of discrete audio channels. This MUST return the value of [[number of channels]].
  55. return m_channels.size();
  56. }
  57. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-getchanneldata
  58. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Float32Array>> AudioBuffer::get_channel_data(WebIDL::UnsignedLong channel) const
  59. {
  60. if (channel >= m_channels.size())
  61. return WebIDL::IndexSizeError::create(realm(), "Channel index is out of range"_fly_string);
  62. return m_channels[channel];
  63. }
  64. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-copyfromchannel
  65. WebIDL::ExceptionOr<void> AudioBuffer::copy_from_channel(JS::Handle<WebIDL::BufferSource> const&, WebIDL::UnsignedLong channel_number, WebIDL::UnsignedLong buffer_offset) const
  66. {
  67. (void)channel_number;
  68. (void)buffer_offset;
  69. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioBuffer:copy_from_channel:"_fly_string);
  70. }
  71. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-copytochannel
  72. WebIDL::ExceptionOr<void> AudioBuffer::copy_to_channel(JS::Handle<WebIDL::BufferSource>&, WebIDL::UnsignedLong channel_number, WebIDL::UnsignedLong buffer_offset) const
  73. {
  74. (void)channel_number;
  75. (void)buffer_offset;
  76. return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioBuffer:copy_to_channel:"_fly_string);
  77. }
  78. AudioBuffer::AudioBuffer(JS::Realm& realm, AudioBufferOptions const& options)
  79. : Bindings::PlatformObject(realm)
  80. , m_length(options.length)
  81. , m_sample_rate(options.sample_rate)
  82. {
  83. }
  84. void AudioBuffer::initialize(JS::Realm& realm)
  85. {
  86. Base::initialize(realm);
  87. WEB_SET_PROTOTYPE_FOR_INTERFACE(AudioBuffer);
  88. }
  89. void AudioBuffer::visit_edges(Cell::Visitor& visitor)
  90. {
  91. Base::visit_edges(visitor);
  92. visitor.visit(m_channels);
  93. }
  94. }