Gallery

Signal studio

A live waveform you shape with sliders, in any of ten themes. Pause it and the app draws nothing at all.

Signal studio: a still of the demo

Run it on your machine:

go run github.com/amken3d/immygo/examples/signal@latest

Source

signal/signal/signal.go
// Package signal is Signal studio: a live waveform you shape with sliders,
// in any of ImmyGo's ten themes. It is a package so that other programs
// (the immygo.app demo bundle) can run it; main.go runs it.
package signal
import (
"fmt"
"math"
"time"
"github.com/amken3d/immygo/theme"
"github.com/amken3d/immygo/ui"
)
// Main runs Signal studio in a window. It blocks until the window closes.
func Main() {
build, themeRef := New()
ui.Run("Signal studio", build, ui.Size(960, 600), ui.WithThemeRef(themeRef))
}
// New builds the app. While the signal runs, each frame asks for the
// next; paused, the app draws nothing until you touch it.
func New() (build func() ui.View, themeRef *ui.ThemeRefValue) {
themeRef = ui.NewThemeRef(themes[0].new())
names := make([]string, len(themes))
for i, t := range themes {
names[i] = t.name
}
picker := ui.Dropdown(names...).SetSelected(0).
OnSelect(func(i int, _ string) { themeRef.Set(themes[i].new()) })
freq := ui.Slider(1, 8, 3)
amp := ui.Slider(0.1, 1, 0.8)
pause := ui.Toggle(false)
var t float64 // seconds the signal has run, so pausing freezes it in place
last := time.Now()
build = func() ui.View {
now := time.Now()
if !pause.Value() {
t += now.Sub(last).Seconds()
ui.Invalidate()
}
last = now
f, a := float64(freq.Value()), float64(amp.Value())
status := ui.Badge("live")
if pause.Value() {
status = ui.Badge("paused · drawing nothing").Secondary()
}
return ui.VStack(
ui.HStack(ui.Text("Signal studio").Title(), status, ui.Spacer(), picker).Spacing(12).Center(),
ui.Flex(1, ui.Card(ui.Expanded(
ui.Polyline(600, func(i int) (x, y float32) {
x = float32(i) / 599
return x, float32(0.5 - 0.45*wave(f, a, t, float64(x)))
}).StrokeWidth(2.5),
))),
ui.HStack(label("Frequency"), ui.Flex(1, freq), ui.Text(fmt.Sprintf("%.1f Hz", f)).Width(56)).Spacing(12).Center(),
ui.HStack(label("Amplitude"), ui.Flex(1, amp), ui.Text(fmt.Sprintf("%.0f%%", a*100)).Width(56)).Spacing(12).Center(),
ui.HStack(label("Pause"), pause).Spacing(12).Center(),
).Spacing(16).Padding(24)
}
return build, themeRef
}
// label is a control's name, in a fixed width so the controls line up.
func label(s string) ui.View { return ui.Text(s).Width(88) }
// wave is the signal at x (0 to 1 across the view) after t seconds: a
// tone and its third harmonic, scaled to amp.
func wave(freq, amp, t, x float64) float64 {
return amp * (0.75*math.Sin(2*math.Pi*freq*x-3*t) + 0.25*math.Sin(6*math.Pi*freq*x+5*t))
}
var themes = []struct {
name string
new func() *theme.Theme
}{
{"Catppuccin Mocha", theme.CatppuccinMocha},
{"Catppuccin Latte", theme.CatppuccinLatte},
{"Fluent Dark", theme.FluentDark},
{"Fluent Light", theme.FluentLight},
{"Nord Dark", theme.NordDark},
{"Nord Light", theme.NordLight},
{"Solarized Dark", theme.SolarizedDark},
{"Solarized Light", theme.SolarizedLight},
{"Material Dark", theme.MaterialDark},
{"Material Light", theme.MaterialLight},
}