Skip to content

Commit

Permalink
Test SineTone generator
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkKremer committed Nov 2, 2023
1 parent 53c3045 commit 198a5e1
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions generators/sine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package generators_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/gopxl/beep"
"github.com/gopxl/beep/generators"
"github.com/gopxl/beep/internal/testtools"
)

func TestSineTone(t *testing.T) {
epsilon := 0.000001

s, err := generators.SineTone(beep.SampleRate(8000), 400)
assert.NoError(t, err)

// Get a full single phase including the last sample.
phaseLength := 8000 / 400
samples := testtools.CollectNum(phaseLength+1, s)

// The sine wave should be 0 at the start, half a phase and at the end of the phase.
assert.InDelta(t, 0, samples[phaseLength*0][0], epsilon)
assert.InDelta(t, 0, samples[phaseLength*0][1], epsilon)
assert.InDelta(t, 0, samples[phaseLength*1/2][0], epsilon)
assert.InDelta(t, 0, samples[phaseLength*1/2][1], epsilon)
assert.InDelta(t, 0, samples[phaseLength*1][0], epsilon)
assert.InDelta(t, 0, samples[phaseLength*1][1], epsilon)

// The sine wave should be in a peak and trough at 1/4th and 3/4th in the phase respectively.
assert.InDelta(t, 1, samples[phaseLength*1/4][0], epsilon)
assert.InDelta(t, 1, samples[phaseLength*1/4][1], epsilon)
assert.InDelta(t, -1, samples[phaseLength*3/4][0], epsilon)
assert.InDelta(t, -1, samples[phaseLength*3/4][1], epsilon)
}

0 comments on commit 198a5e1

Please sign in to comment.