This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.go
More file actions
72 lines (68 loc) · 1.53 KB
/
timer.go
File metadata and controls
72 lines (68 loc) · 1.53 KB
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
package main
import (
"strconv"
"strings"
"time"
)
func makeTiming(bpm string, step int, numEvents int) (timing, timingIncrement time.Duration, err error) {
var bpmA, bpmB int
var timingB time.Duration
bpmList := strings.Split(bpm, "-")
debugf("makeTiming(): bpmList: %v, numEvents: %v", bpmList, numEvents)
switch len(bpmList) {
case 1:
bpmA, err = strconv.Atoi(bpmList[0])
if err != nil {
return
}
case 2:
bpmA, err = strconv.Atoi(bpmList[0])
if err != nil {
return
}
bpmB, err = strconv.Atoi(bpmList[1])
if err != nil {
return
}
default:
return
}
timing = time.Minute / time.Duration(bpmA) / time.Duration(step/4)
debugf("makeTiming(): timing: %v, bpmA: %v, bpmB: %v", timing, bpmA, bpmB)
if (bpmA == bpmB) || bpmB == 0 {
timingIncrement = 0
} else {
timingB = time.Minute / time.Duration(bpmB) / time.Duration(step/4)
timingIncrement = (timingB - timing) / time.Duration(numEvents)
debugf("makeTiming(): timingB: %v, timingIncrement: %v", timingB, timingIncrement)
}
return
}
type varTicker struct {
C <-chan time.Time
ch chan<- time.Time
t *time.Ticker
done chan bool
}
func (t *varTicker) SetDuration(d time.Duration) {
if t.t != nil {
t.t.Stop()
close(t.done)
} else {
var ticker = make(chan time.Time)
t.C = ticker
t.ch = ticker
}
t.done = make(chan bool)
t.t = time.NewTicker(d)
go func(out chan<- time.Time, in <-chan time.Time, done <-chan bool) {
for {
select {
case tick := <-in:
out <- tick
case <-done:
return
}
}
}(t.ch, t.t.C, t.done)
}