Do resampling later, to keep the Seeker

This commit is contained in:
nemunaire 2019-08-19 17:21:06 +02:00
parent ce3dd841c4
commit 6f28271e47
1 changed files with 13 additions and 10 deletions

23
main.go
View File

@ -43,9 +43,7 @@ func xPrintIdle() (idle uint64) {
return
}
func loadFile(path string) (s beep.Streamer, err error) {
var format beep.Format
func loadFile(path string) (s beep.StreamSeekCloser, format beep.Format, err error) {
for _, decoder := range []func(io.ReadCloser) (beep.StreamSeekCloser, beep.Format, error){flac.Decode, mp3.Decode} {
var fd *os.File
@ -65,10 +63,6 @@ func loadFile(path string) (s beep.Streamer, err error) {
return
}
if format.SampleRate != SampleRate {
s = beep.Resample(3, format.SampleRate, SampleRate, s)
}
return
}
@ -83,21 +77,24 @@ func main() {
rand.Seed(time.Now().Unix())
playlist := []beep.Streamer{}
playlist := []beep.StreamSeekCloser{}
formats := []beep.Format{}
// Load playlist
for _, arg := range flag.Args() {
s, err := loadFile(arg)
s, f, err := loadFile(arg)
if err != nil {
log.Printf("Unable to load %s: %s", arg, err)
continue
}
playlist = append(playlist, s)
formats = append(formats, f)
}
// Shuffle the playlist
rand.Shuffle(len(playlist), func(i, j int) {
playlist[i], playlist[j] = playlist[j], playlist[i]
formats[i], formats[j] = formats[j], formats[i]
})
// Create infinite stream
@ -107,7 +104,13 @@ func main() {
if playedItem >= len(playlist) {
playedItem = 0
}
return playlist[playedItem]
// Resample if needed
if formats[playedItem].SampleRate != SampleRate {
return beep.Resample(3, formats[playedItem].SampleRate, SampleRate, playlist[playedItem])
} else {
return playlist[playedItem]
}
})
// Prepare sound player