Files
8bitMyGoUI/media/audioplayer.go
T
ZacharyZhang-NY 0e21870b64 Add media components 346-365; input.SlideBase, guarded Pad
An audio player with transport, scrubber and volume; an equalizer and
a visualizer; a playlist; a cassette, cartridge and record that turn
while they play and hold where they stop; piano keys, a step sequencer
and a tracker you can type into; a waveform, a 16:9 video frame, an
image compare, a gallery, a filmstrip, a record button and an FM tuner.

input exports SlideBase, the undrawn rail its sliders share; the
gallery treats exported Bases as building blocks, not components.
pixel's micro font gains '#' for notes like C#4. pixel.Pad and the
slide rail read input only while their base is enabled, so Inert or a
fresh Disabled stops a drag or a key already sent. AGENTS records why
a drawn container keeps the DISABLED description: MyGo paints a
disabled subtree at half opacity.
2026-10-12 06:17:12 -04:00

66 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package media
import (
"log/slog"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/layout"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// Player is what an AudioPlayer holds: play, place and volume.
type Player struct {
Playing bool
At time.Duration
// Level is 0–8 volume bars.
Level int
Muted bool
}
type AudioPlayerOptions struct {
Disabled bool
}
type AudioPlayerResult struct {
Element *ui.Element
// Changed is play, seek or volume; others are presses.
Changed, Stopped, Previous, Next bool
}
// AudioPlayer shows a track over transport, scrubber and volume.
func AudioPlayer(c *ui.Context, text string, t Track, pl *Player, o AudioPlayerOptions) AudioPlayerResult {
k := pixel.K(c)
var res AudioPlayerResult
res.Element = layout.Panel(c, layout.PanelOptions{Style: pixel.Raised}, func() {
// An LCD shows what plays, ♪ while it does.
ui.Box(c).FillWidth().Height(k.D(2*pixel.LineHeight + 6)).Role(ui.RoleText).Label(t.Title + ", " + t.Artist).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Frame(0, 0, g.W, g.H, pixel.Sunken, pixel.Well)
x := 4
if pl.Playing {
g.Text(x, 3, "♪", pixel.Accent, 1)
x += 10
}
g.Text(x, 3, pixel.Wrap(t.Title, g.W-x-4, 1)[0], pixel.Ink, 1)
g.Text(4, 3+pixel.LineHeight, pixel.Wrap(t.Artist, g.W-8, 1)[0], pixel.Faint, 1)
})
seek := Scrubber(c, k.Msg("seek"), &pl.At, t.Length, ScrubberOptions{Disabled: o.Disabled}).Changed
ui.Row(c).Gap(k.D(8)).AlignItems(ui.Center).Children(func() {
tr := Transport(c, text, &pl.Playing, TransportOptions{Disabled: o.Disabled})
// Stop goes back to the start.
if tr.Stopped {
pl.At = 0
}
vol := Volume(c, k.Msg("volume"), &pl.Level, &pl.Muted, VolumeOptions{Disabled: o.Disabled}).Changed
res.Changed = seek || tr.Changed || vol || tr.Stopped
res.Stopped, res.Previous, res.Next = tr.Stopped, tr.Previous, tr.Next
})
}).Gap(k.D(4)).Role(ui.RoleGroup).Label(text)
if res.Changed || res.Previous || res.Next {
slog.Debug("8bit: audio player", "label", text, "track", t.Title, "playing", pl.Playing, "at", pl.At)
}
return res
}