Checkpoint
This commit is contained in:
54 files changed
+6855
-526
No files matched your search
+147
-32
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.maze.io/harmon/config"
|
||||
@@ -12,6 +13,8 @@ import (
|
||||
"periph.io/x/conn/v3/physic"
|
||||
)
|
||||
|
||||
const streamBuffers = 16
|
||||
|
||||
type Stream interface {
|
||||
hardware.Driver
|
||||
|
||||
@@ -22,9 +25,115 @@ type Stream interface {
|
||||
SampleRate() physic.Frequency
|
||||
|
||||
// Samples is a channel of interleaved audio samples.
|
||||
Samples() <-chan []int16
|
||||
Samples(size int) <-chan Buffer
|
||||
}
|
||||
|
||||
type Processor interface {
|
||||
Process(Buffer)
|
||||
}
|
||||
|
||||
type StreamReader struct {
|
||||
Opener func() (io.Reader, error)
|
||||
|
||||
// Format is the audio sample format.
|
||||
Format Format
|
||||
|
||||
// Ch is the number of channels.
|
||||
Ch int
|
||||
|
||||
// Rate is the sampling rate.
|
||||
Rate physic.Frequency
|
||||
|
||||
// quit signal channel.
|
||||
quit chan struct{}
|
||||
}
|
||||
|
||||
func NewStreamReader(open func() (io.Reader, error), f Format, ch int, rate physic.Frequency) Stream {
|
||||
return &StreamReader{
|
||||
Opener: open,
|
||||
Format: f,
|
||||
Ch: ch,
|
||||
Rate: rate,
|
||||
quit: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *StreamReader) Halt() error {
|
||||
if r.quit != nil {
|
||||
close(r.quit)
|
||||
r.quit = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *StreamReader) Type() string {
|
||||
return "stream"
|
||||
}
|
||||
|
||||
func (r *StreamReader) Channels() int {
|
||||
return r.Ch
|
||||
}
|
||||
|
||||
func (r *StreamReader) SampleRate() physic.Frequency {
|
||||
return r.Rate
|
||||
}
|
||||
|
||||
func (r *StreamReader) Samples(size int) <-chan Buffer {
|
||||
samples := make(chan Buffer, streamBuffers)
|
||||
go r.stream(samples, size)
|
||||
return samples
|
||||
}
|
||||
|
||||
func (r *StreamReader) stream(samples chan<- Buffer, size int) {
|
||||
defer close(samples)
|
||||
|
||||
var (
|
||||
log = logrus.WithField("audio", "stream")
|
||||
ring [streamBuffers]Buffer
|
||||
curr int
|
||||
)
|
||||
for i := 0; i < streamBuffers; i++ {
|
||||
ring[i] = r.Format.NewBuffer(size*r.Ch, r.Ch)
|
||||
}
|
||||
|
||||
log.Debug("starting with %d buffers", streamBuffers)
|
||||
for {
|
||||
log.Debug("opening stream")
|
||||
f, err := r.Opener()
|
||||
if err != nil {
|
||||
log.WithError(err).Warn("opening stream failed, retrying...")
|
||||
time.Sleep(time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
for {
|
||||
n, err := ring[curr].ReadFrom(f)
|
||||
if err != nil {
|
||||
log.WithError(err).Warn("stream closed, retrying...")
|
||||
break
|
||||
}
|
||||
|
||||
log.WithField("samples", n).Trace("stream samples")
|
||||
select {
|
||||
case <-r.quit:
|
||||
if c, ok := f.(io.Closer); ok {
|
||||
if err = c.Close(); err != nil {
|
||||
log.WithError(err).Warn("closing stream failed")
|
||||
}
|
||||
}
|
||||
return
|
||||
case samples <- ring[curr]:
|
||||
default:
|
||||
log.Warn("dropped samples, buffer overrun")
|
||||
}
|
||||
|
||||
curr = (curr + 1) % streamBuffers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
type Reader struct {
|
||||
io.Reader
|
||||
|
||||
@@ -66,60 +175,66 @@ func (r Reader) SampleRate() physic.Frequency {
|
||||
return r.Rate
|
||||
}
|
||||
|
||||
func (r *Reader) Read(b *Buffer) (n int, err error) {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
|
||||
l := len(b.Buf)
|
||||
l -= l % (b.Ch * b.Format.BytesPerSample()) // align to samples
|
||||
n, err = r.Reader.Read(b.Buf[:l]) // read
|
||||
n /= (b.Ch * b.Format.BytesPerSample()) // return sample count
|
||||
return
|
||||
func (r *Reader) Read(b Buffer) (n int, err error) {
|
||||
return b.ReadFrom(r.Reader)
|
||||
}
|
||||
|
||||
func (r *Reader) read(send chan []int16) {
|
||||
const numBuf = 16
|
||||
const numBuf = 16
|
||||
|
||||
func (r *Reader) stream(send chan Buffer, rate physic.Frequency) {
|
||||
var (
|
||||
b = NewBuffer(int(r.Rate/physic.Hertz), r.Ch, r.Format)
|
||||
order = r.Format.Order()
|
||||
buf [numBuf][]int16
|
||||
n int
|
||||
b = r.Format.NewBuffer(int(rate/physic.Hertz), r.Ch) //NewBuffer(int(rate/physic.Hertz), r.Ch, r.Format)
|
||||
buf [numBuf]Buffer
|
||||
n int
|
||||
)
|
||||
for i := range buf {
|
||||
buf[i] = make([]int16, int(r.Rate/physic.Hertz)*r.Ch)
|
||||
}
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"buffers": numBuf,
|
||||
"rate_in": r.Rate,
|
||||
"rate_out": rate,
|
||||
}).Info("audio stream starting")
|
||||
for {
|
||||
if _, err := r.Read(b); err != nil {
|
||||
logrus.WithError(err).Warn("audio stream interrupted")
|
||||
close(send)
|
||||
return
|
||||
}
|
||||
|
||||
samples, _ := b.Decode(buf[n], order)
|
||||
//samples, err := b.Decode(buf[n], order)
|
||||
samples, err := b.ReadFrom(r.Reader)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Warn("samples decode failed")
|
||||
continue
|
||||
}
|
||||
select {
|
||||
case send <- buf[n][:samples]:
|
||||
case send <- buf[n]:
|
||||
default:
|
||||
logrus.WithField("samples", samples).Warn("samples dropped, buffer overrun")
|
||||
}
|
||||
|
||||
n = (n + 1) % numBuf
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reader) Samples() <-chan []int16 {
|
||||
samples := make(chan []int16)
|
||||
go r.read(samples)
|
||||
func (r *Reader) Samples(rate physic.Frequency) <-chan Buffer {
|
||||
samples := make(chan Buffer, numBuf)
|
||||
go r.stream(samples, rate)
|
||||
return samples
|
||||
}
|
||||
*/
|
||||
|
||||
// Open a stream backed by a file (or a FIFO).
|
||||
func Open(name string, f Format, ch int, rate physic.Frequency) (Stream, error) {
|
||||
r, err := os.OpenFile(name, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewReader(r, f, ch, rate), nil
|
||||
func Open(name string, f Format, ch int, rate physic.Frequency) Stream {
|
||||
/*
|
||||
r, err := os.OpenFile(name, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewReader(r, f, ch, rate), nil
|
||||
*/
|
||||
return NewStreamReader(func() (io.Reader, error) {
|
||||
return os.OpenFile(name, os.O_RDONLY, 0)
|
||||
}, f, ch, rate)
|
||||
}
|
||||
|
||||
func openFile(config config.Audio) (Stream, error) {
|
||||
@@ -154,7 +269,7 @@ func openFile(config config.Audio) (Stream, error) {
|
||||
"channels": config.Channels,
|
||||
"rate": rate,
|
||||
}).Debug("open audio driver")
|
||||
return Open(config.Path, f, config.Channels, rate)
|
||||
return Open(config.Path, f, config.Channels, rate), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
Reference in new issue
Block a user