-
Notifications
You must be signed in to change notification settings - Fork 12
/
TemposyncSupport.h
78 lines (71 loc) · 1.92 KB
/
TemposyncSupport.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
/*
* SurgeXT for VCV Rack - a Surge Synth Team product
*
* A set of modules expressing Surge XT into the VCV Rack Module Ecosystem
*
* Copyright 2019 - 2024, Various authors, as described in the github
* transaction log.
*
* Surge XT for VCV Rack is released under the GNU General Public License
* 3.0 or later (GPL-3.0-or-later). A copy of the license is in this
* repository in the file "LICENSE" or at:
*
* or at https://www.gnu.org/licenses/gpl-3.0.en.html
*
* All source for Surge XT for VCV Rack is available at
* https://github.com/surge-synthesizer/surge-rack/
*/
#ifndef SURGE_XT_RACK_SRC_TEMPOSYNCSUPPORT_H
#define SURGE_XT_RACK_SRC_TEMPOSYNCSUPPORT_H
#include <cmath>
#include "Parameter.h"
namespace sst::surgext_rack::temposync_support
{
inline float roundTemposync(float f)
{
float a, b = modff(f, &a);
if (b < 0)
{
b += 1.f;
a -= 1.f;
}
b = powf(2.0f, b);
if (b > 1.41f)
{
b = log2(1.5f);
}
else if (b > 1.167f)
{
b = log2(1.3333333333f);
}
else
{
b = 0.f;
}
return a + b;
};
inline std::string temposyncLabel(float f, bool minus = false)
{
auto ts = temposync_support::roundTemposync(f);
Parameter p;
return p.tempoSyncNotationValue((minus ? -1 : 1) * ts);
}
inline std::string abbreviateSurgeTemposyncLabel(std::string s)
{
std::list<std::pair<std::string, std::string>> replace = {
{"whole note", "W"}, {"note", ""}, {" dotted", "D"}, {" triplet", "T"},
{"whole", "W"}, {"double", "2x"}, {"triple", "3x"}};
for (const auto &[from, to] : replace)
{
auto p = s.find(from);
if (p != std::string::npos)
{
auto pre = s.substr(0, p);
auto post = s.substr(p + from.size());
s = pre + to + post;
}
}
return s;
}
} // namespace sst::surgext_rack::temposync_support
#endif // SURGEXTRACK_TEMPOSYNCSUPPORT_H