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

WIP: Separate Decode into DecodeStreamer and DecodeStreamSeeker functions #132

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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 examples/doppler-stereo-room/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"unicode"

"github.com/gdamore/tcell/v2"

"github.com/gopxl/beep"
"github.com/gopxl/beep/effects"
"github.com/gopxl/beep/mp3"
Expand Down Expand Up @@ -161,7 +162,7 @@ func main() {
if err != nil {
report(err)
}
streamer, format, err := mp3.Decode(f)
streamer, format, err := mp3.DecodeReader(f)
if err != nil {
report(err)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/speedy-player/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"unicode"

"github.com/gdamore/tcell/v2"

"github.com/gopxl/beep"
"github.com/gopxl/beep/effects"
"github.com/gopxl/beep/mp3"
Expand Down Expand Up @@ -151,7 +152,7 @@ func main() {
if err != nil {
report(err)
}
streamer, format, err := mp3.Decode(f)
streamer, format, err := mp3.DecodeReadSeeker(f)
if err != nil {
report(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/1-hello-beep/a/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
log.Fatal(err)
}

streamer, format, err := mp3.Decode(f)
streamer, format, err := mp3.DecodeReader(f)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/1-hello-beep/b/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
log.Fatal(err)
}

streamer, format, err := mp3.Decode(f)
streamer, format, err := mp3.DecodeReader(f)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/2-composing-and-controlling/a/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
log.Fatal(err)
}

streamer, format, err := mp3.Decode(f)
streamer, format, err := mp3.DecodeReader(f)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/2-composing-and-controlling/b/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
log.Fatal(err)
}

streamer, format, err := mp3.Decode(f)
streamer, format, err := mp3.DecodeReadSeeker(f)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/2-composing-and-controlling/c/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
log.Fatal(err)
}

streamer, format, err := mp3.Decode(f)
streamer, format, err := mp3.DecodeReadSeeker(f)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/2-composing-and-controlling/d/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
log.Fatal(err)
}

streamer, format, err := mp3.Decode(f)
streamer, format, err := mp3.DecodeReadSeeker(f)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/3-to-buffer-or-not-to-buffer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
log.Fatal(err)
}

streamer, format, err := mp3.Decode(f)
streamer, format, err := mp3.DecodeReader(f)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/4-making-own-streamers/b/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func main() {
}

