-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
111 lines (90 loc) · 2.04 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
)
var (
musicSheet []byte
)
func init() {
var err error
filename := flag.String("music_sheet", "", "the music sheet to load and play")
beServer := flag.Bool("server", false, "run a multi-hand server")
connectTo := flag.String("connect", "", "the IP of a multi-hand server to connect to")
flag.Parse()
if *filename == "" && !*beServer {
webServer, _ := NewWebServer("website/")
fmt.Println("Running the web server on port 8080")
fmt.Println("Run 'jam -h' if you don't want that")
webServer.OpenBrowser()
webServer.Run()
os.Exit(0)
}
if *beServer {
serv, err := NewSyncServer(":1234")
if err != nil {
fmt.Println("Error creating server:")
panic(err)
}
fmt.Println("Waiting for clients on *:1234\nPress Ctrl-C to start the music")
go serv.WaitForClients()
// Create the handler for SIGINT
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
serv.StopWaiting()
fmt.Println("Let's goooooo")
serv.StartMusic()
os.Exit(0)
} else if *connectTo != "" {
client, err := NewSyncClient(*connectTo)
if err != nil {
fmt.Println("Error creating client:")
panic(err)
}
client.Wait()
fmt.Println("Playing!")
}
musicSheet, err = ioutil.ReadFile(*filename)
if err != nil {
panic(err)
}
}
func main() {
var err error
fmt.Println("beep-jam 0.1")
s := string(musicSheet[:])
jammer, err := NewJammer(s)
if err != nil {
panic(err)
}
// Create the handler for SIGINT
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Println("\n\nGot Ctrl-C'd")
jammer.beeper.Beep(0.0, 1)
os.Exit(1)
}()
// Play the song!
numLines := len(jammer.Lines)
for i := 0; i < numLines; i++ {
progress := int(float64(i) / float64(numLines) * 100)
fmt.Printf("\r[")
for j := 0; j < progress-1; j++ {
fmt.Printf("=")
}
fmt.Printf(">")
for j := progress; j < 100; j++ {
fmt.Printf(" ")
}
fmt.Printf("] %d%% ", progress)
jammer.PlayNext()
jammer.CurrentLine++
}
fmt.Print("\n")
}