forked from gethiox/jack-peak-meter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoit.go
97 lines (74 loc) · 1.64 KB
/
doit.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
package main
import (
"fmt"
"time"
mid "jack-peak-meter/midi"
"github.com/pkg/errors"
)
// const volumeThreshold = 10
// const lowThreshold = 5
const volumeThreshold = 60
// const volumeThreshold = 70
const lowThreshold = 40
// if the new note is higher than the previous one, and it's been more than 200 ms since the last increase, this is a peak. if it's also above some threshold
const waitTime = 200 * time.Millisecond
var lastTimePeak *time.Time
var lastTimeValley *time.Time
var hitCount = 0
var t = time.Now()
var storedValues = []float32{}
func processFrames(value float32, percentage int) {
storedValues = append(storedValues, value)
// fmt.Println(value)
now := time.Now()
if lastTimePeak == nil {
if percentage >= volumeThreshold {
lastTimePeak = &now
hit(percentage)
hitCount++
// ptof(hitCount)
}
return
}
if time.Since(*lastTimePeak) < waitTime {
return
}
if lastTimeValley == nil {
if percentage < lowThreshold {
lastTimeValley = &now
}
return
}
if time.Since(*lastTimeValley) < waitTime {
return
}
diff := lastTimePeak.Sub(*lastTimeValley)
waitingForValley := diff > 0
if waitingForValley {
if percentage < lowThreshold {
// fmt.Println(percentage)
lastTimeValley = &now
}
return
}
if percentage >= volumeThreshold {
lastTimePeak = &now
hit(percentage)
hitCount++
// ptof(hitCount)
}
}
func hit(percentage interface{}) {
go func() {
err := mid.PlayNotes(midiOut, []mid.MidiNote{
{
Note: 50,
Velocity: 127,
},
}, time.Millisecond*10)
if err != nil {
fmt.Println(errors.Wrap(err, "error playing midi note"))
}
// ptof(percentage)
}()
}