-
Notifications
You must be signed in to change notification settings - Fork 30
/
TapTempo.hpp
53 lines (50 loc) · 1.11 KB
/
TapTempo.hpp
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
#ifndef __TapTempo_hpp__
#define __TapTempo_hpp__
// #define TAP_THRESHOLD 64// 256 // 78Hz at 20kHz sampling rate, or 16th notes at 293BPM
template<int TRIGGER_LIMIT>
class TapTempo {
private:
uint32_t limit;
uint32_t trig;
uint16_t speed;
bool ison;
public:
TapTempo(uint32_t tempo) :
limit(tempo), trig(TRIGGER_LIMIT),
speed(2048), ison(false) {}
void trigger(bool on){
trigger(on, 0);
}
void trigger(bool on, int delay){
// if(trig < TAP_THRESHOLD)
// return;
if(on && !ison){
if(trig < TRIGGER_LIMIT){
limit = trig + delay;
}
trig = 0;
// debugMessage("limit/delay", (int)limit, (int)delay);
}
ison = on;
}
void setSpeed(int16_t s){
if(abs(speed-s) > 16){
int64_t delta = (int64_t)limit*(speed-s)/2048;
limit = max(1, limit+delta);
speed = s;
}
}
float getPeriod(){
return float(limit)/TRIGGER_LIMIT;
}
void clock(){
if(trig < TRIGGER_LIMIT)
trig++;
}
void clock(uint32_t steps){
trig += steps;
if(trig > TRIGGER_LIMIT)
trig = TRIGGER_LIMIT;
}
};
#endif // __TapTempo_hpp__