Truly independent web browser
LibWeb: Avoid pops in stitched WebAudio buffers
Zaggy10240539fba52614
diff --git a/Libraries/LibWeb/WebAudio/Rendering/RenderNodes.cpp b/Libraries/LibWeb/WebAudio/Rendering/RenderNodes.cppindex 29784e6ebc0..11a32446d0b 100644--- a/Libraries/LibWeb/WebAudio/Rendering/RenderNodes.cpp+++ b/Libraries/LibWeb/WebAudio/Rendering/RenderNodes.cpp@@ -17,6 +17,8 @@ namespace Web::WebAudio::Rendering {// fundamental frequencies.static constexpr size_t MAX_OSCILLATOR_HARMONICS = 5000;+static constexpr double FRAME_BOUNDARY_SNAPPING_TOLERANCE = 1.0 / 2048;+struct PanGains {float left { 0 };float right { 0 };@@ -84,6 +86,13 @@ void ScheduledSourceRenderNode::process(RenderGraph& graph, RenderContext const&// NB: The start time is clamped to the first render quantum processed after the start message arrived, since the// control thread's currentTime may already have advanced past the scheduled time by then.if (m_start_time_needs_clamping) {+ // AD-HOC: Start times built by summing buffer durations may induce rounding error and place the start part+ // of a frame after the prior buffer's end. Snap starts that are close to a frame boundary onto it+ // to prevent this from becoming audible in continuous streams.+ auto start_frame = *m_start_time * context.sample_rate;+ auto nearest_frame = AK::round(start_frame);+ if (AK::abs(start_frame - nearest_frame) < FRAME_BOUNDARY_SNAPPING_TOLERANCE)+ m_start_time = nearest_frame / context.sample_rate;m_start_time = max(*m_start_time, context.quantum_start_time());m_start_time_needs_clamping = false;}@@ -300,6 +309,12 @@ float AudioBufferSourceRenderNode::sample_buffer(size_t channel, double playhead};if (fraction == 0.f)return sample_at(index);+ // Past the last frame there is nothing to interpolate toward, so the slope leading into it is continued instead.+ if (!m_loop && index == static_cast<i64>(samples.size()) - 1) {+ if (index == 0)+ return sample_at(index);+ return sample_at(index) + fraction * (sample_at(index) - sample_at(index - 1));+ }return sample_at(index) + fraction * (sample_at(index + 1) - sample_at(index));}diff --git a/Tests/LibWeb/Text/expected/WebAudio/AudioBufferSourceNode-summed-start-times.txt b/Tests/LibWeb/Text/expected/WebAudio/AudioBufferSourceNode-summed-start-times.txtnew file mode 100644index 00000000000..33eb545d5bb--- /dev/null+++ b/Tests/LibWeb/Text/expected/WebAudio/AudioBufferSourceNode-summed-start-times.txt@@ -0,0 +1 @@+Frames that are not 0.5: nonediff --git a/Tests/LibWeb/Text/input/WebAudio/AudioBufferSourceNode-summed-start-times.html b/Tests/LibWeb/Text/input/WebAudio/AudioBufferSourceNode-summed-start-times.htmlnew file mode 100644index 00000000000..e856a7de925--- /dev/null+++ b/Tests/LibWeb/Text/input/WebAudio/AudioBufferSourceNode-summed-start-times.html@@ -0,0 +1,31 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ const sampleRate = 44100;+ const bufferLength = 32768;+ const bufferCount = 12;++ promiseTest(async () => {+ const context = new OfflineAudioContext(1, sampleRate + bufferLength * bufferCount, sampleRate);++ // Streaming players schedule each buffer at the previous start plus its duration. The sum misses frame+ // boundaries by rounding error, which must not open gaps between the buffers.+ let when = 1;+ for (let i = 0; i < bufferCount; i++) {+ const buffer = context.createBuffer(1, bufferLength, sampleRate);+ buffer.getChannelData(0).fill(0.5);+ const source = new AudioBufferSourceNode(context, { buffer });+ source.connect(context.destination);+ source.start(when);+ when += buffer.duration;+ }++ const samples = (await context.startRendering()).getChannelData(0);+ const gapFrames = [];+ for (let frame = sampleRate; frame < samples.length; frame++) {+ if (samples[frame] !== 0.5)+ gapFrames.push(frame);+ }+ println(`Frames that are not 0.5: ${gapFrames.length === 0 ? "none" : gapFrames.join(", ")}`);+ });+</script>