Filthy aphid
This commit is contained in:
134
player/player.go
134
player/player.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Player handles alarm sound playback by shelling out to system audio tools.
|
// Player handles alarm sound playback by shelling out to system audio tools.
|
||||||
@@ -11,21 +12,21 @@ type Player struct {
|
|||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
cmd *exec.Cmd
|
cmd *exec.Cmd
|
||||||
playing bool
|
playing bool
|
||||||
|
stopCh chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Player {
|
func New() *Player {
|
||||||
return &Player{}
|
return &Player{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Play starts playing a sound file. If path is "default", it uses a system beep.
|
// resolveSound finds the actual file path for a sound. Returns "" if no file found.
|
||||||
func (p *Player) Play(path string) {
|
func resolveSound(path string) string {
|
||||||
p.mu.Lock()
|
if path != "default" && path != "" {
|
||||||
defer p.mu.Unlock()
|
if _, err := os.Stat(path); err == nil {
|
||||||
|
return path
|
||||||
p.stop()
|
}
|
||||||
|
return ""
|
||||||
if path == "default" || path == "" {
|
}
|
||||||
// Try common system sounds, fall back to terminal bell
|
|
||||||
candidates := []string{
|
candidates := []string{
|
||||||
"/usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga",
|
"/usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga",
|
||||||
"/usr/share/sounds/gnome/default/alerts/bark.ogg",
|
"/usr/share/sounds/gnome/default/alerts/bark.ogg",
|
||||||
@@ -33,50 +34,107 @@ func (p *Player) Play(path string) {
|
|||||||
}
|
}
|
||||||
for _, c := range candidates {
|
for _, c := range candidates {
|
||||||
if _, err := os.Stat(c); err == nil {
|
if _, err := os.Stat(c); err == nil {
|
||||||
path = c
|
return c
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if path == "default" || path == "" {
|
|
||||||
// No sound files found — use terminal bell in a loop
|
|
||||||
go func() {
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
os.Stdout.WriteString("\a")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// Try players in order of preference
|
// findPlayer returns the name and arg builder for the first available audio player.
|
||||||
|
func findPlayer() (string, func(string) []string) {
|
||||||
players := []struct {
|
players := []struct {
|
||||||
name string
|
name string
|
||||||
args func(string) []string
|
args func(string) []string
|
||||||
}{
|
}{
|
||||||
{"paplay", func(f string) []string { return []string{f} }},
|
{"paplay", func(f string) []string { return []string{f} }},
|
||||||
{"aplay", func(f string) []string { return []string{f} }},
|
{"aplay", func(f string) []string { return []string{f} }},
|
||||||
{"mpv", func(f string) []string { return []string{"--no-video", "--loop=inf", f} }},
|
{"mpv", func(f string) []string { return []string{"--no-video", "--no-terminal", f} }},
|
||||||
{"ffplay", func(f string) []string { return []string{"-nodisp", "-autoexit", "-loop", "0", f} }},
|
{"ffplay", func(f string) []string { return []string{"-nodisp", "-autoexit", f} }},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pl := range players {
|
for _, pl := range players {
|
||||||
if _, err := exec.LookPath(pl.name); err == nil {
|
if _, err := exec.LookPath(pl.name); err == nil {
|
||||||
p.cmd = exec.Command(pl.name, pl.args(path)...)
|
return pl.name, pl.args
|
||||||
p.cmd.Stdout = nil
|
}
|
||||||
p.cmd.Stderr = nil
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PlayLoop starts playing a sound file in a loop until Stop() is called.
|
||||||
|
func (p *Player) PlayLoop(path string) {
|
||||||
|
p.mu.Lock()
|
||||||
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
|
p.stop()
|
||||||
|
|
||||||
|
resolved := resolveSound(path)
|
||||||
|
if resolved == "" {
|
||||||
|
// No sound file — bell loop
|
||||||
|
p.stopCh = make(chan struct{})
|
||||||
p.playing = true
|
p.playing = true
|
||||||
go func() {
|
go func() {
|
||||||
_ = p.cmd.Run()
|
ticker := time.NewTicker(2 * time.Second)
|
||||||
p.mu.Lock()
|
defer ticker.Stop()
|
||||||
p.playing = false
|
os.Stdout.WriteString("\a")
|
||||||
p.mu.Unlock()
|
for {
|
||||||
|
select {
|
||||||
|
case <-p.stopCh:
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
os.Stdout.WriteString("\a")
|
||||||
|
}
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
name, argsFn := findPlayer()
|
||||||
|
if name == "" {
|
||||||
|
os.Stdout.WriteString("\a")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Absolute fallback: terminal bell
|
p.stopCh = make(chan struct{})
|
||||||
os.Stdout.WriteString("\a")
|
p.playing = true
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-p.stopCh:
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(name, argsFn(resolved)...)
|
||||||
|
cmd.Stdout = nil
|
||||||
|
cmd.Stderr = nil
|
||||||
|
|
||||||
|
p.mu.Lock()
|
||||||
|
p.cmd = cmd
|
||||||
|
p.mu.Unlock()
|
||||||
|
|
||||||
|
err := cmd.Run()
|
||||||
|
|
||||||
|
p.mu.Lock()
|
||||||
|
p.cmd = nil
|
||||||
|
p.mu.Unlock()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
// Check if we were told to stop
|
||||||
|
select {
|
||||||
|
case <-p.stopCh:
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Brief pause between loops to avoid hammering
|
||||||
|
select {
|
||||||
|
case <-p.stopCh:
|
||||||
|
return
|
||||||
|
case <-time.After(300 * time.Millisecond):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop stops any currently playing sound.
|
// Stop stops any currently playing sound.
|
||||||
@@ -87,12 +145,16 @@ func (p *Player) Stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) stop() {
|
func (p *Player) stop() {
|
||||||
if p.cmd != nil && p.cmd.Process != nil && p.playing {
|
if p.stopCh != nil {
|
||||||
|
close(p.stopCh)
|
||||||
|
p.stopCh = nil
|
||||||
|
}
|
||||||
|
if p.cmd != nil && p.cmd.Process != nil {
|
||||||
_ = p.cmd.Process.Kill()
|
_ = p.cmd.Process.Kill()
|
||||||
_ = p.cmd.Wait()
|
_ = p.cmd.Wait()
|
||||||
p.playing = false
|
|
||||||
p.cmd = nil
|
p.cmd = nil
|
||||||
}
|
}
|
||||||
|
p.playing = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) IsPlaying() bool {
|
func (p *Player) IsPlaying() bool {
|
||||||
|
|||||||
68
ui/model.go
68
ui/model.go
@@ -25,6 +25,8 @@ const (
|
|||||||
type tickMsg time.Time
|
type tickMsg time.Time
|
||||||
type alarmFiredMsg scheduler.AlarmEvent
|
type alarmFiredMsg scheduler.AlarmEvent
|
||||||
type alarmsLoadedMsg []db.Alarm
|
type alarmsLoadedMsg []db.Alarm
|
||||||
|
type snoozeFireMsg db.Alarm
|
||||||
|
type autoTimeoutMsg struct{}
|
||||||
|
|
||||||
// Model is the main bubbletea model.
|
// Model is the main bubbletea model.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
@@ -43,6 +45,8 @@ type Model struct {
|
|||||||
// Alarm firing state
|
// Alarm firing state
|
||||||
firingAlarm *db.Alarm
|
firingAlarm *db.Alarm
|
||||||
firingBlink bool
|
firingBlink bool
|
||||||
|
firingStart time.Time
|
||||||
|
snoozeCount int
|
||||||
|
|
||||||
// Form state
|
// Form state
|
||||||
form *formModel
|
form *formModel
|
||||||
@@ -113,11 +117,27 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
|
|
||||||
case alarmFiredMsg:
|
case alarmFiredMsg:
|
||||||
alarm := msg.Alarm
|
alarm := msg.Alarm
|
||||||
m.firingAlarm = &alarm
|
m.startFiring(&alarm)
|
||||||
m.firingBlink = true
|
|
||||||
m.player.Play(alarm.SoundPath)
|
|
||||||
m.refreshAlarms()
|
m.refreshAlarms()
|
||||||
return m, listenForAlarms(m.scheduler)
|
return m, tea.Batch(
|
||||||
|
listenForAlarms(m.scheduler),
|
||||||
|
autoTimeoutCmd(),
|
||||||
|
)
|
||||||
|
|
||||||
|
case snoozeFireMsg:
|
||||||
|
alarm := db.Alarm(msg)
|
||||||
|
m.snoozeCount++
|
||||||
|
m.startFiring(&alarm)
|
||||||
|
return m, autoTimeoutCmd()
|
||||||
|
|
||||||
|
case autoTimeoutMsg:
|
||||||
|
if m.firingAlarm != nil {
|
||||||
|
m.player.Stop()
|
||||||
|
m.statusMsg = "Alarm auto-dismissed after 5 minutes"
|
||||||
|
m.firingAlarm = nil
|
||||||
|
m.snoozeCount = 0
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
return m.handleKey(msg)
|
return m.handleKey(msg)
|
||||||
@@ -144,13 +164,16 @@ func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
switch key {
|
switch key {
|
||||||
case "enter", " ", "d":
|
case "enter", " ", "d":
|
||||||
m.player.Stop()
|
m.player.Stop()
|
||||||
|
alarm := m.firingAlarm
|
||||||
m.firingAlarm = nil
|
m.firingAlarm = nil
|
||||||
m.statusMsg = "Alarm dismissed"
|
m.snoozeCount = 0
|
||||||
|
m.statusMsg = fmt.Sprintf("Alarm '%s' dismissed", alarm.Name)
|
||||||
case "s":
|
case "s":
|
||||||
m.player.Stop()
|
m.player.Stop()
|
||||||
|
alarm := *m.firingAlarm
|
||||||
m.firingAlarm = nil
|
m.firingAlarm = nil
|
||||||
m.statusMsg = "Snoozed for 5 minutes (not yet implemented)"
|
m.statusMsg = fmt.Sprintf("Snoozed '%s' for 5 minutes", alarm.Name)
|
||||||
// TODO: implement snooze by creating a one-shot alarm 5min from now
|
return m, snoozeCmd(alarm, snoozeDuration)
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
@@ -224,6 +247,30 @@ func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const snoozeDuration = 5 * time.Minute
|
||||||
|
const autoTimeoutDuration = 5 * time.Minute
|
||||||
|
|
||||||
|
func (m *Model) startFiring(alarm *db.Alarm) {
|
||||||
|
m.firingAlarm = alarm
|
||||||
|
m.firingBlink = true
|
||||||
|
m.firingStart = time.Now()
|
||||||
|
m.player.PlayLoop(alarm.SoundPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func snoozeCmd(alarm db.Alarm, after time.Duration) tea.Cmd {
|
||||||
|
return func() tea.Msg {
|
||||||
|
time.Sleep(after)
|
||||||
|
return snoozeFireMsg(alarm)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func autoTimeoutCmd() tea.Cmd {
|
||||||
|
return func() tea.Msg {
|
||||||
|
time.Sleep(autoTimeoutDuration)
|
||||||
|
return autoTimeoutMsg{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Model) refreshAlarms() {
|
func (m *Model) refreshAlarms() {
|
||||||
alarms, err := m.store.ListAlarms()
|
alarms, err := m.store.ListAlarms()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -255,11 +302,14 @@ func (m Model) View() string {
|
|||||||
|
|
||||||
// Firing alarm overlay
|
// Firing alarm overlay
|
||||||
if m.firingAlarm != nil {
|
if m.firingAlarm != nil {
|
||||||
firingText := fmt.Sprintf("🔔 ALARM: %s 🔔", m.firingAlarm.Name)
|
firingText := fmt.Sprintf("ALARM: %s", m.firingAlarm.Name)
|
||||||
if m.firingAlarm.Description != "" {
|
if m.firingAlarm.Description != "" {
|
||||||
firingText += "\n" + m.firingAlarm.Description
|
firingText += "\n" + m.firingAlarm.Description
|
||||||
}
|
}
|
||||||
firingText += "\n\n[Enter/Space/d] Dismiss [s] Snooze"
|
if m.snoozeCount > 0 {
|
||||||
|
firingText += fmt.Sprintf("\n(snoozed %d time(s))", m.snoozeCount)
|
||||||
|
}
|
||||||
|
firingText += "\n\n[Enter/Space/d] Dismiss [s] Snooze 5min"
|
||||||
styled := AlarmFiringStyle.Render(firingText)
|
styled := AlarmFiringStyle.Render(firingText)
|
||||||
styled = lipgloss.PlaceHorizontal(m.width, lipgloss.Center, styled)
|
styled = lipgloss.PlaceHorizontal(m.width, lipgloss.Center, styled)
|
||||||
sections = append(sections, "", styled)
|
sections = append(sections, "", styled)
|
||||||
|
|||||||
Reference in New Issue
Block a user