From 2858cbe05d595dde77fb24b11645ff0f422d4126 Mon Sep 17 00:00:00 2001 From: Mark Kremer Date: Wed, 3 Jan 2024 19:29:08 +0100 Subject: [PATCH] Replace the implementation of Mix with Mixer --- compositors.go | 42 ++++------------------------------- compositors_test.go | 4 +--- internal/testtools/asserts.go | 22 ++++++++++++++++++ 3 files changed, 27 insertions(+), 41 deletions(-) diff --git a/compositors.go b/compositors.go index d874d9f..755cab2 100644 --- a/compositors.go +++ b/compositors.go @@ -98,44 +98,10 @@ func Seq(s ...Streamer) Streamer { // // Mix does not propagate errors from the Streamers. func Mix(s ...Streamer) Streamer { - return StreamerFunc(func(samples [][2]float64) (n int, ok bool) { - var tmp [512][2]float64 - - for len(samples) > 0 { - toStream := len(tmp) - if toStream > len(samples) { - toStream = len(samples) - } - - // clear the samples - for i := range samples[:toStream] { - samples[i] = [2]float64{} - } - - snMax := 0 // max number of streamed samples in this iteration - for _, st := range s { - // mix the stream - sn, sok := st.Stream(tmp[:toStream]) - if sn > snMax { - snMax = sn - } - ok = ok || sok - - for i := range tmp[:sn] { - samples[i][0] += tmp[i][0] - samples[i][1] += tmp[i][1] - } - } - - n += snMax - if snMax < len(tmp) { - break - } - samples = samples[snMax:] - } - - return n, ok - }) + return &Mixer{ + streamers: s, + stopWhenEmpty: true, + } } // Dup returns two Streamers which both stream the same data as the original s. The two Streamers diff --git a/compositors_test.go b/compositors_test.go index 53b9dd5..24a1ca5 100644 --- a/compositors_test.go +++ b/compositors_test.go @@ -91,9 +91,7 @@ func TestMix(t *testing.T) { got := testtools.Collect(beep.Mix(s...)) - if !reflect.DeepEqual(want, got) { - t.Error("Mix not working correctly") - } + testtools.AssertSamplesEqual(t, want, got) } func TestDup(t *testing.T) { diff --git a/internal/testtools/asserts.go b/internal/testtools/asserts.go index 3f48dca..f9afe95 100644 --- a/internal/testtools/asserts.go +++ b/internal/testtools/asserts.go @@ -57,3 +57,25 @@ func AssertStreamerHasCorrectReturnBehaviour(t *testing.T, s beep.Streamer, expe assert.Equal(t, 0, n) assert.NoError(t, s.Err()) } + +func AssertSamplesEqual(t *testing.T, expected, actual [][2]float64) { + t.Helper() + + if len(expected) != len(actual) { + t.Errorf("expected sample data length to be %d, got %d", len(expected), len(actual)) + return + } + + const epsilon = 1e-9 + equals := true + for i := range expected { + if actual[i][0] < expected[i][0]-epsilon || actual[i][0] > expected[i][0]+epsilon || + actual[i][1] < expected[i][1]-epsilon || actual[i][1] > expected[i][1]+epsilon { + equals = false + break + } + } + if !equals { + t.Errorf("the sample data isn't equal to the expected data") + } +}