Checkpoint
This commit is contained in:
138 files changed
+1898
-57607
No files matched your search
+163
@@ -0,0 +1,163 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.maze.io/harmon/config"
|
||||
"go.maze.io/harmon/hardware"
|
||||
"periph.io/x/conn/v3/physic"
|
||||
)
|
||||
|
||||
type Stream interface {
|
||||
hardware.Driver
|
||||
|
||||
// Channels is the number of channels.
|
||||
Channels() int
|
||||
|
||||
// SampleRate is the audio sampling rate.
|
||||
SampleRate() physic.Frequency
|
||||
|
||||
// Samples is a channel of interleaved audio samples.
|
||||
Samples() <-chan []int16
|
||||
}
|
||||
|
||||
type Reader struct {
|
||||
io.Reader
|
||||
|
||||
// Format is the audio sample format.
|
||||
Format Format
|
||||
|
||||
// Ch is the number of channels.
|
||||
Ch int
|
||||
|
||||
// Rate is the sampling rate.
|
||||
Rate physic.Frequency
|
||||
}
|
||||
|
||||
func NewReader(r io.Reader, f Format, ch int, rate physic.Frequency) Stream {
|
||||
return &Reader{
|
||||
Reader: r,
|
||||
Format: f,
|
||||
Ch: ch,
|
||||
Rate: rate,
|
||||
}
|
||||
}
|
||||
|
||||
func (r Reader) Type() string {
|
||||
return "file"
|
||||
}
|
||||
|
||||
func (r Reader) Halt() error {
|
||||
if c, ok := r.Reader.(io.Closer); ok {
|
||||
return c.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r Reader) Channels() int {
|
||||
return r.Ch
|
||||
}
|
||||
|
||||
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(send chan []int16) {
|
||||
const numBuf = 16
|
||||
|
||||
var (
|
||||
b = NewBuffer(int(r.Rate/physic.Hertz), r.Ch, r.Format)
|
||||
order = r.Format.Order()
|
||||
buf [numBuf][]int16
|
||||
n int
|
||||
)
|
||||
for i := range buf {
|
||||
buf[i] = make([]int16, int(r.Rate/physic.Hertz)*r.Ch)
|
||||
}
|
||||
|
||||
for {
|
||||
if _, err := r.Read(b); err != nil {
|
||||
close(send)
|
||||
return
|
||||
}
|
||||
|
||||
samples, _ := b.Decode(buf[n], order)
|
||||
select {
|
||||
case send <- buf[n][:samples]:
|
||||
default:
|
||||
}
|
||||
|
||||
n = (n + 1) % numBuf
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reader) Samples() <-chan []int16 {
|
||||
samples := make(chan []int16)
|
||||
go r.read(samples)
|
||||
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 openFile(config config.Audio) (Stream, error) {
|
||||
if config.Rate <= 0 {
|
||||
return nil, errors.New("audio rate not configured")
|
||||
}
|
||||
if config.Channels <= 0 {
|
||||
return nil, errors.New("audio chanels not configured")
|
||||
}
|
||||
if config.Bits == 0 {
|
||||
return nil, errors.New("audio bits not configured")
|
||||
}
|
||||
|
||||
var (
|
||||
rate = physic.Frequency(config.Rate) * physic.Hertz
|
||||
f Format
|
||||
)
|
||||
switch config.Bits {
|
||||
case 8:
|
||||
f = Int8Format
|
||||
case 16:
|
||||
f = Int16LEFormat
|
||||
case 32:
|
||||
f = Int32LEFormat
|
||||
default:
|
||||
return nil, fmt.Errorf("audio bits should be 8, 16 or 32, got %d", config.Bits)
|
||||
}
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"type": config.Type,
|
||||
"bits": config.Bits,
|
||||
"channels": config.Channels,
|
||||
"rate": rate,
|
||||
}).Debug("open audio driver")
|
||||
return Open(config.Path, f, config.Channels, rate)
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register("file", openFile)
|
||||
Register("fifo", openFile)
|
||||
}
|
||||
Reference in new issue
Block a user