OpenWareLaboratory
TapTempo.h
Go to the documentation of this file.
1 #ifndef __TapTempo_h__
2 #define __TapTempo_h__
3 
4 class TapTempo {
5 protected:
6  const float sr;
7  const size_t min_limit;
8  const size_t max_limit;
9  size_t samples;
10  size_t trig;
11  bool ison;
12 public:
13  TapTempo(float sr, size_t limit) :
14  sr(sr), min_limit(16), max_limit(limit), samples(limit/2), trig(limit), ison(false) {
15  }
16  TapTempo(float sr, size_t min, size_t max) :
17  sr(sr), min_limit(min), max_limit(max), samples(max/2), trig(max), ison(false) {
18  }
19  bool isOn(){
20  return ison;
21  }
22  void trigger(bool on){
23  trigger(on, 0);
24  }
25  void trigger(bool on, int delay){
26  if(on && !ison){
27  if(trig > min_limit && trig < max_limit){
28  samples = trig + delay;
29  }
30  trig = 0;
31  }
32  ison = on;
33  }
34  void clock(){
35  if(trig < min_limit)
36  trig++;
37  }
38  void clock(size_t steps){
39  trig += steps;
40  if(trig > max_limit)
41  trig = max_limit;
42  }
47  return samplePeriodToBpm(samples, sr);
48  }
52  void setBeatsPerMinute(float bpm){
54  }
58  float getFrequency(){
59  return sr/samples;
60  }
64  void setFrequency(float freq){
65  samples = sr/freq;
66  }
71  return samples;
72  }
76  void setPeriodInSamples(size_t value){
77  samples = value;
78  }
79  static float bpmToFrequency(float bpm){
80  return bpm/60;
81  }
82  static float frequencyToBpm(float freq){
83  return freq*60;
84  }
85  static float frequencyToSamplePeriod(float hz, float sr){
86  return sr/hz;
87  }
88  static float bpmToSamplePeriod(float bpm, float sr){
89  return sr/bpmToFrequency(bpm);
90  }
91  static float samplePeriodToBpm(float samples, float sr){
92  return frequencyToBpm(sr/samples);
93  }
94  static TapTempo* create(float sr, size_t max_limit){
95  return new TapTempo(sr, max_limit);
96  }
97  static TapTempo* create(float sr, size_t min, size_t max){
98  return new TapTempo(sr, min, max);
99  }
100  static void destroy(TapTempo* obj){
101  delete obj;
102  }
103 };
104 
105 class AdjustableTapTempo : public TapTempo {
106 protected:
107  float speed;
108  float range = 4;
109  float scale(float s){
110  // s = exp2f(range*s - range*0.5f);
111  return s*s;
112  return s;
113  // s = s*s*s*range;
114  }
115 public:
116  AdjustableTapTempo(float sr, size_t min, size_t max) :
117  TapTempo(sr, min, max), speed(scale(0.5f)) {}
118  [[deprecated("use resetSpeed() instead.")]]
119  void resetAdjustment(uint16_t s){
120  resetSpeed(s/4096.0f);
121  }
122  void resetSpeed(float s){
123  speed = scale(s);
124  }
125  void setRange(float value){
126  range = value*0.75f;
127  }
128  [[deprecated("use adjustSpeed() instead.")]]
129  void adjust(uint16_t s){
130  adjustSpeed(s/4096.0f);
131  }
139  void adjustSpeed(float s){
140  s = scale(s);
141  float mul = 1 + (speed - s)*range;
142  int32_t updated = clamp((size_t)(samples*mul), min_limit, max_limit);
143  if(abs((int32_t)samples - updated) > 16){
144  samples = updated;
145  speed = s;
146  }
147  }
148  static AdjustableTapTempo* create(float sr, size_t limit){
149  return new AdjustableTapTempo(sr, 16, limit);
150  }
151  static AdjustableTapTempo* create(float sr, size_t min, size_t max){
152  return new AdjustableTapTempo(sr, min, max);
153  }
154  static void destroy(AdjustableTapTempo* obj){
155  delete obj;
156  }
157 };
158 
159 #endif /* __TapTempo_h__ */
#define abs(x)
Definition: basicmaths.h:44
#define clamp(x, lo, hi)
Definition: basicmaths.h:47
#define min(a, b)
Definition: basicmaths.h:38
#define max(a, b)
Definition: basicmaths.h:41
void resetAdjustment(uint16_t s)
Definition: TapTempo.h:119
AdjustableTapTempo(float sr, size_t min, size_t max)
Definition: TapTempo.h:116
void adjust(uint16_t s)
Definition: TapTempo.h:129
void adjustSpeed(float s)
Adjust the tap tempo period.
Definition: TapTempo.h:139
float scale(float s)
Definition: TapTempo.h:109
static AdjustableTapTempo * create(float sr, size_t min, size_t max)
Definition: TapTempo.h:151
void resetSpeed(float s)
Definition: TapTempo.h:122
static AdjustableTapTempo * create(float sr, size_t limit)
Definition: TapTempo.h:148
static void destroy(AdjustableTapTempo *obj)
Definition: TapTempo.h:154
void setRange(float value)
Definition: TapTempo.h:125
bool isOn()
Definition: TapTempo.h:19
void trigger(bool on)
Definition: TapTempo.h:22
size_t getPeriodInSamples()
Get tap tempo period in samples.
Definition: TapTempo.h:70
void setBeatsPerMinute(float bpm)
Set tap tempo in BPM.
Definition: TapTempo.h:52
size_t trig
Definition: TapTempo.h:10
static TapTempo * create(float sr, size_t max_limit)
Definition: TapTempo.h:94
float getBeatsPerMinute()
Get tap tempo in BPM.
Definition: TapTempo.h:46
const size_t max_limit
Definition: TapTempo.h:8
static void destroy(TapTempo *obj)
Definition: TapTempo.h:100
void clock()
Definition: TapTempo.h:34
TapTempo(float sr, size_t min, size_t max)
Definition: TapTempo.h:16
void setPeriodInSamples(size_t value)
Set tap tempo period in samples.
Definition: TapTempo.h:76
static float bpmToSamplePeriod(float bpm, float sr)
Definition: TapTempo.h:88
bool ison
Definition: TapTempo.h:11
static float bpmToFrequency(float bpm)
Definition: TapTempo.h:79
void setFrequency(float freq)
Set tap tempo frequency.
Definition: TapTempo.h:64
TapTempo(float sr, size_t limit)
Definition: TapTempo.h:13
static float samplePeriodToBpm(float samples, float sr)
Definition: TapTempo.h:91
float getFrequency()
Get tap tempo frequency.
Definition: TapTempo.h:58
static float frequencyToSamplePeriod(float hz, float sr)
Definition: TapTempo.h:85
void trigger(bool on, int delay)
Definition: TapTempo.h:25
const size_t min_limit
Definition: TapTempo.h:7
size_t samples
Definition: TapTempo.h:9
void clock(size_t steps)
Definition: TapTempo.h:38
static float frequencyToBpm(float freq)
Definition: TapTempo.h:82
static TapTempo * create(float sr, size_t min, size_t max)
Definition: TapTempo.h:97
const float sr
Definition: TapTempo.h:6