Silly walrus

This commit is contained in:
2026-02-02 19:09:15 +02:00
parent effa814f41
commit 007e4d7e00
2 changed files with 84 additions and 84 deletions

View File

@@ -10,9 +10,8 @@ import (
// Player handles alarm sound playback by shelling out to system audio tools.
type Player struct {
mu sync.Mutex
cmd *exec.Cmd
playing bool
stopCh chan struct{}
doneCh chan struct{}
}
func New() *Player {
@@ -61,104 +60,105 @@ func findPlayer() (string, func(string) []string) {
// 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() // kill any previous playback and wait for it to finish
p.stop()
p.mu.Lock()
p.stopCh = make(chan struct{})
p.doneCh = make(chan struct{})
stopCh := p.stopCh
doneCh := p.doneCh
p.mu.Unlock()
resolved := resolveSound(path)
if resolved == "" {
// No sound file — bell loop
p.stopCh = make(chan struct{})
p.playing = true
go func() {
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
os.Stdout.WriteString("\a")
for {
select {
case <-p.stopCh:
return
case <-ticker.C:
os.Stdout.WriteString("\a")
}
}
}()
go p.bellLoop(stopCh, doneCh)
return
}
name, argsFn := findPlayer()
if name == "" {
os.Stdout.WriteString("\a")
close(doneCh)
return
}
p.stopCh = make(chan struct{})
p.playing = true
go p.audioLoop(name, argsFn, resolved, stopCh, doneCh)
}
// audioLoop runs the audio player in a loop. The goroutine fully owns the
// process — no other goroutine touches cmd. Coordination is only via channels.
func (p *Player) audioLoop(name string, argsFn func(string) []string, path string, stopCh, doneCh chan struct{}) {
defer close(doneCh)
go func() {
for {
// Check if we should stop before starting a new process
select {
case <-p.stopCh:
case <-stopCh:
return
default:
}
cmd := exec.Command(name, argsFn(resolved)...)
cmd := exec.Command(name, argsFn(path)...)
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:
if err := cmd.Start(); err != nil {
return
default:
}
}
// Brief pause between loops to avoid hammering
// Wait for either the process to finish or a stop signal
processDone := make(chan error, 1)
go func() {
processDone <- cmd.Wait()
}()
select {
case <-p.stopCh:
case <-stopCh:
// Kill the process and wait for it to exit
_ = cmd.Process.Kill()
<-processDone
return
case <-processDone:
// Process finished naturally, loop again after a brief pause
}
// Pause between loops
select {
case <-stopCh:
return
case <-time.After(300 * time.Millisecond):
}
}
}()
}
// Stop stops any currently playing sound.
func (p *Player) bellLoop(stopCh, doneCh chan struct{}) {
defer close(doneCh)
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
os.Stdout.WriteString("\a")
for {
select {
case <-stopCh:
return
case <-ticker.C:
os.Stdout.WriteString("\a")
}
}
}
// Stop stops any currently playing sound and waits for the playback goroutine to exit.
func (p *Player) Stop() {
p.mu.Lock()
defer p.mu.Unlock()
p.stop()
}
func (p *Player) stop() {
if p.stopCh != nil {
close(p.stopCh)
stopCh := p.stopCh
doneCh := p.doneCh
p.stopCh = nil
}
if p.cmd != nil && p.cmd.Process != nil {
_ = p.cmd.Process.Kill()
_ = p.cmd.Wait()
p.cmd = nil
}
p.playing = false
}
p.doneCh = nil
p.mu.Unlock()
func (p *Player) IsPlaying() bool {
p.mu.Lock()
defer p.mu.Unlock()
return p.playing
if stopCh != nil {
close(stopCh)
}
if doneCh != nil {
<-doneCh // wait for goroutine to actually finish
}
}

BIN
woke

Binary file not shown.