Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move beep.Silence to generators.Silence #131

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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