Checkpoint
This commit is contained in:
160 files changed
+76293
-387
No files matched your search
@@ -0,0 +1,7 @@
|
||||
package sound
|
||||
|
||||
import "github.com/BeatGlow/audio"
|
||||
|
||||
type Processor interface {
|
||||
ProcessBuffer(audio.Buffer[float64])
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package sound
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/BeatGlow/beatglow/config"
|
||||
)
|
||||
|
||||
type Opener = func(config.Audio) (Source, error)
|
||||
|
||||
var drivers = make(map[string]Opener)
|
||||
|
||||
func Register(name string, opener Opener) {
|
||||
if _, dupe := drivers[name]; dupe {
|
||||
panic(fmt.Sprintf("sound source %q already registered", name))
|
||||
}
|
||||
drivers[name] = opener
|
||||
}
|
||||
|
||||
func New(config config.Audio) (Source, error) {
|
||||
opener, ok := drivers[config.Type]
|
||||
if ok {
|
||||
return opener(config)
|
||||
}
|
||||
return nil, fmt.Errorf("sound: no %q driver was found", config.Type)
|
||||
}
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
package sound
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/BeatGlow/audio"
|
||||
"github.com/BeatGlow/beatglow/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Register("fifo", newReader)
|
||||
Register("file", newReader)
|
||||
}
|
||||
|
||||
type Source interface {
|
||||
String() string
|
||||
|
||||
// ReadBuffer reads samples.
|
||||
ReadBuffer(audio.Buffer[float64]) (int, error)
|
||||
|
||||
// Channels is the number of channels.
|
||||
Channels() int
|
||||
|
||||
// BitDepth is the number of bits per sample.
|
||||
BitDepth() int
|
||||
|
||||
// SampleRate is the sample rate in samples per second.
|
||||
SampleRate() int
|
||||
}
|
||||
|
||||
type Reader struct {
|
||||
io.ReadCloser
|
||||
|
||||
// Name of the reader (path).
|
||||
Name string
|
||||
|
||||
// Ch is the number of channels.
|
||||
Ch int
|
||||
|
||||
// Bits is the bit depth.
|
||||
Bits int
|
||||
|
||||
// Rate is the sample rate.
|
||||
Rate int
|
||||
|
||||
// Order is the byte order.
|
||||
Order binary.ByteOrder
|
||||
|
||||
// Opener is a function that can be called if the reader is closed.
|
||||
Opener func() (io.ReadCloser, error)
|
||||
|
||||
// buffers are allocated on the fly
|
||||
buffer []byte
|
||||
bufferFloat audio.Samples[float64]
|
||||
bufferInt8 audio.Samples[int8]
|
||||
bufferInt16 audio.Samples[int16]
|
||||
}
|
||||
|
||||
func newReader(config config.Audio) (Source, error) {
|
||||
var (
|
||||
ch = config.Channels
|
||||
bits = config.Bits
|
||||
rate = config.Rate
|
||||
order binary.ByteOrder = binary.LittleEndian
|
||||
)
|
||||
if ch == 0 {
|
||||
ch = 2
|
||||
}
|
||||
if bits == 0 {
|
||||
bits = 16
|
||||
}
|
||||
if rate == 0 {
|
||||
rate = 44100
|
||||
}
|
||||
if config.BigEndian {
|
||||
order = binary.BigEndian
|
||||
}
|
||||
return &Reader{
|
||||
Name: config.Path,
|
||||
Ch: ch,
|
||||
Bits: bits,
|
||||
Rate: rate,
|
||||
Order: order,
|
||||
Opener: func() (io.ReadCloser, error) { return os.Open(config.Path) },
|
||||
buffer: make([]byte, 1024*ch*(bits>>4)),
|
||||
bufferFloat: make(audio.Samples[float64], 1024*ch),
|
||||
bufferInt8: make(audio.Samples[int8], 1024*ch*(bits>>4)),
|
||||
bufferInt16: make(audio.Samples[int16], 1024*ch*(bits>>4)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s Reader) String() string {
|
||||
return "reader " + s.Name
|
||||
}
|
||||
|
||||
func (s Reader) Channels() int {
|
||||
return s.Ch
|
||||
}
|
||||
|
||||
func (s Reader) BitDepth() int {
|
||||
return s.Bits
|
||||
}
|
||||
|
||||
func (s Reader) SampleRate() int {
|
||||
return s.Rate
|
||||
}
|
||||
|
||||
func (s *Reader) ReadBuffer(buffer audio.Buffer[float64]) (int, error) {
|
||||
// Ensure we have an open reader.
|
||||
if s.ReadCloser == nil {
|
||||
var err error
|
||||
if s.ReadCloser, err = s.Opener(); err != nil {
|
||||
s.ReadCloser = nil
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
switch s.Bits {
|
||||
case 8:
|
||||
return s.readBufferInt8(buffer)
|
||||
case 16:
|
||||
return s.readBufferInt16(buffer)
|
||||
default:
|
||||
return 0, fmt.Errorf("sound: unsupported bit depth %d for Reader", s.Bits)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Reader) readBufferInt8(buffer audio.Buffer[float64]) (int, error) {
|
||||
var (
|
||||
samples = buffer.Samples()
|
||||
channels = buffer.Channels()
|
||||
length = samples * channels
|
||||
)
|
||||
|
||||
// Dynamically resize, if needed.
|
||||
if l := len(s.bufferFloat); l != length {
|
||||
s.buffer = make([]byte, length)
|
||||
s.bufferFloat = make(audio.Samples[float64], length)
|
||||
s.bufferInt8 = make(audio.Samples[int8], length)
|
||||
}
|
||||
|
||||
// Read bytes
|
||||
if n, err := io.ReadFull(s.ReadCloser, s.buffer); err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
||||
// Convert
|
||||
s.bufferInt8.Decode(s.buffer, s.Order) // bytes -> int8
|
||||
s.bufferInt8.ToFloat(s.bufferFloat) // int8 -> float64
|
||||
|
||||
if channels == 1 {
|
||||
// Straight conversion into the target buffer.
|
||||
return copy(buffer[0], s.bufferFloat), nil
|
||||
}
|
||||
|
||||
// Deinterleave.
|
||||
audio.Deinterleave(buffer, s.bufferFloat, channels)
|
||||
|
||||
return samples, nil
|
||||
}
|
||||
|
||||
func (s *Reader) readBufferInt16(buffer audio.Buffer[float64]) (int, error) {
|
||||
var (
|
||||
samples = buffer.Samples()
|
||||
channels = buffer.Channels()
|
||||
length = samples * channels
|
||||
)
|
||||
|
||||
// Dynamically resize, if needed.
|
||||
//if l := len(s.bufferFloat); l != length {
|
||||
s.buffer = make([]byte, length*2)
|
||||
s.bufferFloat = make(audio.Samples[float64], length)
|
||||
s.bufferInt16 = make(audio.Samples[int16], length)
|
||||
//}
|
||||
|
||||
// Read bytes
|
||||
if n, err := io.ReadFull(s.ReadCloser, s.buffer); err != nil {
|
||||
return n, err
|
||||
}
|
||||
//logrus.Printf("read %d bytes: %#02x", len(s.buffer), s.buffer)
|
||||
|
||||
// Convert
|
||||
s.bufferInt16.Decode(s.buffer, s.Order) // bytes -> int16
|
||||
s.bufferInt16.ToFloat(s.bufferFloat) // int16 -> float64
|
||||
|
||||
if channels == 1 {
|
||||
// Straight conversion into the target buffer.
|
||||
return copy(buffer[0], s.bufferFloat), nil
|
||||
}
|
||||
|
||||
// Deinterleave.
|
||||
audio.Deinterleave(buffer, s.bufferFloat, channels)
|
||||
|
||||
return samples, nil
|
||||
}
|
||||
Reference in new issue
Block a user