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

+132
View File
@@ -0,0 +1,132 @@
package audio
import (
"math"
"math/cmplx"
"github.com/BeatGlow/beatglow/internal/calc"
"gonum.org/v1/gonum/dsp/fourier"
)
// FrequencySpectrumAnalyzer structure to hold analysis parameters
type FrequencySpectrumAnalyzer struct {
SampleRate float64
BitDepth int
NumBins int
MinFrequency float64
MaxFrequency float64
avgSamples []float64
coefficients []complex128
bins []float64
}
// dBFS calculates dBFS for the given amplitude value
func dBFS(amplitude float64) float64 {
if amplitude == 0 {
return -math.Inf(1) // -Infinity for silence
}
return 20 * math.Log10(amplitude)
}
func normalize(values []float64) {
var max float64
for _, v := range values {
if v = math.Abs(v); v > max {
max = v
}
}
if max == 0 {
return
}
for i := range values {
values[i] /= max
}
}
// AnalyzeFrequencySpectrum performs the FFT and returns the energy levels in dBFS for each frequency bin
func (a *FrequencySpectrumAnalyzer) AnalyzeFrequencySpectrum(buffer *Buffer) []float64 {
samples := len(buffer.Samples[0])
//if len(a.avgSamples) != samples {
a.avgSamples = make([]float64, samples)
a.coefficients = make([]complex128, samples/2+1)
//}
channels := len(buffer.Samples)
if channels == 1 {
for i, sample := range buffer.Samples[0] {
a.avgSamples[i] = float64(sample)
}
} else {
for i := range buffer.Samples[0] {
a.avgSamples[i] = float64(buffer.Samples[0][i]+buffer.Samples[1][i]) / 2
}
}
// Apply windowing function
//normalize(a.avgSamples)
//calc.Hamming(a.avgSamples, a.avgSamples)
calc.Hann(a.avgSamples, a.avgSamples)
// Perform FFT on the signal
fft := fourier.NewFFT(samples)
fft.Coefficients(a.coefficients, a.avgSamples)
// Calculate the energy levels in dBFS for each frequency bin
var (
frequencyBinSize = a.SampleRate / float64(samples)
minBin = int(math.Round(a.MinFrequency / frequencyBinSize))
maxBin = int(math.Round(a.MaxFrequency / frequencyBinSize))
)
// Make sure the minimum bin is not lower than 0, and the maximum bin doesn't exceed half of the buffer size
if minBin < 0 {
minBin = 0
}
if maxBin > samples/2 {
maxBin = samples / 2
}
//if len(a.bins) != a.NumBins {
a.bins = make([]float64, a.NumBins)
//}
// Compute dBFS levels for the selected frequency range
for i := 0; i < a.NumBins; i++ {
var (
// Calculate the frequency range for this bin
frequencyScale = (a.MaxFrequency - a.MinFrequency) / float64(a.NumBins)
frequencyStart = a.MinFrequency + float64(i)*frequencyScale
frequencyEnd = a.MinFrequency + float64(i+1)*frequencyScale
// Determine the FFT bin range corresponding to the frequency range of this bin
startBin = int((frequencyStart / frequencyBinSize) + .5)
endBin = int((frequencyEnd / frequencyBinSize) + .5)
)
// Ensure the bins are within the valid range
if startBin < 0 {
startBin = 0
}
if endBin > samples/2 {
endBin = samples / 2
}
// Average the magnitudes of the FFT bins in this range and accumulate the result
var totalAmplitude float64
var count int
for bin := startBin; bin < endBin; bin++ {
x := a.coefficients[bin]
//amplitude := real(x)*real(x) + imag(x)*imag(x) // math.Hypot(real(x), imag(x))
amplitude := math.Min(cmplx.Abs(x), 1.0)
totalAmplitude += amplitude
count++
}
// Calculate the average dBFS for this frequency bin
if count > 0 {
a.bins[i] = math.Min((totalAmplitude/float64(count))*10, 1.0)
}
}
return a.bins
}
+97
View File
@@ -0,0 +1,97 @@
package audio
import "math"
// Float is all floating point number types (and Samples).
type Float interface {
~float32 | ~float64
}
// Clamp a sample in [-1..+1].
func Clamp[T Float](sample T) T {
if sample < -1 {
return -1
}
if sample > +1 {
return +1
}
return sample
}
// Abs a slice of values.
func Abs[T Float](values []T) {
for i, v := range values {
values[i] = T(math.Abs(float64(v)))
}
}
// Mean of values.
func Mean[T Float](values []T) T {
if len(values) == 0 {
return 0
}
// an algorithm that attempts to retain accuracy
// with widely different values.
var parts []float64
for _, v := range values {
var (
x = float64(v)
i int
)
for _, p := range parts {
sum := p + x
var err float64
switch ax, ap := math.Abs(x), math.Abs(p); {
case ax < ap:
err = x - (sum - p)
case ap < ax:
err = p - (sum - x)
}
if err != 0 {
parts[i] = err
i++
}
x = sum
}
parts = append(parts[:i], x)
}
var sum float64
for _, x := range parts {
sum += x
}
return T(sum / float64(len(values)))
}
// RMS calculates the root mean square of values.
func RMS[T Float](values []T) T {
if len(values) == 0 {
return 0
}
var squares float64
for _, v := range values {
squares += float64(v) * float64(v)
}
return T(math.Sqrt(squares / float64(len(values))))
}
// Sum of values.
func Sum[T Float](values []T) T {
if len(values) == 0 {
return 0
}
var sum T
for _, v := range values {
sum += v
}
return sum
}
// SQNR is the signal-to-quantization-noise ratio.
func SQNR(bits int) float64 {
return 20 * math.Log10(math.Pow(2, float64(bits)))
}
+241
View File
@@ -0,0 +1,241 @@
package audio
import (
"encoding/binary"
"io"
"math"
"github.com/sirupsen/logrus"
)
// Sample is a type alias for float64.
type Sample float64
// Format is the format of a Buffer or another audio source.
type Format interface {
// BitsPerSample is the number of bits per sample.
BitsPerSample() int
// Order is the byte order.
Order() binary.ByteOrder
// DecodeFromBytes reads one sample from b. No bounds checking is performed on b!
DecodeFromBytes(b []byte) Sample
// EncodeToBytes writes on sample to b.
EncodeToBytes(b []byte, s Sample)
}
type pcmFormat uint8
const (
Int8 pcmFormat = iota
Int16
Int16BE
Int32
Int32BE
Float32
Float32BE
Float64
Float64BE
)
func makeFormat(bits int) pcmFormat {
switch bits {
case 8:
return Int8
case 16:
return Int16
case 32:
return Int32
default:
return Int16
}
}
var pcmfFormatBitsPerSample = map[pcmFormat]int{
Int8: 8,
Int16: 16,
Int16BE: 16,
Int32: 32,
Int32BE: 32,
Float32: 32,
Float32BE: 32,
Float64: 64,
Float64BE: 64,
}
func (f pcmFormat) BitsPerSample() int {
if v, ok := pcmfFormatBitsPerSample[f]; ok {
return v
}
return 0
}
func (f pcmFormat) Order() binary.ByteOrder {
if f%2 == 0 {
return binary.BigEndian
}
return binary.LittleEndian
}
func (f pcmFormat) DecodeFromBytes(b []byte) Sample {
order := f.Order()
switch f {
case Int8:
return Sample(int8(b[0])) / math.MaxInt8
case Int16, Int16BE:
return Sample(int16(order.Uint16(b))) / math.MaxInt16
case Int32, Int32BE:
return Sample(int32(order.Uint32(b))) / math.MaxInt32
case Float32, Float32BE:
return Sample(math.Float32frombits(order.Uint32(b)))
case Float64, Float64BE:
return Sample(math.Float64frombits(order.Uint64(b)))
default:
return 0
}
}
func (f pcmFormat) EncodeToBytes(b []byte, s Sample) {
order := f.Order()
switch f {
case Int8:
b[0] = byte(int8(s * math.MaxInt8))
case Int16, Int16BE:
order.PutUint16(b, uint16(int16(s*math.MaxInt16)))
case Int32, Int32BE:
order.PutUint32(b, uint32(int32(s*math.MaxInt32)))
case Float32, Float32BE:
order.PutUint32(b, math.Float32bits(float32(s)))
case Float64, Float64BE:
order.PutUint64(b, math.Float64bits(float64(s)))
}
}
var pcmFormatNames = map[pcmFormat]string{
Int8: "8-bit signed PCM",
Int16: "16-bit signed PCM",
Int16BE: "16-bit signed PCM (BE)",
Int32: "32-bit signed PCM",
Int32BE: "32-bit signed PCM (BE)",
Float32: "32-bit floating piont PCM",
Float32BE: "32-bit floating point PCM (BE)",
Float64: "64-bit floating point PCM",
Float64BE: "64-bit floating point PCM (BE)",
}
func (f pcmFormat) String() string {
if s, ok := pcmFormatNames[f]; ok {
return s
}
return "unknown"
}
// Buffer of audio samples, only supports mono and stereo samples.
type Buffer struct {
Format Format
Rate Rate
Samples [][]Sample
buf []byte
}
func NewBuffer(format Format, rate Rate, n, channels int) *Buffer {
if channels < 1 {
channels = 1
}
if channels > 2 {
channels = 2
}
logrus.WithFields(logrus.Fields{
"channels": channels,
"rate": rate,
"format": format,
"samples": n,
}).Debug("new audio buffer")
samples := make([][]Sample, channels)
for channel := 0; channel < channels; channel++ {
samples[channel] = make([]Sample, n)
}
return &Buffer{
Format: format,
Rate: rate,
Samples: samples,
}
}
// DecodeFrom decodes samples read from r.
func (b *Buffer) DecodeFrom(r io.Reader) (n int, err error) {
if s, ok := r.(SamplesReader); ok {
// Use the SamplesReader interface, if available.
return s.ReadSamples(b.Samples)
}
var (
channels = len(b.Samples)
samples = len(b.Samples[0])
bitsPerSample = b.Format.BitsPerSample()
bytesPerSample = bitsPerSample / 8
bytesRequired = channels * samples * bytesPerSample
)
// Resize buffer, if required.
if len(b.buf) < bytesRequired {
b.buf = make([]byte, bytesRequired)
} else {
b.buf = b.buf[:bytesRequired]
}
// Read raw bytes.
if _, err = io.ReadFull(r, b.buf); err != nil {
return
}
// Convert bytes to samples, deinterleave multi channel audio.
var o, c int
for i, l := 0, bytesRequired/bytesPerSample; i < l; i++ {
b.Samples[0][c] = b.Format.DecodeFromBytes(b.buf[o : o+bytesPerSample])
c = (c + 1) % channels
o = o + bytesPerSample
}
return bytesRequired / bytesPerSample, nil
}
// ToMono converts the buffer to mono. If the buffer is already mono, it returns the buffer.
func (b *Buffer) ToMono() *Buffer {
if len(b.Samples) == 1 {
return b
}
// Convert stereo buffer to mono.
o := NewBuffer(b.Format, b.Rate, len(b.Samples[0]), 2)
for i := range o.Samples[0] {
o.Samples[0][i] = (b.Samples[0][i] + b.Samples[1][i]) / 2
}
return o
}
// ToStereo converts the buffer to stereo. If the buffer is already stereo, it returns the buffer.
func (b *Buffer) ToStereo() *Buffer {
if len(b.Samples) == 2 {
return b
}
// Convert mono buffer to stereo.
o := NewBuffer(b.Format, b.Rate, len(b.Samples[0]), 1)
copy(o.Samples[0], b.Samples[0])
copy(o.Samples[1], b.Samples[0])
return o
}
type BufferProcessor interface {
ProcessBuffer(buffer *Buffer)
}
type Processor interface {
ProcessSamples(samples [][]Sample)
}
+23
View File
@@ -0,0 +1,23 @@
package audio
import "testing"
func TestFormatBitsPerSample(t *testing.T) {
testCases := []struct {
Test Format
Want int
}{
{Int8, 8},
{Int16, 16},
{Int16BE, 16},
{Int32, 32},
{Int32BE, 32},
}
for _, test := range testCases {
t.Run(test.Test.(pcmFormat).String(), func(it *testing.T) {
if v := test.Test.BitsPerSample(); v != test.Want {
it.Errorf("expected %d bits per sample, got %d", test.Want, v)
}
})
}
}
+147
View File
@@ -0,0 +1,147 @@
package audio
import (
"errors"
"fmt"
"io"
"os"
"github.com/sirupsen/logrus"
"github.com/BeatGlow/beatglow/config"
)
func init() {
Register("file", openReader)
Register("fifo", openReader)
}
// Source of audio samples.
type Source interface {
io.Reader
String() string
// Channels is the number of channels.
Channels() int
// Format of the source.
Format() Format
// SampleRate of the source.
SampleRate() Rate
}
// NotFound signals the reader type can not be found.
type NotFound struct {
Type string
}
func (err NotFound) Error() string {
return fmt.Sprintf("audio source %q not available", err.Type)
}
// SourceOpener can open an audio stream source.
type SourceOpener func(config.Audio) (Source, error)
var drivers = make(map[string]SourceOpener)
func Register(name string, opener SourceOpener) {
if _, dupe := drivers[name]; dupe {
panic(fmt.Sprintf("audio source %q already registered", name))
}
drivers[name] = opener
}
func NewSource(config config.Audio) (Source, error) {
opener, ok := drivers[config.Type]
if ok {
return opener(config)
}
return nil, NotFound{Type: config.Type}
}
type ReaderSource struct {
io.ReadCloser
// Name of the reader (path).
Name string
// Ch is the number of channels.
Ch int
// Format is the audio format.
Fmt Format
// Rate is the sample rate.
Rate Rate
// Opener is a function that can be called if the reader is closed.
Opener func() (io.ReadCloser, error)
}
func openReader(config config.Audio) (Source, error) {
var (
ch = config.Channels
bits = config.Bits
rate = Rate(config.Rate)
)
if ch == 0 {
ch = 2
}
if bits == 0 {
bits = 16
}
if rate == 0 {
rate = 44100
}
return &ReaderSource{
Name: config.Path,
Ch: ch,
Fmt: makeFormat(bits),
Rate: rate,
Opener: func() (io.ReadCloser, error) {
return os.Open(config.Path)
},
}, nil
}
func (s *ReaderSource) String() string {
return "reader:" + s.Name
}
func (s *ReaderSource) Channels() int {
return s.Ch
}
func (s *ReaderSource) Format() Format {
return s.Fmt
}
func (s *ReaderSource) SampleRate() Rate {
return s.Rate
}
func (r *ReaderSource) Read(b []byte) (int, error) {
var err error
if r.ReadCloser == nil {
if r.ReadCloser, err = r.Opener(); err != nil {
return 0, err
}
}
var n int
if n, err = r.ReadCloser.Read(b); err != nil {
logrus.WithError(err).Warn("source failed to read; closing")
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
_ = r.ReadCloser.Close()
r.ReadCloser = nil
}
}
return n, err
}
type SamplesReader interface {
// ReadSamples reads samples, it returns the number of samples read.
ReadSamples(samples [][]Sample) (int, error)
}
+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?")
}
}
}
+60
View File
@@ -0,0 +1,60 @@
package audio
import (
"fmt"
"strconv"
"time"
)
// Rate is the sampling rate.
type Rate int
// Frequency is the sampling frequency.
func (r Rate) Frequency() Frequency {
return 1 / Frequency(r)
}
// Duration of n samples at this rate.
func (r Rate) Duration(n int) time.Duration {
return time.Second * time.Duration(n) / time.Duration(r)
}
// Number of sample during duration d.
func (r Rate) Samples(d time.Duration) int {
return int(d * time.Duration(r) / time.Second)
}
func (r Rate) String() string {
return strconv.FormatInt(int64(r), 10)
}
// Frequency is a type alias for float64.
type Frequency float64
const (
Hz Frequency = 1
KHz Frequency = 1e3
MHz Frequency = 1e6
GHz Frequency = 1e9
THz Frequency = 1e12
)
func (f Frequency) String() string {
switch {
case f > THz:
return fmt.Sprintf("%gTHz", f/THz)
case f > GHz:
return fmt.Sprintf("%gGHz", f/GHz)
case f > MHz:
return fmt.Sprintf("%gMHz", f/MHz)
case f > KHz:
return fmt.Sprintf("%gkHz", f/KHz)
default:
return fmt.Sprintf("%gHz", f)
}
}
// Rate is the sampling rate for this frequency.
func (r Frequency) Rate() Rate {
return Rate(1 / r)
}