Skip to content

Commit 125d59a

Browse files
committed
Add note voltage constants to Quantity expression parser such as "c4v = 0V" and "a#v". Refactor teVarsInit().
1 parent 872aa03 commit 125d59a

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/Quantity.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,32 @@ static void teVarsInit() {
5858
{"a", 9},
5959
{"b", 11},
6060
};
61+
auto pushNoteName = [&](const std::string& name, int semi, int oct = 4) {
62+
double voltage = oct - 4 + semi / 12.0;
63+
teVariables.push_back({name, 440.0 * std::exp2(voltage - 9 / 12.0)});
64+
teVariables.push_back({name + "v", voltage});
65+
};
66+
// Example: c, cs (or c#), and cb
67+
// This overwrites Euler's number "e", but the note name is more important here, and you can type exp(1) instead.
68+
for (const Note& note : notes) {
69+
pushNoteName(string::f("%s", note.name), note.semi);
70+
pushNoteName(string::f("%ss", note.name), note.semi + 1);
71+
pushNoteName(string::f("%sb", note.name), note.semi - 1);
72+
}
73+
// Example: c4, cs4 (or c#4), and cb4
6174
for (const Note& note : notes) {
62-
// Example: c, cs (or c#), and cb
63-
teVariables.push_back({string::f("%s", note.name), 440.f * std::pow(2.0, (note.semi - 9) / 12.f)});
64-
teVariables.push_back({string::f("%ss", note.name), 440.f * std::pow(2.0, (note.semi - 9 + 1) / 12.f)});
65-
teVariables.push_back({string::f("%sb", note.name), 440.f * std::pow(2.0, (note.semi - 9 - 1) / 12.f)});
6675
for (int oct = 0; oct <= 9; oct++) {
67-
// Example: c4, cs4 (or c#4), and cb4
68-
teVariables.push_back({string::f("%s%d", note.name, oct), 440.f * std::pow(2.0, oct - 4 + (note.semi - 9) / 12.f)});
69-
teVariables.push_back({string::f("%ss%d", note.name, oct), 440.f * std::pow(2.0, oct - 4 + (note.semi - 9 + 1) / 12.f)});
70-
teVariables.push_back({string::f("%sb%d", note.name, oct), 440.f * std::pow(2.0, oct - 4 + (note.semi - 9 - 1) / 12.f)});
76+
pushNoteName(string::f("%s%d", note.name, oct), note.semi, oct);
77+
pushNoteName(string::f("%ss%d", note.name, oct), note.semi + 1, oct);
78+
pushNoteName(string::f("%sb%d", note.name, oct), note.semi - 1, oct);
7179
}
7280
}
7381

7482
// Build teVars from teVariables
83+
// After this point, the addresses of name.c_str() and values in teVariables can't be changed.
7584
teVars.reserve(teVariables.size());
76-
for (size_t i = 0; i < teVariables.size(); i++) {
77-
teVars.push_back({teVariables[i].name.c_str(), &teVariables[i].value, TE_VARIABLE, NULL});
85+
for (const TeVariable& teVariable : teVariables) {
86+
teVars.push_back({teVariable.name.c_str(), &teVariable.value, TE_VARIABLE, NULL});
7887
}
7988

8089
// Add custom functions

0 commit comments

Comments
 (0)