Skip to content

Commit

Permalink
🎨 Simplify file decoder func
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jul 23, 2023
1 parent bb005de commit 0b1ccbb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
16 changes: 6 additions & 10 deletions internal/stream/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,23 @@ import (
"github.com/gabe565/relax-sounds/internal/util"
)

type File struct {
fs.File
}

var ErrUnsupportedFileType = errors.New("unsupported file type")

func (f File) Decode() (beep.StreamSeekCloser, beep.Format, error) {
contentType, err := util.GetTypeFromFile(f.File)
func Decode(f fs.File) (beep.StreamSeekCloser, beep.Format, error) {
contentType, err := util.GetTypeFromFile(f)
if err != nil {
return nil, beep.Format{}, err
}

switch contentType {
case "application/ogg", "audio/ogg":
return vorbis.Decode(f.File)
return vorbis.Decode(f)
case "audio/mpeg":
return mp3.Decode(f.File)
return mp3.Decode(f)
case "audio/wave", "audio/x-wav":
return wav.Decode(f.File)
return wav.Decode(f)
case "audio/x-flac":
return flac.Decode(f.File)
return flac.Decode(f)
}
return nil, beep.Format{}, fmt.Errorf("%w: %s", ErrUnsupportedFileType, contentType)
}
4 changes: 1 addition & 3 deletions internal/stream/streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ func (s Streamer) Close() error {
}

func NewStreamer(rawFile fs.File, entry preset.Track) (streamer Streamer, err error) {
f := File{File: rawFile}

closer, format, err := f.Decode()
closer, format, err := Decode(rawFile)
if err != nil {
return streamer, err
}
Expand Down

0 comments on commit 0b1ccbb

Please sign in to comment.