-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeasureTime.h
84 lines (76 loc) · 1.48 KB
/
MeasureTime.h
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
// Code by L.Geerinckx 28-1-2021 | https://github.com/PierreIsCoding
class MeasureTime
{
private:
int samples;
unsigned long currentMillis;
unsigned long previousMillis = 0;
int *sampleBank;
int index = 0;
unsigned long minTap; // minimal tap tempo 10 BPM
public:
MeasureTime(int,int);
void addSample();
void on();
int getTiming();
void reset();
};
MeasureTime::MeasureTime(int _samples, int _minTap=60000)
{
minTap=_minTap;
samples = _samples;
sampleBank = new int[_samples];
reset();
previousMillis = millis();
}
void MeasureTime::addSample()
{
currentMillis = millis();
int timeDif = currentMillis - previousMillis;
if (timeDif <= minTap && timeDif > 0) // check if timing is ok
{
sampleBank[index] = timeDif;
if (index < samples - 1)
{
index++;
}
else
{
index = 0;
}
}
else
{
// else reset samplebank
reset();
}
previousMillis = currentMillis;
}
void MeasureTime::reset()
{
for (int i = 0; i < samples; i++)
{
sampleBank[index] = 0;
}
}
int MeasureTime::getTiming()
{
int total = 0;
int count = 0;
for (int i = 0; i < samples; i++)
{
if (sampleBank[i] > 0)
{
total += sampleBank[i];
count++;
}
}
if (count != 0)
{
return 60 * count / (total * 0.001);
}
else
{
return -1;
}
}