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

+217
View File
@@ -0,0 +1,217 @@
package widget
import (
"image"
"image/draw"
"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/pixel"
)
type StereoSpectrum struct {
Display display.Display
Position image.Point
buffer draw.Image
height int
width int
bands [2][]float64
peaks [2][][]float64
samples [2]audio.Samples[float64]
power [2][]dsp.FrequencyPower
analyzer *dsp.FrequencyPowerCalculator[float64]
minFrequency int
maxFrequency int
hold int
waterfall bool
changed bool
}
func NewStereoSpectrum(display display.Display, at image.Point, width, height int) *StereoSpectrum {
bins := width / spectrumBandPixels / 2
return &StereoSpectrum{
Display: display,
Position: at,
buffer: ui.BufferFor(display.ColorModel(), width, height),
height: height,
width: width,
bands: makeStereoBands(bins),
peaks: makeStereoPeaks(bins),
analyzer: dsp.NewFrequencyPowerCalculator[float64](44100, window.Blackman1024),
minFrequency: 500,
maxFrequency: 15000,
hold: spectrumPeakHold,
}
}
func (w *StereoSpectrum) SetWaterwall(on bool) {
w.waterfall = on
var bins int
if on {
bins = w.width / 2
} else {
bins = w.width / spectrumBandPixels / 2
}
w.bands = makeStereoBands(bins)
w.peaks = makeStereoPeaks(bins)
}
func (w *StereoSpectrum) SetRange(min, max int) {
if min > max {
min, max = max, min
}
w.minFrequency = min
w.maxFrequency = max
}
func (w *StereoSpectrum) SetHold(hold int) {
w.hold = hold
}
func (w *StereoSpectrum) Render(refresh bool) bool {
if !(refresh || w.changed) {
return false
}
bounds := w.buffer.Bounds()
//draw.Draw(w.buffer, bounds, image.Black, image.Point{}, draw.Src)
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 i := range w.bands[0] {
w.drawPoints(0, i)
w.drawPoints(1, i)
}
} else {
draw.Draw(w.buffer, w.buffer.Bounds(), image.Black, image.Point{}, draw.Src)
for i := range w.bands[0] {
w.drawBar(0, i)
w.drawBar(1, i)
}
}
draw.Draw(w.Display, bounds.Add(w.Position), w.buffer, image.Point{}, draw.Src)
return true
}
func (w *StereoSpectrum) drawBar(channel, i int) {
var offset int
if channel == 0 {
offset = (w.width / 2) - (i+1)*spectrumBandPixels
} else {
offset = (w.width / 2) + i*spectrumBandPixels
}
height := int(float64(w.height)*w.bands[channel][i] + .5)
for y := w.height - height; y < w.height; y++ {
colorY := pixel.Gray4{Y: 0xf - uint8(float64(y)/float64(w.height)*12+.5)}
for x := 0; x < spectrumBandPixels-1; x++ {
w.buffer.Set(offset+x, y, colorY)
}
}
if w.hold <= 0 {
return
}
height = w.height - int(float64(w.height)*calc.Max(w.peaks[channel][i])+5)
for x := 0; x < spectrumBandPixels-1; x++ {
w.buffer.Set(offset+x, height, pixel.On)
}
}
func (w *StereoSpectrum) drawPoints(channel, i int) {
var offset int
if channel == 0 {
offset = (w.width / 2) - (i + 1)
} else {
offset = (w.width / 2) + i
}
intesity := uint8(16*w.bands[channel][i] + .5)
w.buffer.Set(offset, w.height-1, pixel.Gray4{Y: intesity})
}
func (w *StereoSpectrum) ProcessBuffer(buffer audio.Buffer[float64]) {
var (
channels = buffer.Channels()
)
// Convert buffers to mono and high pass on 50Hz.
if channels == 1 { // unlikely
w.samples[0] = filter.HighPass(w.samples[0], buffer[0], 50, 44100)
w.samples[1] = w.samples[0]
} else {
w.samples[0] = filter.HighPass(w.samples[0], buffer[0], 50, 44100)
w.samples[1] = filter.HighPass(w.samples[1], buffer[1], 50, 44100)
}
// Calculate spectral powers.
w.power[0] = w.analyzer.Apply(w.power[0], w.samples[0])
w.power[1] = w.analyzer.Apply(w.power[1], w.samples[1])
// Adjust scale.
for i := range w.power[0] {
w.power[0][i].Magnitude /= 8
w.power[1][i].Magnitude /= 8
}
// Compress powers to our visible bars.
var (
highPassIndex = frequencyAbove(w.power[0], w.minFrequency)
lowPassIndex = frequencyBelow(w.power[0], w.maxFrequency)
)
summarizePowerBands(w.bands[0], w.power[0][highPassIndex:lowPassIndex], lowPassIndex-highPassIndex)
summarizePowerBands(w.bands[1], w.power[1][highPassIndex:lowPassIndex], lowPassIndex-highPassIndex)
// Calculate peaks per band.
if w.hold > 0 {
for c := range w.bands {
for i, v := range w.bands[c] {
w.peaks[c][i] = append(w.peaks[c][i], v)
for len(w.peaks[c][i]) > w.hold {
w.peaks[c][i] = w.peaks[c][i][1:]
}
}
}
}
w.changed = true
}
func makeStereoBands(n int) [2][]float64 {
return [2][]float64{
make([]float64, n),
make([]float64, n),
}
}
func makeStereoPeaks(n int) [2][][]float64 {
return [2][][]float64{
make([][]float64, n),
make([][]float64, n),
}
}