Added standalone analyzer layout

This commit is contained in:
maze authored and maze committed 2026-10-29 17:29:54 +01:00
1 parent 0196bb3707
commit f9bc5bb3b8
5 files changed
+110 -13

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
package layout
import (
"image/color"
"github.com/BeatGlow/audio"
"github.com/BeatGlow/beatglow/app"
"github.com/BeatGlow/beatglow/app/widget"
"github.com/BeatGlow/beatglow/config"
)
type analyzerLayout struct {
base
model color.Model
widget *widget.Spectrum
settings *config.Settings
index int
}
func Analyzer(ctx app.Context) app.Layout {
var (
display = ctx.Display()
bounds = display.Bounds()
settings = ctx.Settings()
styles = app.StylesFor(display.ColorModel())
layout = &analyzerLayout{
base: base{name: "analyzer"},
widget: widget.NewSpectrum(bounds.Size()),
settings: settings,
}
)
layout.widget.SetStyles(*styles)
layout.widgets = []app.Widget{layout.widget}
return layout
}
func (l *analyzerLayout) Show(ctx app.Context) {
channels := 2
if l.settings.Spectrum.Mono {
channels = 1
}
l.widget.
SetWaterfall(l.settings.Spectrum.Waterfall).
SetChannels(channels).
SetRange(l.settings.Spectrum.MinFrequency, l.settings.Spectrum.MaxFrequency).
SetHold(int(l.settings.Spectrum.Hold))
l.base.Show(ctx)
}
func (l *analyzerLayout) ProcessBuffer(buffer audio.Buffer[float64]) {
l.widget.ProcessBuffer(buffer)
}
func (l *analyzerLayout) HandleRotary(ctx app.Context, delta int) {
l.settings.Spectrum.Waterfall = !l.settings.Spectrum.Waterfall
l.widget.SetWaterfall(l.settings.Spectrum.Waterfall)
}
var (
_ app.Layout = (*analyzerLayout)(nil)
_ app.BufferProcessor = (*analyzerLayout)(nil)
)
+1
View File
@@ -23,6 +23,7 @@ type shaderLayout struct {
func Shader(ctx app.Context) app.Layout { func Shader(ctx app.Context) app.Layout {
display := ctx.Display() display := ctx.Display()
layout := &shaderLayout{ layout := &shaderLayout{
base: base{name: "shader"},
model: display.ColorModel(), model: display.ColorModel(),
shader: shader.New(display.ColorModel(), display.Bounds().Size()), shader: shader.New(display.ColorModel(), display.Bounds().Size()),
effects: []shader.Effect{ effects: []shader.Effect{
+28 -9
View File
@@ -64,7 +64,10 @@ func (w *Spectrum) String() string {
if w.waterfall { if w.waterfall {
variant = "waterfall" variant = "waterfall"
} }
return channels + " " + variant if w.buffer != nil {
return channels + " " + variant + ":" + w.buffer.Bounds().Size().String()
}
return channels + " " + variant + ":nil"
} }
func (w *Spectrum) update(channels, bins int) { func (w *Spectrum) update(channels, bins int) {
@@ -97,6 +100,21 @@ func (w *Spectrum) SetChannels(channels int) *Spectrum {
return w return w
} }
func (w *Spectrum) SetHold(hold int) *Spectrum {
w.mu.Lock()
w.hold = hold
w.mu.Unlock()
return w
}
func (w *Spectrum) SetRange(min, max int) *Spectrum {
w.mu.Lock()
w.minFrequency = min
w.maxFrequency = max
w.mu.Unlock()
return w
}
func (w *Spectrum) SetWaterfall(on bool) *Spectrum { func (w *Spectrum) SetWaterfall(on bool) *Spectrum {
w.mu.Lock() w.mu.Lock()
w.waterfall = on w.waterfall = on
@@ -115,10 +133,6 @@ func (w *Spectrum) Resize(size image.Point) app.Widget {
} }
func (w *Spectrum) Refresh(ctx app.Context) bool { func (w *Spectrum) Refresh(ctx app.Context) bool {
if !w.IsVisible() {
return false
}
bounds := w.bounds.Load() bounds := w.bounds.Load()
if w.changed.CompareAndSwap(true, false) { if w.changed.CompareAndSwap(true, false) {
if w.buffer == nil { if w.buffer == nil {
@@ -134,7 +148,6 @@ func (w *Spectrum) Refresh(ctx app.Context) bool {
} }
app.ImageCopyBounds(ctx.Display(), w.buffer, bounds.Add(w.position.Load())) app.ImageCopyBounds(ctx.Display(), w.buffer, bounds.Add(w.position.Load()))
return true return true
} }
@@ -226,14 +239,20 @@ func (w *Spectrum) refreshWaterfall(bounds image.Rectangle) {
height := bounds.Dy() height := bounds.Dy()
if w.channels == 1 { if w.channels == 1 {
for i, v := range w.bands[0] { for i, v := range w.bands[0] {
w.drawMonoPoint(i, v, height) w.drawPoint(i, v, height)
}
} else {
offset := bounds.Dx() / 2
for i := range w.bands[0] {
w.drawPoint(i, w.bands[0][i], height)
w.drawPoint(offset+i, w.bands[1][i], height)
} }
} }
} }
func (w *Spectrum) drawMonoPoint(i int, level float64, maxHeight int) { func (w *Spectrum) drawPoint(x int, level float64, maxHeight int) {
intesity := uint8(16*level + .5) intesity := uint8(16*level + .5)
w.buffer.Set(i, maxHeight-1, pixel.Gray4{Y: intesity}) w.buffer.Set(x, maxHeight-1, pixel.Gray4{Y: intesity})
} }
func (w *Spectrum) StatusChanged(ctx app.Context, status *player.Status) { func (w *Spectrum) StatusChanged(ctx app.Context, status *player.Status) {
+12
View File
@@ -190,14 +190,19 @@ func (c *controllerContext) handleInput(event hardware.InputEvent) {
c.handleRotaryPress() c.handleRotaryPress()
} }
case hardware.RotaryLongPressEvent: case hardware.RotaryLongPressEvent:
var handled bool
for _, layout := range c.layouts { for _, layout := range c.layouts {
if layout.IsVisible() { if layout.IsVisible() {
if handler, ok := layout.(app.ButtonInputHandler); ok { if handler, ok := layout.(app.ButtonInputHandler); ok {
log.WithField("layout", layout.String()).Debug("dispatch button long press") log.WithField("layout", layout.String()).Debug("dispatch button long press")
handler.HandleButtonLongPress(c) handler.HandleButtonLongPress(c)
handled = true
} }
} }
} }
if !handled {
c.handleRotaryLongPress()
}
default: default:
log.Warn("unhandled input event") log.Warn("unhandled input event")
} }
@@ -228,4 +233,11 @@ func (c *controllerContext) handleRotaryPress() {
c.Show(c.layouts[nextIndex]) c.Show(c.layouts[nextIndex])
} }
func (c *controllerContext) handleRotaryLongPress() {
log := logrus.NewEntry(logrus.StandardLogger())
log.Debug("no handler for rotary press event, switching to settings")
c.Show(c.settingsLayout)
}
var _ app.Context = (*controllerContext)(nil) var _ app.Context = (*controllerContext)(nil)
+3 -4
View File
@@ -50,7 +50,7 @@ func New(config *config.Config) (*Controller, error) {
c.teardown(log) c.teardown(log)
return nil, err return nil, err
} }
if err := c.configureApps(log); err != nil { if err := c.configureLayouts(log); err != nil {
log.WithError(err).Error("fatal error setting up apps") log.WithError(err).Error("fatal error setting up apps")
c.teardown(log) c.teardown(log)
return nil, err return nil, err
@@ -104,13 +104,12 @@ func (c *Controller) configureInputs(log *logrus.Entry, settings *config.Input)
return return
} }
func (c *Controller) configureApps(_ *logrus.Entry) (err error) { func (c *Controller) configureLayouts(_ *logrus.Entry) (err error) {
c.ctx.addSettings(layout.Settings(&c.ctx)) c.ctx.addSettings(layout.Settings(&c.ctx))
c.ctx.addLayout(layout.Playing(&c.ctx)) c.ctx.addLayout(layout.Playing(&c.ctx))
//c.ctx.addLayout(layout.Spectrum(&c.ctx)) c.ctx.addLayout(layout.Analyzer(&c.ctx))
c.ctx.addLayout(layout.Shader(&c.ctx)) c.ctx.addLayout(layout.Shader(&c.ctx))
c.ctx.Show(nil) c.ctx.Show(nil)
return return
} }