From 0196bb37071d47df5820d22e34692e9de2a47d0f Mon Sep 17 00:00:00 2001 From: maze Date: Tue, 28 Jan 2025 12:46:08 +0100 Subject: [PATCH] Fixed rotating layouts; Added Shader layout --- app/app.go | 3 ++ app/helper.go | 4 +- app/layout/base.go | 28 ++----------- app/layout/settings.go | 90 ++++++++++++++++++++---------------------- app/layout/shader.go | 76 +++++++++++++++++++++++++++++++++++ app/shader/shader.go | 5 +++ app/widget/base.go | 2 +- app/widget/image.go | 30 +++++++++++--- app/widget/menu.go | 2 +- app/widget/spectrum.go | 2 +- app/widget/text.go | 2 +- context.go | 54 ++++++++++++++++++++----- controller.go | 2 + 13 files changed, 207 insertions(+), 93 deletions(-) create mode 100644 app/layout/shader.go diff --git a/app/app.go b/app/app.go index 2f17790..97027ac 100644 --- a/app/app.go +++ b/app/app.go @@ -19,6 +19,9 @@ type Layout interface { // Show the layout. Show(Context) + // IsVisible checks if the layout is active. + IsVisible() bool + // Widgets used by this screen. Widgets() []Widget } diff --git a/app/helper.go b/app/helper.go index 8a62110..df85c52 100644 --- a/app/helper.go +++ b/app/helper.go @@ -16,8 +16,8 @@ import ( ) // BufferFor creates a new image buffer for the provided display matching its color model. -func BufferFor(ctx Context, size image.Point) draw.Image { - switch ctx.Display().ColorModel() { +func BufferFor(model color.Model, size image.Point) draw.Image { + switch model { case pixel.MonoModel: return pixel.NewMonoImage(size.X, size.Y) case pixel.Gray2Model: diff --git a/app/layout/base.go b/app/layout/base.go index d1c7958..fb72eb4 100644 --- a/app/layout/base.go +++ b/app/layout/base.go @@ -7,11 +7,9 @@ import ( ) type base struct { - name string - widgets []app.Widget - visible bool - onButtonPress func(app.Context) - onButtonLongPress func(app.Context) + name string + widgets []app.Widget + visible bool } func (l *base) log() *logrus.Entry { @@ -50,23 +48,3 @@ func (l *base) Show(ctx app.Context) { } l.visible = true } - -func (i *base) OnButtonPress(action func(app.Context)) { - i.onButtonPress = action -} - -func (i *base) OnButtonLongPress(action func(app.Context)) { - i.onButtonLongPress = action -} - -func (l *base) HandleButtonPress(ctx app.Context) { - if l.onButtonPress != nil { - l.onButtonPress(ctx) - } -} - -func (l *base) HandleButtonLongPress(ctx app.Context) { - if l.onButtonLongPress != nil { - l.onButtonLongPress(ctx) - } -} diff --git a/app/layout/settings.go b/app/layout/settings.go index faa110e..afa12c8 100644 --- a/app/layout/settings.go +++ b/app/layout/settings.go @@ -44,67 +44,61 @@ func Settings(ctx app.Context) app.Layout { } func (l *settings) HandleRotary(ctx app.Context, delta int) { - if l.menuWidget.IsVisible() { - log := l.log() - log.WithField("item", l.index).Debug("menu item changed") - l.menu.Change(delta) - if l.menu.IsEditing() { - // React on changed value, if it's an immediate change. - switch item := l.menu.Items[l.menu.Position]; item.Label { - case "Display contrast": - if err := ctx.Display().SetContrast(*(item.Value.(*uint8))); err != nil { - log.WithError(err).Error("error updating contrast") - } - default: - log.WithField("label", item.Label).Debug("no immediate change") + log := l.log() + log.WithField("item", l.index).Debug("menu item changed") + l.menu.Change(delta) + if l.menu.IsEditing() { + // React on changed value, if it's an immediate change. + switch item := l.menu.Items[l.menu.Position]; item.Label { + case "Display contrast": + if err := ctx.Display().SetContrast(*(item.Value.(*uint8))); err != nil { + log.WithError(err).Error("error updating contrast") } + default: + log.WithField("label", item.Label).Debug("no immediate change") } - l.menuWidget.Redraw() } + l.menuWidget.Redraw() } func (l *settings) HandleButtonPress(ctx app.Context) { - if l.menuWidget.IsVisible() { - log := l.log() - if l.menu.IsEditing() { - log.Debug("exit edit mode") - l.menu.ToggleEdit() - } else if l.menu.IsSubMenu() { - log.Debug("enter sub menu") - l.menu = l.menu.EnterSubMenu() - } else if item := l.menu.ActiveItem(); item.Value == menu.Back { - log.Debug("exit sub menu") - menu := l.menu.ExitSubMenu() - if menu == nil { - // Close settings layout, exiting root. - ctx.Show(nil) - return - } - l.menu = menu - } else { - log.Debug("enter edit mode") - l.menu.ToggleEdit() + log := l.log() + if l.menu.IsEditing() { + log.Debug("exit edit mode") + l.menu.ToggleEdit() + } else if l.menu.IsSubMenu() { + log.Debug("enter sub menu") + l.menu = l.menu.EnterSubMenu() + } else if item := l.menu.ActiveItem(); item.Value == menu.Back { + log.Debug("exit sub menu") + menu := l.menu.ExitSubMenu() + if menu == nil { + // Close settings layout, exiting root. + ctx.Show(nil) + return } - - // Trigger re-render. - l.menuWidget.Update(l.menu) + l.menu = menu + } else { + log.Debug("enter edit mode") + l.menu.ToggleEdit() } + + // Trigger re-render. + l.menuWidget.Update(l.menu) } func (l *settings) HandleButtonLongPress(ctx app.Context) { - if l.menuWidget.IsVisible() { - // Rewind to root menu - for { - parent := l.menu.Parent - if parent == nil { - break - } - l.menu = parent + // Rewind to root menu + for { + parent := l.menu.Parent + if parent == nil { + break } - - // Close settings layout. - ctx.Show(nil) + l.menu = parent } + + // Close settings layout. + ctx.Show(nil) } func foreachStructField(v reflect.Value, f func(fv reflect.Value, sf reflect.StructField) (stop bool)) { diff --git a/app/layout/shader.go b/app/layout/shader.go new file mode 100644 index 0000000..e37dd2e --- /dev/null +++ b/app/layout/shader.go @@ -0,0 +1,76 @@ +package layout + +import ( + "image/color" + "sync" + + "github.com/BeatGlow/audio" + "github.com/BeatGlow/beatglow/app" + "github.com/BeatGlow/beatglow/app/shader" + "github.com/BeatGlow/beatglow/app/widget" +) + +type shaderLayout struct { + base + model color.Model + effects []shader.Effect + index int + mu sync.RWMutex + shader *shader.Shader + widget *widget.Canvas +} + +func Shader(ctx app.Context) app.Layout { + display := ctx.Display() + layout := &shaderLayout{ + model: display.ColorModel(), + shader: shader.New(display.ColorModel(), display.Bounds().Size()), + effects: []shader.Effect{ + &shader.AudioIntensityBarsEffect{}, + &shader.AudioNoiseEffect{}, + &shader.AudioRipplesEffect{}, + &shader.ChannelSeparationEffect{}, + &shader.MoireRotatingZoomerEffect{}, + &shader.PulsatingCirclesEffect{}, + &shader.RadialAudioIntensityEffect{}, + &shader.StereoMirrorEffect{}, + &shader.StereoPulseEffect{}, + &shader.StereoRadialEffect{}, + &shader.StereoRippleEffect{}, + &shader.StereoSpectrumEffect{}, + &shader.StereoSwirlEffect{}, + &shader.StereoWaveformEffect{}, + }, + widget: widget.NewCanvas(display.Bounds().Size(), display.ColorModel()), + } + layout.shader.Effects = []shader.Effect{layout.effects[0]} + layout.widgets = []app.Widget{layout.widget} + return layout +} + +func (l *shaderLayout) ProcessBuffer(buffer audio.Buffer[float64]) { + if l.visible { + l.mu.Lock() + l.shader.ProcessBuffer(buffer) + app.ImageCopy(l.widget.Buffer(), l.shader.Render()) + //l.widget.Update(l.shader.Render(), l.model) + l.mu.Unlock() + } +} + +func (l *shaderLayout) HandleRotary(_ app.Context, delta int) { + l.index += delta + for l.index < 0 { + l.index = len(l.effects) + l.index + } + for l.index >= len(l.effects) { + l.index = l.index - len(l.effects) + } + l.shader.Effects[0] = l.effects[l.index] +} + +var ( + _ app.Layout = (*shaderLayout)(nil) + _ app.BufferProcessor = (*shaderLayout)(nil) + _ app.RotaryInputHandler = (*shaderLayout)(nil) +) diff --git a/app/shader/shader.go b/app/shader/shader.go index c302f1d..05b124f 100644 --- a/app/shader/shader.go +++ b/app/shader/shader.go @@ -43,6 +43,11 @@ func (s *Shader) ProcessBuffer(buffer audio.Buffer[float64]) { for channel, channels := 0, len(s.buffer); channel < channels; channel++ { copy(s.buffer[channel], buffer[channel]) } + + // Normalize channels + for c := range s.buffer { + audio.Normalize(s.buffer[c]) + } } func (s *Shader) Render() image.Image { diff --git a/app/widget/base.go b/app/widget/base.go index d5d8588..a4c6caf 100644 --- a/app/widget/base.go +++ b/app/widget/base.go @@ -135,7 +135,7 @@ func (b *Base) Refresh(ctx app.Context) app.Widget { } if b.buffer == nil { - b.buffer = app.BufferFor(ctx, b.Bounds().Size()) + b.buffer = app.BufferFor(ctx.Display().ColorModel(), b.Bounds().Size()) } return b.super() diff --git a/app/widget/image.go b/app/widget/image.go index a6b0752..de47710 100644 --- a/app/widget/image.go +++ b/app/widget/image.go @@ -3,6 +3,7 @@ package widget import ( "image" "image/color" + "image/draw" "github.com/makeworld-the-better-one/dither/v2" "github.com/nfnt/resize" @@ -56,11 +57,6 @@ func (w *Image) Update(image image.Image, model color.Model) app.Widget { func (w *Image) Refresh(ctx app.Context) bool { log := logrus.WithField("widget", w.String()) - if !w.IsVisible() { - log.Trace("not refreshing; not visisble") - return false - } - if !w.changed.CompareAndSwap(true, false) { log.Trace("not refreshing; not changed") return false @@ -116,4 +112,28 @@ func convertImageFor(model color.Model, image image.Image) image.Image { return d.Dither(image) } +type Canvas struct { + Base +} + +func NewCanvas(size image.Point, model color.Model) *Canvas { + w := new(Canvas) + w.buffer = app.BufferFor(model, size) + w.Extend(w) + return w +} + +func (w *Canvas) Buffer() draw.Image { + return w.buffer +} + +func (w *Canvas) String() string { + return "canvas:" + w.buffer.Bounds().Size().String() +} + +func (w *Canvas) Refresh(ctx app.Context) bool { + app.ImageCopy(ctx.Display(), w.buffer) + return true +} + var _ app.Widget = (*Image)(nil) diff --git a/app/widget/menu.go b/app/widget/menu.go index ceb3bc0..971d10b 100644 --- a/app/widget/menu.go +++ b/app/widget/menu.go @@ -55,7 +55,7 @@ func (w *Menu) Refresh(ctx app.Context) bool { if w.changed.CompareAndSwap(true, false) || w.buffer == nil || (w.curr != w.next && w.next != nil) { if w.buffer == nil { - w.buffer = app.BufferFor(ctx, size) + w.buffer = app.BufferFor(ctx.Display().ColorModel(), size) w.Root.SetPageSize(size.Y / 16) } diff --git a/app/widget/spectrum.go b/app/widget/spectrum.go index fad5b64..b259c94 100644 --- a/app/widget/spectrum.go +++ b/app/widget/spectrum.go @@ -122,7 +122,7 @@ func (w *Spectrum) Refresh(ctx app.Context) bool { bounds := w.bounds.Load() if w.changed.CompareAndSwap(true, false) { if w.buffer == nil { - w.buffer = app.BufferFor(ctx, bounds.Size()) + w.buffer = app.BufferFor(ctx.Display().ColorModel(), bounds.Size()) app.ImageCopy(w.buffer, w.styles.Window.Background()) } diff --git a/app/widget/text.go b/app/widget/text.go index 4b7bf75..5a3a83a 100644 --- a/app/widget/text.go +++ b/app/widget/text.go @@ -120,7 +120,7 @@ func (w *Text) Refresh(ctx app.Context) bool { // Resize buffer if required. if w.buffer == nil || resize.X+resize.Y > 0 { log.WithField("size", resize).Debug("allocating new buffer") - w.buffer = app.BufferFor(ctx, resize) + w.buffer = app.BufferFor(ctx.Display().ColorModel(), resize) } // Fill background. diff --git a/context.go b/context.go index 1bd2d81..2933275 100644 --- a/context.go +++ b/context.go @@ -168,23 +168,34 @@ func (c *controllerContext) handleInput(event hardware.InputEvent) { switch event := event.(type) { case hardware.RotaryEvent: for _, layout := range c.layouts { - if handler, ok := layout.(app.RotaryInputHandler); ok { - log.WithField("layout", layout.String()).Debug("dispatch rotary input") - handler.HandleRotary(c, event.Delta) + if layout.IsVisible() { + if handler, ok := layout.(app.RotaryInputHandler); ok { + log.WithField("layout", layout.String()).Debug("dispatch rotary input") + handler.HandleRotary(c, event.Delta) + } } } case hardware.RotaryPressEvent: + var handled bool for _, layout := range c.layouts { - if handler, ok := layout.(app.ButtonInputHandler); ok { - log.WithField("layout", layout.String()).Debug("dispatch button press") - handler.HandleButtonPress(c) + if layout.IsVisible() { + if handler, ok := layout.(app.ButtonInputHandler); ok { + log.WithField("layout", layout.String()).Debug("dispatch button press") + handler.HandleButtonPress(c) + handled = true + } } } + if !handled { + c.handleRotaryPress() + } case hardware.RotaryLongPressEvent: for _, layout := range c.layouts { - if handler, ok := layout.(app.ButtonInputHandler); ok { - log.WithField("layout", layout.String()).Debug("dispatch button long press") - handler.HandleButtonLongPress(c) + if layout.IsVisible() { + if handler, ok := layout.(app.ButtonInputHandler); ok { + log.WithField("layout", layout.String()).Debug("dispatch button long press") + handler.HandleButtonLongPress(c) + } } } default: @@ -192,4 +203,29 @@ func (c *controllerContext) handleInput(event hardware.InputEvent) { } } +func (c *controllerContext) handleRotaryPress() { + log := logrus.NewEntry(logrus.StandardLogger()) + log.Debug("no handler for rotary press event, switching layouts") + + var activeIndex int + for i, layout := range c.layouts { + if layout == c.active { + activeIndex = i + break + } + } + if activeIndex == 0 { + // We're in settings, shouldn't be able to get here since settings handle rotary press events + log.Debug("huh? how did we get here?") + return + } + + nextIndex := activeIndex + 1 + if nextIndex >= len(c.layouts) { + nextIndex = 1 + } + + c.Show(c.layouts[nextIndex]) +} + var _ app.Context = (*controllerContext)(nil) diff --git a/controller.go b/controller.go index 7f4396b..792baaf 100644 --- a/controller.go +++ b/controller.go @@ -107,6 +107,8 @@ func (c *Controller) configureInputs(log *logrus.Entry, settings *config.Input) func (c *Controller) configureApps(_ *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.Shader(&c.ctx)) c.ctx.Show(nil) return