| 38 | static std::vector<te_variable> teVars; |
| 39 | |
| 40 | static void teVarsInit() { |
| 41 | if (!teVars.empty()) |
| 42 | return; |
| 43 | |
| 44 | // Math variables |
| 45 | teVariables.push_back({"inf", INFINITY}); |
| 46 | |
| 47 | // Note names |
| 48 | struct Note { |
| 49 | std::string name; |
| 50 | int semi; |
| 51 | }; |
| 52 | const static std::vector<Note> notes = { |
| 53 | {"c", 0}, |
| 54 | {"d", 2}, |
| 55 | {"e", 4}, |
| 56 | {"f", 5}, |
| 57 | {"g", 7}, |
| 58 | {"a", 9}, |
| 59 | {"b", 11}, |
| 60 | }; |
| 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 |
| 74 | for (const Note& note : notes) { |
| 75 | for (int oct = 0; oct <= 9; oct++) { |
| 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); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Build teVars from teVariables |
| 83 | // After this point, the addresses of name.c_str() and values in teVariables can't be changed. |
| 84 | teVars.reserve(teVariables.size()); |
| 85 | for (const TeVariable& teVariable : teVariables) { |
| 86 | teVars.push_back({teVariable.name.c_str(), &teVariable.value, TE_VARIABLE, NULL}); |
| 87 | } |
| 88 | |
| 89 | // Add custom functions |
| 90 | teVars.push_back({"log2", (void*) (double(*)(double)) std::log2, TE_FUNCTION1 | TE_FLAG_PURE, NULL}); |
| 91 | |
| 92 | teVars.push_back({"gaintodb", (void*) (double(*)(double)) [](double x) -> double { |
| 93 | return std::log10(x) * 20; |
| 94 | }, TE_FUNCTION1 | TE_FLAG_PURE, NULL}); |
| 95 | teVars.push_back({"dbtogain", (void*) (double(*)(double)) [](double x) -> double { |
| 96 | return std::pow(10, x / 20); |
| 97 | }, TE_FUNCTION1 | TE_FLAG_PURE, NULL}); |