AudioBuffer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Vector.h>
  8. #include <LibJS/Forward.h>
  9. #include <LibJS/Runtime/ArrayBuffer.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/WebIDL/Buffers.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. #include <LibWeb/WebIDL/Types.h>
  14. namespace Web::WebAudio {
  15. struct AudioBufferOptions {
  16. WebIDL::UnsignedLong number_of_channels { 1 };
  17. WebIDL::UnsignedLong length {};
  18. float sample_rate {};
  19. };
  20. // https://webaudio.github.io/web-audio-api/#AudioContext
  21. class AudioBuffer final : public Bindings::PlatformObject {
  22. WEB_PLATFORM_OBJECT(AudioBuffer, Bindings::PlatformObject);
  23. JS_DECLARE_ALLOCATOR(AudioBuffer);
  24. public:
  25. static WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> create(JS::Realm&, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate);
  26. static WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> construct_impl(JS::Realm&, AudioBufferOptions const&);
  27. virtual ~AudioBuffer() override;
  28. float sample_rate() const;
  29. WebIDL::UnsignedLong length() const;
  30. double duration() const;
  31. WebIDL::UnsignedLong number_of_channels() const;
  32. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Float32Array>> get_channel_data(WebIDL::UnsignedLong channel) const;
  33. WebIDL::ExceptionOr<void> copy_from_channel(JS::Handle<WebIDL::BufferSource> const&, WebIDL::UnsignedLong channel_number, WebIDL::UnsignedLong buffer_offset = 0) const;
  34. WebIDL::ExceptionOr<void> copy_to_channel(JS::Handle<WebIDL::BufferSource> const&, WebIDL::UnsignedLong channel_number, WebIDL::UnsignedLong buffer_offset = 0);
  35. private:
  36. explicit AudioBuffer(JS::Realm&, AudioBufferOptions const&);
  37. virtual void initialize(JS::Realm&) override;
  38. virtual void visit_edges(Cell::Visitor&) override;
  39. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-number-of-channels-slot
  40. // The number of audio channels for this AudioBuffer, which is an unsigned long.
  41. //
  42. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-internal-data-slot
  43. // A data block holding the audio sample data.
  44. Vector<JS::NonnullGCPtr<JS::Float32Array>> m_channels; // [[internal data]] / [[number_of_channels]]
  45. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-length-slot
  46. // The length of each channel of this AudioBuffer, which is an unsigned long.
  47. WebIDL::UnsignedLong m_length {}; // [[length]]
  48. // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-sample-rate-slot
  49. // The sample-rate, in Hz, of this AudioBuffer, a float.
  50. float m_sample_rate {}; // [[sample rate]]
  51. };
  52. }