Truly independent web browser
AudioServer: Clip the mix to the range devices play
Luke Wilde2753b023d6ad
diff --git a/Services/AudioServer/PlaybackStreamMixer.cpp b/Services/AudioServer/PlaybackStreamMixer.cppindex e02cb01e1d0..0a1c6add8f7 100644--- a/Services/AudioServer/PlaybackStreamMixer.cpp+++ b/Services/AudioServer/PlaybackStreamMixer.cpp@@ -261,6 +261,9 @@ ReadonlySpan<float> PlaybackStreamMixer::MixState::mix_into(Span<float> buffer,client.ring.publish_consumption(client.played_at_ns, client.state == ClientState::Playing && consumed_frames > 0);}}+ // The mix is clipped to the [-1, 1] that devices play, rather than leaving sound beyond it to each platform.+ for (auto& sample : buffer)+ sample = clamp(sample, -1.0f, 1.0f);for (auto drained : drained_clients) {m_control_loop.deferred_invoke([self = NonnullRefPtr(*this), drained] {if (self->m_mixer)diff --git a/Tests/AudioServer/TestPlaybackStreamMixer.cpp b/Tests/AudioServer/TestPlaybackStreamMixer.cppindex 2ea2f1d1783..1753e92cd07 100644--- a/Tests/AudioServer/TestPlaybackStreamMixer.cpp+++ b/Tests/AudioServer/TestPlaybackStreamMixer.cpp@@ -313,6 +313,31 @@ TEST_CASE(gain_changes_apply_to_the_next_callback)EXPECT_EQ(sample, 0.25f);}+TEST_CASE(the_mix_is_clipped_to_what_devices_play)+{+ MixerFixture fixture;+ auto first = fixture.add_client(1);+ auto second = fixture.add_client(2);+ fixture.mixer->resume_client(1);+ fixture.mixer->resume_client(2);++ auto mix = [&](float first_sample, float second_sample) {+ EXPECT_EQ(first.try_push(constant_frames(64, first_sample)), 64u);+ EXPECT_EQ(second.try_push(constant_frames(64, second_sample)), 64u);+ return fixture.device->render(64);+ };+ for (auto sample : mix(0.75f, 0.75f))+ EXPECT_EQ(sample, 1.0f);+ for (auto sample : mix(-0.75f, -0.75f))+ EXPECT_EQ(sample, -1.0f);+ for (auto sample : mix(0.25f, 0.5f))+ EXPECT_EQ(sample, 0.75f);++ // Sound beyond the range can still be cancelled out by the rest of the mix.+ for (auto sample : mix(1.5f, -1.0f))+ EXPECT_EQ(sample, 0.5f);+}+TEST_CASE(resuming_a_client_whose_ring_ran_empty_does_not_count_it_twice){MixerFixture fixture;