-
Notifications
You must be signed in to change notification settings - Fork 17
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 #127 from gopxl/fix-flac-decode-eof
Fix flac.Decode handling of io.EOF
- Loading branch information
Showing
10 changed files
with
150 additions
and
74 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
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,39 @@ | ||
package flac_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/gopxl/beep/flac" | ||
"github.com/gopxl/beep/internal/testtools" | ||
) | ||
|
||
func TestDecoder_Stream(t *testing.T) { | ||
f, err := os.Open(testtools.TestFilePath("valid_44100hz_22050_samples.flac")) | ||
assert.NoError(t, err) | ||
defer f.Close() | ||
|
||
streamer, _, err := flac.Decode(f) | ||
assert.NoError(t, err) | ||
|
||
// Case 1: return ok with all requested samples | ||
buf := testtools.CollectNum(22000, streamer) | ||
assert.Lenf(t, buf, 22000, "streamer quit prematurely; expected %d samples, got %d", 22000, len(buf)) | ||
assert.NoError(t, streamer.Err()) | ||
|
||
buf = make([][2]float64, 512) | ||
|
||
// Case 2: return ok with 0 < n < 512 samples | ||
n, ok := streamer.Stream(buf[:]) | ||
assert.True(t, ok) | ||
assert.Equal(t, 50, n) | ||
assert.NoError(t, streamer.Err()) | ||
|
||
// Case 3: return !ok with n == 0 | ||
n, ok = streamer.Stream(buf[:]) | ||
assert.False(t, ok) | ||
assert.Equal(t, 0, n) | ||
assert.NoError(t, streamer.Err()) | ||
} |
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
Binary file not shown.
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,23 @@ | ||
package testtools | ||
|
||
import "github.com/gopxl/beep" | ||
|
||
// Collect drains Streamer s and returns all the samples it streamed. | ||
func Collect(s beep.Streamer) [][2]float64 { | ||
var ( | ||
result [][2]float64 | ||
buf [479][2]float64 | ||
) | ||
for { | ||
n, ok := s.Stream(buf[:]) | ||
if !ok { | ||
return result | ||
} | ||
result = append(result, buf[:n]...) | ||
} | ||
} | ||
|
||
// CollectNum collects num samples from the streamer in chunks and returns them all. | ||
func CollectNum(num int, s beep.Streamer) [][2]float64 { | ||
return Collect(beep.Take(num, s)) | ||
} |
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,48 @@ | ||
package testtools | ||
|
||
import ( | ||
"math/rand" | ||
|
||
"github.com/gopxl/beep" | ||
) | ||
|
||
// RandomDataStreamer generates numSamples random samples and returns a StreamSeeker to stream them. | ||
func RandomDataStreamer(numSamples int) (s beep.StreamSeeker, data [][2]float64) { | ||
data = make([][2]float64, numSamples) | ||
for i := range data { | ||
data[i][0] = rand.Float64()*2 - 1 | ||
data[i][1] = rand.Float64()*2 - 1 | ||
} | ||
return &dataStreamer{data, 0}, data | ||
} | ||
|
||
type dataStreamer struct { | ||
data [][2]float64 | ||
pos int | ||
} | ||
|
||
func (ds *dataStreamer) Stream(samples [][2]float64) (n int, ok bool) { | ||
if ds.pos >= len(ds.data) { | ||
return 0, false | ||
} | ||
n = copy(samples, ds.data[ds.pos:]) | ||
ds.pos += n | ||
return n, true | ||
} | ||
|
||
func (ds *dataStreamer) Err() error { | ||
return nil | ||
} | ||
|
||
func (ds *dataStreamer) Len() int { | ||
return len(ds.data) | ||
} | ||
|
||
func (ds *dataStreamer) Position() int { | ||
return ds.pos | ||
} | ||
|
||
func (ds *dataStreamer) Seek(p int) error { | ||
ds.pos = p | ||
return nil | ||
} |
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,16 @@ | ||
package testtools | ||
|
||
import ( | ||
"path/filepath" | ||
"runtime" | ||
) | ||
|
||
var ( | ||
// See https://stackoverflow.com/a/38644571 | ||
_, callerPath, _, _ = runtime.Caller(0) | ||
TestDataDir = filepath.Join(filepath.Dir(callerPath), "../testdata") | ||
) | ||
|
||
func TestFilePath(path string) string { | ||
return filepath.Join(TestDataDir, path) | ||
} |
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