-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from gopxl/move-silence-func-to-generators
Move `beep.Silence` to `generators.Silence`
- Loading branch information
Showing
5 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters