-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
backgroundnoiseitem.go
79 lines (66 loc) · 1.2 KB
/
backgroundnoiseitem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package noizy
import (
"io/fs"
"log/slog"
"fyne.io/systray"
"github.com/gopxl/beep"
"github.com/gopxl/beep/speaker"
"github.com/gopxl/beep/vorbis"
)
type BackgroundNoiseItem struct {
*beep.Ctrl
*systray.MenuItem
fs.File
SoundTitle string
}
func (b *BackgroundNoiseItem) OnClick() {
item := b.MenuItem
for range item.ClickedCh {
b.Toggle()
}
}
func (b *BackgroundNoiseItem) Toggle() {
item := b.MenuItem
if item.Checked() {
b.ToggleStop()
} else {
b.TogglePlay()
}
}
func (b *BackgroundNoiseItem) TogglePlay() {
item := b.MenuItem
if item.Checked() {
return
}
slog.Debug("Toggle play for sound", "sound", b.SoundTitle)
b.Play()
item.Check()
}
func (b *BackgroundNoiseItem) ToggleStop() {
item := b.MenuItem
if !item.Checked() {
return
}
slog.Debug("Toggle stop for sound", "sound", b.SoundTitle)
b.Stop()
item.Uncheck()
}
func (b *BackgroundNoiseItem) Play() {
streamer, _, err := vorbis.Decode(b.File)
if err != nil {
panic(err)
}
ctrl := &beep.Ctrl{
Streamer: beep.Loop(-1, streamer),
Paused: false,
}
b.Ctrl = ctrl
speaker.Play(ctrl)
}
func (b *BackgroundNoiseItem) Stop() {
if b.Ctrl != nil {
speaker.Lock()
b.Ctrl.Paused = true
speaker.Unlock()
}
}