dndmusicbot/loop/loop.go

61 lines
886 B
Go
Raw Normal View History

2022-11-18 21:18:12 +00:00
package loop
import "github.com/gopxl/beep"
2022-11-18 21:18:12 +00:00
type loop struct {
s beep.StreamSeekCloser
remains int
}
func Loop(count int, s beep.StreamSeekCloser) beep.StreamSeekCloser {
return &loop{
s: s,
remains: count,
}
}
func (l *loop) Stream(samples [][2]float64) (n int, ok bool) {
if l.remains == 0 || l.s.Err() != nil {
return 0, false
}
for len(samples) > 0 {
sn, sok := l.s.Stream(samples)
if !sok {
if l.remains > 0 {
l.remains--
}
if l.remains == 0 {
break
}
err := l.s.Seek(0)
if err != nil {
return n, true
}
continue
}
samples = samples[sn:]
n += sn
}
return n, true
}
func (l *loop) Close() error {
return l.s.Close()
}
func (l *loop) Len() int {
return 0
}
func (l *loop) Position() int {
return 0
}
func (l *loop) Seek(p int) error {
return nil
}
func (l *loop) Err() error {
return l.s.Err()
}