Checkpoint

This commit is contained in:
maze authored and maze committed 2026-10-29 17:29:41 +01:00
1 parent 85a9ec684c
commit c5d6137fab
160 files changed
+76293 -387

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
package audio
import (
"context"
"time"
"github.com/sirupsen/logrus"
)
const streamDoubleBufferSize = 16
type StreamCancelFunc func()
// Stream reads from a source using a buffer size of n samples.
func Stream(source Source, n int) (<-chan *Buffer, StreamCancelFunc) {
channel := make(chan *Buffer, streamDoubleBufferSize)
ctx, cancel := context.WithCancel(context.Background())
go stream(ctx, channel, source, n)
return channel, StreamCancelFunc(cancel)
}
func stream(ctx context.Context, stream chan<- *Buffer, source Source, n int) {
defer close(stream)
buffers := make([]*Buffer, streamDoubleBufferSize)
for i := 0; i < streamDoubleBufferSize; i++ {
buffers[i] = NewBuffer(source.Format(), source.SampleRate(), n, source.Channels())
}
var bufferPos int
for {
select {
case <-ctx.Done():
return
default:
}
buffer := buffers[bufferPos]
bufferPos = (bufferPos + 1) % streamDoubleBufferSize
if _, err := buffer.DecodeFrom(source); err != nil {
logrus.WithError(err).WithField("source", source).Warn("decode audio failed")
time.Sleep(time.Second)
continue
}
select {
case <-ctx.Done():
return
case stream <- buffer:
default:
logrus.WithField("source", source).Trace("buffer overrun, host too slow?")
}
}
}