double MyParameters::getTimeByNote(const double inBpm,const int inNote) const noexcept
{
const double secondPerBeat = 60.0 / inBpm;
}
60.0 / inBpm is a formula used to calculate how many seconds one beat lasts.
Since BPM (beats per minute) indicates how many beats occur in one minute (60 seconds), dividing 60 by the BPM gives you the duration of a single beat in seconds.
double MyParameters::getTimeByNote(const double inBpm,const int inNote) const noexcept
{
enum Notes
{
SixteenthTriplet,
ThirtySecondDotted,
Sixteenth, // 1/16
EighthTriplet,
SixteenthDotted,
Eighth, // 1/8
QuarterTriplet,
EighthDotted,
Quarter, // 1/4
HalfTriplet,
QuarterDotted,
Half, // 1/2
Triplet,
HalfDotted
};
static constexpr double scalars[] = { 0.25 * 2.0 / 3.0, // SixteenthTriplet,
0.125 * 1.5, // ThirtySecondDotted,
0.25, // Sixteenth,
0.5 * 2.0 / 3.0, // EighthTriplet
0.25 * 1.5, // SixteenthDotted,
0.5, // Eighth,
1.0 * 2.0 / 3.0, // QuarterTriplet,
0.5 * 1.5, // EighthDotted,
1.0, // Quarter,
2.0 * 2.0 / 3.0, // HalfTriplet,
1.0 * 1.5, // QuarterDotted,
2.0, // Half,
4.0 * 2.0 / 3.0, // Triplet,
2.0 * 1.5 }; // HalfDotted,
return secondPerBeat * scalars[inNote];
}
Static Variable in function
A variable declared as static inside a function appears local, but it is actually stored in the data segment like a global variable.
It is initialized only once, and its value is retained across multiple function calls.
This makes it ideal for constants or stateful variables that need to be shared efficiently between calls.
Constexpr
Normally, constants are calculated when the program runs or stored in memory like global variables.
But if you use constexpr, the value is calculated while compiling, so it doesn’t need to be recalculated during runtime.