From 94354ea7fb61e8bf521f6e1a29def1ea1c5c233e Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Thu, 2 May 2024 00:39:06 +1200 Subject: [PATCH] LibWeb: Clamp AudioParam's value between min and max The spec isn't _super_ clear on how this is meant to be done, but the way I understand this is that we should simply clamp the returned 'current value' between 'min' and 'max'. Firefox does not appear to do this clamping, but Chrome does. --- Tests/LibWeb/Text/expected/WebAudio/OscillatorNode.txt | 2 ++ Tests/LibWeb/Text/input/WebAudio/OscillatorNode.html | 4 ++++ Userland/Libraries/LibWeb/WebAudio/AudioParam.cpp | 5 ++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Tests/LibWeb/Text/expected/WebAudio/OscillatorNode.txt b/Tests/LibWeb/Text/expected/WebAudio/OscillatorNode.txt index 46b512cb366..2a088b22e0b 100644 --- a/Tests/LibWeb/Text/expected/WebAudio/OscillatorNode.txt +++ b/Tests/LibWeb/Text/expected/WebAudio/OscillatorNode.txt @@ -10,3 +10,5 @@ Error: 'InvalidStateError: Oscillator node type cannot be set to 'custom'', type oscillator node type: 'triangle' [object AudioParam] current: 440, default: 440, min: -22050, max: 22050, rate: a-rate [object AudioParam] current: -52, default: 440, min: -22050, max: 22050, rate: a-rate +[object AudioParam] current: 22050, default: 440, min: -22050, max: 22050, rate: a-rate +[object AudioParam] current: -22050, default: 440, min: -22050, max: 22050, rate: a-rate diff --git a/Tests/LibWeb/Text/input/WebAudio/OscillatorNode.html b/Tests/LibWeb/Text/input/WebAudio/OscillatorNode.html index 0c66f2f9efc..408c7b92155 100644 --- a/Tests/LibWeb/Text/input/WebAudio/OscillatorNode.html +++ b/Tests/LibWeb/Text/input/WebAudio/OscillatorNode.html @@ -42,5 +42,9 @@ dumpAudioParam(frequency); frequency.value = -52; dumpAudioParam(frequency); + frequency.value = 100_000; + dumpAudioParam(frequency); + frequency.value = -22051; + dumpAudioParam(frequency); }); diff --git a/Userland/Libraries/LibWeb/WebAudio/AudioParam.cpp b/Userland/Libraries/LibWeb/WebAudio/AudioParam.cpp index fc5a7568a73..5a8f0eaabd7 100644 --- a/Userland/Libraries/LibWeb/WebAudio/AudioParam.cpp +++ b/Userland/Libraries/LibWeb/WebAudio/AudioParam.cpp @@ -31,9 +31,12 @@ JS::NonnullGCPtr AudioParam::create(JS::Realm& realm, float default_ AudioParam::~AudioParam() = default; // https://webaudio.github.io/web-audio-api/#dom-audioparam-value +// https://webaudio.github.io/web-audio-api/#simple-nominal-range float AudioParam::value() const { - return m_current_value; + // Each AudioParam includes minValue and maxValue attributes that together form the simple nominal range + // for the parameter. In effect, value of the parameter is clamped to the range [minValue, maxValue]. + return clamp(m_current_value, min_value(), max_value()); } // https://webaudio.github.io/web-audio-api/#dom-audioparam-value