-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbpm_sync_lfo.hpp
More file actions
282 lines (251 loc) · 11 KB
/
Copy pathbpm_sync_lfo.hpp
File metadata and controls
282 lines (251 loc) · 11 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#pragma once
#include <juce_audio_processors/juce_audio_processors.h>
#include <bit>
#include <mutex>
namespace pluginshared {
class BpmSyncLFO {
public:
static constexpr std::array kBaseRateTable{
1.0f / 64.0f, 1.0f / 32.0f, 1.0f / 16.0f, 1.0f / 8.0f, 1.0f / 4.0f, 1.0f / 2.0f, 1.0f, 2.0f, 4.0f, 8.0f,
};
static constexpr std::array kBaseRateTextTable{
"1/64", "1/32", "1/16", "1/8", "1/4", "1/2", "1", "2", "4", "8",
};
static constexpr size_t kRateTableSize = kBaseRateTable.size() * 3 * 2 + 1;
static constexpr std::array kRateMulTable = [] {
std::array<float, kRateTableSize> output_table{};
// negative 0 positive
// 1.5f 1.0f 0.66f
size_t wpos = 0;
for (auto it = kBaseRateTable.rbegin(); it != kBaseRateTable.rend(); ++it) {
auto val = *it;
output_table[wpos++] = -1.5f * val;
output_table[wpos++] = -1.0f * val;
output_table[wpos++] = -2.0f / 3.0f * val;
}
output_table[wpos++] = 0.0f;
for (auto val : kBaseRateTable) {
output_table[wpos++] = 2.0f / 3.0f * val;
output_table[wpos++] = 1.0f * val;
output_table[wpos++] = 1.5f * val;
}
return output_table;
}();
inline static std::array<juce::String, kRateTableSize> s_rate_name_table;
inline static std::once_flag s_tempo_table_init_flag;
static void TryInitTempoTable() {
std::call_once(s_tempo_table_init_flag, [] {
size_t wpos = 0;
for (auto val : kBaseRateTextTable) {
s_rate_name_table[wpos++] = juce::String{"-"} + val + "T";
s_rate_name_table[wpos++] = juce::String{"-"} + val;
s_rate_name_table[wpos++] = juce::String{"-"} + val + "D";
}
s_rate_name_table[wpos++] = "freeze";
for (auto it = kBaseRateTextTable.rbegin(); it != kBaseRateTextTable.rend(); ++it) {
auto val = *it;
s_rate_name_table[wpos++] = juce::String{val} + "D";
s_rate_name_table[wpos++] = juce::String{val};
s_rate_name_table[wpos++] = juce::String{val} + "T";
}
});
}
static int GetTempoValueIndex(juce::StringRef rate_name) {
if (rate_name.text[0] == '0' && rate_name.length() == 1) {
return GetTempoValueIndex("freeze");
}
auto it = std::find(s_rate_name_table.begin(), s_rate_name_table.end(), rate_name);
jassert(it != s_rate_name_table.end());
int where = static_cast<int>(it - s_rate_name_table.begin());
return where;
}
BpmSyncLFO() {
TryInitTempoTable();
}
BpmSyncLFO(juce::String name, float min_freq, float max_freq, float interval, float warp, bool warpnp,
juce::StringRef begin_tempo, juce::StringRef end_tempo, float default_hz, juce::StringRef default_tempo,
bool default_is_free) {
TryInitTempoTable();
name_ = std::move(name);
std::construct_at(&free_freq_range_, min_freq, max_freq, interval, warp, warpnp);
tempo_begin_idx_ = GetTempoValueIndex(begin_tempo);
tempo_end_idx_ = GetTempoValueIndex(end_tempo);
default_free_hz_ = default_hz;
default_tempo_idx_ = GetTempoValueIndex(default_tempo);
default_is_free_ = default_is_free;
}
struct LfoInfo2 {
float lfo_freq;
float lfo_phase;
bool sync_lfo;
};
[[nodiscard]] LfoInfo2 SyncBpm2(juce::AudioPlayHead* head) {
float fbpm = 120.0f;
float fppq = 0.0f;
float lfo_phase = 0.0f;
bool sync_lfo = false;
if (head != nullptr) {
auto pos = head->getPosition();
if (auto bpm = pos->getBpm(); bpm) {
fbpm = static_cast<float>(*bpm);
}
if (auto ppq = pos->getPpqPosition(); ppq) {
fppq = static_cast<float>(*ppq);
sync_lfo = true;
}
if (!pos->getIsPlaying()) {
sync_lfo = false;
}
}
float lfo_freq = 0.0f;
FreqAttrubute freq_attr = GetFreqAttribute();
if (!freq_attr.tempo_sync) {
lfo_freq = free_freq_range_.convertFrom0to1(param_freq->get());
sync_lfo = false;
}
else {
if (!freq_attr.ppq_sync) {
sync_lfo = false;
}
float findex =
std::lerp(static_cast<float>(tempo_begin_idx_), static_cast<float>(tempo_end_idx_), param_freq->get());
int index = static_cast<int>(findex);
index = std::clamp(index, tempo_begin_idx_, tempo_end_idx_);
float sync_rate = kRateMulTable[static_cast<size_t>(index)];
if (!freq_attr.tempo_snap) {
int nindex = std::min(index + 1, tempo_end_idx_);
float next_rate = kRateMulTable[static_cast<size_t>(nindex)];
sync_rate = std::lerp(sync_rate, next_rate, findex - static_cast<float>(index));
}
float sync_phase = sync_rate * fppq;
sync_phase -= std::floor(sync_phase);
lfo_phase = sync_phase;
lfo_freq = sync_rate * fbpm / 60.0f;
}
if (should_reset_phase_.exchange(false, std::memory_order_acquire)) {
lfo_phase = reseted_phase_;
sync_lfo = true;
}
return LfoInfo2{lfo_freq, lfo_phase, sync_lfo};
}
[[nodiscard]]
std::pair<std::unique_ptr<juce::AudioParameterInt>, std::unique_ptr<juce::AudioParameterFloat>> Build() {
FreqAttrubute attr_init{};
if (!default_is_free_) {
attr_init.tempo_sync = -1;
attr_init.tempo_snap = -1;
}
auto ptype = std::make_unique<juce::AudioParameterInt>(juce::ParameterID{name_ + "_type", 1}, name_ + "_type",
0, std::numeric_limits<int32_t>::max(),
std::bit_cast<int>(attr_init));
param_type = ptype.get();
auto attr =
juce::AudioParameterFloatAttributes{}
.withStringFromValueFunction([this, float_numeric = GetFloatNumericText(free_freq_range_.interval)](
auto x, auto) -> juce::String {
FreqAttrubute freq_attr = GetFreqAttribute();
if (freq_attr.tempo_sync) {
int index = static_cast<int>(
std::lerp(static_cast<float>(tempo_begin_idx_), static_cast<float>(tempo_end_idx_), x));
index = std::clamp(index, tempo_begin_idx_, tempo_end_idx_);
return s_rate_name_table[static_cast<size_t>(index)];
}
else {
return juce::String{free_freq_range_.convertFrom0to1(x), float_numeric};
}
})
.withValueFromStringFunction([this](juce::String x) -> float {
FreqAttrubute freq_attr = GetFreqAttribute();
if (freq_attr.tempo_sync) {
auto it = std::find(s_rate_name_table.begin(), s_rate_name_table.end(), x);
if (it == s_rate_name_table.end()) return 0.0f;
int where = static_cast<int>(it - s_rate_name_table.begin());
where = std::clamp(where, tempo_begin_idx_, tempo_end_idx_);
float val01 = static_cast<float>(where - tempo_begin_idx_)
/ static_cast<float>(tempo_end_idx_ - tempo_begin_idx_);
return val01;
}
else {
return free_freq_range_.convertTo0to1(x.getFloatValue());
}
});
auto pfreq = std::make_unique<juce::AudioParameterFloat>(
juce::ParameterID{name_ + "_freq", 1}, name_ + "_freq", juce::NormalisableRange<float>{0, 1},
default_is_free_ ? GetDefaultHzVal01() : GetDefaultTempoVal01(), attr);
param_freq = pfreq.get();
return {std::move(ptype), std::move(pfreq)};
}
[[nodiscard]]
std::pair<std::unique_ptr<juce::AudioParameterInt>, std::unique_ptr<juce::AudioParameterFloat>> Build(
juce::String name, float min_freq, float max_freq, float interval, float warp, bool warpnp,
juce::StringRef begin_tempo, juce::StringRef end_tempo, float default_hz, juce::StringRef default_tempo,
bool default_is_free) {
name_ = std::move(name);
std::construct_at(&free_freq_range_, min_freq, max_freq, interval, warp, warpnp);
tempo_begin_idx_ = GetTempoValueIndex(begin_tempo);
tempo_end_idx_ = GetTempoValueIndex(end_tempo);
default_free_hz_ = default_hz;
default_tempo_idx_ = GetTempoValueIndex(default_tempo);
default_is_free_ = default_is_free;
return Build();
}
struct FreqAttrubute {
int tempo_sync : 1;
int ppq_sync : 1;
int tempo_snap : 1;
};
FreqAttrubute GetFreqAttribute() const {
return std::bit_cast<FreqAttrubute>(param_type->get());
}
void SetFreqAttribute(FreqAttrubute attr) {
param_type->setValueNotifyingHost(param_type->convertTo0to1(static_cast<float>(std::bit_cast<int>(attr))));
}
void ResetToDefaultHz() {
param_freq->setValueNotifyingHost(free_freq_range_.convertTo0to1(default_free_hz_));
FreqAttrubute attr = GetFreqAttribute();
attr.tempo_sync = 0;
SetFreqAttribute(attr);
}
void ResetToDefaultTempo() {
float val01 = static_cast<float>(default_tempo_idx_ - tempo_begin_idx_)
/ static_cast<float>(tempo_end_idx_ - tempo_begin_idx_);
param_freq->setValueNotifyingHost(val01);
FreqAttrubute attr = GetFreqAttribute();
attr.tempo_sync = -1;
SetFreqAttribute(attr);
}
float GetDefaultHzVal01() {
return free_freq_range_.convertTo0to1(default_free_hz_);
}
float GetDefaultTempoVal01() {
float val01 = static_cast<float>(default_tempo_idx_ - tempo_begin_idx_)
/ static_cast<float>(tempo_end_idx_ - tempo_begin_idx_);
return val01;
}
void TryResetPhase(float phase) {
reseted_phase_ = phase;
should_reset_phase_.store(true, std::memory_order_release);
}
juce::AudioParameterFloat* param_freq{};
juce::AudioParameterInt* param_type{};
private:
static int GetFloatNumericText(float interval) {
float v = 1.0f;
int num = 0;
while (v > interval) {
v /= 10.0f;
++num;
}
return num;
}
juce::String name_;
juce::NormalisableRange<float> free_freq_range_;
int tempo_begin_idx_;
int tempo_end_idx_;
float default_free_hz_;
int default_tempo_idx_;
bool default_is_free_;
float reseted_phase_{};
std::atomic<bool> should_reset_phase_{};
};
} // namespace pluginshared