@@ -58,23 +58,32 @@ static void teVarsInit() {
58
58
{" a" , 9 },
59
59
{" b" , 11 },
60
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
61
74
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 )});
66
75
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);
71
79
}
72
80
}
73
81
74
82
// Build teVars from teVariables
83
+ // After this point, the addresses of name.c_str() and values in teVariables can't be changed.
75
84
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 });
78
87
}
79
88
80
89
// Add custom functions
0 commit comments