-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
119 lines (95 loc) · 2.05 KB
/
app.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"context"
"embed"
"log"
"os"
"path/filepath"
"time"
"github.com/gen2brain/beeep"
"github.com/gopxl/beep/v2/mp3"
"github.com/gopxl/beep/v2/speaker"
)
//go:embed files/audio.mp3
var file embed.FS
//go:embed files/icon.png
var iconFile embed.FS
// App struct
type App struct {
ctx context.Context
done chan bool
tempFiles []string
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{
tempFiles: []string{},
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) shutdown(ctx context.Context) {
a.cleanup()
}
func (a *App) cleanup() {
for _, file := range a.tempFiles {
os.Remove(file)
}
}
func (a *App) extractFile(embedFS embed.FS, path string) (string, error) {
data, err := embedFS.ReadFile(path)
if err != nil {
return "", err
}
tmpFile, err := os.CreateTemp("", filepath.Base(path))
if err != nil {
return "", err
}
if _, err := tmpFile.Write(data); err != nil {
tmpFile.Close()
return "", err
}
tmpFile.Close()
a.tempFiles = append(a.tempFiles, tmpFile.Name())
return tmpFile.Name(), nil
}
func (a *App) Notify() {
var iconPath string
var err error
if len(a.tempFiles) > 0 {
iconPath = a.tempFiles[0]
} else {
iconPath, err = a.extractFile(iconFile, "files/icon.png")
}
if err != nil {
beeep.Notify("Time is up!", "The timer you set has finished!", "")
} else {
beeep.Notify("Time is up!", "The timer you set has finished!", iconPath)
}
f, err := file.Open("files/audio.mp3")
if err != nil {
log.Fatal(err)
return
}
streamer, format, err := mp3.Decode(f)
if err != nil {
log.Fatal(err)
return
}
speaker.Init(format.SampleRate, format.SampleRate.N((time.Second / 10)))
a.done = make(chan bool)
speaker.Play(streamer)
<-a.done // Wait until channel closes which is when playback is done
speaker.Clear()
streamer.Close()
f.Close()
}
func (a *App) Stop() {
if a.done != nil {
close(a.done)
a.done = nil
}
}