浏览代码

LibWeb: Implement BaseAudioContext.createBuffer

This is a simple factory function which effectively just calls the
AudioBuffer constructor.
Shannon Booth 1 年之前
父节点
当前提交
71ccd8ad25

+ 1 - 0
Userland/Libraries/LibWeb/Forward.h

@@ -691,6 +691,7 @@ class Table;
 }
 
 namespace Web::WebAudio {
+class AudioBuffer;
 class AudioContext;
 class AudioNode;
 class AudioParam;

+ 5 - 0
Userland/Libraries/LibWeb/WebAudio/AudioBuffer.cpp

@@ -18,6 +18,11 @@ namespace Web::WebAudio {
 
 JS_DEFINE_ALLOCATOR(AudioBuffer);
 
+WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> AudioBuffer::create(JS::Realm& realm, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate)
+{
+    return construct_impl(realm, { number_of_channels, length, sample_rate });
+}
+
 WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> AudioBuffer::construct_impl(JS::Realm& realm, AudioBufferOptions const& options)
 {
     auto& vm = realm.vm();

+ 1 - 0
Userland/Libraries/LibWeb/WebAudio/AudioBuffer.h

@@ -28,6 +28,7 @@ class AudioBuffer final : public Bindings::PlatformObject {
     JS_DECLARE_ALLOCATOR(AudioBuffer);
 
 public:
+    static WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> create(JS::Realm&, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate);
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> construct_impl(JS::Realm&, AudioBufferOptions const&);
 
     virtual ~AudioBuffer() override;

+ 9 - 0
Userland/Libraries/LibWeb/WebAudio/BaseAudioContext.cpp

@@ -8,6 +8,7 @@
 #include <LibWeb/Bindings/BaseAudioContextPrototype.h>
 #include <LibWeb/Bindings/Intrinsics.h>
 #include <LibWeb/HTML/EventNames.h>
+#include <LibWeb/WebAudio/AudioBuffer.h>
 #include <LibWeb/WebAudio/BaseAudioContext.h>
 #include <LibWeb/WebAudio/DynamicsCompressorNode.h>
 #include <LibWeb/WebAudio/OscillatorNode.h>
@@ -38,6 +39,14 @@ WebIDL::CallbackType* BaseAudioContext::onstatechange()
     return event_handler_attribute(HTML::EventNames::statechange);
 }
 
+// https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer
+WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> BaseAudioContext::create_buffer(WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate)
+{
+    // Creates an AudioBuffer of the given size. The audio data in the buffer will be zero-initialized (silent).
+    // A NotSupportedError exception MUST be thrown if any of the arguments is negative, zero, or outside its nominal range.
+    return AudioBuffer::create(realm(), number_of_channels, length, sample_rate);
+}
+
 // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createoscillator
 WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> BaseAudioContext::create_oscillator()
 {

+ 1 - 0
Userland/Libraries/LibWeb/WebAudio/BaseAudioContext.h

@@ -47,6 +47,7 @@ public:
 
     static WebIDL::ExceptionOr<void> verify_audio_options_inside_nominal_range(JS::Realm&, WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate);
 
+    WebIDL::ExceptionOr<JS::NonnullGCPtr<AudioBuffer>> create_buffer(WebIDL::UnsignedLong number_of_channels, WebIDL::UnsignedLong length, float sample_rate);
     WebIDL::ExceptionOr<JS::NonnullGCPtr<OscillatorNode>> create_oscillator();
     WebIDL::ExceptionOr<JS::NonnullGCPtr<DynamicsCompressorNode>> create_dynamics_compressor();
 

+ 2 - 1
Userland/Libraries/LibWeb/WebAudio/BaseAudioContext.idl

@@ -1,5 +1,6 @@
 #import <DOM/EventTarget.idl>
 #import <DOM/EventHandler.idl>
+#import <WebAudio/AudioBuffer.idl>
 #import <WebAudio/DynamicsCompressorNode.idl>
 #import <WebAudio/OscillatorNode.idl>
 
@@ -24,7 +25,7 @@ interface BaseAudioContext : EventTarget {
 
     [FIXME] AnalyserNode createAnalyser ();
     [FIXME] BiquadFilterNode createBiquadFilter ();
-    [FIXME] AudioBuffer createBuffer (unsigned long numberOfChannels, unsigned long length, float sampleRate);
+    AudioBuffer createBuffer(unsigned long numberOfChannels, unsigned long length, float sampleRate);
     [FIXME] AudioBufferSourceNode createBufferSource ();
     [FIXME] ChannelMergerNode createChannelMerger (optional unsigned long numberOfInputs = 6);
     [FIXME] ChannelSplitterNode createChannelSplitter (optional unsigned long numberOfOutputs = 6);