2021-12-29 15:59:18 +01:00
|
|
|
#pragma once
|
2021-09-14 21:02:23 +02:00
|
|
|
|
2022-10-18 04:59:51 +02:00
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
2021-05-28 19:50:57 +02:00
|
|
|
#include <numeric>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-12-29 15:59:18 +01:00
|
|
|
#include "core/time.h"
|
|
|
|
|
|
|
|
namespace kku
|
|
|
|
{
|
|
|
|
|
2022-10-18 04:59:51 +02:00
|
|
|
template <typename Grade,
|
|
|
|
typename = std::enable_if_t<std::is_enum<Grade>::value>>
|
2021-05-28 19:50:57 +02:00
|
|
|
class PrecisionEvaluator
|
|
|
|
{
|
2022-10-18 04:59:51 +02:00
|
|
|
public:
|
|
|
|
PrecisionEvaluator(const std::vector<microsec> &intervals, microsec offset)
|
|
|
|
: _offset(offset), _intervals(intervals)
|
2021-05-28 19:50:57 +02:00
|
|
|
{
|
2021-06-17 21:13:25 +02:00
|
|
|
_start_handling_offset = _offset - intervals.back();
|
|
|
|
_end_handling_offset = _offset + intervals.back();
|
2021-05-28 19:50:57 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 04:59:51 +02:00
|
|
|
inline microsec offset() const noexcept { return _offset; }
|
2021-05-28 19:50:57 +02:00
|
|
|
|
|
|
|
inline bool isActive(microsec music_play_offset) const noexcept
|
|
|
|
{
|
2022-10-18 04:59:51 +02:00
|
|
|
return music_play_offset > _start_handling_offset &&
|
|
|
|
music_play_offset < _end_handling_offset;
|
2021-05-28 19:50:57 +02:00
|
|
|
}
|
|
|
|
|
2021-12-29 15:59:18 +01:00
|
|
|
Grade calculatePrecision(microsec odds) const
|
2021-05-28 19:50:57 +02:00
|
|
|
{
|
|
|
|
microsec shift_from_perfect = std::abs(odds - offset());
|
|
|
|
|
2021-12-29 15:59:18 +01:00
|
|
|
std::cout << "Shift " << ((odds > _offset) ? "late: " : "early: ")
|
|
|
|
<< shift_from_perfect << "\n";
|
2021-06-17 21:13:25 +02:00
|
|
|
|
2021-05-28 19:50:57 +02:00
|
|
|
std::size_t raw_grade;
|
|
|
|
for (raw_grade = 0; raw_grade < _intervals.size(); ++raw_grade)
|
|
|
|
{
|
2021-06-07 20:19:58 +02:00
|
|
|
if (shift_from_perfect <= _intervals.at(raw_grade))
|
2021-05-28 19:50:57 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-06-11 18:58:44 +02:00
|
|
|
return static_cast<Grade>(raw_grade);
|
2021-05-28 19:50:57 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 04:59:51 +02:00
|
|
|
private:
|
2021-05-28 19:50:57 +02:00
|
|
|
microsec _offset;
|
|
|
|
microsec _start_handling_offset;
|
|
|
|
microsec _end_handling_offset;
|
|
|
|
|
2021-06-11 18:58:44 +02:00
|
|
|
/* Amount of values in enum instanced as GradeS
|
2021-05-28 19:50:57 +02:00
|
|
|
* represents capacity of _intervals.
|
2021-06-11 18:58:44 +02:00
|
|
|
* So, for each V value in GradeS enum, _intervals[V]
|
2021-05-28 19:50:57 +02:00
|
|
|
* should return time shift from V - 1.
|
|
|
|
* V0 is PERFECT SCORE and the last V represents the worst
|
|
|
|
* grades which is death of note by expiration */
|
|
|
|
|
2021-06-23 21:18:33 +02:00
|
|
|
const std::vector<microsec> _intervals;
|
2021-05-28 19:50:57 +02:00
|
|
|
};
|
|
|
|
|
2022-10-18 04:59:51 +02:00
|
|
|
} // namespace kku
|