AudioBufferSourceNode.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2024, Bar Yemini <bar.ye651@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/Bindings/AudioBufferSourceNodePrototype.h>
  8. #include <LibWeb/WebAudio/AudioBuffer.h>
  9. #include <LibWeb/WebAudio/AudioParam.h>
  10. #include <LibWeb/WebAudio/AudioScheduledSourceNode.h>
  11. namespace Web::WebAudio {
  12. // https://webaudio.github.io/web-audio-api/#AudioBufferSourceOptions
  13. struct AudioBufferSourceOptions {
  14. GC::Ptr<AudioBuffer> buffer;
  15. float detune { 0 };
  16. bool loop { false };
  17. double loop_end { 0 };
  18. double loop_start { 0 };
  19. float playback_rate { 1 };
  20. };
  21. // https://webaudio.github.io/web-audio-api/#AudioBufferSourceNode
  22. class AudioBufferSourceNode : public AudioScheduledSourceNode {
  23. WEB_PLATFORM_OBJECT(AudioBufferSourceNode, AudioScheduledSourceNode);
  24. GC_DECLARE_ALLOCATOR(AudioBufferSourceNode);
  25. public:
  26. virtual ~AudioBufferSourceNode() override;
  27. WebIDL::ExceptionOr<void> set_buffer(GC::Ptr<AudioBuffer>);
  28. GC::Ptr<AudioBuffer> buffer() const;
  29. GC::Ref<AudioParam> playback_rate() const;
  30. GC::Ref<AudioParam> detune() const;
  31. WebIDL::ExceptionOr<void> set_loop(bool);
  32. bool loop() const;
  33. WebIDL::ExceptionOr<void> set_loop_start(double);
  34. double loop_start() const;
  35. WebIDL::ExceptionOr<void> set_loop_end(double);
  36. double loop_end() const;
  37. WebIDL::UnsignedLong number_of_inputs() override { return 0; }
  38. WebIDL::UnsignedLong number_of_outputs() override { return 2; }
  39. WebIDL::ExceptionOr<void> start(Optional<double>, Optional<double>, Optional<double>);
  40. static WebIDL::ExceptionOr<GC::Ref<AudioBufferSourceNode>> create(JS::Realm&, GC::Ref<BaseAudioContext>, AudioBufferSourceOptions const& = {});
  41. static WebIDL::ExceptionOr<GC::Ref<AudioBufferSourceNode>> construct_impl(JS::Realm&, GC::Ref<BaseAudioContext>, AudioBufferSourceOptions const& = {});
  42. protected:
  43. AudioBufferSourceNode(JS::Realm&, GC::Ref<BaseAudioContext>, AudioBufferSourceOptions const& = {});
  44. virtual void initialize(JS::Realm&) override;
  45. virtual void visit_edges(Cell::Visitor&) override;
  46. private:
  47. GC::Ptr<AudioBuffer> m_buffer;
  48. GC::Ref<AudioParam> m_playback_rate;
  49. GC::Ref<AudioParam> m_detune;
  50. bool m_loop { false };
  51. double m_loop_start { 0.0 };
  52. double m_loop_end { 0.0 };
  53. };
  54. }