AudioBuffer.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/AudioBufferPrototype.h>
  11. #include <LibWeb/Bindings/Intrinsics.h>
  12. #include <LibWeb/WebAudio/AudioBuffer.h>
  13. #include <LibWeb/WebAudio/BaseAudioContext.h>
  14. #include <LibWeb/WebIDL/DOMException.h>
  15. namespace Web::WebAudio {
  16. GC_DEFINE_ALLOCATOR(AudioBuffer);
  17. WebIDL::ExceptionOr<GC::Ref<AudioBuffer>> AudioBuffer::create(JS::Realm& realm, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate)
  18. {
  19. return construct_impl(realm, { number_of_channels, length, sample_rate });
  20. }
  21. WebIDL::ExceptionOr<GC::Ref<AudioBuffer>> AudioBuffer::construct_impl(JS::Realm& realm, AudioBufferOptions const& options)
  22. {
  23. // 1. If any of the values in options lie outside its nominal range, throw a NotSupportedError exception and abort the following steps.
  24. TRY(BaseAudioContext::verify_audio_options_inside_nominal_range(realm, options.number_of_channels, options.length, options.sample_rate));
  25. // 2. Let b be a new AudioBuffer object.
  26. // 3. Respectively assign the values of the attributes numberOfChannels, length, sampleRate of the AudioBufferOptions passed in the
  27. // constructor to the internal slots [[number of channels]], [[length]], [[sample rate]].
  28. auto buffer = realm.create<AudioBuffer>(realm, options);
  29. // 4. Set the internal slot [[internal data]] of this AudioBuffer to the result of calling CreateByteDataBlock([[length]] * [[number of channels]]).
  30. buffer->m_channels.ensure_capacity(options.number_of_channels);
  31. for (WebIDL::UnsignedLong i = 0; i < options.number_of_channels; ++i)
  32. buffer->m_channels.unchecked_append(TRY(JS::Float32Array::create(realm, options.length)));
  33. return buffer;
  34. }
  35. AudioBuffer::~AudioBuffer() = default;
  36. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-samplerate
  37. float AudioBuffer::sample_rate() const
  38. {
  39. // The sample-rate for the PCM audio data in samples per second. This MUST return the value of [[sample rate]].
  40. return m_sample_rate;
  41. }
  42. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-length
  43. WebIDL::UnsignedLong AudioBuffer::length() const
  44. {
  45. // Length of the PCM audio data in sample-frames. This MUST return the value of [[length]].
  46. return m_length;
  47. }
  48. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-duration
  49. double AudioBuffer::duration() const
  50. {
  51. // Duration of the PCM audio data in seconds.
  52. // This is computed from the [[sample rate]] and the [[length]] of the AudioBuffer by performing a division between the [[length]] and the [[sample rate]].
  53. return m_length / static_cast<double>(m_sample_rate);
  54. }
  55. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-numberofchannels
  56. WebIDL::UnsignedLong AudioBuffer::number_of_channels() const
  57. {
  58. // The number of discrete audio channels. This MUST return the value of [[number of channels]].
  59. return m_channels.size();
  60. }
  61. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-getchanneldata
  62. WebIDL::ExceptionOr<GC::Ref<JS::Float32Array>> AudioBuffer::get_channel_data(WebIDL::UnsignedLong channel) const
  63. {
  64. if (channel >= m_channels.size())
  65. return WebIDL::IndexSizeError::create(realm(), "Channel index is out of range"_string);
  66. return m_channels[channel];
  67. }
  68. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-copyfromchannel
  69. WebIDL::ExceptionOr<void> AudioBuffer::copy_from_channel(GC::Root<WebIDL::BufferSource> const& destination, WebIDL::UnsignedLong channel_number, WebIDL::UnsignedLong buffer_offset) const
  70. {
  71. // The copyFromChannel() method copies the samples from the specified channel of the AudioBuffer to the destination array.
  72. //
  73. // Let buffer be the AudioBuffer with Nb frames, let Nf be the number of elements in the destination array, and k be the value
  74. // of bufferOffset. Then the number of frames copied from buffer to destination is max(0,min(Nb−k,Nf)). If this is less than Nf,
  75. // then the remaining elements of destination are not modified.
  76. auto& vm = this->vm();
  77. if (!is<JS::Float32Array>(*destination->raw_object()))
  78. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Float32Array");
  79. auto& float32_array = static_cast<JS::Float32Array&>(*destination->raw_object());
  80. auto const channel = TRY(get_channel_data(channel_number));
  81. auto channel_length = channel->data().size();
  82. if (buffer_offset >= channel_length)
  83. return {};
  84. u32 count = min(float32_array.data().size(), channel_length - buffer_offset);
  85. channel->data().slice(buffer_offset, count).copy_to(float32_array.data());
  86. return {};
  87. }
  88. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-copytochannel
  89. WebIDL::ExceptionOr<void> AudioBuffer::copy_to_channel(GC::Root<WebIDL::BufferSource> const& source, WebIDL::UnsignedLong channel_number, WebIDL::UnsignedLong buffer_offset)
  90. {
  91. // The copyToChannel() method copies the samples to the specified channel of the AudioBuffer from the source array.
  92. //
  93. // A UnknownError may be thrown if source cannot be copied to the buffer.
  94. //
  95. // Let buffer be the AudioBuffer with Nb frames, let Nf be the number of elements in the source array, and k be the value
  96. // of bufferOffset. Then the number of frames copied from source to the buffer is max(0,min(Nb−k,Nf)). If this is less than Nf,
  97. // then the remaining elements of buffer are not modified.
  98. auto& vm = this->vm();
  99. if (!is<JS::Float32Array>(*source->raw_object()))
  100. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Float32Array");
  101. auto const& float32_array = static_cast<JS::Float32Array const&>(*source->raw_object());
  102. auto channel = TRY(get_channel_data(channel_number));
  103. auto channel_length = channel->data().size();
  104. if (buffer_offset >= channel_length)
  105. return {};
  106. u32 count = min(float32_array.data().size(), channel_length - buffer_offset);
  107. float32_array.data().slice(0, count).copy_to(channel->data().slice(buffer_offset, count));
  108. return {};
  109. }
  110. AudioBuffer::AudioBuffer(JS::Realm& realm, AudioBufferOptions const& options)
  111. : Bindings::PlatformObject(realm)
  112. , m_length(options.length)
  113. , m_sample_rate(options.sample_rate)
  114. {
  115. }
  116. void AudioBuffer::initialize(JS::Realm& realm)
  117. {
  118. Base::initialize(realm);
  119. WEB_SET_PROTOTYPE_FOR_INTERFACE(AudioBuffer);
  120. }
  121. void AudioBuffer::visit_edges(Cell::Visitor& visitor)
  122. {
  123. Base::visit_edges(visitor);
  124. visitor.visit(m_channels);
  125. }
  126. }