Skip to content

Truly independent web browser

LibAudio: Mix only finite, bounded samples from shared rings

Luke Wilded4667beac471
diff --git a/Libraries/LibAudio/SharedAudioFrameRing.cpp b/Libraries/LibAudio/SharedAudioFrameRing.cpp
index a49e760646c..cdd9d9666e8 100644
--- a/Libraries/LibAudio/SharedAudioFrameRing.cpp
+++ b/Libraries/LibAudio/SharedAudioFrameRing.cpp
@@ -8,9 +8,14 @@
#include <LibAudio/SharedAudioFrameRing.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
+#include <math.h>
namespace Audio {
+// Far above anything a device can play, yet small enough that mixing bounded samples from any realistic number of
+// streams, with gains of at most 1, stays finite.
+static constexpr float MAXIMUM_SAMPLE_MAGNITUDE = 16.0f;
+
size_t SharedAudioFrameRing::buffer_size_for(u64 frame_capacity, u32 channel_count)
{
return SAMPLES_OFFSET + frame_capacity * channel_count * sizeof(float);
@@ -108,8 +113,13 @@ size_t SharedAudioFrameRing::pop_mixing_into(Span<float> mix, float gain)
auto sample_capacity = frame_capacity * channel_count;
auto ring_index = (head & (frame_capacity - 1)) * channel_count;
+ // The producer can write anything into the samples, even while they are read, so each is read once and made
+ // finite and bounded before it is mixed.
for (size_t i = 0; i < frames_to_read * channel_count; i++) {
- mix[i] += m_samples[ring_index] * gain;
+ auto sample = m_samples[ring_index];
+ if (!isfinite(sample))
+ sample = 0.0f;
+ mix[i] += clamp(sample, -MAXIMUM_SAMPLE_MAGNITUDE, MAXIMUM_SAMPLE_MAGNITUDE) * gain;
if (++ring_index == sample_capacity)
ring_index = 0;
}
diff --git a/Tests/LibAudio/TestSharedAudioFrameRing.cpp b/Tests/LibAudio/TestSharedAudioFrameRing.cpp
index 1b49fa2b0da..a90149b01b2 100644
--- a/Tests/LibAudio/TestSharedAudioFrameRing.cpp
+++ b/Tests/LibAudio/TestSharedAudioFrameRing.cpp
@@ -9,6 +9,7 @@
#include <LibCore/System.h>
#include <LibTest/TestCase.h>
#include <LibThreading/Thread.h>
+#include <math.h>
static constexpr u32 SAMPLE_RATE = 48000;
static constexpr u32 CHANNEL_COUNT = 2;
@@ -124,7 +125,8 @@ TEST_CASE(mixing_pop_wraps_with_six_channels)
static constexpr u32 channel_count = 6;
auto producer = MUST(Audio::SharedAudioFrameRing::create(SAMPLE_RATE, channel_count, 8));
auto consumer = map_consumer_side(producer);
- auto stamp = [](u64 frame, u32 channel) { return static_cast<float>(frame * 8 + channel); };
+ // Kept within the magnitude a mixing pop lets through.
+ auto stamp = [](u64 frame, u32 channel) { return static_cast<float>(frame) + (static_cast<float>(channel) * 0.125f); };
auto push_frames = [&](u64 first_frame, size_t frame_count) {
Vector<float> samples;
for (u64 frame = first_frame; frame < first_frame + frame_count; ++frame)
@@ -148,6 +150,32 @@ TEST_CASE(mixing_pop_wraps_with_six_channels)
EXPECT_EQ(mix[(frame - 5) * channel_count + channel], stamp(frame, channel));
}
+TEST_CASE(mixing_pop_keeps_the_mix_finite)
+{
+ auto producer = MUST(Audio::SharedAudioFrameRing::create(SAMPLE_RATE, CHANNEL_COUNT, 8));
+ auto consumer = map_consumer_side(producer);
+ auto huge = NumericLimits<float>::max();
+ EXPECT_EQ(producer.try_push(Vector<float> { NAN, INFINITY, -INFINITY, 0.5f, huge, -huge, huge, -huge }), 4u);
+
+ // Non-finite samples add nothing, and the rest pass through.
+ Vector<float> mix;
+ mix.resize(2 * CHANNEL_COUNT);
+ mix.fill(0.0f);
+ EXPECT_EQ(consumer.pop_mixing_into(mix, 1.0f), 2u);
+ EXPECT_EQ(mix[0], 0.0f);
+ EXPECT_EQ(mix[1], 0.0f);
+ EXPECT_EQ(mix[2], 0.0f);
+ EXPECT_EQ(mix[3], 0.5f);
+
+ // Huge samples are bounded, so adding two of them up stays finite.
+ mix.resize(CHANNEL_COUNT);
+ mix.fill(0.0f);
+ EXPECT_EQ(consumer.pop_mixing_into(mix, 1.0f), 1u);
+ EXPECT_EQ(consumer.pop_mixing_into(mix, 1.0f), 1u);
+ EXPECT(isfinite(mix[0]) && mix[0] > 0.0f);
+ EXPECT(isfinite(mix[1]) && mix[1] < 0.0f);
+}
+
TEST_CASE(discard_all_empties_the_ring)
{
auto producer = MUST(Audio::SharedAudioFrameRing::create(SAMPLE_RATE, CHANNEL_COUNT, 8));