Files
beatglow/ui/widget/spectrum.go
T
2026-10-29 17:29:41 +01:00

263 lines
6.4 KiB
Go

package widget
import (
"image"
"math"
"github.com/BeatGlow/audio"
"github.com/BeatGlow/audio/dsp"
"github.com/BeatGlow/audio/dsp/window"
"github.com/BeatGlow/audio/filter"
"github.com/BeatGlow/beatglow/internal/calc"
"github.com/BeatGlow/beatglow/ui"
"github.com/BeatGlow/display"
"github.com/BeatGlow/display/draw"
"github.com/BeatGlow/display/pixel"
)
const (
spectrumBandPixels = 7
spectrumPeakHold = 20
)
type Spectrum struct {
Display display.Display
Position image.Point
buffer draw.Image
width int
height int
mono []float64
bands []float64
peaks [][]float64
scale float64
thresholdDBFS float64
analyzer *dsp.FrequencyPowerCalculator[float64]
minFrequency int
maxFrequency int
hold int
waterfall bool
changed bool
}
func NewSpectrum(display display.Display, at image.Point, width, height int) *Spectrum {
bins := width / spectrumBandPixels
return &Spectrum{
Display: display,
Position: at,
buffer: ui.BufferFor(display.ColorModel(), width, height),
width: width,
height: height,
bands: make([]float64, bins),
peaks: make([][]float64, bins),
thresholdDBFS: -dsp.SQNR(16),
analyzer: dsp.NewFrequencyPowerCalculator[float64](44100, window.Blackman(1024)),
minFrequency: 500,
maxFrequency: 15000,
hold: spectrumPeakHold,
scale: 1.0,
}
}
func (w *Spectrum) SetWaterwall(on bool) {
w.waterfall = on
var bins int
if on {
bins = w.width
} else {
bins = w.width / spectrumBandPixels
}
w.bands = make([]float64, bins)
w.peaks = make([][]float64, bins)
}
func (w *Spectrum) SetRange(min, max int) {
if min > max {
min, max = max, min
}
w.minFrequency = min
w.maxFrequency = max
}
func (w *Spectrum) SetHold(hold int) {
if hold < 1 {
hold = 1
}
w.hold = hold
}
func (w *Spectrum) Render(redraw bool) bool {
if !(redraw || w.changed) {
return false
}
if w.waterfall {
switch buffer := w.buffer.(type) {
case *pixel.Gray2Image:
copy(buffer.Pix, buffer.Pix[buffer.Stride:])
case *pixel.Gray4Image:
copy(buffer.Pix, buffer.Pix[buffer.Stride:])
case *pixel.CRGB15Image:
copy(buffer.Pix, buffer.Pix[buffer.Stride:])
case *pixel.CRGB16Image:
copy(buffer.Pix, buffer.Pix[buffer.Stride:])
case *image.RGBA:
copy(buffer.Pix, buffer.Pix[buffer.Stride:])
default:
for y := w.height - 1; y > 0; y-- {
draw.Draw(w.buffer, image.Rect(0, y, w.width, y+1), w.buffer, image.Pt(0, y-1), draw.Src)
}
}
for x, level := range w.bands {
intesity := uint8(16*level + .5)
w.buffer.Set(x, w.height-1, pixel.Gray4{Y: intesity})
}
} else {
draw.Draw(w.buffer, w.buffer.Bounds(), image.Black, image.Point{}, draw.Src)
for i, level := range w.bands {
height := int(float64(w.height)*level + .5)
offset := i * spectrumBandPixels
//logrus.Infof("band %d at height %d (%f)", i, height, level)
for y := w.height - height; y < w.height; y++ {
for x := 0; x < spectrumBandPixels-1; x++ {
w.buffer.Set(offset+x, y, pixel.Gray4{Y: 0x8})
}
}
if w.hold <= 0 {
continue
}
height = w.height - int(float64(w.height)*calc.Max(w.peaks[i])+.5)
for x := 0; x < spectrumBandPixels-1; x++ {
w.buffer.Set(offset+x, height, pixel.On)
}
}
}
draw.Draw(w.Display, w.buffer.Bounds().Add(w.Position), w.buffer, image.Point{}, draw.Src)
return true
}
func (w *Spectrum) ProcessBuffer(buffer audio.Buffer[float64]) {
// Dynamically resize if needed.
if samples := buffer.Samples(); len(w.mono) != samples {
w.mono = make([]float64, samples)
}
// Convert buffer to mono.
buffer.ToMono(w.mono)
// High pass filter on 50Hz to remove DC bias.
w.mono = filter.HighPass(nil, w.mono, 50, 44100)
// Calculate spectral powers.
powers := w.analyzer.Apply(nil, w.mono)
// Adjust scale
for _, v := range powers {
w.scale = math.Min(math.Max(w.scale, v.Magnitude), 8)
}
// Compress powers to our visible bins.
var (
frequencyBinSize = 44100 / float64(len(w.mono))
frequencyMin = float64(powers[0].Frequency)
frequencyMax = float64(powers[len(powers)-1].Frequency)
frequencyScale = (frequencyMax - frequencyMin) / float64(len(w.bands))
)
for i := range w.bands {
var (
frequencyStart = frequencyMin + float64(i)*frequencyScale
frequencyEnd = frequencyMin + float64(i+1)*frequencyScale
binStart = int((frequencyStart / frequencyBinSize) + .5)
binEnd = int((frequencyEnd / frequencyBinSize) + .5)
)
var total float64
var count int
for bin := binStart; bin < binEnd; bin++ {
total += math.Min(powers[bin].Magnitude/w.scale, 1)
count += 1
}
if count == 0 {
w.bands[i] = 0
} else {
w.bands[i] = total / float64(count)
}
}
// Peak level per band.
for i, v := range w.bands {
w.peaks[i] = append(w.peaks[i], v)
if len(w.peaks[i]) > w.hold {
w.peaks[i] = w.peaks[i][1:]
}
}
/*
logrus.Infof("%d peaks from %d samples in %dHz-%dHz: %f..%f %f..%f [%d/%d] scale %f",
len(w.bands), len(powers),
powers[0].Frequency, powers[len(powers)-1].Frequency,
calc.Min(w.bands), calc.Max(w.bands),
calc.Min(w.mono), calc.Max(w.mono),
buffer.Channels(), buffer.Samples(),
w.scale)
*/
w.changed = true
}
func summarizePowerBands(bands []float64, powers []dsp.FrequencyPower, samples int) {
var (
frequencyBinSize = 44100 / float64(samples)
frequencyMin = float64(powers[0].Frequency)
frequencyMax = float64(powers[len(powers)-1].Frequency)
frequencyScale = (frequencyMax - frequencyMin) / float64(len(bands))
)
for i := range bands {
var (
frequencyStart = frequencyMin + float64(i)*frequencyScale
frequencyEnd = frequencyMin + float64(i+1)*frequencyScale
binStart = int((frequencyStart / frequencyBinSize) + .5)
binEnd = int((frequencyEnd / frequencyBinSize) + .5)
)
var total float64
var count int
for bin := binStart; bin < binEnd; bin++ {
total += math.Min(powers[bin].Magnitude, 1)
count += 1
}
if count == 0 {
bands[i] = 0
} else {
bands[i] = total / float64(count)
}
}
}
func frequencyAbove(powers []dsp.FrequencyPower, frequency int) (i int) {
var power dsp.FrequencyPower
for i, power = range powers {
if power.Frequency >= frequency {
return
}
}
return
}
func frequencyBelow(powers []dsp.FrequencyPower, frequency int) (i int) {
var power dsp.FrequencyPower
for i = len(powers) - 1; i >= 0; i-- {
if power.Frequency <= frequency {
return
}
}
return
}