Checkpoint
This commit is contained in:
138 files changed
+1898
-57607
No files matched your search
@@ -0,0 +1,56 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Buffer struct {
|
||||
Buf []byte
|
||||
Ch int
|
||||
Format Format
|
||||
}
|
||||
|
||||
// NewBuffer returns a buffer of n samples and ch channels.
|
||||
func NewBuffer(n, ch int, f Format) *Buffer {
|
||||
if ch < 1 {
|
||||
ch = 1
|
||||
}
|
||||
return &Buffer{
|
||||
Buf: make([]byte, n*ch*f.BytesPerSample()),
|
||||
Ch: ch,
|
||||
Format: f,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Buffer) Decode(data any, order binary.ByteOrder) (n int, err error) {
|
||||
switch data := data.(type) {
|
||||
case []int8:
|
||||
var x byte
|
||||
for n, x = range b.Buf {
|
||||
data[n] = int8(x)
|
||||
}
|
||||
case []uint8:
|
||||
n = copy(data, b.Buf)
|
||||
case []int16:
|
||||
for n = range data {
|
||||
data[n] = int16(order.Uint16(b.Buf[n<<1:]))
|
||||
}
|
||||
case []int32:
|
||||
for n = range data {
|
||||
data[n] = int32(order.Uint32(b.Buf[n<<2:]))
|
||||
}
|
||||
case []float32:
|
||||
for n = range data {
|
||||
data[n] = math.Float32frombits(order.Uint32(b.Buf[n<<2:]))
|
||||
}
|
||||
case []float64:
|
||||
for n = range data {
|
||||
data[n] = math.Float64frombits(order.Uint64(b.Buf[n<<3:]))
|
||||
}
|
||||
default:
|
||||
return 0, fmt.Errorf("can't decode to %T", data)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package audio
|
||||
|
||||
// Complex128 represents a slice of 64-bit floating-point linear PCM encoded
|
||||
// audio samples, in the range of -1 to +1.
|
||||
type Complex128 []complex128
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Complex128) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Complex128) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Complex128) At(i int) float64 {
|
||||
return real(p[i])
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Complex128) Set(i int, s float64) {
|
||||
p[i] = complex(s, 0)
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Complex128) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Complex128) Make(length, capacity int) Slice {
|
||||
return make(Float64, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Complex128) CopyTo(dst Slice) int {
|
||||
d, ok := dst.(Complex128)
|
||||
if ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package audio
|
||||
|
||||
// Float32 represents a slice of 32-bit floating-point linear PCM encoded
|
||||
// audio samples, in the range of -1 to +1.
|
||||
type Float32 []float32
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Float32) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Float32) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Float32) At(i int) float64 {
|
||||
return float64(p[i])
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Float32) Set(i int, s float64) {
|
||||
p[i] = float32(s)
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Float32) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Float32) Make(length, capacity int) Slice {
|
||||
return make(Float32, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Float32) CopyTo(dst Slice) int {
|
||||
d, ok := dst.(Float32)
|
||||
if ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package audio
|
||||
|
||||
// Float64 represents a slice of 64-bit floating-point linear PCM encoded
|
||||
// audio samples, in the range of -1 to +1.
|
||||
type Float64 []float64
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Float64) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Float64) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Float64) At(i int) float64 {
|
||||
return p[i]
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Float64) Set(i int, s float64) {
|
||||
p[i] = s
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Float64) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Float64) Make(length, capacity int) Slice {
|
||||
return make(Float64, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Float64) CopyTo(dst Slice) int {
|
||||
d, ok := dst.(Float64)
|
||||
if ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
|
||||
// ToFloat64 converts src to a Float64 slice.
|
||||
func ToFloat64(src Slice) Float64 {
|
||||
if s, ok := src.(Float64); ok {
|
||||
return s
|
||||
}
|
||||
|
||||
dst := make(Float64, src.Len())
|
||||
for i, l := 0, src.Len(); i < l; i++ {
|
||||
dst[i] = src.At(i)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// Int16 represents a signed 16-bit linear PCM encoded audio sample.
|
||||
type Int16 []int16
|
||||
|
||||
// Int16ToFloat64 converts a Int16 encoded audio sample to Float64.
|
||||
func Int16ToFloat64(s int16) float64 {
|
||||
return float64(s) / float64(math.MaxInt16)
|
||||
}
|
||||
|
||||
// Float64ToInt16 converts a Float64 encoded audio sample to Int16.
|
||||
func Float64ToInt16(s float64) int16 {
|
||||
return int16(math.Floor((s * float64(math.MaxInt16)) + 0.5))
|
||||
}
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Int16) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Int16) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Int16) At(i int) float64 {
|
||||
return Int16ToFloat64(p[i])
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Int16) Set(i int, s float64) {
|
||||
p[i] = Float64ToInt16(s)
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Int16) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Int16) Make(length, capacity int) Slice {
|
||||
return make(Int16, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Int16) CopyTo(dst Slice) int {
|
||||
if d, ok := dst.(Int16); ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
if d, ok := dst.(Int8); ok {
|
||||
for i, v := range p {
|
||||
d[i] = int8(v >> 8)
|
||||
}
|
||||
return p.Len()
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package audio
|
||||
|
||||
import "math"
|
||||
|
||||
// Int8 represents a signed 8-bit linear PCM encoded audio sample.
|
||||
type Int8 []int8
|
||||
|
||||
// Int8ToFloat64 converts a Int8 encoded audio sample to Float64.
|
||||
func Int8ToFloat64(s int8) float64 {
|
||||
return float64(s) / float64(math.MaxInt8)
|
||||
}
|
||||
|
||||
// Float64ToInt8 converts a Float64 encoded audio sample to Int8.
|
||||
func Float64ToInt8(s float64) int8 {
|
||||
return int8(math.Floor((s * float64(math.MaxInt8)) + 0.5))
|
||||
}
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Int8) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Int8) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Int8) At(i int) float64 {
|
||||
return Int8ToFloat64(p[i])
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Int8) Set(i int, s float64) {
|
||||
p[i] = Float64ToInt8(s)
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Int8) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Int8) Make(length, capacity int) Slice {
|
||||
return make(Int8, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Int8) CopyTo(dst Slice) int {
|
||||
if d, ok := dst.(Int8); ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
if d, ok := dst.(Int16); ok {
|
||||
for i, v := range p {
|
||||
d[i] = int16(v) * 255
|
||||
}
|
||||
return p.Len()
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package audio
|
||||
|
||||
func Deinterleave[T any](dst [][]T, src []T) {
|
||||
if len(src) == 0 {
|
||||
return
|
||||
} else if len(dst) == 1 {
|
||||
copy(dst[0], src)
|
||||
return
|
||||
}
|
||||
|
||||
c := len(dst)
|
||||
for i, s := range src {
|
||||
dst[i%c][i/c] = s
|
||||
}
|
||||
}
|
||||
|
||||
func Interleave[T any](dst []T, src [][]T) {
|
||||
if len(src) == 0 {
|
||||
return
|
||||
} else if len(src) == 1 {
|
||||
copy(dst, src[0])
|
||||
return
|
||||
}
|
||||
|
||||
c := len(src)
|
||||
for i := range dst {
|
||||
dst[i] = src[i%c][i/c]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.maze.io/harmon/config"
|
||||
)
|
||||
|
||||
// NotFound signals the player type can not be found.
|
||||
type NotFound struct {
|
||||
Type string
|
||||
}
|
||||
|
||||
func (err NotFound) Error() string {
|
||||
return fmt.Sprintf("audio driver %q can not be found", err.Type)
|
||||
}
|
||||
|
||||
type Opener func(config config.Audio) (Stream, error)
|
||||
|
||||
var (
|
||||
protocols sync.Map
|
||||
)
|
||||
|
||||
func Register(name string, opener Opener) {
|
||||
logrus.WithField("type", name).Debug("register audio driver")
|
||||
if _, loaded := protocols.LoadOrStore(name, opener); loaded {
|
||||
panic(fmt.Sprintf("audio type %q already registered", name))
|
||||
}
|
||||
}
|
||||
|
||||
func New(config config.Audio) (Stream, error) {
|
||||
logrus.WithField("type", config.Type).Debug("load audio driver")
|
||||
|
||||
opener, ok := protocols.Load(config.Type)
|
||||
if ok {
|
||||
return opener.(Opener)(config)
|
||||
}
|
||||
return nil, NotFound{config.Type}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var ErrFormat = errors.New("invalid audio format")
|
||||
|
||||
type Format uint
|
||||
|
||||
// Audio format.
|
||||
const (
|
||||
ByteFormat Format = iota
|
||||
Int8Format
|
||||
Int16LEFormat
|
||||
Int16BEFormat
|
||||
Int32LEFormat
|
||||
Int32BEFormat
|
||||
invalidFormat
|
||||
)
|
||||
|
||||
func (f Format) Order() binary.ByteOrder {
|
||||
switch f {
|
||||
case Int16LEFormat, Int32LEFormat:
|
||||
return binary.LittleEndian
|
||||
case Int16BEFormat, Int32BEFormat:
|
||||
return binary.BigEndian
|
||||
default:
|
||||
return binary.NativeEndian
|
||||
}
|
||||
}
|
||||
|
||||
func (f Format) BytesPerSample() int {
|
||||
switch f {
|
||||
case ByteFormat, Int8Format:
|
||||
return 8
|
||||
case Int16LEFormat, Int16BEFormat:
|
||||
return 16
|
||||
case Int32LEFormat, Int32BEFormat:
|
||||
return 32
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
-201
@@ -1,201 +0,0 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"git.maze.io/go/bragi/internal/resample"
|
||||
"periph.io/x/conn/v3/physic"
|
||||
)
|
||||
|
||||
// Slice is a generic audio slice, it can conceptually be thought of as a
|
||||
// slice of some audio encoding type.
|
||||
//
|
||||
// Conversion between two encoded audio slices is as simple as:
|
||||
//
|
||||
// dst, ok := src.(MuLaw)
|
||||
// if !ok {
|
||||
// // Create a new slice of the target encoding and copy the samples over
|
||||
// // because src is not MuLaw encoded.
|
||||
// dst = make(MuLaw, src.Len())
|
||||
// src.CopyTo(dst)
|
||||
// }
|
||||
type Slice interface {
|
||||
// Len returns the number of elements in the slice.
|
||||
//
|
||||
// Equivalent slice syntax:
|
||||
//
|
||||
// len(b)
|
||||
Len() int
|
||||
|
||||
// Cap returns the number of elements in the slice.
|
||||
//
|
||||
// Equivalent slice syntax:
|
||||
//
|
||||
// cap(b)
|
||||
Cap() int
|
||||
|
||||
// Set sets the specified index in the slice to the specified Float64
|
||||
// encoded audio sample, s.
|
||||
//
|
||||
// If the slice's audio samples are not stored in Float64 encoding, then
|
||||
// the sample should be converted to the slice's internal format and then
|
||||
// stored.
|
||||
//
|
||||
// Just like slices, slice indices must be non-negative; and no greater
|
||||
// than (Len() - 1), or else a panic may occur.
|
||||
//
|
||||
// Equivalent slice syntax:
|
||||
//
|
||||
// b[index] = s
|
||||
// -> b.Set(index, s)
|
||||
//
|
||||
Set(index int, s float64)
|
||||
|
||||
// At returns the Float64 encoded audio sample at the specified index in
|
||||
// the slice.
|
||||
//
|
||||
// If the slice's audio samples are not stored in Float64 encoding, then
|
||||
// the sample should be converted to Float64 encoding, and subsequently
|
||||
// returned.
|
||||
//
|
||||
// Just like slices, slice indices must be non-negative; and no greater
|
||||
// than (Len() - 1), or else a panic may occur.
|
||||
//
|
||||
// Equivalent slice syntax:
|
||||
//
|
||||
// b[index]
|
||||
// -> b.At(index)
|
||||
//
|
||||
At(index int) float64
|
||||
|
||||
// Slice returns a new slice of the slice, using the low and high
|
||||
// parameters.
|
||||
//
|
||||
// Equivalent slice syntax:
|
||||
//
|
||||
// b[low:high]
|
||||
// -> b.Slice(low, high)
|
||||
//
|
||||
// b[2:]
|
||||
// -> b.Slice(2, a.Len())
|
||||
//
|
||||
// b[:3]
|
||||
// -> b.Slice(0, 3)
|
||||
//
|
||||
// b[:]
|
||||
// -> b.Slice(0, a.Len())
|
||||
//
|
||||
Slice(low, high int) Slice
|
||||
|
||||
// Make creates and returns a new slice of this slices type. This allows
|
||||
// allocating a new slice of exactly the same type for lossless copying of
|
||||
// data without knowing about the underlying type.
|
||||
//
|
||||
// It is exactly the same syntax as the make builtin:
|
||||
//
|
||||
// make(MuLaw, len, cap)
|
||||
//
|
||||
// Where cap cannot be less than len.
|
||||
Make(length, capacity int) Slice
|
||||
|
||||
// CopyTo operates exactly like the copy builtin, but this slice is always
|
||||
// the source operand. Equivalent slice syntax:
|
||||
//
|
||||
// copy(dst, src)
|
||||
// -> src.CopyTo(dst)
|
||||
//
|
||||
CopyTo(dst Slice) int
|
||||
}
|
||||
|
||||
// sliceCopy copies copies audio samples from the source slice to the
|
||||
// destination slice. Returns the number of elements copied, which is the
|
||||
// minimum of the dst.Len() and src.Len() values.
|
||||
func sliceCopy(dst, src Slice) int {
|
||||
var i int
|
||||
for i = 0; i < src.Len() && i < dst.Len(); i++ {
|
||||
dst.Set(i, src.At(i))
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// Min returns the minimum value in the Slice.
|
||||
func Min(s Slice) float64 {
|
||||
if s.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
min := math.MaxFloat64
|
||||
for i, l := 0, s.Len(); i < l; i++ {
|
||||
if v := s.At(i); v < min {
|
||||
min = v
|
||||
}
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
||||
// Max returns the minimum value in the Slice.
|
||||
func Max(s Slice) float64 {
|
||||
if s.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
max := -math.MaxFloat64
|
||||
for i, l := 0, s.Len(); i < l; i++ {
|
||||
if v := s.At(i); v > max {
|
||||
max = v
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
// Limits returns the minimum and maximum value in the Slice.
|
||||
func Limits(s Slice) (min, max float64) {
|
||||
if s.Len() == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
min = +math.MaxFloat64
|
||||
max = -math.MaxFloat64
|
||||
for i, l := 0, s.Len(); i < l; i++ {
|
||||
v := s.At(i)
|
||||
if v < min {
|
||||
min = v
|
||||
}
|
||||
if v > max {
|
||||
max = v
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Normalize the samples in the buffer.
|
||||
func Normalize(s Slice) {
|
||||
min, max := Limits(s)
|
||||
for i, l := 0, s.Len(); i < l; i++ {
|
||||
val := s.At(i)
|
||||
s.Set(i, 2*((val-min)/(max-min))-1)
|
||||
}
|
||||
}
|
||||
|
||||
// Saturate the samples in the buffer to fit in [-1, 1].
|
||||
func Saturate(s Slice) {
|
||||
for i, l := 0, s.Len(); i < l; i++ {
|
||||
v := s.At(i)
|
||||
if v > 1 {
|
||||
s.Set(i, 1)
|
||||
} else if v < -1 {
|
||||
s.Set(i, -1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Resample an audio slice.
|
||||
func Resample(dst, src Slice, dstRate, srcRate physic.Frequency, channels, quality int) (read, written int) {
|
||||
d := ToFloat64(dst)
|
||||
s := ToFloat64(src)
|
||||
r := resample.NewWithSkipZeros(channels, int(srcRate/physic.Hertz), int(dstRate/physic.Hertz), quality)
|
||||
read, written = r.ProcessFloat64(0, s, d)
|
||||
d.CopyTo(dst)
|
||||
return
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package audio
|
||||
|
||||
// InterleavedStereoToMono converts interleaved stereo to mono samples.
|
||||
//
|
||||
// The size of src should be twice the size of dst. No bounds checking
|
||||
// is performed while writing to dst.
|
||||
func InterleavedStereoToMono(dst, src Slice) {
|
||||
var (
|
||||
i, j int
|
||||
l = src.Len() / 2
|
||||
)
|
||||
for ; i < l; i, j = i+1, j+2 {
|
||||
dst.Set(i, (src.At(j)+src.At(j+1))/2)
|
||||
}
|
||||
}
|
||||
+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)
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package audio
|
||||
|
||||
import "math"
|
||||
|
||||
// Uint8 represents a unsigned 8-bit linear PCM encoded audio sample.
|
||||
type Uint8 []uint8
|
||||
|
||||
// Uint8ToFloat64 converts a Uint8 encoded audio sample to Float64.
|
||||
func Uint8ToFloat64(s uint8) float64 {
|
||||
// In 0 to 1 range
|
||||
f := float64(s) / float64(math.MaxUint8)
|
||||
|
||||
// Switch to -1 to +1 range
|
||||
f *= 2
|
||||
f--
|
||||
return f
|
||||
}
|
||||
|
||||
// Float64ToUint8 converts a Float64 encoded audio sample to Uint8.
|
||||
func Float64ToUint8(s float64) uint8 {
|
||||
// In -1 to +1 range, switch to 0 to 1
|
||||
s++
|
||||
s /= 2
|
||||
return uint8(math.Floor((s * float64(math.MaxUint8)) + 0.5))
|
||||
}
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Uint8) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Uint8) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Uint8) At(i int) float64 {
|
||||
return Uint8ToFloat64(p[i])
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Uint8) Set(i int, s float64) {
|
||||
p[i] = Float64ToUint8(s)
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Uint8) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Uint8) Make(length, capacity int) Slice {
|
||||
return make(Uint8, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Uint8) CopyTo(dst Slice) int {
|
||||
d, ok := dst.(Uint8)
|
||||
if ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
Reference in new issue
Block a user