audio: make Take propagate errors
This commit is contained in:
parent
81dbcb5bd1
commit
55c94b6cc5
|
@ -7,22 +7,36 @@ import (
|
||||||
|
|
||||||
// Take returns a Streamer which streams s for at most d duration.
|
// Take returns a Streamer which streams s for at most d duration.
|
||||||
//
|
//
|
||||||
// TODO: should Take propagate an error?
|
// The returned Streamer propagates s's errors throught Err.
|
||||||
func Take(d time.Duration, s Streamer) Streamer {
|
func Take(d time.Duration, s Streamer) Streamer {
|
||||||
currSample := 0
|
return &take{
|
||||||
numSamples := int(math.Ceil(d.Seconds() * SampleRate))
|
s: s,
|
||||||
return StreamerFunc(func(samples [][2]float64) (n int, ok bool) {
|
currSample: 0,
|
||||||
if currSample >= numSamples {
|
numSamples: int(math.Ceil(d.Seconds() * SampleRate)),
|
||||||
return 0, false
|
}
|
||||||
}
|
}
|
||||||
toStream := numSamples - currSample
|
|
||||||
if len(samples) < toStream {
|
type take struct {
|
||||||
toStream = len(samples)
|
s Streamer
|
||||||
}
|
currSample int
|
||||||
sn, sok := s.Stream(samples[:toStream])
|
numSamples int
|
||||||
currSample += sn
|
}
|
||||||
return sn, sok
|
|
||||||
})
|
func (t *take) Stream(samples [][2]float64) (n int, ok bool) {
|
||||||
|
if t.currSample >= t.numSamples {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
toStream := t.numSamples - t.currSample
|
||||||
|
if len(samples) < toStream {
|
||||||
|
toStream = len(samples)
|
||||||
|
}
|
||||||
|
sn, sok := t.s.Stream(samples[:toStream])
|
||||||
|
t.currSample += sn
|
||||||
|
return sn, sok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *take) Err() error {
|
||||||
|
return t.s.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seq takes zero or more Streamers and returns a Streamer which streams them one by one without pauses.
|
// Seq takes zero or more Streamers and returns a Streamer which streams them one by one without pauses.
|
||||||
|
|
Loading…
Reference in New Issue