-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (54 loc) · 1.14 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
package main
import (
"fmt"
"os"
"time"
"golang.org/x/term"
)
type BPMCalculator struct {
PrevTime time.Time
Count int
TotalTime float64
BPM float64
}
func bpmCalculator() *BPMCalculator {
return &BPMCalculator{}
}
func (b *BPMCalculator) updateBpm() {
if b.Count > 0 {
elapsedTime := time.Since(b.PrevTime).Seconds()
b.BPM = 60 / elapsedTime
b.TotalTime += elapsedTime
}
b.PrevTime = time.Now()
b.Count++
}
func main() {
fmt.Println("Press any key (press 'q' to terminate):")
bpmCalculator := bpmCalculator()
// Disable echoing of input
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
fmt.Println("Error:", err)
return
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
for {
var buffer [1]byte
_, err := os.Stdin.Read(buffer[:])
if err != nil {
fmt.Println("Error reading input:", err)
break
}
if buffer[0] == 'q' {
fmt.Println("Terminating...")
break
}
bpmCalculator.updateBpm()
if bpmCalculator.Count > 1 {
averageBPM := int(float64(bpmCalculator.Count-1) / bpmCalculator.TotalTime * 60)
fmt.Printf("\rAverage BPM: %d", averageBPM)
}
}
fmt.Println()
}