Skip to content

Commit

Permalink
Use custom SizedReadSeeker type
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Oct 12, 2023
1 parent ca63705 commit 91f0b79
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
9 changes: 9 additions & 0 deletions audio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package go_librespot

import "io"

type SizedReadSeeker interface {
io.ReadSeeker

Size() int64
}
3 changes: 2 additions & 1 deletion audio/replay_gain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package audio
import (
"encoding/binary"
log "github.com/sirupsen/logrus"
librespot "go-librespot"
"io"
"math"
)
Expand All @@ -14,7 +15,7 @@ type ReplayGain struct {
albumPeak float32
}

func ExtractReplayGainMetadata(r io.ReaderAt, limit int64) (io.ReadSeeker, *ReplayGain, error) {
func ExtractReplayGainMetadata(r io.ReaderAt, limit int64) (librespot.SizedReadSeeker, *ReplayGain, error) {
payload := make([]byte, 16)
if _, err := r.ReadAt(payload, 144); err != nil {
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (p *Player) NewStream(tid librespot.TrackId, bitrate int, trackPosition int
return nil, fmt.Errorf("failed reading ReplayGain metadata: %w", err)
}

stream, err := vorbis.New(audioStream, *trackMeta.Duration, rawStream.Size(), norm.GetTrackFactor(1))
stream, err := vorbis.New(audioStream, *trackMeta.Duration, norm.GetTrackFactor(1))
if err != nil {
return nil, fmt.Errorf("failed initializing ogg vorbis stream: %w", err)
}
Expand Down
11 changes: 4 additions & 7 deletions vorbis/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
log "github.com/sirupsen/logrus"
librespot "go-librespot"
"io"
"strings"
"sync"
Expand All @@ -23,9 +24,6 @@ type Decoder struct {
// gain is the default track gain.
gain float32

// size is the input length in bytes.
size int64

// duration is the input length in milliseconds.
duration int32

Expand Down Expand Up @@ -61,7 +59,7 @@ type Decoder struct {
// This structure is intended to be private.
block vorbis.Block

input io.ReadSeeker
input librespot.SizedReadSeeker
pcm [][][]float32
buf []float32
stopChan chan struct{}
Expand All @@ -79,10 +77,9 @@ type Info struct {
}

// New creates and initialises a new OggVorbis decoder for the provided bytestream.
func New(r io.ReadSeeker, duration int32, size int64, gain float32) (*Decoder, error) {
func New(r librespot.SizedReadSeeker, duration int32, gain float32) (*Decoder, error) {
d := &Decoder{
input: r,
size: size,
duration: duration,
gain: gain,
stopChan: make(chan struct{}),
Expand Down Expand Up @@ -345,7 +342,7 @@ func (d *Decoder) SetPositionMs(pos int64) (err error) {
d.Lock()
defer d.Unlock()

_, err = d.input.Seek(pos*d.size/int64(d.duration), io.SeekStart)
_, err = d.input.Seek(pos*d.input.Size()/int64(d.duration), io.SeekStart)
if err != nil {
return fmt.Errorf("failed seeking input: %w", err)
}
Expand Down

0 comments on commit 91f0b79

Please sign in to comment.