Skip to content

Truly independent web browser

LibWeb: Apply the specified PannerNode distance bounds

Shannon Booth01f1f54a8171
diff --git a/Libraries/LibWeb/Rust/src/web_audio/rendering/render_nodes.rs b/Libraries/LibWeb/Rust/src/web_audio/rendering/render_nodes.rs
index d113b506d04..b4167255a6b 100644
--- a/Libraries/LibWeb/Rust/src/web_audio/rendering/render_nodes.rs
+++ b/Libraries/LibWeb/Rust/src/web_audio/rendering/render_nodes.rs
@@ -1319,37 +1319,36 @@ fn compute_azimuth(
azimuth
}
-// https://webaudio.github.io/web-audio-api/#Spatialization-distance-effects
+// https://webaudio.github.io/web-audio-api/#enumdef-distancemodeltype
fn compute_distance_gain(distance: f64, config: &SetPannerParameters) -> f64 {
let ref_distance = config.ref_distance;
let max_distance = config.max_distance;
let rolloff_factor = config.rolloff_factor;
- // AD-HOC: The distance is clamped to the interval spanned by refDistance and maxDistance for every distance
- // model, matching other engines.
- let clamped_distance = clamp(
- distance,
- cpp_min(ref_distance, max_distance),
- cpp_max(ref_distance, max_distance),
- );
match config.distance_model {
DistanceModelType::Linear => {
- if ref_distance == max_distance {
- return 1. - clamp(rolloff_factor, 0., 1.);
+ let lower_distance = cpp_min(ref_distance, max_distance);
+ let upper_distance = cpp_max(ref_distance, max_distance);
+ let rolloff_factor = clamp(rolloff_factor, 0., 1.);
+ if lower_distance == upper_distance {
+ return 1. - rolloff_factor;
}
- 1. - clamp(rolloff_factor, 0., 1.) * (clamped_distance - ref_distance) / (max_distance - ref_distance)
+ let distance = clamp(distance, lower_distance, upper_distance);
+ 1. - rolloff_factor * (distance - lower_distance) / (upper_distance - lower_distance)
}
DistanceModelType::Inverse => {
- if ref_distance + rolloff_factor * (clamped_distance - ref_distance) == 0. {
- return 1.;
+ if ref_distance == 0. {
+ return 0.;
}
- ref_distance / (ref_distance + rolloff_factor * (clamped_distance - ref_distance))
+ let distance = cpp_max(distance, ref_distance);
+ ref_distance / (ref_distance + rolloff_factor * (distance - ref_distance))
}
DistanceModelType::Exponential => {
if ref_distance == 0. {
- return 1.;
+ return 0.;
}
- math::pow(clamped_distance / ref_distance, -rolloff_factor)
+ let distance = cpp_max(distance, ref_distance);
+ math::pow(distance / ref_distance, -rolloff_factor)
}
}
}
diff --git a/Tests/LibWeb/Text/expected/WebAudio/PannerNode-distance-models.txt b/Tests/LibWeb/Text/expected/WebAudio/PannerNode-distance-models.txt
new file mode 100644
index 00000000000..61afcaa6a09
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/WebAudio/PannerNode-distance-models.txt
@@ -0,0 +1,5 @@
+inverse beyond maxDistance: 0.2500
+inverse with zero refDistance: 0.0000
+exponential beyond maxDistance: 0.2500
+exponential with zero refDistance: 0.0000
+linear with reversed bounds: 0.7500
diff --git a/Tests/LibWeb/Text/input/WebAudio/PannerNode-distance-models.html b/Tests/LibWeb/Text/input/WebAudio/PannerNode-distance-models.html
new file mode 100644
index 00000000000..045165c86d9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/WebAudio/PannerNode-distance-models.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ async function distanceGain(distanceModel, refDistance, maxDistance, positionZ, rolloffFactor = 1) {
+ const context = new OfflineAudioContext(2, 128, 48000);
+ const source = new ConstantSourceNode(context);
+ const panner = new PannerNode(context, {
+ distanceModel, refDistance, maxDistance, positionZ, rolloffFactor
+ });
+ source.connect(panner).connect(context.destination);
+ source.start();
+ const output = await context.startRendering();
+ return (output.getChannelData(0)[0] * Math.SQRT2).toFixed(4);
+ }
+
+ promiseTest(async () => {
+ for (const model of ["inverse", "exponential"]) {
+ println(`${model} beyond maxDistance: ${await distanceGain(model, 1, 2, -4)}`);
+ println(`${model} with zero refDistance: ${await distanceGain(model, 0, 2, -4)}`);
+ }
+ println(`linear with reversed bounds: ${await distanceGain("linear", 4, 2, -2.5)}`);
+ });
+</script>