-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathding.go
78 lines (67 loc) · 1.65 KB
/
ding.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"time"
"github.com/acmujica/ding/audio"
"github.com/faiface/beep"
"github.com/faiface/beep/speaker"
"github.com/faiface/beep/wav"
)
//go:generate go-bindata -pkg audio -o audio/audio.go audio/
func main() {
// Default measurement is seconds parse for alternative
isHours := flag.Bool("h", false, "Sets the measurement to seconds")
isMinutes := flag.Bool("m", false, "Sets the measurement to minutes")
wantsLouder := flag.Bool("l", false, "Adds a louder alarm")
flag.Parse()
numberFlt, err := strconv.ParseFloat(os.Args[len(os.Args)-1], 64)
if err != nil {
fmt.Println("Could not read value for timer.")
}
// Create wav file
data, err := audio.Asset("audio/hand-bell.wav")
if err != nil {
fmt.Println("Could not open bell file.")
}
if *wantsLouder {
data, err = audio.Asset("audio/alarm.wav")
if err != nil {
fmt.Println("Could not open bell file.")
}
}
f, _ := ioutil.TempFile("", "audio")
defer f.Close()
filePath := f.Name()
_, err = f.Write(data)
if err != nil {
log.Fatal(err)
}
// Run Timer
var waitTime time.Duration
switch {
case *isHours:
waitTime = time.Duration(numberFlt) * time.Hour
case *isMinutes:
waitTime = time.Duration(numberFlt) * time.Minute
default:
waitTime = time.Duration(numberFlt) * time.Second
}
timer := time.NewTimer(waitTime)
<-timer.C
// Play wav file
f, _ = os.Open(filePath)
s, format, _ := wav.Decode(f)
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
done := make(chan struct{})
speaker.Play(beep.Seq(s, beep.Callback(func() {
close(done)
})))
<-done
// Cleanup
os.Remove(filePath)
}