Truly independent web browser
LibWeb: Let a collected ConvolverNode play out its tail
sideshowbarkeraf0e8eabaea0
diff --git a/Libraries/LibWeb/Rust/src/web_audio/rendering/render_graph.rs b/Libraries/LibWeb/Rust/src/web_audio/rendering/render_graph.rsindex 04905f91b2e..ccd05564e36 100644--- a/Libraries/LibWeb/Rust/src/web_audio/rendering/render_graph.rs+++ b/Libraries/LibWeb/Rust/src/web_audio/rendering/render_graph.rs@@ -19,6 +19,8 @@ use crate::web_audio::control_message::ControlMessage;// https://webaudio.github.io/web-audio-api/#rendering-looppub struct RenderGraph {nodes: NodeMap,+ // Nodes whose AudioNode is gone, but which still have a tail to play. Each is removed once its tail has played out.+ nodes_finishing_tails: Vec<u64>,destination_id: u64,current_quantum: Cell<u64>,incoming_connections_dirty: Cell<bool>,@@ -28,6 +30,7 @@ impl Default for RenderGraph {fn default() -> Self {Self {nodes: NodeMap::default(),+ nodes_finishing_tails: Vec::new(),destination_id: 0,current_quantum: Cell::new(0),incoming_connections_dirty: Cell::new(false),@@ -37,6 +40,7 @@ impl Default for RenderGraph {impl RenderGraph {pub fn apply_control_messages(&mut self, messages: Vec<ControlMessage>) {+ self.remove_nodes_whose_tails_have_played_out();for message in messages {match message {ControlMessage::AddNode(add_node) => {@@ -45,7 +49,14 @@ impl RenderGraph {self.incoming_connections_dirty.set(true);}ControlMessage::RemoveNode(remove_node) => {- if self.nodes.remove(&remove_node.node_id).is_some() {+ // A node that still has a tail keeps its connections and goes on rendering it, as nothing can+ // reach its AudioNode to stop it.+ let has_tail_remaining = self+ .node(remove_node.node_id)+ .is_some_and(|node| unsafe { node.node.has_tail_remaining() });+ if has_tail_remaining {+ self.nodes_finishing_tails.push(remove_node.node_id);+ } else if self.nodes.remove(&remove_node.node_id).is_some() {self.incoming_connections_dirty.set(true);}}@@ -81,6 +92,26 @@ impl RenderGraph {}}+ fn remove_nodes_whose_tails_have_played_out(&mut self) {+ if self.nodes_finishing_tails.is_empty() {+ return;+ }+ let nodes = &mut self.nodes;+ let mut removed_any = false;+ self.nodes_finishing_tails.retain(|node_id| {+ let has_tail_remaining = nodes+ .get(node_id)+ .is_some_and(|node| unsafe { node.node.has_tail_remaining() });+ if !has_tail_remaining {+ removed_any |= nodes.remove(node_id).is_some();+ }+ has_tail_remaining+ });+ if removed_any {+ self.incoming_connections_dirty.set(true);+ }+ }+// Renders a single quantum by pulling the destination node and any active source nodes.pub fn render_quantum(&self, context: &RenderContext) {self.current_quantum.set(self.current_quantum.get().wrapping_add(1));diff --git a/Libraries/LibWeb/Rust/src/web_audio/rendering/render_node.rs b/Libraries/LibWeb/Rust/src/web_audio/rendering/render_node.rsindex 433d0013406..9ab177faab4 100644--- a/Libraries/LibWeb/Rust/src/web_audio/rendering/render_node.rs+++ b/Libraries/LibWeb/Rust/src/web_audio/rendering/render_node.rs@@ -359,6 +359,13 @@ pub(crate) trait RenderNode: Send {unsafe fn take_ended_notification(&self) -> bool {false}++ // Whether this node may still produce output for input it has already received: the tail of a node whose tail-time+ // is yes. Such a node keeps playing after its AudioNode is gone, until the tail has played out.+ // https://webaudio.github.io/web-audio-api/#tail-time+ unsafe fn has_tail_remaining(&self) -> bool {+ false+ }}pub struct NodeHandle {diff --git a/Libraries/LibWeb/Rust/src/web_audio/rendering/render_nodes.rs b/Libraries/LibWeb/Rust/src/web_audio/rendering/render_nodes.rsindex ea1eeceb52e..3ae3ea8001e 100644--- a/Libraries/LibWeb/Rust/src/web_audio/rendering/render_nodes.rs+++ b/Libraries/LibWeb/Rust/src/web_audio/rendering/render_nodes.rs@@ -98,6 +98,9 @@ pub(super) trait NodeProcessor: Send {fn take_ended_notification(&mut self) -> bool {false}+ fn has_tail_remaining(&self) -> bool {+ false+ }}struct NativeRenderNode<T: NodeProcessor> {@@ -127,6 +130,10 @@ impl<T: NodeProcessor> RenderNode for NativeRenderNode<T> {unsafe fn take_ended_notification(&self) -> bool {unsafe { &mut *self.processor.get() }.take_ended_notification()}++ unsafe fn has_tail_remaining(&self) -> bool {+ unsafe { &*self.processor.get() }.has_tail_remaining()+ }}pub(super) fn node_handle<T: NodeProcessor + 'static>(data: RenderNodeData, processor: T) -> Box<NodeHandle> {@@ -1422,6 +1429,9 @@ struct ConvolverRenderNode {// per bin, and single precision loses too much of the quiet parts of the result.accumulator_real: Vec<f64>,accumulator_imag: Vec<f64>,++ // How many quanta in a row have brought only silence, which tells how much of the tail is left to play.+ silent_input_quanta: usize,}// Folds a second channel's history into the first the way down-mixing folds the channels themselves.@@ -1440,9 +1450,25 @@ impl NodeProcessor for ConvolverRenderNode {}}+ // The window and the delay line hold the last partition_count + 2 quanta of input, so the tail has played out once+ // that many quanta of silence have gone through.+ fn has_tail_remaining(&self) -> bool {+ let Some(kernel) = &self.kernel else {+ return false;+ };+ self.silent_input_quanta < kernel.partition_count() + 2+ }+unsafe fn process(&mut self, data: &RenderNodeData, graph: &RenderGraph, context: &RenderContext) {unsafe {let input = BusRef(data.pull_input(graph, context, 0).cast_mut());+ let input_is_silent =+ (0..input.channel_count()).all(|channel| input.channel(channel).iter().all(|sample| *sample == 0.));+ self.silent_input_quanta = if input_is_silent {+ self.silent_input_quanta.saturating_add(1)+ } else {+ 0+ };let convolver_output = BusRef(data.output(0));let input_channel_count = input.channel_count()@@ -2054,6 +2080,8 @@ pub fn create_convolver_render_node(node_id: u64, quantum_size: usize) -> Box<Notransform_imag: vec![0.; 2 * quantum_size],accumulator_real: vec![0.; quantum_size + 1],accumulator_imag: vec![0.; quantum_size + 1],+ // Nothing has come in yet, so there's no tail to play.+ silent_input_quanta: usize::MAX,},)}diff --git a/Tests/LibWeb/Text/expected/WebAudio/convolver-tail-plays-after-its-node-is-collected.txt b/Tests/LibWeb/Text/expected/WebAudio/convolver-tail-plays-after-its-node-is-collected.txtnew file mode 100644index 00000000000..e7bee7bda78--- /dev/null+++ b/Tests/LibWeb/Text/expected/WebAudio/convolver-tail-plays-after-its-node-is-collected.txt@@ -0,0 +1 @@+the echo arrives: truediff --git a/Tests/LibWeb/Text/input/WebAudio/convolver-tail-plays-after-its-node-is-collected.html b/Tests/LibWeb/Text/input/WebAudio/convolver-tail-plays-after-its-node-is-collected.htmlnew file mode 100644index 00000000000..97d7c2bd986--- /dev/null+++ b/Tests/LibWeb/Text/input/WebAudio/convolver-tail-plays-after-its-node-is-collected.html@@ -0,0 +1,31 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // A ConvolverNode's tail keeps playing after its input has ended, even when nothing references the node any more and+ // it gets collected in the meantime.+ promiseTest(async () => {+ const sampleRate = 48000;+ const context = new OfflineAudioContext(1, sampleRate, sampleRate);++ // An impulse response that echoes its input half a second later.+ const impulseResponse = context.createBuffer(1, sampleRate / 2 + 1, sampleRate);+ impulseResponse.getChannelData(0)[sampleRate / 2] = 1;++ (() => {+ const convolver = new ConvolverNode(context, { buffer: impulseResponse, disableNormalization: true });+ const source = new ConstantSourceNode(context);+ source.connect(convolver).connect(context.destination);+ source.start(0);+ source.stop(128 / sampleRate);+ })();++ // Collect the nodes once the source has ended, while the echo is still to come.+ context.suspend(0.25).then(async () => {+ await internals.gcAsync();+ context.resume();+ });++ const samples = (await context.startRendering()).getChannelData(0);+ println(`the echo arrives: ${samples[sampleRate / 2 + 64] === 1}`);+ });+</script>