Skip to content

Truly independent web browser

LibWeb: Initialize AudioBufferSourceNode buffers through the setter

Shannon Booth6f33b2dd1784
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_buffer_source_node.rs b/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_buffer_source_node.rs
index 1f76916fffa..0bc4a142ed7 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_buffer_source_node.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_buffer_source_node.rs
@@ -240,6 +240,7 @@ impl AudioBufferSourceNode {
Ok(())
}
+ // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-audiobuffersourcenode
pub(crate) fn create(
cx: &mut CallContext,
context: AnyBaseAudioContext,
@@ -250,7 +251,7 @@ impl AudioBufferSourceNode {
let node = allocate(cx.vm(), |defer_gc| Self {
node: AudioScheduledSourceNode::new(defer_gc, context),
- buffer: Cell::new(options.buffer.flatten()),
+ buffer: Cell::new(None),
playback_rate: AudioParam::create(
cx.vm(),
context,
@@ -281,6 +282,8 @@ impl AudioBufferSourceNode {
node.playback_rate.set_owner(AnyAudioNode::AudioBufferSourceNode(node));
node.detune.set_owner(AnyAudioNode::AudioBufferSourceNode(node));
+ node.set_buffer(cx, options.buffer.flatten())?;
+
// Default options for channel count and interpretation
// https://webaudio.github.io/web-audio-api/#AudioBufferSourceNode
let default_options = AudioNodeDefaultOptions {
@@ -305,7 +308,6 @@ impl AudioBufferSourceNode {
Ok(node)
}
- // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-audiobuffersourcenode
pub(crate) fn construct(
cx: &mut CallContext,
context: AnyBaseAudioContext,
diff --git a/Tests/LibWeb/Text/expected/WebAudio/AudioBufferSourceNode-constructor-buffer.txt b/Tests/LibWeb/Text/expected/WebAudio/AudioBufferSourceNode-constructor-buffer.txt
new file mode 100644
index 00000000000..721de5fa689
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/WebAudio/AudioBufferSourceNode-constructor-buffer.txt
@@ -0,0 +1,3 @@
+Constructor installed the buffer: true
+InvalidStateError
+First non-null assignment succeeded: true
diff --git a/Tests/LibWeb/Text/input/WebAudio/AudioBufferSourceNode-constructor-buffer.html b/Tests/LibWeb/Text/input/WebAudio/AudioBufferSourceNode-constructor-buffer.html
new file mode 100644
index 00000000000..4806eb74eff
--- /dev/null
+++ b/Tests/LibWeb/Text/input/WebAudio/AudioBufferSourceNode-constructor-buffer.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const context = new OfflineAudioContext(1, 128, 8000);
+ const first = context.createBuffer(1, 128, 8000);
+ const second = context.createBuffer(1, 128, 8000);
+ const source = new AudioBufferSourceNode(context, { buffer: first });
+ println(`Constructor installed the buffer: ${source.buffer === first}`);
+ source.buffer = null;
+ try {
+ source.buffer = second;
+ println("Replaced the buffer");
+ } catch (error) {
+ println(error.name);
+ }
+
+ const empty = new AudioBufferSourceNode(context, { buffer: null });
+ empty.buffer = second;
+ println(`First non-null assignment succeeded: ${empty.buffer === second}`);
+ });
+</script>