-
Notifications
You must be signed in to change notification settings - Fork 2
/
detector.go
65 lines (51 loc) · 1.34 KB
/
detector.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
package precise
import "math"
type TriggerOption func(*TriggerDetector)
// WithSensitivity sets the detector sensitivity
func WithSensitivity(sensitivity float32) TriggerOption {
return func(t *TriggerDetector) {
t.sensitivity = sensitivity
}
}
// WithTriggerLevel sets the number of triggers required to return a
// valid trigger
func WithTriggerLevel(triggerLevel int) TriggerOption {
return func(t *TriggerDetector) {
t.triggerLevel = triggerLevel
}
}
// NewTriggerDetector creates a new TriggerDetector
func NewTriggerDetector(chunkSize int, opts ...TriggerOption) *TriggerDetector {
t := &TriggerDetector{
chunkSize: chunkSize,
sensitivity: 0.5,
triggerLevel: 3,
}
for _, opt := range opts {
opt(t)
}
return t
}
type TriggerDetector struct {
chunkSize int
sensitivity float32
triggerLevel int
activation int
}
// Update adds the current probability to the detection history
func (t *TriggerDetector) Update(prob float32) bool {
chunkActivated := prob > 1.0-t.sensitivity
if chunkActivated || t.activation < 0 {
t.activation += 1
hasActivated := t.activation > t.triggerLevel
if hasActivated || chunkActivated && t.activation < 0 {
t.activation = int(math.Floor(float64(-(8 * 2048) / t.chunkSize)))
}
if hasActivated {
return true
}
} else if t.activation > 0 {
t.activation -= 1
}
return false
}