// Decode it.
streamer, format, err := mp3.Decode(f)
streamer, format, err := mp3.DecodeReader(f)
if err != nil {
fmt.Println(err)
continue
Expand Down
121 changes: 86 additions & 35 deletions flac/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,55 @@ import (
//
// Do not close the supplied Reader, instead, use the Close method of the returned
// StreamSeekCloser when you want to release the resources.
func Decode(r io.Reader) (s beep.StreamSeekCloser, format beep.Format, err error) {
d := decoder{r: r}
defer func() { // hacky way to always close r if an error occurred
if closer, ok := d.r.(io.Closer); ok {
if err != nil {
closer.Close()
}
}
}()
//
// Deprecated: Decode has been replaced with DecodeReader and DecodeReadSeeker.
func Decode(r io.Reader) (beep.StreamSeekCloser, beep.Format, error) {
d := SeekableDecoder{d: Decoder{r: r}}
var err error

rs, seeker := r.(io.ReadSeeker)
if seeker {
d.stream, err = flac.NewSeek(rs)
d.seekEnabled = true
d.d.stream, err = flac.NewSeek(rs)
d.d.seekEnabled = true
} else {
d.stream, err = flac.New(r)
d.d.stream, err = flac.New(r)
}

if err != nil {
if closer, ok := r.(io.Closer); ok {
closer.Close()
}
return nil, beep.Format{}, errors.Wrap(err, "flac")
}
format = beep.Format{
SampleRate: beep.SampleRate(d.stream.Info.SampleRate),
NumChannels: int(d.stream.Info.NChannels),
Precision: int(d.stream.Info.BitsPerSample / 8),

return &d, d.d.format(), nil
}

func DecodeReader(r io.ReadCloser) (*Decoder, beep.Format, error) {
d := Decoder{r: r}
var err error
d.stream, err = flac.New(r)
if err != nil {
r.Close()
return nil, beep.Format{}, errors.Wrap(err, "flac")
}
return &d, format, nil

return &d, d.format(), nil
}

type decoder struct {
func DecodeReadSeeker(r io.ReadSeekCloser) (*SeekableDecoder, beep.Format, error) {
d := SeekableDecoder{d: Decoder{r: r, seekEnabled: true}}
var err error
d.d.stream, err = flac.NewSeek(r)
if err != nil {
r.Close()
return nil, beep.Format{}, errors.Wrap(err, "flac")
}

return &d, d.d.format(), nil
}

type Decoder struct {
r io.Reader
stream *flac.Stream
buf [][2]float64
Expand All @@ -53,7 +72,7 @@ type decoder struct {
seekEnabled bool
}

func (d *decoder) Stream(samples [][2]float64) (n int, ok bool) {
func (d *Decoder) Stream(samples [][2]float64) (n int, ok bool) {
if d.err != nil {
return 0, false
}
Expand Down Expand Up @@ -82,7 +101,7 @@ func (d *decoder) Stream(samples [][2]float64) (n int, ok bool) {
}

// refill decodes audio samples to fill the decode buffer.
func (d *decoder) refill() error {
func (d *Decoder) refill() error {
// Empty buffer.
d.buf = d.buf[:0]
// Parse audio frame.
Expand Down Expand Up @@ -139,30 +158,19 @@ func (d *decoder) refill() error {
return nil
}

func (d *decoder) Err() error {
func (d *Decoder) Err() error {
return d.err
}

func (d *decoder) Len() int {
func (d *Decoder) Len() int {
return int(d.stream.Info.NSamples)
}

func (d *decoder) Position() int {
func (d *Decoder) Position() int {
return d.pos
}

// p represents flac sample num perhaps?
func (d *decoder) Seek(p int) error {
if !d.seekEnabled {
return errors.New("flac.decoder.Seek: not enabled")
}

pos, err := d.stream.Seek(uint64(p))
d.pos = int(pos)
return err
}

func (d *decoder) Close() error {
func (d *Decoder) Close() error {
if closer, ok := d.r.(io.Closer); ok {
err := closer.Close()
if err != nil {
Expand All @@ -171,3 +179,46 @@ func (d *decoder) Close() error {
}
return nil
}

func (d *Decoder) format() beep.Format {
return beep.Format{
SampleRate: beep.SampleRate(d.stream.Info.SampleRate),
NumChannels: int(d.stream.Info.NChannels),
Precision: int(d.stream.Info.BitsPerSample / 8),
}
}

type SeekableDecoder struct {
d Decoder
}

func (sd *SeekableDecoder) Stream(samples [][2]float64) (n int, ok bool) {
return sd.d.Stream(samples)
}

func (sd *SeekableDecoder) Err() error {
return sd.d.Err()
}

func (sd *SeekableDecoder) Len() int {
return sd.d.Len()
}

func (sd *SeekableDecoder) Position() int {
return sd.d.Position()
}

func (sd *SeekableDecoder) Close() error {
return sd.d.Close()
}

// Seek seeks to the start of the frame containing the given absolute sample number.
func (sd *SeekableDecoder) Seek(p int) error {
if !sd.d.seekEnabled {
return errors.New("flac.decoder.Seek: not enabled")
}

pos, err := sd.d.stream.Seek(uint64(p))
sd.d.pos = int(pos)
return err
}
2 changes: 1 addition & 1 deletion flac/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestDecoder_ReturnBehaviour(t *testing.T) {
assert.NoError(t, err)
defer f.Close()

s, _, err := flac.Decode(f)
s, _, err := flac.DecodeReadSeeker(f)
assert.NoError(t, err)
assert.Equal(t, 22050, s.Len())

Expand Down
Loading