From f9bc5bb3b892a0ffd0a9784b29231e474dcd65a3 Mon Sep 17 00:00:00 2001 From: maze Date: Tue, 28 Jan 2025 13:16:15 +0100 Subject: [PATCH] Added standalone analyzer layout --- app/layout/analyzer.go | 66 ++++++++++++++++++++++++++++++++++++++++++ app/layout/shader.go | 1 + app/widget/spectrum.go | 37 +++++++++++++++++------ context.go | 12 ++++++++ controller.go | 7 ++--- 5 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 app/layout/analyzer.go diff --git a/app/layout/analyzer.go b/app/layout/analyzer.go new file mode 100644 index 0000000..39f4db6 --- /dev/null +++ b/app/layout/analyzer.go @@ -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) +) diff --git a/app/layout/shader.go b/app/layout/shader.go index e37dd2e..a4b14bd 100644 --- a/app/layout/shader.go +++ b/app/layout/shader.go @@ -23,6 +23,7 @@ type shaderLayout struct { func Shader(ctx app.Context) app.Layout { display := ctx.Display() layout := &shaderLayout{ + base: base{name: "shader"}, model: display.ColorModel(), shader: shader.New(display.ColorModel(), display.Bounds().Size()), effects: []shader.Effect{ diff --git a/app/widget/spectrum.go b/app/widget/spectrum.go index b259c94..9e3cfd0 100644 --- a/app/widget/spectrum.go +++ b/app/widget/spectrum.go @@ -64,7 +64,10 @@ func (w *Spectrum) String() string { if w.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) { @@ -97,6 +100,21 @@ func (w *Spectrum) SetChannels(channels int) *Spectrum { 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 { w.mu.Lock() w.waterfall = on @@ -115,10 +133,6 @@ func (w *Spectrum) Resize(size image.Point) app.Widget { } func (w *Spectrum) Refresh(ctx app.Context) bool { - if !w.IsVisible() { - return false - } - bounds := w.bounds.Load() if w.changed.CompareAndSwap(true, false) { 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())) - return true } @@ -226,14 +239,20 @@ func (w *Spectrum) refreshWaterfall(bounds image.Rectangle) { height := bounds.Dy() if w.channels == 1 { 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) - 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) { diff --git a/context.go b/context.go index 2933275..1ec43bc 100644 --- a/context.go +++ b/context.go @@ -190,14 +190,19 @@ func (c *controllerContext) handleInput(event hardware.InputEvent) { c.handleRotaryPress() } case hardware.RotaryLongPressEvent: + var handled bool for _, layout := range c.layouts { if layout.IsVisible() { if handler, ok := layout.(app.ButtonInputHandler); ok { log.WithField("layout", layout.String()).Debug("dispatch button long press") handler.HandleButtonLongPress(c) + handled = true } } } + if !handled { + c.handleRotaryLongPress() + } default: log.Warn("unhandled input event") } @@ -228,4 +233,11 @@ func (c *controllerContext) handleRotaryPress() { 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) diff --git a/controller.go b/controller.go index 792baaf..d5e2524 100644 --- a/controller.go +++ b/controller.go @@ -50,7 +50,7 @@ func New(config *config.Config) (*Controller, error) { c.teardown(log) 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") c.teardown(log) return nil, err @@ -104,13 +104,12 @@ func (c *Controller) configureInputs(log *logrus.Entry, settings *config.Input) 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.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.Show(nil) - return }