-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphone.go
81 lines (68 loc) · 1.74 KB
/
phone.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
package main
import (
"log"
"math/rand"
"os"
"os/signal"
"path/filepath"
"sync"
"time"
"github.com/holgerjh/halloween-phone/config"
"github.com/holgerjh/halloween-phone/player"
"github.com/holgerjh/halloween-phone/pulse"
"github.com/holgerjh/halloween-phone/tracks"
)
func main() {
log.Println("Halloween Phone started")
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
config_path := filepath.Join(home, ".config", "halloween-phone.cfg")
cfg, err := config.LoadConfig(config_path)
if err != nil {
panic(err)
}
log.Printf("Running with config %+v", cfg)
rand.Seed(int64(time.Now().UnixMilli()))
log.Printf("Initializing pulseaudio")
pulseData, err := pulse.SetupPulseDevice(cfg.Mic)
if err != nil {
panic(err)
}
log.Printf("Loading track db")
db, err := tracks.LoadTracksIntoNewDB(cfg.TrackFolder)
if err != nil {
panic(err)
}
log.Printf("Loaded track db")
shutdown := make(chan int)
wg := sync.WaitGroup{}
startPlayThread(pulseData.VirtualMicPath, &wg, db, cfg, shutdown)
blockUntilSigINTandSendTerminate(shutdown, &wg)
log.Printf("Cleaning up pulse audio")
err = pulse.UnloadPulse(pulseData, cfg.Mic)
if err != nil {
panic(err)
}
log.Printf("All done, goodbye!")
}
func blockUntilSigINTandSendTerminate(shutdown chan int, wg *sync.WaitGroup) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
for range c {
log.Printf("Caught SIGINT, terminating")
close(shutdown)
break
}
log.Printf("Waiting for child threads to terminate")
wg.Wait()
}
func startPlayThread(micFile string, wg *sync.WaitGroup, db *tracks.TrackDB, cfg *config.Config, shutdown <-chan int) {
log.Printf("Starting player thread")
wg.Add(1)
go func() {
player.Loop(micFile, db, cfg, shutdown)
wg.Done()
}()
}