Skip to content

Truly independent web browser

LibWeb: Use speaker down-mixing for AnalyserNode samples

Shannon Bootha6b4f34cd11a
diff --git a/Libraries/LibWeb/Rust/src/web_audio/rendering/analyser_render_node.rs b/Libraries/LibWeb/Rust/src/web_audio/rendering/analyser_render_node.rs
index 7a5a8a735e6..a07e7d89ef5 100644
--- a/Libraries/LibWeb/Rust/src/web_audio/rendering/analyser_render_node.rs
+++ b/Libraries/LibWeb/Rust/src/web_audio/rendering/analyser_render_node.rs
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#![allow(clippy::needless_range_loop)]
+use super::audio_bus::AudioBus;
use super::audio_frame_ring::{AudioFrameRing, AudioFrameRingCallbacks};
use super::render_graph::RenderGraph;
use super::render_node::*;
@@ -14,7 +14,7 @@ use crate::web_audio::control_message::FfiSharedCppObject;
// https://webaudio.github.io/web-audio-api/#AnalyserNode
struct AnalyserRenderNode {
time_domain_ring: AudioFrameRing,
- mono_scratch: Vec<f32>,
+ mono_scratch: AudioBus,
discard_scratch: Vec<f32>,
}
@@ -27,23 +27,12 @@ impl NodeProcessor for AnalyserRenderNode {
BusRef(data.output(0)).copy_from(&input);
// https://webaudio.github.io/web-audio-api/#current-time-domain-data
- // The input signal must be down-mixed to mono as if channelCount is 1, channelCountMode
- // is "max" and channelInterpretation is "speakers".
- // AD-HOC: The down-mix is an unweighted channel average, which matches the speakers rules
- // for mono and stereo input; inputs with more channels would need the weighted
- // speakers down-mix.
- let channel_count = input.channel_count();
- for frame in 0..context.quantum_size {
- let mut sum = 0.;
- for channel_index in 0..channel_count {
- sum += input.channel(channel_index)[frame];
- }
- self.mono_scratch[frame] = if channel_count > 0 {
- sum / channel_count as f32
- } else {
- 0.
- };
- }
+ // When the current time-domain data are computed, the input signal must be down-mixed to mono as if
+ // channelCount is 1, channelCountMode is "max" and channelInterpretation is "speakers". This is independent
+ // of the settings for the AnalyserNode itself.
+ // AD-HOC: Down-mix the input as mixed by the AnalyserNode's own channel settings, like other engines.
+ self.mono_scratch.zero();
+ self.mono_scratch.sum_from(&*input.0, true);
// The ring must always end up holding the most recent samples, so a full ring (the page
// is not currently polling any of the analyser getters) drops its oldest quantum first.
@@ -54,7 +43,7 @@ impl NodeProcessor for AnalyserRenderNode {
if self.time_domain_ring.frames_free() < context.quantum_size {
self.time_domain_ring.try_pop(&mut self.discard_scratch);
}
- self.time_domain_ring.try_push(&self.mono_scratch);
+ self.time_domain_ring.try_push(self.mono_scratch.channel(0));
}
}
}
@@ -85,7 +74,7 @@ pub unsafe fn create_analyser_render_node(
data,
AnalyserRenderNode {
time_domain_ring,
- mono_scratch: vec![0.; quantum_size],
+ mono_scratch: AudioBus::new(1, quantum_size),
discard_scratch: vec![0.; quantum_size],
},
)
diff --git a/Tests/LibWeb/Text/expected/WebAudio/AnalyserNode-speaker-downmix.txt b/Tests/LibWeb/Text/expected/WebAudio/AnalyserNode-speaker-downmix.txt
new file mode 100644
index 00000000000..d9462849215
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/WebAudio/AnalyserNode-speaker-downmix.txt
@@ -0,0 +1,6 @@
+Downmix: 0.5000
+Output preserves channels: true
+Downmix: 1.4571
+Output preserves channels: true
+Downmix: 1.0000
+Output uses the node's settings: true
diff --git a/Tests/LibWeb/Text/input/WebAudio/AnalyserNode-speaker-downmix.html b/Tests/LibWeb/Text/input/WebAudio/AnalyserNode-speaker-downmix.html
new file mode 100644
index 00000000000..260c624b983
--- /dev/null
+++ b/Tests/LibWeb/Text/input/WebAudio/AnalyserNode-speaker-downmix.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ async function analyseChannels(samples, options = { channelInterpretation: "discrete" }) {
+ const context = new OfflineAudioContext(samples.length, 128, 48000);
+ const buffer = context.createBuffer(samples.length, 128, context.sampleRate);
+ for (const [channel, sample] of samples.entries())
+ buffer.getChannelData(channel).fill(sample);
+ const source = new AudioBufferSourceNode(context, { buffer });
+ const analyser = new AnalyserNode(context, { fftSize: 128, ...options });
+ source.connect(analyser).connect(context.destination);
+ source.start();
+ const output = await context.startRendering();
+ const mono = new Float32Array(analyser.fftSize);
+ analyser.getFloatTimeDomainData(mono);
+ println(`Downmix: ${mono[127].toFixed(4)}`);
+ return output;
+ }
+
+ promiseTest(async () => {
+ for (const samples of [[1, 0, 0.5, 0.5], [1, 0, 0.5, 1, 0.5, 0]]) {
+ const output = await analyseChannels(samples);
+ println(`Output preserves channels: ${samples.every((sample, channel) => output.getChannelData(channel)[127] === sample)}`);
+ }
+
+ const output = await analyseChannels([1, 0], {
+ channelCount: 1, channelCountMode: "explicit", channelInterpretation: "discrete"
+ });
+ println(`Output uses the node's settings: ${output.getChannelData(0)[127] === 1 && output.getChannelData(1)[127] === 1}`);
+ });
+</script>