Skip to content

Commit

Permalink
Merge pull request #131 from gopxl/move-silence-func-to-generators
Browse files Browse the repository at this point in the history
Move `beep.Silence` to `generators.Silence`
  • Loading branch information
MarkKremer authored Nov 9, 2023
2 parents 0648543 + 11b5aeb commit 12f8f93
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
3 changes: 2 additions & 1 deletion buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

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

func TestFormatEncodeDecode(t *testing.T) {
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestBufferAppendPop(t *testing.T) {

for format := range formats {
b := beep.NewBuffer(format)
b.Append(beep.Silence(768))
b.Append(generators.Silence(768))
if b.Len() != 768 {
t.Fatalf("buffer length isn't equal to appended stream length: expected: %v, actual: %v (NumChannels: %v)", 768, b.Len(), format.NumChannels)
}
Expand Down
31 changes: 31 additions & 0 deletions generators/silence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package generators

import "github.com/gopxl/beep"

// Silence returns a Streamer which streams num samples of silence. If num is negative, silence is
// streamed forever.
func Silence(num int) beep.Streamer {
if num < 0 {
return beep.StreamerFunc(func(samples [][2]float64) (m int, ok bool) {
for i := range samples {
samples[i] = [2]float64{}
}
return len(samples), true
})
}

return beep.StreamerFunc(func(samples [][2]float64) (n int, ok bool) {
if num <= 0 {
return 0, false
}
if num < len(samples) {
samples = samples[:num]
}
for i := range samples {
samples[i] = [2]float64{}
}
num -= len(samples)

return len(samples), true
})
}
30 changes: 30 additions & 0 deletions generators/silence_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package generators_test

import (
"testing"

"github.com/stretchr/testify/assert"

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

func TestSilence_StreamsFiniteSamples(t *testing.T) {
s := generators.Silence(100)

got := testtools.CollectNum(200, s)
assert.Equal(t, make([][2]float64, 100), got)

got = testtools.CollectNum(200, s)
assert.Len(t, got, 0)
}

func TestSilence_StreamsInfiniteSamples(t *testing.T) {
s := generators.Silence(-1)

got := testtools.CollectNum(200, s)
assert.Equal(t, make([][2]float64, 200), got)

got = testtools.CollectNum(200, s)
assert.Equal(t, make([][2]float64, 200), got)
}
2 changes: 2 additions & 0 deletions streamers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package beep

// Silence returns a Streamer which streams num samples of silence. If num is negative, silence is
// streamed forever.
//
// Deprecated: beep.Silence has been moved to generators.Silence
func Silence(num int) Streamer {
return StreamerFunc(func(samples [][2]float64) (n int, ok bool) {
if num == 0 {
Expand Down
7 changes: 5 additions & 2 deletions wav/encode_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package wav

import (
"testing"

"github.com/gopxl/beep"
"github.com/gopxl/beep/generators"

"github.com/orcaman/writerseeker"
"github.com/stretchr/testify/assert"
"testing"
)

func TestEncode(t *testing.T) {
Expand All @@ -14,7 +17,7 @@ func TestEncode(t *testing.T) {
Precision: 2,
}
var w writerseeker.WriterSeeker
var s = beep.Silence(5)
var s = generators.Silence(5)

err := Encode(&w, s, f)
if err != nil {
Expand Down

0 comments on commit 12f8f93

Please sign in to comment.